151 lines
4.5 KiB
TypeScript
151 lines
4.5 KiB
TypeScript
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");
|
|
}
|
|
});
|
|
|
|
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;
|
|
}
|
|
});
|