test(cleanup): isolate session lock queue coverage

This commit is contained in:
Ayaan Zaidi
2026-03-27 13:50:02 +05:30
parent d6662e2aa7
commit 3a7cf5364f

View File

@@ -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<typeof import("../agents/session-write-lock.js")>();
return {
...original,
acquireSessionWriteLock: acquireSessionWriteLockMock,
};
});
({ getSessionStoreLockQueueSizeForTest, withSessionStoreLockForTest } =
await import("../config/sessions/store.js"));
({ cleanupSessionStateForTest } = await import("./session-state-cleanup.js"));
}
function createDeferred<T>() {
let resolve!: (value: T | PromiseLike<T>) => void;
@@ -19,8 +36,14 @@ function createDeferred<T>() {
}
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 () => {