Refactor runtime around isolated bot instances
This commit is contained in:
@@ -5,37 +5,58 @@ 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 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);
|
||||
const first = new DurableSessionStore(file, identity);
|
||||
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.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);
|
||||
const second = new DurableSessionStore(file, identity);
|
||||
await second.open();
|
||||
assert.equal(second.getSelectedRole("qq:chat", "assistant"), "ops");
|
||||
assert.equal(second.getBinding("qq:chat", "ops")?.nativeSessionId, "native-1");
|
||||
assert.equal(second.getBinding("qq:chat")?.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-"));
|
||||
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");
|
||||
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");
|
||||
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("refuses a second writer lock", async () => {
|
||||
const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "gori-store-lock-"));
|
||||
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); await first.open();
|
||||
const second = new DurableSessionStore(file);
|
||||
await assert.rejects(second.open(), /locked by another/);
|
||||
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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user