@@ -2,6 +2,8 @@ import { spawnSync } from "node:child_process";
22import {
33 existsSync ,
44 chmodSync ,
5+ constants as fsConstants ,
6+ cpSync ,
57 mkdirSync ,
68 readFileSync ,
79 realpathSync ,
@@ -10,10 +12,13 @@ import {
1012 writeFileSync ,
1113} from "node:fs" ;
1214import { join } from "node:path" ;
13- import { afterEach , describe , expect , it } from "vitest" ;
15+ import { afterAll , afterEach , describe , expect , it } from "vitest" ;
1416import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js" ;
1517
1618const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
19+ const templateDirs = useAutoCleanupTempDirTracker ( afterAll ) ;
20+ let fixtureTemplate : ReturnType < typeof createFixtureTemplate > | undefined ;
21+ let reviewFixtureTemplate : ReturnType < typeof createReviewFixtureTemplate > | undefined ;
1722const repoRoot = process . cwd ( ) ;
1823const commonScript = join ( repoRoot , "scripts/pr-lib/common.sh" ) ;
1924const worktreeScript = join ( repoRoot , "scripts/pr-lib/worktree.sh" ) ;
@@ -38,15 +43,24 @@ function git(root: string, ...args: string[]) {
3843 return result . stdout . trim ( ) ;
3944}
4045
41- function createFixture ( ) : Fixture {
42- const root = tempDirs . make ( "openclaw-pr-worktree-containment-" ) ;
46+ function createFixtureTemplate ( ) {
47+ const root = templateDirs . make ( "openclaw-pr-worktree-containment-template -" ) ;
4348 git ( root , "init" , "--initial-branch=main" ) ;
4449 git ( root , "config" , "user.name" , "OpenClaw Test" ) ;
4550 git ( root , "config" , "user.email" , "[email protected] " ) ; 4651 writeFileSync ( join ( root , "fixture.txt" ) , "main\n" ) ;
4752 git ( root , "add" , "fixture.txt" ) ;
4853 git ( root , "commit" , "-m" , "main fixture" ) ;
4954 const mainSha = git ( root , "rev-parse" , "HEAD" ) ;
55+ return { root, mainSha } ;
56+ }
57+
58+ function createFixture ( ) : Fixture {
59+ const template = ( fixtureTemplate ??= createFixtureTemplate ( ) ) ;
60+ const root = tempDirs . make ( "openclaw-pr-worktree-containment-" ) ;
61+ // Copy complete history before worktrees exist; each case owns its fetch and refs.
62+ cpSync ( template . root , root , { recursive : true , mode : fsConstants . COPYFILE_FICLONE } ) ;
63+ const { mainSha } = template ;
5064 git ( root , "remote" , "add" , "origin" , root ) ;
5165 git ( root , "fetch" , "origin" ) ;
5266 git ( root , "checkout" , "-b" , "sibling/work" ) ;
@@ -60,8 +74,8 @@ function createFixture(): Fixture {
6074 } ;
6175}
6276
63- function createReviewFixture ( ) : ReviewFixture {
64- const root = tempDirs . make ( "openclaw-pr-review-transition-" ) ;
77+ function createReviewFixtureTemplate ( ) {
78+ const root = templateDirs . make ( "openclaw-pr-review-transition-template -" ) ;
6579 git ( root , "init" , "--initial-branch=main" ) ;
6680 git ( root , "config" , "user.name" , "OpenClaw Test" ) ;
6781 git ( root , "config" , "user.email" , "[email protected] " ) ; @@ -90,6 +104,14 @@ function createReviewFixture(): ReviewFixture {
90104 git ( root , "add" , "main-only.txt" ) ;
91105 git ( root , "commit" , "-m" , "advance main fixture" ) ;
92106 const mainSha = git ( root , "rev-parse" , "HEAD" ) ;
107+ return { root, mainSha, prASha, prBSha } ;
108+ }
109+
110+ function createReviewFixture ( ) : ReviewFixture {
111+ const template = ( reviewFixtureTemplate ??= createReviewFixtureTemplate ( ) ) ;
112+ const root = tempDirs . make ( "openclaw-pr-review-transition-" ) ;
113+ cpSync ( template . root , root , { recursive : true , mode : fsConstants . COPYFILE_FICLONE } ) ;
114+ const { mainSha, prASha, prBSha } = template ;
93115 git ( root , "remote" , "add" , "origin" , root ) ;
94116 git ( root , "fetch" , "origin" ) ;
95117
0 commit comments