11// Changed Lanes tests cover changed lanes script behavior.
22import { execFileSync , spawnSync } from "node:child_process" ;
3- import { existsSync , mkdirSync , unlinkSync , writeFileSync } from "node:fs" ;
3+ import { existsSync , mkdirSync , readFileSync , unlinkSync , writeFileSync } from "node:fs" ;
44import path from "node:path" ;
55import { afterEach , describe , expect , it } from "vitest" ;
66import {
@@ -10,6 +10,7 @@ import {
1010 isLiveDockerPackageScriptOnlyChange ,
1111 isPackageScriptOnlyChange ,
1212 listChangedPathsFromGit ,
13+ listStagedChangedPaths ,
1314} from "../../scripts/changed-lanes.mjs" ;
1415import {
1516 buildChangedCheckCrabboxArgs ,
@@ -31,6 +32,7 @@ import {
3132 shouldRunTestTempCreationReport ,
3233 createShrinkwrapGuardCommand ,
3334} from "../../scripts/check-changed.mjs" ;
35+ import { resolveOxfmtInvocation } from "../../scripts/format-docs.mjs" ;
3436import { isDirectRunPath } from "../../scripts/lib/direct-run.mjs" ;
3537import { cleanupTempDirs , makeTempRepoRoot } from "../helpers/temp-repo.js" ;
3638
@@ -88,6 +90,30 @@ function writeRepoFile(repoDir: string, filePath: string, contents: string): voi
8890 writeFileSync ( absolutePath , contents , "utf8" ) ;
8991}
9092
93+ // Executes the exact "format changed files" plan command with the repo-pinned oxfmt,
94+ // reconstructing `pnpm format:check <plan args>`. Guards the runtime verdict, not just
95+ // plan construction: a misformatted added file must fail, deleted paths must not.
96+ function runChangedFormatLaneWithRepoOxfmt ( cwd : string , changedPaths : string [ ] ) {
97+ const plan = createChangedCheckPlan ( detectChangedLanes ( changedPaths ) ) ;
98+ const formatCommand = plan . commands . find ( ( command ) => command . name === "format changed files" ) ;
99+ expect ( formatCommand ?. args [ 0 ] ) . toBe ( "format:check" ) ;
100+ const packageJson = JSON . parse ( readFileSync ( path . join ( repoRoot , "package.json" ) , "utf8" ) ) as {
101+ scripts : Record < string , string > ;
102+ } ;
103+ const [ scriptBin , ...scriptArgs ] = packageJson . scripts [ "format:check" ] . split ( " " ) ;
104+ expect ( scriptBin ) . toBe ( "oxfmt" ) ;
105+ const invocation = resolveOxfmtInvocation (
106+ [ ...scriptArgs , ...( formatCommand ?. args . slice ( 1 ) ?? [ ] ) ] ,
107+ { repoRoot } ,
108+ ) ;
109+ return spawnSync ( invocation . command , invocation . args , {
110+ cwd,
111+ encoding : "utf8" ,
112+ shell : invocation . shell ,
113+ windowsVerbatimArguments : invocation . windowsVerbatimArguments ,
114+ } ) ;
115+ }
116+
91117function createSyntheticMergeRepo ( prefix : string ) : { dir : string ; staleBase : string } {
92118 const dir = makeTempRepoRoot ( tempDirs , prefix ) ;
93119 git ( dir , [ "init" , "-q" , "--initial-branch=main" ] ) ;
@@ -456,6 +482,74 @@ describe("scripts/changed-lanes", () => {
456482 } ) ;
457483 } ) ;
458484
485+ it ( "includes staged added, modified, and deleted files in the changed format check" , ( ) => {
486+ const dir = makeTempRepoRoot ( tempDirs , "openclaw-changed-lanes-staged-format-" ) ;
487+ git ( dir , [ "init" , "-q" , "--initial-branch=main" ] ) ;
488+ writeRepoFile ( dir , "src/modified.ts" , "export const modified = { value: 1 };\n" ) ;
489+ writeRepoFile ( dir , "src/removed.ts" , "export const removed = { value: 1 };\n" ) ;
490+ git ( dir , [ "add" , "." ] ) ;
491+ git ( dir , [
492+ "-c" ,
493+ 494+ "-c" ,
495+ "user.name=Test User" ,
496+ "commit" ,
497+ "-q" ,
498+ "-m" ,
499+ "initial" ,
500+ ] ) ;
501+ writeRepoFile ( dir , "src/added.test.ts" , "export const added={value:1};\n" ) ;
502+ writeRepoFile ( dir , "src/modified.ts" , "export const modified={value:2};\n" ) ;
503+ git ( dir , [ "add" , "src/added.test.ts" , "src/modified.ts" ] ) ;
504+ git ( dir , [ "rm" , "-q" , "src/removed.ts" ] ) ;
505+
506+ const paths = listStagedChangedPaths ( dir ) ;
507+ const plan = createChangedCheckPlan ( detectChangedLanes ( paths ) ) ;
508+
509+ expect ( paths ) . toEqual ( [ "src/added.test.ts" , "src/modified.ts" , "src/removed.ts" ] ) ;
510+ expect ( plan . commands . find ( ( command ) => command . name === "format changed files" ) ) . toEqual ( {
511+ name : "format changed files" ,
512+ args : [
513+ "format:check" ,
514+ "--no-error-on-unmatched-pattern" ,
515+ "--" ,
516+ "src/added.test.ts" ,
517+ "src/modified.ts" ,
518+ "src/removed.ts" ,
519+ ] ,
520+ } ) ;
521+ } ) ;
522+
523+ it ( "fails the changed format check on a misformatted added file and passes once formatted" , ( ) => {
524+ const dir = makeTempRepoRoot ( tempDirs , "openclaw-changed-format-added-" ) ;
525+ writeRepoFile ( dir , "src/added.test.ts" , "export const added={value:1};\n" ) ;
526+
527+ const dirty = runChangedFormatLaneWithRepoOxfmt ( dir , [ "src/added.test.ts" ] ) ;
528+ expect ( dirty . status ) . not . toBe ( 0 ) ;
529+ expect ( `${ dirty . stdout } ${ dirty . stderr } ` ) . toContain ( "added.test.ts" ) ;
530+
531+ writeRepoFile ( dir , "src/added.test.ts" , "export const added = { value: 1 };\n" ) ;
532+ const formatted = runChangedFormatLaneWithRepoOxfmt ( dir , [ "src/added.test.ts" ] ) ;
533+ expect ( formatted . status ) . toBe ( 0 ) ;
534+ } ) ;
535+
536+ it ( "fails the changed format check on a misformatted modified file" , ( ) => {
537+ const dir = makeTempRepoRoot ( tempDirs , "openclaw-changed-format-modified-" ) ;
538+ writeRepoFile ( dir , "src/modified.ts" , "export const modified={value:2};\n" ) ;
539+
540+ const result = runChangedFormatLaneWithRepoOxfmt ( dir , [ "src/modified.ts" ] ) ;
541+ expect ( result . status ) . not . toBe ( 0 ) ;
542+ expect ( `${ result . stdout } ${ result . stderr } ` ) . toContain ( "modified.ts" ) ;
543+ } ) ;
544+
545+ it ( "does not fail the changed format check for deleted paths" , ( ) => {
546+ const dir = makeTempRepoRoot ( tempDirs , "openclaw-changed-format-deleted-" ) ;
547+ writeRepoFile ( dir , "src/kept.ts" , "export const kept = { value: 1 };\n" ) ;
548+
549+ const result = runChangedFormatLaneWithRepoOxfmt ( dir , [ "src/deleted.ts" , "src/kept.ts" ] ) ;
550+ expect ( result . status ) . toBe ( 0 ) ;
551+ } ) ;
552+
459553 it ( "uses the merge commit first parent instead of a stale PR payload base" , ( ) => {
460554 const { dir, staleBase } = createSyntheticMergeRepo ( "openclaw-changed-lanes-merge-" ) ;
461555
0 commit comments