import assert from "node:assert/strict"; import test from "node:test"; import { createPinia, setActivePinia } from "pinia"; import { useSettingsStore, commandBody, applyStatusForKind, } from "../src/stores/settings.ts"; test("control-plane commands keep the edited version and typed values", () => { assert.deepEqual( JSON.parse( commandBody( { max_model_tool_steps: 12, max_runtime_seconds: 900, max_consecutive_gate_failures: 3, }, 7, ), ), { value: { max_model_tool_steps: 12, max_runtime_seconds: 900, max_consecutive_gate_failures: 3, }, expected_version: 7, }, ); }); test("apply status names a process behind the stored version and one that stopped reporting", () => { const records = [ { process: "archied", kind: "tool-settings", applied_version: 7, reported_at: "", state: "current", }, { process: "archie-gateway", kind: "tool-settings", applied_version: 6, reported_at: "", state: "current", }, { process: "archie-messaging", kind: "tool-settings", applied_version: 7, reported_at: "", state: "unknown", }, { process: "archied", kind: "plugin-settings", applied_version: 2, reported_at: "", state: "current", }, ]; const processes = ["archied", "archie-gateway", "archie-messaging"]; assert.deepEqual(applyStatusForKind(records, processes, "tool-settings", 7), [ { process: "archied", state: "running", version: 7, error: "", storedVersion: 7, reportedAt: "" }, { process: "archie-gateway", state: "pending-restart", version: 6, error: "", storedVersion: 7, reportedAt: "", }, { process: "archie-messaging", state: "unknown", version: 7, error: "", storedVersion: 7, reportedAt: "" }, ]); }); test("apply status shows a process that has never reported, and one that rejected the edit", () => { const processes = ["archied", "archie-gateway"]; const records = [ { process: "archied", kind: "tool-settings", applied_version: 6, reported_at: "", state: "failed", error: "validate database settings: bad policy", storedVersion: 7, reportedAt: "", }, ]; assert.deepEqual(applyStatusForKind(records, processes, "tool-settings", 7), [ { process: "archied", state: "failed", version: 6, error: "validate database settings: bad policy", storedVersion: 7, reportedAt: "", }, { process: "archie-gateway", state: "not-reporting", version: 0, error: "", storedVersion: 7, }, ]); }); test("save errors appear on the page only when not presented in the review drawer", async () => { setActivePinia(createPinia()); const store = useSettingsStore(); const kind = "channel-settings"; const realFetch = globalThis.fetch; globalThis.fetch = async () => new Response(JSON.stringify({ resource: { kind, version: 1, value: { operator: "" } } })); try { await store.loadResource(kind); } finally { globalThis.fetch = realFetch; } store.formFor(kind)!.api.setFieldValue("operator", "changed"); store.stateFor(kind).error = "Save failed"; store.stateFor("provider-settings").error = "Load failed"; for (const [reviewing, expected] of [[false, "Save failed"], [true, undefined], [false, "Save failed"]] as const) { store.reviewing = reviewing; assert.equal(store.pageErrorFor(kind), expected); assert.equal(store.stateFor(kind).error, "Save failed", "drawer retains the error"); assert.equal(store.pageErrorFor("provider-settings"), "Load failed", "unreviewed errors stay visible"); } }); // A rejected save must not silently retry against the same stale version. test("a conflicting settings save keeps the draft for review and retries with the latest version", async (t) => { for (const [kind, value] of [ ["workflow-execution-settings", { max_model_tool_steps: 5 }], ["scheduling-policy", { poll_interval: "1m" }], ] as const) { await t.test(kind, async (t) => { setActivePinia(createPinia()); const store = useSettingsStore(); const resourcePath = `/api/control-plane/resources/${kind}`; const initial = { kind, version: 7, value: {} }; let resource = initial; let failRead = false; const sent: unknown[] = []; t.mock.method(globalThis, "fetch", async (path: string, init: RequestInit) => { if (path === "/api/capabilities") return Response.json({ sections: { settings: false } }); if (path === "/api/control-plane/catalog") { return Response.json({ resources: [{ kind, commands: ["replace"] }] }); } if (path === resourcePath) { return failRead ? new Response("Could not read the latest version", { status: 503 }) : Response.json({ resource }); } assert.equal(path, `${resourcePath}/commands/replace`); assert.equal(init.method, "POST"); const command = JSON.parse(String(init.body)); sent.push(command); if (command.expected_version !== resource.version) { return new Response("Changed elsewhere", { status: 409 }); } resource = { ...resource, version: resource.version + 1, value: command.value }; return Response.json({ resource }); }); await store.load(); store.setDraftValue(kind, value); resource = { ...resource, version: 8 }; failRead = true; assert.equal((await store.saveDrafts()).failed, kind); assert.match(store.pageErrorFor(kind) ?? "", /Could not read the latest version/); assert.deepEqual(store.draftValue(kind), value); failRead = false; assert.equal((await store.saveDrafts()).failed, kind); assert.match(store.pageErrorFor(kind) ?? "", /Changed elsewhere/); assert.deepEqual(store.draftValue(kind), value); assert.deepEqual(sent, [{ value, expected_version: 7 }, { value, expected_version: 7 }], "no automatic overwrite"); assert.ok(!(await store.saveDrafts()).failed); assert.deepEqual(sent.at(-1), { value, expected_version: 8 }); assert.deepEqual(store.dirtyKinds, []); assert.equal(store.pageErrorFor(kind), undefined); }); } }); test("an unset resource opens without changes and an untouched save keeps it unset", async () => { setActivePinia(createPinia()); const store = useSettingsStore(); const kind = "credential-bindings"; const sent: unknown[] = []; const realFetch = globalThis.fetch; globalThis.fetch = async (_input, init) => { if (init?.body) sent.push(JSON.parse(String(init.body)).value); return new Response(JSON.stringify({ resource: { kind, version: init?.body ? 2 : 1, value: null } })); }; try { await store.loadResource(kind); assert.deepEqual(store.changesFor(kind), [], "opening an unset section is not an edit"); assert.equal(store.draftValue(kind), null); assert.equal(await store.formFor(kind)!.submit(), true); assert.deepEqual(sent, [null], "saving stores null, not {}"); } finally { globalThis.fetch = realFetch; } });