diff --git a/src/gateway/server-startup-post-attach.test.ts b/src/gateway/server-startup-post-attach.test.ts index 325364a52be2..af80c89be150 100644 --- a/src/gateway/server-startup-post-attach.test.ts +++ b/src/gateway/server-startup-post-attach.test.ts @@ -563,15 +563,19 @@ describe("startGatewayPostAttachRuntime", () => { it("skips heavy restart sentinel refresh when no sentinel file exists", async () => { const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-no-sentinel-")); - vi.stubEnv("OPENCLAW_STATE_DIR", stateDir); - hoisted.refreshLatestUpdateRestartSentinel.mockClear(); + try { + await withEnvAsync({ OPENCLAW_STATE_DIR: stateDir }, async () => { + hoisted.refreshLatestUpdateRestartSentinel.mockClear(); - const result = await testing.refreshLatestUpdateRestartSentinelIfPresent(); + const result = await testing.refreshLatestUpdateRestartSentinelIfPresent(); - expect(result).toBeNull(); - expect(hoisted.refreshLatestUpdateRestartSentinel).not.toHaveBeenCalled(); - closeOpenClawStateDatabaseForTest(); - fs.rmSync(stateDir, { recursive: true, force: true }); + expect(result).toBeNull(); + expect(hoisted.refreshLatestUpdateRestartSentinel).not.toHaveBeenCalled(); + }); + } finally { + closeOpenClawStateDatabaseForTest(); + fs.rmSync(stateDir, { recursive: true, force: true }); + } }); it("refreshes the restart sentinel when the sentinel row exists", async () => { @@ -585,15 +589,16 @@ describe("startGatewayPostAttachRuntime", () => { }, { OPENCLAW_STATE_DIR: stateDir } as NodeJS.ProcessEnv, ); - vi.stubEnv("OPENCLAW_STATE_DIR", stateDir); - const sentinel = { kind: "update", status: "ok", ts: 1 } as const; - hoisted.refreshLatestUpdateRestartSentinel.mockClear(); - hoisted.refreshLatestUpdateRestartSentinel.mockResolvedValue(sentinel); + await withEnvAsync({ OPENCLAW_STATE_DIR: stateDir }, async () => { + const sentinel = { kind: "update", status: "ok", ts: 1 } as const; + hoisted.refreshLatestUpdateRestartSentinel.mockClear(); + hoisted.refreshLatestUpdateRestartSentinel.mockResolvedValue(sentinel); - const result = await testing.refreshLatestUpdateRestartSentinelIfPresent(); + const result = await testing.refreshLatestUpdateRestartSentinelIfPresent(); - expect(result).toBe(sentinel); - expect(hoisted.refreshLatestUpdateRestartSentinel).toHaveBeenCalledOnce(); + expect(result).toBe(sentinel); + expect(hoisted.refreshLatestUpdateRestartSentinel).toHaveBeenCalledOnce(); + }); } finally { closeOpenClawStateDatabaseForTest(); fs.rmSync(stateDir, { recursive: true, force: true });