import assert from "node:assert/strict"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import { DurableSessionStore } from "../src/core/durable-session-store.js"; test("persists chat role and native session across reopen", async () => { const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "gori-store-")); const file = path.join(dir, "state.json"); const first = new DurableSessionStore(file); await first.open(); await first.setSelectedRole("qq:chat", "ops"); await first.setBinding({ chatKey: "qq:chat", roleId: "ops", backendId: "kimi", nativeSessionId: "native-1", workspace: "/tmp", roleFingerprint: "fp", createdAt: 1, updatedAt: 1 }); await first.close(); assert.equal((await fs.promises.readdir(dir)).some((name) => name.endsWith(".tmp")), false); const second = new DurableSessionStore(file); await second.open(); assert.equal(second.getSelectedRole("qq:chat", "assistant"), "ops"); assert.equal(second.getBinding("qq:chat", "ops")?.nativeSessionId, "native-1"); await second.close(); }); test("preserves corrupt state and fails explicitly", async () => { const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "gori-store-bad-")); const file = path.join(dir, "state.json"); await fs.promises.writeFile(file, "not-json"); const store = new DurableSessionStore(file); await assert.rejects(store.open(), /original file was preserved/); assert.equal(await fs.promises.readFile(file, "utf8"), "not-json"); }); test("refuses a second writer lock", async () => { const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "gori-store-lock-")); const file = path.join(dir, "state.json"); const first = new DurableSessionStore(file); await first.open(); const second = new DurableSessionStore(file); await assert.rejects(second.open(), /locked by another/); await first.close(); });