90 lines
4.8 KiB
TypeScript
90 lines
4.8 KiB
TypeScript
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, chatKeyFor } from "../src/core/durable-session-store.js";
|
|
|
|
const identity = { botId: "test-bot", platform: "qq" };
|
|
|
|
const binding = { chatKey: "qq:chat", agentId: "kimi", nativeSessionId: "native-1", assistantWorkspace: "/tmp/assistant", botFingerprint: "fp", createdAt: 1, updatedAt: 1 };
|
|
|
|
test("persists state v3 assistant 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(binding);
|
|
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 text = await fs.promises.readFile(file, "utf8");
|
|
const persisted = JSON.parse(text);
|
|
assert.equal(persisted.version, 3); assert.equal(persisted.botId, "test-bot"); assert.equal(persisted.platform, "qq");
|
|
assert.deepEqual(persisted.bindings["qq:chat"], binding);
|
|
assert.equal("mainTask" in persisted, false);
|
|
assert.doesNotMatch(text, /message|prompt|summary|content|lease/i);
|
|
|
|
const second = new DurableSessionStore(file, identity);
|
|
await second.open();
|
|
assert.deepEqual(second.getBinding("qq:chat"), binding);
|
|
assert.equal(second.stats().bindings, 1);
|
|
await second.touchBinding("qq:chat");
|
|
const touched = second.getBinding("qq:chat");
|
|
assert.ok((touched?.updatedAt ?? 0) >= 1);
|
|
assert.deepEqual(await second.deleteBinding("qq:chat"), touched);
|
|
assert.equal(second.getBinding("qq:chat"), undefined);
|
|
await second.close();
|
|
});
|
|
|
|
test("rejects state v1/v2 and identity mismatch without rewriting the original file", async () => {
|
|
const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "gori-store-id-"));
|
|
const file = path.join(dir, "state.json");
|
|
const v1 = '{"version":1,"bindings":{}}\n';
|
|
await fs.promises.writeFile(file, v1);
|
|
await assert.rejects(new DurableSessionStore(file, identity).open(), /requires state v3/);
|
|
assert.equal(await fs.promises.readFile(file, "utf8"), v1);
|
|
|
|
const v2 = JSON.stringify({ version: 2, botId: "test-bot", platform: "qq", bindings: {}, mainTask: { ownerChatKey: "qq:chat", state: "running", workspace: "/tmp", botFingerprint: "fp", startedAt: 1, updatedAt: 1 } });
|
|
await fs.promises.writeFile(file, v2);
|
|
await assert.rejects(new DurableSessionStore(file, identity).open(), /requires state v3/);
|
|
assert.equal(await fs.promises.readFile(file, "utf8"), v2);
|
|
|
|
await fs.promises.writeFile(file, JSON.stringify({ version: 3, botId: "other", platform: "qq", bindings: {} }));
|
|
await assert.rejects(new DurableSessionStore(file, identity).open(), /identity mismatch/);
|
|
await fs.promises.writeFile(file, JSON.stringify({ version: 3, botId: "test-bot", platform: "feishu", bindings: {} }));
|
|
await assert.rejects(new DurableSessionStore(file, identity).open(), /identity mismatch/);
|
|
|
|
await fs.promises.writeFile(file, JSON.stringify({ version: 3, botId: "test-bot", platform: "qq", bindings: { "qq:chat": { chatKey: "qq:other" } } }));
|
|
await assert.rejects(new DurableSessionStore(file, identity).open(), /invalid state v3 binding/);
|
|
|
|
const legacyField = { ...binding, workspace: "/tmp" } as Record<string, unknown>;
|
|
delete legacyField.assistantWorkspace;
|
|
await fs.promises.writeFile(file, JSON.stringify({ version: 3, botId: "test-bot", platform: "qq", bindings: { "qq:chat": legacyField } }));
|
|
await assert.rejects(new DurableSessionStore(file, identity).open(), /invalid state v3 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);
|
|
});
|
|
|
|
test("chatKeyFor builds platform-qualified keys", () => {
|
|
assert.equal(chatKeyFor("qq", "group:abc"), "qq:group:abc");
|
|
assert.equal(chatKeyFor("feishu", "oc_1"), "feishu:oc_1");
|
|
});
|