@@ -43,10 +43,12 @@ describe("compartment state lease", () => {
4343 const db = makeDb ( ) ;
4444 const first = acquireCompartmentLease ( db , "ses" , "holder-a" ) ;
4545 expect ( first ) . not . toBeNull ( ) ;
46+
4647 db . prepare ( "UPDATE compartment_state_lease SET expires_at = ? WHERE session_id = ?" ) . run (
4748 Date . now ( ) + 1_000 ,
4849 "ses" ,
4950 ) ;
51+
5052 const second = acquireCompartmentLease ( db , "ses" , "holder-a" ) ;
5153 expect ( second ) . not . toBeNull ( ) ;
5254 expect ( second ! . expiresAt ) . toBeGreaterThan ( first ! . acquiredAt + 1_000 ) ;
@@ -56,10 +58,12 @@ describe("compartment state lease", () => {
5658 it ( "lets another holder reclaim an expired lease" , ( ) => {
5759 const db = makeDb ( ) ;
5860 expect ( acquireCompartmentLease ( db , "ses" , "holder-a" ) ) . not . toBeNull ( ) ;
61+
5962 db . prepare ( "UPDATE compartment_state_lease SET expires_at = ? WHERE session_id = ?" ) . run (
6063 Date . now ( ) - 1 ,
6164 "ses" ,
6265 ) ;
66+
6367 expect ( acquireCompartmentLease ( db , "ses" , "holder-b" ) ) . not . toBeNull ( ) ;
6468 expect ( isCompartmentLeaseHeld ( db , "ses" , "holder-b" ) ) . toBe ( true ) ;
6569 expect ( isCompartmentLeaseHeld ( db , "ses" , "holder-a" ) ) . toBe ( false ) ;
@@ -70,6 +74,7 @@ describe("compartment state lease", () => {
7074 const db = makeDb ( ) ;
7175 expect ( acquireCompartmentLease ( db , "ses" , "holder-a" ) ) . not . toBeNull ( ) ;
7276 expect ( renewCompartmentLease ( db , "ses" , "holder-b" ) ) . toBe ( false ) ;
77+
7378 db . prepare ( "UPDATE compartment_state_lease SET expires_at = ? WHERE session_id = ?" ) . run (
7479 Date . now ( ) - 1 ,
7580 "ses" ,
@@ -81,10 +86,12 @@ describe("compartment state lease", () => {
8186 it ( "release is a no-op after another holder reclaims the row" , ( ) => {
8287 const db = makeDb ( ) ;
8388 expect ( acquireCompartmentLease ( db , "ses" , "holder-a" ) ) . not . toBeNull ( ) ;
89+
8490 db . prepare ( "UPDATE compartment_state_lease SET expires_at = ? WHERE session_id = ?" ) . run (
8591 Date . now ( ) - 1 ,
8692 "ses" ,
8793 ) ;
94+
8895 expect ( acquireCompartmentLease ( db , "ses" , "holder-b" ) ) . not . toBeNull ( ) ;
8996 releaseCompartmentLease ( db , "ses" , "holder-a" ) ;
9097 expect ( isCompartmentLeaseHeld ( db , "ses" , "holder-b" ) ) . toBe ( true ) ;
@@ -105,7 +112,11 @@ describe("compartment state lease", () => {
105112 } finally {
106113 closeQuietly ( dbA ) ;
107114 closeQuietly ( dbB ) ;
108- rmSync ( dir , { recursive : true , force : true } ) ;
115+ try {
116+ rmSync ( dir , { recursive : true , force : true , maxRetries : 10 , retryDelay : 100 } ) ;
117+ } catch {
118+ // Ignore EBUSY on Windows
119+ }
109120 }
110121 } ) ;
111122
@@ -114,10 +125,13 @@ describe("compartment state lease", () => {
114125 const path = join ( dir , "context.db" ) ;
115126 const setup = makeDb ( path ) ;
116127 closeQuietly ( setup ) ;
128+
117129 try {
118- const pluginRoot = process . cwd ( ) . endsWith ( "/packages/plugin" )
119- ? process . cwd ( )
120- : join ( process . cwd ( ) , "packages" , "plugin" ) ;
130+ const projectRoot = process . cwd ( ) . includes ( "packages" )
131+ ? join ( process . cwd ( ) , ".." , ".." )
132+ : process . cwd ( ) ;
133+ const pluginRoot = join ( projectRoot , "packages" , "plugin" ) ;
134+
121135 const script = `
122136 const sqlite = await import(${ JSON . stringify ( `file://${ pluginRoot } /src/shared/sqlite.ts` ) } );
123137 const storageDb = await import(${ JSON . stringify ( `file://${ pluginRoot } /src/features/magic-context/storage-db.ts` ) } );
@@ -128,13 +142,18 @@ describe("compartment state lease", () => {
128142 db.close();
129143 console.log(JSON.stringify({ ok }));
130144 ` ;
145+
131146 const [ a , b ] = await Promise . all ( [
132147 $ `bun -e ${ script } holder-a` . json ( ) as Promise < { ok : boolean } > ,
133148 $ `bun -e ${ script } holder-b` . json ( ) as Promise < { ok : boolean } > ,
134149 ] ) ;
135150 expect ( [ a . ok , b . ok ] . filter ( Boolean ) ) . toHaveLength ( 1 ) ;
136151 } finally {
137- rmSync ( dir , { recursive : true , force : true } ) ;
152+ try {
153+ rmSync ( dir , { recursive : true , force : true , maxRetries : 10 , retryDelay : 100 } ) ;
154+ } catch {
155+ // Ignore EBUSY on Windows
156+ }
138157 }
139158 } ) ;
140159} ) ;
0 commit comments