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"; const identity = { botId: "test-bot", platform: "qq" }; test("persists state v2 binding across reopen with 0600 atomic file", 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, identity); await first.open(); await first.setBinding({ chatKey: "qq:chat", agentId: "kimi", nativeSessionId: "native-1", workspace: "/tmp", botFingerprint: "fp", createdAt: 1, updatedAt: 1 }); await first.close(); assert.equal((await fs.promises.readdir(dir)).some((name) => name.endsWith(".tmp")), false); assert.equal((await fs.promises.stat(file)).mode & 0o777, 0o600); const persisted = JSON.parse(await fs.promises.readFile(file, "utf8")); assert.equal(persisted.version, 2); assert.equal(persisted.botId, "test-bot"); assert.equal(persisted.platform, "qq"); const second = new DurableSessionStore(file, identity); await second.open(); assert.equal(second.getBinding("qq:chat")?.nativeSessionId, "native-1"); await second.close(); }); test("rejects old state and bot or platform identity mismatch without rewriting", async () => { const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "gori-store-id-")); const file = path.join(dir, "state.json"); const old = '{"version":1,"bindings":{}}\n'; await fs.promises.writeFile(file, old); await assert.rejects(new DurableSessionStore(file, identity).open(), /requires state v2/); assert.equal(await fs.promises.readFile(file, "utf8"), old); await fs.promises.writeFile(file, JSON.stringify({ version: 2, botId: "other", platform: "qq", bindings: {} })); await assert.rejects(new DurableSessionStore(file, identity).open(), /identity mismatch/); await fs.promises.writeFile(file, JSON.stringify({ version: 2, botId: "test-bot", platform: "feishu", bindings: {} })); await assert.rejects(new DurableSessionStore(file, identity).open(), /identity mismatch/); await fs.promises.writeFile(file, JSON.stringify({ version: 2, botId: "test-bot", platform: "qq", bindings: { "qq:chat": { chatKey: "qq:other" } } })); await assert.rejects(new DurableSessionStore(file, identity).open(), /invalid state v2 binding/); }); test("preserves corrupt state and refuses a second writer lock", async () => { const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "gori-store-bad-")); const badFile = path.join(dir, "bad.json"); await fs.promises.writeFile(badFile, "not-json"); await assert.rejects(new DurableSessionStore(badFile, identity).open(), /original file was preserved/); assert.equal(await fs.promises.readFile(badFile, "utf8"), "not-json"); const file = path.join(dir, "state.json"); const first = new DurableSessionStore(file, identity); await first.open(); await assert.rejects(new DurableSessionStore(file, identity).open(), /locked by another/); await first.close(); await fs.promises.writeFile(`${file}.lock`, "2147483647\n", { mode: 0o600 }); const recovered = new DurableSessionStore(file, identity); await recovered.open(); await recovered.close(); assert.equal(fs.existsSync(`${file}.lock`), false); });