df369dd7fd
- Migrated test mocking from bun:test mock API to vitest spyOn pattern across 9 test files. - Extracted mock setup logic into reusable helper functions with Symbol.dispose cleanup pattern. - Replaced manual beforeEach/afterEach and try-finally blocks with TypeScript 5.2 using declarations. - Removed 178 lines of boilerplate mock initialization and restoration code from test suite.
60 lines
2.3 KiB
TypeScript
60 lines
2.3 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "bun:test";
|
|
import * as fs from "node:fs";
|
|
import * as fsp from "node:fs/promises";
|
|
import * as os from "node:os";
|
|
import * as path from "node:path";
|
|
|
|
describe("FileSessionStorage.deleteSessionWithArtifacts", () => {
|
|
let tempDir: string;
|
|
let storage: { deleteSessionWithArtifacts(sessionPath: string): Promise<void> };
|
|
|
|
beforeEach(async () => {
|
|
tempDir = await fsp.mkdtemp(path.join(os.tmpdir(), "omp-session-storage-"));
|
|
const { FileSessionStorage } = await import("../src/session/session-storage");
|
|
storage = new FileSessionStorage();
|
|
});
|
|
|
|
afterEach(async () => {
|
|
vi.restoreAllMocks();
|
|
await fsp.rm(tempDir, { recursive: true, force: true });
|
|
});
|
|
|
|
async function createSessionFile(name: string): Promise<string> {
|
|
const sessionPath = path.join(tempDir, `${name}.jsonl`);
|
|
await Bun.write(
|
|
sessionPath,
|
|
`${JSON.stringify({ type: "session", id: "session-id", timestamp: "2025-01-01T00:00:00Z", cwd: tempDir })}\n`,
|
|
);
|
|
return sessionPath;
|
|
}
|
|
|
|
it("succeeds when the artifact directory is already absent", async () => {
|
|
const sessionPath = await createSessionFile("missing-artifacts");
|
|
const artifactsDir = sessionPath.slice(0, -6);
|
|
|
|
expect(fs.existsSync(sessionPath)).toBe(true);
|
|
expect(fs.existsSync(artifactsDir)).toBe(false);
|
|
|
|
await expect(storage.deleteSessionWithArtifacts(sessionPath)).resolves.toBeUndefined();
|
|
expect(fs.existsSync(sessionPath)).toBe(false);
|
|
expect(fs.existsSync(artifactsDir)).toBe(false);
|
|
});
|
|
|
|
it("throws when artifact cleanup fails after the session file is deleted", async () => {
|
|
const sessionPath = await createSessionFile("cleanup-failure");
|
|
const artifactsDir = sessionPath.slice(0, -6);
|
|
await fsp.mkdir(artifactsDir, { recursive: true });
|
|
await Bun.write(path.join(artifactsDir, "artifact.txt"), "artifact payload");
|
|
|
|
const rmError = new Error("permission denied");
|
|
const rmSpy = vi.spyOn(fsp, "rm").mockRejectedValueOnce(rmError);
|
|
|
|
await expect(storage.deleteSessionWithArtifacts(sessionPath)).rejects.toThrow(
|
|
`Session file deleted but failed to remove artifacts directory ${artifactsDir}: permission denied`,
|
|
);
|
|
expect(rmSpy).toHaveBeenCalledWith(artifactsDir, { recursive: true, force: true });
|
|
expect(fs.existsSync(sessionPath)).toBe(false);
|
|
expect(fs.existsSync(artifactsDir)).toBe(true);
|
|
});
|
|
});
|