1+ import assert from "node:assert/strict" ;
12import { execFileSync , spawn } from "node:child_process" ;
2- import { chmodSync , existsSync , readdirSync , writeFileSync } from "node:fs" ;
3- import { delimiter , join , resolve } from "node:path" ;
3+ import { chmodSync , existsSync , readFileSync , writeFileSync } from "node:fs" ;
4+ import { delimiter , dirname , join , resolve } from "node:path" ;
45import { afterEach , describe , expect , it } from "vitest" ;
56import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js" ;
67
@@ -29,6 +30,8 @@ describe("Plugin SDK API diff CLI", () => {
2930 const runnerTemp = tempDirs . make ( "plugin-sdk-api-diff-temp-" ) ;
3031 const binDir = tempDirs . make ( "plugin-sdk-api-diff-bin-" ) ;
3132 const pnpmMarker = join ( binDir , "pnpm-started" ) ;
33+ const runnerSentinel = join ( runnerTemp , "runner-owned.txt" ) ;
34+ writeFileSync ( runnerSentinel , "preserve\n" ) ;
3235
3336 git ( repo , [ "init" , "--quiet" , "--initial-branch=main" ] ) ;
3437 writeFileSync ( join ( repo , "README.md" ) , "fixture\n" ) ;
@@ -94,7 +97,14 @@ describe("Plugin SDK API diff CLI", () => {
9497 try {
9598 await waitFor ( ( ) => existsSync ( pnpmMarker ) || closed , 10_000 ) ;
9699 expect ( closed , stderr ) . toBe ( false ) ;
97- expect ( git ( repo , [ "worktree" , "list" ] ) ) . toContain ( runnerTemp ) ;
100+ const revisionRoot = git ( repo , [ "worktree" , "list" , "--porcelain" , "-z" ] )
101+ . split ( "\0" )
102+ . filter ( ( record ) => record . startsWith ( "worktree " ) )
103+ . map ( ( record ) => resolve ( record . slice ( "worktree " . length ) ) )
104+ . find ( ( root ) => dirname ( dirname ( root ) ) === runnerTemp ) ;
105+ assert ( revisionRoot , "expected a registered revision worktree under runner temp" ) ;
106+ const temporaryRoot = dirname ( revisionRoot ) ;
107+ expect ( existsSync ( temporaryRoot ) ) . toBe ( true ) ;
98108 const interruptedAt = Date . now ( ) ;
99109 child . kill ( "SIGTERM" ) ;
100110 const exitCode = await Promise . race ( [
@@ -107,7 +117,9 @@ describe("Plugin SDK API diff CLI", () => {
107117 expect ( exitCode ) . toBe ( 143 ) ;
108118 expect ( Date . now ( ) - interruptedAt ) . toBeLessThan ( 5_000 ) ;
109119 expect ( git ( repo , [ "worktree" , "list" ] ) ) . not . toContain ( runnerTemp ) ;
110- expect ( readdirSync ( runnerTemp ) ) . toEqual ( [ ] ) ;
120+ // Cleanup owns its temporary root, not runner instrumentation beside it.
121+ expect ( existsSync ( temporaryRoot ) ) . toBe ( false ) ;
122+ expect ( readFileSync ( runnerSentinel , "utf8" ) ) . toBe ( "preserve\n" ) ;
111123 } finally {
112124 if ( ! closed ) {
113125 child . kill ( "SIGKILL" ) ;
0 commit comments