@@ -62,13 +62,14 @@ interface FsMockOptions {
6262 /** map of dir path -> entries returned by readdirSync */
6363 dirs ?: Record < string , string [ ] > ;
6464 touchError ?: Error ;
65+ initialMtimeMs ?: number ;
6566}
6667
67- function installFsMocks ( { existing, dirs, touchError } : FsMockOptions ) {
68+ function installFsMocks ( { existing, dirs, touchError, initialMtimeMs = 0 } : FsMockOptions ) {
6869 // Mutable, and returned, so tests can pre-seed a "lock already held" path or
6970 // assert the lock dir doesn't leak after ensureBrowser resolves.
7071 const paths = new Set ( existing ) ;
71- const mtimes = new Map ( [ ...existing ] . map ( ( p ) => [ p , 0 ] ) ) ;
72+ const mtimes = new Map ( [ ...existing ] . map ( ( p ) => [ p , initialMtimeMs ] ) ) ;
7273 vi . doMock ( "node:fs" , ( ) => ( {
7374 existsSync : ( p : string ) => paths . has ( p ) ,
7475 readdirSync : ( p : string ) => {
@@ -174,6 +175,7 @@ describe("findBrowser — cache resolution", () => {
174175 } ) ;
175176
176177 afterEach ( ( ) => {
178+ vi . useRealTimers ( ) ;
177179 Object . defineProperty ( process , "platform" , { value : origPlatform , configurable : true } ) ;
178180 Object . defineProperty ( process , "arch" , { value : origArch , configurable : true } ) ;
179181 vi . restoreAllMocks ( ) ;
@@ -347,6 +349,23 @@ describe("findBrowser — cache resolution", () => {
347349 expect ( paths . has ( HF_LOCK ) ) . toBe ( false ) ;
348350 } ) ;
349351
352+ it ( "withInstallLock recovers when a crashed reclaimer leaves both lock directories" , async ( ) => {
353+ vi . useFakeTimers ( ) ;
354+ const paths = installFsMocks ( {
355+ existing : new Set ( [ CACHE_ROOT , HF_LOCK , HF_RECLAIM_LOCK ] ) ,
356+ } ) ;
357+
358+ const { withInstallLock } = await import ( "./manager.js" ) ;
359+ const acquisition = withInstallLock ( async ( ) => "done" , TEST_LOCK_TIMINGS ) ;
360+ await vi . advanceTimersByTimeAsync ( TEST_LOCK_TIMINGS . pollMs * 2 ) ;
361+
362+ await expect ( Promise . race ( [ acquisition , Promise . resolve ( "still waiting" ) ] ) ) . resolves . toBe (
363+ "done" ,
364+ ) ;
365+ expect ( paths . has ( HF_LOCK ) ) . toBe ( false ) ;
366+ expect ( paths . has ( HF_RECLAIM_LOCK ) ) . toBe ( false ) ;
367+ } ) ;
368+
350369 it ( "withInstallLock does not reclaim another waiter's fresh lock after this waiter timed out" , async ( ) => {
351370 // Regression guard for the timeout-reclaim race: if multiple waiters cross
352371 // the stale-lock deadline together, waiter A can reclaim the stale lock and
@@ -392,7 +411,10 @@ describe("findBrowser — cache resolution", () => {
392411 } ) ;
393412
394413 it ( "withInstallLock reports progress while waiting instead of staying silent" , async ( ) => {
395- const paths = installFsMocks ( { existing : new Set ( [ CACHE_ROOT , HF_LOCK ] ) } ) ;
414+ const paths = installFsMocks ( {
415+ existing : new Set ( [ CACHE_ROOT , HF_LOCK ] ) ,
416+ initialMtimeMs : Date . now ( ) ,
417+ } ) ;
396418 const warnSpy = vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } ) ;
397419
398420 const { withInstallLock } = await import ( "./manager.js" ) ;
0 commit comments