11import { execFileSync } from "node:child_process" ;
2- import { existsSync , mkdirSync , readFileSync , writeFileSync } from "node:fs" ;
2+ import {
3+ constants as fsConstants ,
4+ cpSync ,
5+ existsSync ,
6+ mkdirSync ,
7+ readFileSync ,
8+ writeFileSync ,
9+ } from "node:fs" ;
310import path from "node:path" ;
11+ import { afterAll } from "vitest" ;
12+ import { createTempDirTracker } from "../helpers/temp-dir.js" ;
13+
14+ const templateDirs = createTempDirTracker ( ) ;
15+ let performanceTemplate : string | undefined ;
16+ afterAll ( ( ) => {
17+ templateDirs . cleanup ( ) ;
18+ performanceTemplate = undefined ;
19+ } ) ;
420
521export type PerformanceFixtureOptions = {
622 mode : "target" | "record" | "tested" | "kova" | "baseline" | "prepare" | "publish" ;
@@ -38,11 +54,6 @@ export function preparePerformanceFixture(root: string, options: PerformanceFixt
3854 mkdirSync ( path . dirname ( file ) , { recursive : true } ) ;
3955 writeFileSync ( file , value ) ;
4056 } ;
41- mkdirSync ( temp , { recursive : true } ) ;
42- mkdirSync ( seed ) ;
43- run ( workspace , "init" , "--bare" , "--initial-branch=main" , remote ) ;
44- run ( seed , "init" , "--initial-branch=main" ) ;
45- write ( path . join ( seed , "README.md" ) , "fixture\n" ) ;
4657 const dest = "openclaw-performance/main/123-1/mock-provider" ;
4758 const previous = "openclaw-performance/main/100-1/mock-provider" ;
4859 const pointer = "openclaw-performance/main/latest-mock-provider.json" ;
@@ -53,19 +64,37 @@ export function preparePerformanceFixture(root: string, options: PerformanceFixt
5364 run ( cwd , "add" , "." ) ;
5465 run ( cwd , "commit" , "-m" , "fixture report" ) ;
5566 } ;
56- if ( options . baseline !== "absent" ) {
57- commitReport ( seed , options . duplicate ? dest : previous ) ;
58- if ( options . baseline === "invalid" ) write ( path . join ( seed , pointer ) , "{invalid" ) ;
59- if ( options . baseline === "trailing-newline" )
60- write ( path . join ( seed , pointer ) , JSON . stringify ( { path : previous + "\n" } ) ) ;
67+ const reuseDefault = ! options . baseline && ! options . duplicate ;
68+ const copyOptions = { recursive : true , mode : fsConstants . COPYFILE_FICLONE } ;
69+ if ( reuseDefault && performanceTemplate ) {
70+ cpSync ( performanceTemplate , workspace , copyOptions ) ;
71+ } else {
72+ mkdirSync ( temp , { recursive : true } ) ;
73+ mkdirSync ( seed ) ;
74+ run ( workspace , "init" , "--bare" , "--initial-branch=main" , remote ) ;
75+ run ( seed , "init" , "--initial-branch=main" ) ;
76+ write ( path . join ( seed , "README.md" ) , "fixture\n" ) ;
77+ if ( options . baseline !== "absent" ) {
78+ commitReport ( seed , options . duplicate ? dest : previous ) ;
79+ if ( options . baseline === "invalid" ) write ( path . join ( seed , pointer ) , "{invalid" ) ;
80+ if ( options . baseline === "trailing-newline" )
81+ write ( path . join ( seed , pointer ) , JSON . stringify ( { path : previous + "\n" } ) ) ;
82+ }
83+ run ( seed , "add" , "." ) ;
84+ run ( seed , "commit" , "--allow-empty" , "-m" , "fixture seed" ) ;
85+ run ( seed , "push" , remote , "HEAD:main" ) ;
86+ run ( workspace , "init" , "--initial-branch=main" ) ;
87+ write ( path . join ( workspace , "src/config/zod-schema.agent-defaults.ts" ) , " mediaModels: z\n" ) ;
88+ run ( workspace , "add" , "src" ) ;
89+ run ( workspace , "commit" , "-m" , "fixture target" ) ;
90+ if ( reuseDefault ) {
91+ // Snapshot complete repositories before any scenario mutation. Copies own
92+ // their refs, index and objects; no live process or absolute remote is shared.
93+ const template = templateDirs . make ( "openclaw-performance-template-" ) ;
94+ cpSync ( workspace , template , copyOptions ) ;
95+ performanceTemplate = template ;
96+ }
6197 }
62- run ( seed , "add" , "." ) ;
63- run ( seed , "commit" , "--allow-empty" , "-m" , "fixture seed" ) ;
64- run ( seed , "push" , remote , "HEAD:main" ) ;
65- run ( workspace , "init" , "--initial-branch=main" ) ;
66- write ( path . join ( workspace , "src/config/zod-schema.agent-defaults.ts" ) , " mediaModels: z\n" ) ;
67- run ( workspace , "add" , "src" ) ;
68- run ( workspace , "commit" , "-m" , "fixture target" ) ;
6998 const target = run ( workspace , "rev-parse" , "HEAD" ) ;
7099 mkdirSync ( reports ) ;
71100 let reportCommit = "a" . repeat ( 40 ) ;
0 commit comments