11import { execFileSync } from "node:child_process" ;
22import fs from "node:fs" ;
3- import os from "node:os" ;
43import path from "node:path" ;
54import { afterEach , describe , expect , it } from "vitest" ;
65import { verifyScriptDeclarationContracts } from "../../scripts/check-script-declarations.mjs" ;
6+ import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js" ;
77
8- describe ( "script declaration contracts" , ( ) => {
9- const tempDirs : string [ ] = [ ] ;
10-
11- afterEach ( ( ) => {
12- for ( const tempDir of tempDirs . splice ( 0 ) ) {
13- fs . rmSync ( tempDir , { recursive : true , force : true } ) ;
14- }
15- } ) ;
8+ const tempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
169
10+ describe ( "script declaration contracts" , ( ) => {
1711 it ( "fails deliberate export drift and passes after regenerating the declaration" , ( ) => {
18- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
19- tempDirs . push ( root ) ;
12+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
2013 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
2114 fs . mkdirSync ( path . join ( root , "test" ) , { recursive : true } ) ;
2215 fs . writeFileSync (
@@ -47,8 +40,7 @@ describe("script declaration contracts", () => {
4740 } ) ;
4841
4942 it ( "requires declarations for scripts imported by typed sources" , ( ) => {
50- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
51- tempDirs . push ( root ) ;
43+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
5244 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
5345 fs . mkdirSync ( path . join ( root , "test" ) , { recursive : true } ) ;
5446 fs . writeFileSync ( path . join ( root , "scripts" , "missing.mjs" ) , "export const value = 1;\n" ) ;
@@ -69,8 +61,7 @@ describe("script declaration contracts", () => {
6961 } ) ;
7062
7163 it ( "rejects declaration sidecars whose runtime script is missing" , ( ) => {
72- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
73- tempDirs . push ( root ) ;
64+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
7465 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
7566 fs . mkdirSync ( path . join ( root , "test" ) , { recursive : true } ) ;
7667 fs . writeFileSync ( path . join ( root , "scripts" , "orphan.d.mts" ) , "export const value: 1;\n" ) ;
@@ -91,8 +82,7 @@ describe("script declaration contracts", () => {
9182 } ) ;
9283
9384 it ( "ignores tracked declaration pairs deleted from the working tree" , ( ) => {
94- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
95- tempDirs . push ( root ) ;
85+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
9686 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
9787 execFileSync ( "git" , [ "init" , "-q" ] , { cwd : root } ) ;
9888 const runtimePath = path . join ( root , "scripts" , "removed.mjs" ) ;
@@ -107,8 +97,7 @@ describe("script declaration contracts", () => {
10797 } ) ;
10898
10999 it ( "applies ESM default and ambiguity rules to star re-exports" , ( ) => {
110- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
111- tempDirs . push ( root ) ;
100+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
112101 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
113102 fs . writeFileSync (
114103 path . join ( root , "scripts" , "left.mjs" ) ,
@@ -143,8 +132,7 @@ describe("script declaration contracts", () => {
143132 } ) ;
144133
145134 it ( "propagates ambiguity through nested star re-exports" , ( ) => {
146- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
147- tempDirs . push ( root ) ;
135+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
148136 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
149137 fs . writeFileSync ( path . join ( root , "scripts" , "left.mjs" ) , "export const shared = 1;\n" ) ;
150138 fs . writeFileSync ( path . join ( root , "scripts" , "right.mjs" ) , "export const shared = 2;\n" ) ;
@@ -173,8 +161,7 @@ describe("script declaration contracts", () => {
173161 } ) ;
174162
175163 it ( "fails closed on cyclic star graphs" , ( ) => {
176- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
177- tempDirs . push ( root ) ;
164+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
178165 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
179166 fs . writeFileSync ( path . join ( root , "scripts" , "a.mjs" ) , 'export * from "./b.mjs";\n' ) ;
180167 fs . writeFileSync (
@@ -194,8 +181,7 @@ describe("script declaration contracts", () => {
194181 } ) ;
195182
196183 it ( "fails closed on explicit re-exports of ambiguous bindings" , ( ) => {
197- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
198- tempDirs . push ( root ) ;
184+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
199185 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
200186 fs . writeFileSync ( path . join ( root , "scripts" , "left.mjs" ) , "export const shared = 1;\n" ) ;
201187 fs . writeFileSync ( path . join ( root , "scripts" , "right.mjs" ) , "export const shared = 2;\n" ) ;
@@ -227,8 +213,7 @@ describe("script declaration contracts", () => {
227213 } ) ;
228214
229215 it ( "fails closed when a star re-export cannot be resolved" , ( ) => {
230- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
231- tempDirs . push ( root ) ;
216+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
232217 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
233218 fs . writeFileSync ( path . join ( root , "scripts" , "barrel.mjs" ) , 'export * from "package";\n' ) ;
234219 fs . writeFileSync ( path . join ( root , "scripts" , "barrel.d.mts" ) , "export {};\n" ) ;
@@ -245,8 +230,7 @@ describe("script declaration contracts", () => {
245230 } ) ;
246231
247232 it ( "resolves declaration stars to declarations before runtime modules" , ( ) => {
248- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
249- tempDirs . push ( root ) ;
233+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
250234 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
251235 fs . mkdirSync ( path . join ( root , "lib" ) , { recursive : true } ) ;
252236 fs . writeFileSync (
@@ -275,8 +259,7 @@ describe("script declaration contracts", () => {
275259 } ) ;
276260
277261 it ( "does not count type-only named or default declaration exports" , ( ) => {
278- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
279- tempDirs . push ( root ) ;
262+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
280263 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
281264 fs . writeFileSync (
282265 path . join ( root , "scripts" , "types.mjs" ) ,
@@ -299,8 +282,7 @@ describe("script declaration contracts", () => {
299282 } ) ;
300283
301284 it ( "preserves local binding identity through renamed star paths" , ( ) => {
302- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
303- tempDirs . push ( root ) ;
285+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
304286 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
305287 fs . writeFileSync (
306288 path . join ( root , "scripts" , "origin.mjs" ) ,
@@ -335,8 +317,7 @@ describe("script declaration contracts", () => {
335317 } ) ;
336318
337319 it ( "preserves statically named exports from external modules" , ( ) => {
338- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
339- tempDirs . push ( root ) ;
320+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
340321 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
341322 fs . writeFileSync (
342323 path . join ( root , "scripts" , "external.mjs" ) ,
@@ -356,8 +337,7 @@ describe("script declaration contracts", () => {
356337 } ) ;
357338
358339 it ( "fails closed on external declaration re-exports with unknown value provenance" , ( ) => {
359- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
360- tempDirs . push ( root ) ;
340+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
361341 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
362342 fs . writeFileSync ( path . join ( root , "scripts" , "external.mjs" ) , "export const Foo = 1;\n" ) ;
363343 fs . writeFileSync (
@@ -379,8 +359,7 @@ describe("script declaration contracts", () => {
379359 } ) ;
380360
381361 it ( "fails closed on re-exported external declaration imports" , ( ) => {
382- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
383- tempDirs . push ( root ) ;
362+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
384363 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
385364 fs . writeFileSync ( path . join ( root , "scripts" , "external.mjs" ) , "export const Foo = 1;\n" ) ;
386365 fs . writeFileSync (
@@ -402,8 +381,7 @@ describe("script declaration contracts", () => {
402381 } ) ;
403382
404383 it ( "accepts statically value-bearing external namespace declaration imports" , ( ) => {
405- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
406- tempDirs . push ( root ) ;
384+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
407385 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
408386 fs . writeFileSync (
409387 path . join ( root , "scripts" , "external.mjs" ) ,
@@ -423,8 +401,7 @@ describe("script declaration contracts", () => {
423401 } ) ;
424402
425403 it ( "retains runtime exports imported from opaque local modules" , ( ) => {
426- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
427- tempDirs . push ( root ) ;
404+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
428405 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
429406 fs . writeFileSync ( path . join ( root , "scripts" , "config.json" ) , '{"enabled":true}\n' ) ;
430407 fs . writeFileSync (
@@ -445,8 +422,7 @@ describe("script declaration contracts", () => {
445422 } ) ;
446423
447424 it ( "fails closed on missing exports from analyzable local modules" , ( ) => {
448- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
449- tempDirs . push ( root ) ;
425+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
450426 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
451427 fs . writeFileSync ( path . join ( root , "scripts" , "origin.mjs" ) , "export const value = 1;\n" ) ;
452428 fs . writeFileSync (
@@ -467,8 +443,7 @@ describe("script declaration contracts", () => {
467443 } ) ;
468444
469445 it ( "fails closed on named JSON imports" , ( ) => {
470- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
471- tempDirs . push ( root ) ;
446+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
472447 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
473448 fs . writeFileSync ( path . join ( root , "scripts" , "config.json" ) , '{"enabled":true}\n' ) ;
474449 fs . writeFileSync (
@@ -492,8 +467,7 @@ describe("script declaration contracts", () => {
492467 } ) ;
493468
494469 it ( "resolves CommonJS imports to adjacent declaration sidecars" , ( ) => {
495- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
496- tempDirs . push ( root ) ;
470+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
497471 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
498472 fs . writeFileSync ( path . join ( root , "scripts" , "helper.cjs" ) , "exports.value = 1;\n" ) ;
499473 fs . writeFileSync ( path . join ( root , "scripts" , "helper.d.cts" ) , "export const value: 1;\n" ) ;
@@ -520,8 +494,7 @@ describe("script declaration contracts", () => {
520494 } ) ;
521495
522496 it ( "distinguishes namespace objects from named external bindings" , ( ) => {
523- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-script-declarations-" ) ) ;
524- tempDirs . push ( root ) ;
497+ const root = tempDirs . make ( "openclaw-script-declarations-" ) ;
525498 fs . mkdirSync ( path . join ( root , "scripts" ) , { recursive : true } ) ;
526499 fs . writeFileSync (
527500 path . join ( root , "scripts" , "namespace.mjs" ) ,
0 commit comments