11import fs from "node:fs" ;
2- import os from "node:os" ;
32import path from "node:path" ;
43import { afterEach , describe , expect , it } from "vitest" ;
54import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js" ;
6- import { resolvePluginNpmGenerationProjectDir } from "./install-paths.js" ;
5+ import {
6+ resolvePluginNpmGenerationProjectDir ,
7+ resolvePluginNpmProjectDir ,
8+ } from "./install-paths.js" ;
79import { RETAINED_MANAGED_NPM_KEEP_FILES_REASON } from "./managed-npm-retention-contract.js" ;
8-
9- const retentionTempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
1010import {
1111 cleanupRetainedManagedNpmInstallGenerations ,
1212 hasRetainedManagedNpmInstallMarker ,
1313 markRetainedManagedNpmInstall ,
1414} from "./managed-npm-retention.js" ;
1515
16+ const retentionTempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
17+
1618describe ( "managed npm retention" , ( ) => {
17- it ( "cleans retired generations while preserving the active install root" , async ( ) => {
18- const stateDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-retention-" ) ) ;
19- const npmDir = path . join ( stateDir , "npm" ) ;
20- const packageName = "@openclaw/codex" ;
21- const oldProjectRoot = resolvePluginNpmGenerationProjectDir ( {
22- npmDir,
23- packageName,
24- generationKey : "codex-v1" ,
25- } ) ;
26- const activeProjectRoot = resolvePluginNpmGenerationProjectDir ( {
27- npmDir,
28- packageName,
29- generationKey : "codex-v2" ,
30- } ) ;
31- const oldPackageDir = path . join ( oldProjectRoot , "node_modules" , "@openclaw" , "codex" ) ;
32- const activePackageDir = path . join ( activeProjectRoot , "node_modules" , "@openclaw" , "codex" ) ;
33- fs . mkdirSync ( oldPackageDir , { recursive : true } ) ;
34- fs . mkdirSync ( activePackageDir , { recursive : true } ) ;
35- await markRetainedManagedNpmInstall ( {
36- packageDir : oldPackageDir ,
37- pluginId : "codex" ,
38- reason : "test-retired-generation" ,
39- } ) ;
19+ it . each ( [ "ordinary" , "generation" ] as const ) (
20+ "cleans a retired %s project while preserving the active install root" ,
21+ async ( layout ) => {
22+ const stateDir = retentionTempDirs . make ( "openclaw-retention-" ) ;
23+ const npmDir = path . join ( stateDir , "npm" ) ;
24+ const packageName = "@openclaw/codex" ;
25+ const oldProjectRoot =
26+ layout === "ordinary"
27+ ? resolvePluginNpmProjectDir ( { npmDir, packageName } )
28+ : resolvePluginNpmGenerationProjectDir ( {
29+ npmDir,
30+ packageName,
31+ generationKey : "codex-v1" ,
32+ } ) ;
33+ const activeProjectRoot = resolvePluginNpmGenerationProjectDir ( {
34+ npmDir,
35+ packageName,
36+ generationKey : "codex-v2" ,
37+ } ) ;
38+ const oldPackageDir = path . join ( oldProjectRoot , "node_modules" , "@openclaw" , "codex" ) ;
39+ const activePackageDir = path . join ( activeProjectRoot , "node_modules" , "@openclaw" , "codex" ) ;
40+ fs . mkdirSync ( oldPackageDir , { recursive : true } ) ;
41+ fs . mkdirSync ( activePackageDir , { recursive : true } ) ;
42+ await markRetainedManagedNpmInstall ( {
43+ packageDir : oldPackageDir ,
44+ pluginId : "codex" ,
45+ reason : "test-retired-generation" ,
46+ } ) ;
4047
41- try {
4248 await expect (
4349 cleanupRetainedManagedNpmInstallGenerations ( {
4450 npmDir,
@@ -48,13 +54,11 @@ describe("managed npm retention", () => {
4854 expect ( fs . existsSync ( oldProjectRoot ) ) . toBe ( false ) ;
4955 expect ( fs . existsSync ( activeProjectRoot ) ) . toBe ( true ) ;
5056 expect ( hasRetainedManagedNpmInstallMarker ( activePackageDir ) ) . toBe ( false ) ;
51- } finally {
52- fs . rmSync ( stateDir , { recursive : true , force : true } ) ;
53- }
54- } ) ;
57+ } ,
58+ ) ;
5559
5660 it ( "cleans retained packages from the legacy shared npm root" , async ( ) => {
57- const stateDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-retention-" ) ) ;
61+ const stateDir = retentionTempDirs . make ( "openclaw-retention-" ) ;
5862 const npmDir = path . join ( stateDir , "npm" ) ;
5963 const packageDir = path . join ( npmDir , "node_modules" , "@openclaw" , "codex" ) ;
6064 fs . mkdirSync ( packageDir , { recursive : true } ) ;
@@ -64,17 +68,55 @@ describe("managed npm retention", () => {
6468 reason : "test-legacy-generation" ,
6569 } ) ;
6670
67- try {
68- await expect (
69- cleanupRetainedManagedNpmInstallGenerations ( {
70- npmDir,
71- } ) ,
72- ) . resolves . toBe ( 1 ) ;
73- expect ( fs . existsSync ( packageDir ) ) . toBe ( false ) ;
74- expect ( hasRetainedManagedNpmInstallMarker ( packageDir ) ) . toBe ( false ) ;
75- } finally {
76- fs . rmSync ( stateDir , { recursive : true , force : true } ) ;
77- }
71+ await expect (
72+ cleanupRetainedManagedNpmInstallGenerations ( {
73+ npmDir,
74+ } ) ,
75+ ) . resolves . toBe ( 1 ) ;
76+ expect ( fs . existsSync ( packageDir ) ) . toBe ( false ) ;
77+ expect ( hasRetainedManagedNpmInstallMarker ( packageDir ) ) . toBe ( false ) ;
78+ } ) ;
79+
80+ it ( "preserves a noncanonical project root even when it has a retained marker" , async ( ) => {
81+ const stateDir = retentionTempDirs . make ( "openclaw-retention-noncanonical-" ) ;
82+ const npmDir = path . join ( stateDir , "npm" ) ;
83+ const projectRoot = path . join ( npmDir , "projects" , "noncanonical-sibling" ) ;
84+ const packageDir = path . join ( projectRoot , "node_modules" , "@openclaw" , "codex" ) ;
85+ const siblingFile = path . join ( projectRoot , "must-remain.txt" ) ;
86+ fs . mkdirSync ( packageDir , { recursive : true } ) ;
87+ fs . writeFileSync ( siblingFile , "preserve me" , "utf8" ) ;
88+ await markRetainedManagedNpmInstall ( {
89+ packageDir,
90+ pluginId : "codex" ,
91+ reason : "test-retired-generation" ,
92+ } ) ;
93+
94+ await expect ( cleanupRetainedManagedNpmInstallGenerations ( { npmDir } ) ) . resolves . toBe ( 0 ) ;
95+ expect ( fs . readFileSync ( siblingFile , "utf8" ) ) . toBe ( "preserve me" ) ;
96+ } ) ;
97+
98+ it ( "does not follow a substituted managed projects directory" , async ( ) => {
99+ const stateDir = retentionTempDirs . make ( "openclaw-retention-symlink-" ) ;
100+ const npmDir = path . join ( stateDir , "npm" ) ;
101+ const outsideProjectsDir = retentionTempDirs . make ( "openclaw-retention-outside-" ) ;
102+ fs . mkdirSync ( npmDir , { recursive : true } ) ;
103+ fs . symlinkSync ( outsideProjectsDir , path . join ( npmDir , "projects" ) , "dir" ) ;
104+ const projectRoot = resolvePluginNpmProjectDir ( {
105+ npmDir,
106+ packageName : "@openclaw/codex" ,
107+ } ) ;
108+ const packageDir = path . join ( projectRoot , "node_modules" , "@openclaw" , "codex" ) ;
109+ const sentinel = path . join ( projectRoot , "must-remain.txt" ) ;
110+ fs . mkdirSync ( packageDir , { recursive : true } ) ;
111+ fs . writeFileSync ( sentinel , "preserve me" , "utf8" ) ;
112+ await markRetainedManagedNpmInstall ( {
113+ packageDir,
114+ pluginId : "codex" ,
115+ reason : "test-retired-generation" ,
116+ } ) ;
117+
118+ await expect ( cleanupRetainedManagedNpmInstallGenerations ( { npmDir } ) ) . resolves . toBe ( 0 ) ;
119+ expect ( fs . readFileSync ( sentinel , "utf8" ) ) . toBe ( "preserve me" ) ;
78120 } ) ;
79121
80122 it . each ( [ "project" , "legacy" ] as const ) (
@@ -98,13 +140,9 @@ describe("managed npm retention", () => {
98140 reason : RETAINED_MANAGED_NPM_KEEP_FILES_REASON ,
99141 } ) ;
100142
101- try {
102- await expect ( cleanupRetainedManagedNpmInstallGenerations ( { npmDir } ) ) . resolves . toBe ( 0 ) ;
103- expect ( fs . existsSync ( packageDir ) ) . toBe ( true ) ;
104- expect ( hasRetainedManagedNpmInstallMarker ( packageDir ) ) . toBe ( true ) ;
105- } finally {
106- fs . rmSync ( stateDir , { recursive : true , force : true } ) ;
107- }
143+ await expect ( cleanupRetainedManagedNpmInstallGenerations ( { npmDir } ) ) . resolves . toBe ( 0 ) ;
144+ expect ( fs . existsSync ( packageDir ) ) . toBe ( true ) ;
145+ expect ( hasRetainedManagedNpmInstallMarker ( packageDir ) ) . toBe ( true ) ;
108146 } ,
109147 ) ;
110148} ) ;
0 commit comments