70 lines
2.7 KiB
TypeScript
70 lines
2.7 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import { diffValues, saveInOrder } from "../src/lib/changes.ts";
|
|
|
|
test("diffValues lists each changed leaf with its path", () => {
|
|
const before = {
|
|
poll_interval: "30s",
|
|
max_retries: 3,
|
|
dispatch: { label: "archie", reaction: "eyes" },
|
|
profiles: ["a"],
|
|
servers: [{ name: "x", enabled: true }],
|
|
};
|
|
const after = {
|
|
poll_interval: "1m0s",
|
|
max_retries: 3,
|
|
dispatch: { label: "archie", reaction: "rocket", extra: true },
|
|
profiles: ["a", "b"],
|
|
servers: [{ name: "x", enabled: false }],
|
|
};
|
|
assert.deepEqual(diffValues(before, after), [
|
|
{ path: "poll_interval", before: "30s", after: "1m0s" },
|
|
{ path: "dispatch.reaction", before: "eyes", after: "rocket" },
|
|
{ path: "dispatch.extra", before: undefined, after: true },
|
|
{ path: "profiles", before: ["a"], after: ["a", "b"] },
|
|
{ path: "servers.0.enabled", before: true, after: false },
|
|
]);
|
|
});
|
|
|
|
test("diffValues reports nothing for equal values and a removed row as one change", () => {
|
|
assert.deepEqual(diffValues({ a: [1, 2] }, { a: [1, 2] }), []);
|
|
assert.deepEqual(
|
|
diffValues({ rows: [{ n: 1 }, { n: 2 }] }, { rows: [{ n: 1 }] }),
|
|
[{ path: "rows.1", before: { n: 2 }, after: undefined }],
|
|
);
|
|
});
|
|
|
|
test("diffValues treats an absent field and its zero value as the same", () => {
|
|
assert.deepEqual(diffValues({ owner: "a" }, { owner: "a", persistent_storage: false, max_retries: 0, base: "" }), []);
|
|
assert.deepEqual(diffValues({ rows: [{ n: 1 }] }, { rows: [{ n: 1, on: false }] }), []);
|
|
assert.deepEqual(diffValues({ owner: "a" }, { owner: "a", persistent_storage: true }), [
|
|
{ path: "persistent_storage", before: undefined, after: true },
|
|
]);
|
|
});
|
|
|
|
test("saveInOrder saves one at a time and stops at the first failure", async () => {
|
|
const calls: string[] = [];
|
|
const outcome = await saveInOrder(["a", "b", "c"], async (kind) => {
|
|
calls.push(kind);
|
|
return kind !== "b";
|
|
});
|
|
assert.deepEqual(calls, ["a", "b"]);
|
|
assert.deepEqual(outcome, { saved: ["a"], failed: "b", untouched: ["c"] });
|
|
});
|
|
|
|
test("saveInOrder reports every kind saved when none fails", async () => {
|
|
assert.deepEqual(
|
|
await saveInOrder(["a", "b"], async () => true),
|
|
{ saved: ["a", "b"], failed: undefined, untouched: [] },
|
|
);
|
|
});
|
|
|
|
test("review expands an added schedule into inspectable field changes", () => {
|
|
assert.deepEqual(diffValues([], [{ id: "daily", detail: "Daily check", schedule: { interval: "24h" } }], "", true), [
|
|
{ path: "0.id", before: undefined, after: "daily" },
|
|
{ path: "0.detail", before: undefined, after: "Daily check" },
|
|
{ path: "0.schedule.interval", before: undefined, after: "24h" },
|
|
]);
|
|
});
|