diff --git a/src/test-utils/session-state-cleanup.test.ts b/src/test-utils/session-state-cleanup.test.ts index 2259369005f..9c469a313c2 100644 --- a/src/test-utils/session-state-cleanup.test.ts +++ b/src/test-utils/session-state-cleanup.test.ts @@ -1,12 +1,29 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; -import { afterEach, describe, expect, it } from "vitest"; -import { - getSessionStoreLockQueueSizeForTest, - withSessionStoreLockForTest, -} from "../config/sessions/store.js"; -import { cleanupSessionStateForTest } from "./session-state-cleanup.js"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +const acquireSessionWriteLockMock = vi.hoisted(() => + vi.fn(async () => ({ release: vi.fn(async () => {}) })), +); + +let getSessionStoreLockQueueSizeForTest: typeof import("../config/sessions/store.js").getSessionStoreLockQueueSizeForTest; +let withSessionStoreLockForTest: typeof import("../config/sessions/store.js").withSessionStoreLockForTest; +let cleanupSessionStateForTest: typeof import("./session-state-cleanup.js").cleanupSessionStateForTest; + +async function loadFreshCleanupModules() { + vi.resetModules(); + vi.doMock("../agents/session-write-lock.js", async (importOriginal) => { + const original = await importOriginal(); + return { + ...original, + acquireSessionWriteLock: acquireSessionWriteLockMock, + }; + }); + ({ getSessionStoreLockQueueSizeForTest, withSessionStoreLockForTest } = + await import("../config/sessions/store.js")); + ({ cleanupSessionStateForTest } = await import("./session-state-cleanup.js")); +} function createDeferred() { let resolve!: (value: T | PromiseLike) => void; @@ -19,8 +36,14 @@ function createDeferred() { } describe("cleanupSessionStateForTest", () => { + beforeEach(async () => { + acquireSessionWriteLockMock.mockClear(); + await loadFreshCleanupModules(); + }); + afterEach(async () => { await cleanupSessionStateForTest(); + vi.restoreAllMocks(); }); it("waits for in-flight session store locks before clearing test state", async () => {