@@ -295,22 +295,39 @@ describe("CI test timing refit", () => {
295295 ) . toEqual ( first . timings ) ;
296296 } ) ;
297297
298- it ( "fetches only successful matching job logs, paginates, and keeps dry-run and unchanged refits byte-stable" , ( ) => {
299- const directory = realpathSync ( mkdtempSync ( path . join ( tmpdir ( ) , "openclaw-ci-refit-" ) ) ) ;
300- const fakeGh = path . join ( directory , "gh" ) ;
301- const output = path . join ( directory , "timings.json" ) ;
302- const root = fileURLToPath ( new URL ( "../../" , import . meta. url ) ) ;
303- const log = uiLog ( { [ measuredFile ] : 130 } ) ;
304- writeFileSync (
305- fakeGh ,
306- `#!/usr/bin/env node
298+ it . each ( [
299+ { label : "main push retries" , metadata : { } , invalidField : undefined } ,
300+ {
301+ label : "manual dispatch from main" ,
302+ metadata : { event : "workflow_dispatch" } ,
303+ invalidField : "event" ,
304+ } ,
305+ {
306+ label : "push to another branch" ,
307+ metadata : { head_branch : "feature" } ,
308+ invalidField : "head_branch" ,
309+ } ,
310+ { label : "missing head commit" , metadata : { head_sha : null } , invalidField : "head_sha" } ,
311+ ] ) (
312+ "validates $label before fetching samples and preserves dry-run/unchanged bytes" ,
313+ ( { metadata, invalidField } ) => {
314+ const directory = realpathSync ( mkdtempSync ( path . join ( tmpdir ( ) , "openclaw-ci-refit-" ) ) ) ;
315+ const fakeGh = path . join ( directory , "gh" ) ;
316+ const output = path . join ( directory , "timings.json" ) ;
317+ const requests = path . join ( directory , "requests.json" ) ;
318+ const root = fileURLToPath ( new URL ( "../../" , import . meta. url ) ) ;
319+ const log = uiLog ( { [ measuredFile ] : 130 } ) ;
320+ writeFileSync (
321+ fakeGh ,
322+ `#!/usr/bin/env node
307323const args = process.argv.slice(2);
308324const endpoint = args[1];
309325if (args[0] === "api" && args[1] === "--help") {
310326 console.log("--allow-escape-sequences");
311- } else if (endpoint === "repos/fixture/repo/actions/workflows/ci.yml/runs?branch=main&status=success&per_page=3&page=1") {
327+ } else if (endpoint.replace("&event=push", "") === "repos/fixture/repo/actions/workflows/ci.yml/runs?branch=main&status=success&per_page=3&page=1") {
312328 if (!args.includes("--jq")) process.exit(2);
313- console.log(JSON.stringify([1, 2, 3].map(id => ({id, created_at: "2026-08-27T23:00:00Z", status: "completed", conclusion: "success"}))));
329+ require("node:fs").writeFileSync(${ JSON . stringify ( requests ) } , JSON.stringify(args));
330+ console.log(JSON.stringify([1, 2, 3].map(id => ({id, created_at: "2026-08-27T23:00:00Z", status: "completed", conclusion: "success", event: "push", head_branch: "main", head_sha: "a".repeat(40), run_attempt: 2, ...${ JSON . stringify ( metadata ) } }))));
314331} else if (endpoint.includes("actions/runs/") && endpoint.includes("/jobs?filter=all&")) {
315332 if (!args.at(-1).includes("labels")) process.exit(2);
316333 const jobs = endpoint.endsWith("page=1")
@@ -334,65 +351,81 @@ if (args[0] === "api" && args[1] === "--help") {
334351 process.exit(2);
335352}
336353` ,
337- ) ;
338- chmodSync ( fakeGh , 0o755 ) ;
339- const original = `${ JSON . stringify (
340- {
341- ...baseline ,
342- compactGroupSeconds : { blacksmith : { deleted : 30 } , github : { } } ,
343- } ,
344- null ,
345- 2 ,
346- ) } \n`;
347- writeFileSync ( output , original ) ;
348- const invoke = ( dryRun : boolean ) =>
349- spawnSync (
350- process . execPath ,
351- [
352- "--import" ,
353- "tsx" ,
354- "scripts/ci-refit-test-timings.mts" ,
355- "--runs" ,
356- "3" ,
357- "--repo" ,
358- "fixture/repo" ,
359- "--out" ,
360- output ,
361- ...( dryRun ? [ "--dry-run" ] : [ ] ) ,
362- ] ,
354+ ) ;
355+ chmodSync ( fakeGh , 0o755 ) ;
356+ const original = `${ JSON . stringify (
363357 {
364- cwd : root ,
365- encoding : "utf8" ,
366- env : { ...process . env , OPENCLAW_GH_BIN : fakeGh , GH_TOKEN : "fixture-token" } ,
358+ ...baseline ,
359+ compactGroupSeconds : { blacksmith : { deleted : 30 } , github : { } } ,
367360 } ,
368- ) ;
369- try {
370- const dryRun = invoke ( true ) ;
371- expect ( dryRun . status , dryRun . stderr ) . toBe ( 0 ) ;
372- expect ( dryRun . stdout ) . toContain ( `| uiE2e.fileSeconds.${ measuredFile } | 100 | 130 | 30.0% |` ) ;
373- expect ( dryRun . stdout ) . toContain (
374- "| compactGroupSeconds.blacksmith.deleted | 30 | — | removed |" ,
375- ) ;
376- expect ( dryRun . stdout ) . toContain ( "Sampled successful main CI runs: 1, 2, 3" ) ;
377- expect ( readFileSync ( output , "utf8" ) ) . toBe ( original ) ;
378- const write = invoke ( false ) ;
379- expect ( write . status , write . stderr ) . toBe ( 0 ) ;
380- const updated = readFileSync ( output , "utf8" ) ;
381- const timings = ciTestTimingsSchema . parse ( JSON . parse ( updated ) ) ;
382- expect ( timings . uiE2e . fileSeconds [ measuredFile ] ) . toBe ( 130 ) ;
383- expect ( timings . compactGroupSeconds ) . toEqual ( {
384- blacksmith : { "core-unit-src-security-2" : 20 } ,
385- github : { "core-unit-src-security-2" : 50 } ,
386- } ) ;
387- expect ( updated . endsWith ( "\n" ) ) . toBe ( true ) ;
388- const unchanged = invoke ( false ) ;
389- expect ( unchanged . status , unchanged . stderr ) . toBe ( 0 ) ;
390- expect ( unchanged . stdout ) . toContain ( "No timing changes" ) ;
391- expect ( readFileSync ( output , "utf8" ) ) . toBe ( updated ) ;
392- } finally {
393- rmSync ( directory , { recursive : true , force : true } ) ;
394- }
395- } ) ;
361+ null ,
362+ 2 ,
363+ ) } \n`;
364+ writeFileSync ( output , original ) ;
365+ const invoke = ( dryRun : boolean ) =>
366+ spawnSync (
367+ process . execPath ,
368+ [
369+ "--import" ,
370+ "tsx" ,
371+ "scripts/ci-refit-test-timings.mts" ,
372+ "--runs" ,
373+ "3" ,
374+ "--repo" ,
375+ "fixture/repo" ,
376+ "--out" ,
377+ output ,
378+ ...( dryRun ? [ "--dry-run" ] : [ ] ) ,
379+ ] ,
380+ {
381+ cwd : root ,
382+ encoding : "utf8" ,
383+ env : { ...process . env , OPENCLAW_GH_BIN : fakeGh , GH_TOKEN : "fixture-token" } ,
384+ } ,
385+ ) ;
386+ try {
387+ const dryRun = invoke ( true ) ;
388+ if ( invalidField ) {
389+ expect ( dryRun . status , dryRun . stderr ) . toBe ( 1 ) ;
390+ expect ( dryRun . stderr ) . toContain ( invalidField ) ;
391+ expect ( dryRun . stdout ) . not . toContain ( "Sampled successful main CI runs" ) ;
392+ expect ( readFileSync ( output , "utf8" ) ) . toBe ( original ) ;
393+ return ;
394+ }
395+ expect ( dryRun . status , dryRun . stderr ) . toBe ( 0 ) ;
396+ expect ( JSON . parse ( readFileSync ( requests , "utf8" ) ) ) . toEqual ( [
397+ "api" ,
398+ "repos/fixture/repo/actions/workflows/ci.yml/runs?branch=main&event=push&status=success&per_page=3&page=1" ,
399+ "--jq" ,
400+ "[.workflow_runs[] | {id, created_at, status, conclusion, event, head_branch, head_sha}]" ,
401+ ] ) ;
402+ expect ( dryRun . stdout ) . toContain (
403+ `| uiE2e.fileSeconds.${ measuredFile } | 100 | 130 | 30.0% |` ,
404+ ) ;
405+ expect ( dryRun . stdout ) . toContain (
406+ "| compactGroupSeconds.blacksmith.deleted | 30 | — | removed |" ,
407+ ) ;
408+ expect ( dryRun . stdout ) . toContain ( "Sampled successful main CI runs: 1, 2, 3" ) ;
409+ expect ( readFileSync ( output , "utf8" ) ) . toBe ( original ) ;
410+ const write = invoke ( false ) ;
411+ expect ( write . status , write . stderr ) . toBe ( 0 ) ;
412+ const updated = readFileSync ( output , "utf8" ) ;
413+ const timings = ciTestTimingsSchema . parse ( JSON . parse ( updated ) ) ;
414+ expect ( timings . uiE2e . fileSeconds [ measuredFile ] ) . toBe ( 130 ) ;
415+ expect ( timings . compactGroupSeconds ) . toEqual ( {
416+ blacksmith : { "core-unit-src-security-2" : 20 } ,
417+ github : { "core-unit-src-security-2" : 50 } ,
418+ } ) ;
419+ expect ( updated . endsWith ( "\n" ) ) . toBe ( true ) ;
420+ const unchanged = invoke ( false ) ;
421+ expect ( unchanged . status , unchanged . stderr ) . toBe ( 0 ) ;
422+ expect ( unchanged . stdout ) . toContain ( "No timing changes" ) ;
423+ expect ( readFileSync ( output , "utf8" ) ) . toBe ( updated ) ;
424+ } finally {
425+ rmSync ( directory , { recursive : true , force : true } ) ;
426+ }
427+ } ,
428+ ) ;
396429} ) ;
397430
398431describe ( "committed CI timing loader" , ( ) => {
0 commit comments