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 { ProposalStore } from "../src/core/proposal-store.js"; const identity = { botId: "test-bot", platform: "qq" }; const input = { title: "Refactor store", goal: "Move to assistant proposals", steps: ["design", "implement", "test"], ownerChatKey: "qq:chat", requesterUserId: "user-1" }; test("creates, updates and reloads proposals with 0600 atomic file", async () => { const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "gori-proposals-")); const file = path.join(dir, "proposals.json"); const first = new ProposalStore(file, identity); await first.open(); const created = await first.create(input); assert.equal(created.status, "proposed"); assert.ok(created.id.length > 0); assert.equal(created.createdAt, created.updatedAt); const confirmedAt = created.createdAt + 1000; const updated = await first.update(created.id, { status: "working", confirmedAt, startedAt: confirmedAt + 1, workerNativeSessionId: "native-1", workerProcessGroup: { pgid: 4321, token: "worker-token" }, pending: { kind: "step", summary: "needs dirty confirm", question: "overwrite?", nextStep: "edit file" }, lastWorkerSummary: "half done" }); assert.equal(updated.status, "working"); assert.ok(updated.updatedAt >= created.updatedAt); 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, 1); assert.equal(persisted.botId, "test-bot"); assert.equal(persisted.platform, "qq"); assert.equal(persisted.proposals[created.id].workerProcessGroup.pgid, 4321); const second = new ProposalStore(file, identity); await second.open(); const loaded = second.get(created.id); assert.deepEqual(loaded, updated); assert.deepEqual(second.list().map((proposal) => proposal.id), [created.id]); assert.deepEqual(second.list({ status: "working" }).map((proposal) => proposal.id), [created.id]); assert.equal(second.list({ status: "queued" }).length, 0); assert.equal(second.stats().proposals, 1); await second.close(); }); test("orders list by confirmation time for queued proposals", async () => { const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "gori-proposals-order-")); const file = path.join(dir, "proposals.json"); const store = new ProposalStore(file, identity); await store.open(); const firstCreated = await store.create({ ...input, title: "first created" }); const secondCreated = await store.create({ ...input, title: "second created" }); await store.update(secondCreated.id, { status: "queued", confirmedAt: 100 }); await store.update(firstCreated.id, { status: "queued", confirmedAt: 200 }); assert.deepEqual(store.list({ status: "queued" }).map((proposal) => proposal.title), ["second created", "first created"]); await store.close(); }); test("validates proposal fields on create and update", async () => { const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "gori-proposals-invalid-")); const file = path.join(dir, "proposals.json"); const store = new ProposalStore(file, identity); await store.open(); await assert.rejects(store.create({ ...input, title: "" }), /title/); await assert.rejects(store.create({ ...input, steps: ["ok", ""] }), /steps/); const created = await store.create(input); await assert.rejects(store.update(created.id, { status: "bogus" as never }), /status/); await assert.rejects(store.update(created.id, { workerProcessGroup: { pgid: 1, token: "t" } }), /workerProcessGroup/); await assert.rejects(store.update(created.id, { pending: { kind: "bogus", summary: "s" } as never }), /pending/); await assert.rejects(store.update("missing", { status: "queued" }), /unknown proposal/); assert.equal(store.get(created.id)?.status, "proposed"); await store.close(); }); test("rejects identity or version mismatch and preserves corrupt state", async () => { const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "gori-proposals-id-")); const file = path.join(dir, "proposals.json"); const wrongVersion = '{"version":2,"botId":"test-bot","platform":"qq","proposals":{}}\n'; await fs.promises.writeFile(file, wrongVersion); await assert.rejects(new ProposalStore(file, identity).open(), /expected version 1/); assert.equal(await fs.promises.readFile(file, "utf8"), wrongVersion); await fs.promises.writeFile(file, JSON.stringify({ version: 1, botId: "other", platform: "qq", proposals: {} })); await assert.rejects(new ProposalStore(file, identity).open(), /identity mismatch/); await fs.promises.writeFile(file, JSON.stringify({ version: 1, botId: "test-bot", platform: "feishu", proposals: {} })); await assert.rejects(new ProposalStore(file, identity).open(), /identity mismatch/); await fs.promises.writeFile(file, "not-json"); await assert.rejects(new ProposalStore(file, identity).open(), /original file was preserved/); assert.equal(await fs.promises.readFile(file, "utf8"), "not-json"); await fs.promises.writeFile(file, JSON.stringify({ version: 1, botId: "test-bot", platform: "qq", proposals: { abc: { id: "other" } } })); await assert.rejects(new ProposalStore(file, identity).open(), /invalid proposal/); }); test("refuses a second writer and recovers a stale lock", async () => { const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "gori-proposals-lock-")); const file = path.join(dir, "proposals.json"); const first = new ProposalStore(file, identity); await first.open(); await assert.rejects(new ProposalStore(file, identity).open(), /locked by another/); await first.close(); await fs.promises.writeFile(`${file}.lock`, "2147483647\n", { mode: 0o600 }); const recovered = new ProposalStore(file, identity); await recovered.open(); await recovered.close(); assert.equal(fs.existsSync(`${file}.lock`), false); });