@@ -10,6 +10,8 @@ import { resolveConfigPath, resolveStateDir } from "../config/paths.js";
1010import { createSuiteTempRootTracker } from "../test-helpers/temp-dir.js" ;
1111import { acquireGatewayLock , GatewayLockError , type GatewayLockOptions } from "./gateway-lock.js" ;
1212
13+ type GatewayLock = NonNullable < Awaited < ReturnType < typeof acquireGatewayLock > > > ;
14+
1315const fixtureRootTracker = createSuiteTempRootTracker ( { prefix : "openclaw-gateway-lock-" } ) ;
1416let fixtureRoot = "" ;
1517const realNow = Date . now . bind ( Date ) ;
@@ -47,6 +49,14 @@ async function acquireForTest(
4749 } ) ;
4850}
4951
52+ function expectGatewayLock ( lock : Awaited < ReturnType < typeof acquireGatewayLock > > ) : GatewayLock {
53+ expect ( lock ) . toEqual ( expect . objectContaining ( { release : expect . any ( Function ) } ) ) ;
54+ if ( lock === null ) {
55+ throw new Error ( "Expected gateway lock" ) ;
56+ }
57+ return lock ;
58+ }
59+
5060function resolveLockPath ( env : NodeJS . ProcessEnv ) {
5161 const stateDir = resolveStateDir ( env ) ;
5262 const configPath = resolveConfigPath ( env , stateDir ) ;
@@ -175,17 +185,17 @@ describe("gateway lock", () => {
175185 vi . useRealTimers ( ) ;
176186 const env = await makeEnv ( ) ;
177187 const lock = await acquireForTest ( env , { timeoutMs : 50 } ) ;
178- expect ( lock ) . not . toBeNull ( ) ;
188+ const acquiredLock = expectGatewayLock ( lock ) ;
179189
180190 const pending = acquireForTest ( env , {
181191 timeoutMs : 15 ,
182192 readProcessCmdline : ( ) => [ "openclaw" , "gateway" , "run" ] ,
183193 } ) ;
184194 await expect ( pending ) . rejects . toBeInstanceOf ( GatewayLockError ) ;
185195
186- await lock ? .release ( ) ;
196+ await acquiredLock . release ( ) ;
187197 const lock2 = await acquireForTest ( env ) ;
188- await lock2 ? .release ( ) ;
198+ await expectGatewayLock ( lock2 ) . release ( ) ;
189199 } ) ;
190200
191201 it ( "treats recycled linux pid as stale when start time mismatches" , async ( ) => {
@@ -204,9 +214,9 @@ describe("gateway lock", () => {
204214 pollIntervalMs : 5 ,
205215 platform : "linux" ,
206216 } ) ;
207- expect ( lock ) . not . toBeNull ( ) ;
217+ const acquiredLock = expectGatewayLock ( lock ) ;
208218
209- await lock ? .release ( ) ;
219+ await acquiredLock . release ( ) ;
210220 spy . mockRestore ( ) ;
211221 } ) ;
212222
@@ -259,8 +269,7 @@ describe("gateway lock", () => {
259269 platform : "darwin" ,
260270 port : 18789 ,
261271 } ) ;
262- expect ( lock ) . not . toBeNull ( ) ;
263- await lock ?. release ( ) ;
272+ await expectGatewayLock ( lock ) . release ( ) ;
264273 connectSpy . mockRestore ( ) ;
265274 } ) ;
266275
@@ -329,8 +338,7 @@ describe("gateway lock", () => {
329338 port : 18789 ,
330339 readProcessCmdline : ( ) => [ "chrome.exe" , "--no-sandbox" ] ,
331340 } ) ;
332- expect ( lock ) . not . toBeNull ( ) ;
333- await lock ?. release ( ) ;
341+ await expectGatewayLock ( lock ) . release ( ) ;
334342
335343 connectSpy . mockRestore ( ) ;
336344 } ) ;
@@ -394,8 +402,7 @@ describe("gateway lock", () => {
394402 port : 18789 ,
395403 readProcessCmdline : ( ) => [ "/Applications/Safari.app/Contents/MacOS/Safari" ] ,
396404 } ) ;
397- expect ( lock ) . not . toBeNull ( ) ;
398- await lock ?. release ( ) ;
405+ await expectGatewayLock ( lock ) . release ( ) ;
399406
400407 connectSpy . mockRestore ( ) ;
401408 } ) ;
0 commit comments