- Activity lists restores from refresh runs, so the Refresh permission is enough to see them; an administrator's restore jobs add the database, the person and the snapshot - an undo is listed under the restore it reverses, and a redo under that undo; opening a restore or an undo opens its undo or redo dialog - the dialog has an explicit undo mode: it describes what is put back instead of the snapshot that was open, and an interrupted restore offers Roll back - a restore whose safety backup retention has forgotten shows "Safety backup expired" instead of a link, and the dialog says how long a safety copy is kept Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
190 lines
12 KiB
JavaScript
190 lines
12 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { activityCounts, buildActivity, dayLabel } from "../src/lib/activity.ts";
|
|
|
|
const NOW = Date.parse("2026-11-14T12:00:00Z");
|
|
const sec = (iso) => Date.parse(iso) / 1000;
|
|
const snapshot = (id, orbit_name, taken_at, size_bytes = 1024 ** 3) => ({ id, orbit_name, orbit_id: orbit_name, taken_at, size_bytes });
|
|
const target = { id: "t", name: "pg-staging", outpost_id: "o", engine: "postgres", application_id: "a", identity: { instance: "1", database: "orders" } };
|
|
const restore = (id, status, phase, at, extra = {}) => ({ id, snapshot_id: "s1", target, actor: "sen", status, phase, safety_snapshot_id: null, created_at: sec(at), updated_at: sec(at), ...extra });
|
|
const orbit = (id, last_status, extra = {}) => ({ id, name: id, provider: "filesystem", source: "filesystem", access: "hub", last_status, dry_run: false, ...extra });
|
|
const build = (input) => buildActivity({ snapshots: [], orbits: [], captureJobs: [], restoreJobs: [], now: NOW, ...input });
|
|
|
|
test("puts what needs a person first, then running work, then history newest first", () => {
|
|
const snapshots = [snapshot("s1", "media-vault", "2026-11-14T02:00:00Z"), snapshot("s2", "nightly-pg", "2026-11-13T01:00:00Z")];
|
|
const restoreJobs = [
|
|
restore("done", "completed", "completed", "2026-11-12T15:00:00Z"),
|
|
restore("locked", "recovery_required", "apply_failed", "2026-11-13T22:10:00Z"),
|
|
restore("live", "running", "safety", "2026-11-14T10:52:00Z"),
|
|
];
|
|
const entries = build({ snapshots, restoreJobs });
|
|
assert.deepEqual(entries.map(({ group }) => group), ["needs", "running", "history", "history", "history"]);
|
|
assert.deepEqual(entries.map(({ id }) => id), ["restore-locked", "restore-live", "snapshot-s1", "snapshot-s2", "restore-done"]);
|
|
assert.deepEqual(activityCounts(entries), { running: 1, needs: 1 });
|
|
});
|
|
|
|
test("names a restore by direction and tells where it leads", () => {
|
|
const [entry] = build({ snapshots: [snapshot("s1", "media-vault", "2026-11-14T02:00:00Z")], restoreJobs: [restore("a", "completed", "completed", "2026-11-14T03:00:00Z")] })
|
|
.filter(({ kind }) => kind === "restore");
|
|
assert.match(entry.title, /^media-vault · .+ → pg-staging \/ orders$/);
|
|
assert.deepEqual(entry.opens, { kind: "snapshot", id: "s1" });
|
|
const [orphan] = build({ restoreJobs: [restore("b", "completed", "completed", "2026-11-14T03:00:00Z", { snapshot_id: "gone-1234-5678" })] });
|
|
assert.equal(orphan.opens, null);
|
|
assert.match(orphan.title, /^Snapshot gone-123 →/);
|
|
});
|
|
|
|
test("shows the step of a job in flight and ends each state in the right tone", () => {
|
|
const entries = build({ restoreJobs: [
|
|
restore("q", "queued", "queued", "2026-11-14T10:00:00Z"),
|
|
restore("s", "running", "safety", "2026-11-14T10:01:00Z"),
|
|
restore("p", "applying", "applying", "2026-11-14T10:02:00Z"),
|
|
restore("f", "failed", "wrong_target", "2026-11-14T09:00:00Z"),
|
|
restore("c", "cancelled", "cancelled", "2026-11-14T08:00:00Z"),
|
|
restore("r", "reconciled", "reconciled", "2026-11-14T07:00:00Z"),
|
|
] });
|
|
const by = Object.fromEntries(entries.map((entry) => [entry.id, entry]));
|
|
assert.equal(by["restore-q"].progress, 0);
|
|
assert.equal(by["restore-s"].progress, 2);
|
|
assert.equal(by["restore-p"].progress, 3);
|
|
assert.deepEqual([by["restore-f"].tone, by["restore-c"].tone, by["restore-r"].tone], ["red", "grey", "green"]);
|
|
assert.equal(by["restore-r"].state, "Recovered");
|
|
});
|
|
|
|
test("folds an outpost backup into its snapshot, and keeps unfinished or failed ones", () => {
|
|
const orbits = [orbit("mail", "running", { access: "outpost", application_id: "app" })];
|
|
const profile = { application_id: "app", orbit_id: "mail", outpost_id: "o", format: "filesystem" };
|
|
const job = (id, status, phase, snapshot_id = null) => ({ id, profile, actor: "schedule", status, phase, created_at: sec("2026-11-14T09:00:00Z"), snapshot_id, offsite_status: null });
|
|
const entries = build({
|
|
snapshots: [snapshot("s1", "mail", "2026-11-14T09:05:00Z")],
|
|
orbits,
|
|
captureJobs: [job("done", "completed", "completed", "s1"), job("live", "running", "uploading"), job("bad", "failed", "upload_failed")],
|
|
});
|
|
assert.equal(entries.filter(({ kind }) => kind === "backup").length, 3);
|
|
const stored = entries.find(({ id }) => id === "snapshot-s1");
|
|
assert.match(stored.meta, /via Outpost · by schedule/);
|
|
assert.equal(entries.find(({ id }) => id === "backup-live").progress, 3);
|
|
assert.equal(entries.find(({ id }) => id === "backup-bad").tone, "red");
|
|
assert.equal(entries.some(({ id }) => id.startsWith("orbit-mail")), false);
|
|
});
|
|
|
|
test("reads a hub orbit's last status when the hub keeps no job list for it", () => {
|
|
const entries = build({ orbits: [orbit("a", "running"), orbit("b", "failed"), orbit("c", "completed")], now: NOW });
|
|
assert.deepEqual(entries.map(({ id, group }) => [id, group]), [["orbit-b-failed", "needs"], ["orbit-a-running", "running"]]);
|
|
assert.equal(entries[0].at, null);
|
|
});
|
|
|
|
test("labels days relative to now", () => {
|
|
assert.equal(dayLabel(NOW - 3600_000, NOW), "Today");
|
|
assert.equal(dayLabel(NOW - 86_400_000, NOW), "Yesterday");
|
|
assert.notEqual(dayLabel(NOW - 5 * 86_400_000, NOW), "Today");
|
|
});
|
|
|
|
test("lists an undo under the restore it reverses, and a redo under that undo", () => {
|
|
const snapshots = [snapshot("s1", "ledger", "2026-11-14T02:00:00Z")];
|
|
const run = (id, job, extra = {}) => ({ id, restore_job_id: job, relation_id: "r", status: "completed", ...extra });
|
|
const refreshRuns = [
|
|
run("r1", "j1", { undo_available: false }),
|
|
run("r2", "j2", { undo: true, undo_of: "r1", undo_available: false }),
|
|
run("r3", "j3", { undo: true, undo_of: "r2", undo_available: true }),
|
|
];
|
|
const restoreJobs = [
|
|
restore("j1", "completed", "completed", "2026-11-14T03:00:00Z"),
|
|
restore("j2", "completed", "completed", "2026-11-14T04:00:00Z", { snapshot_id: "safety-1" }),
|
|
restore("j3", "completed", "completed", "2026-11-14T05:00:00Z", { snapshot_id: "safety-2" }),
|
|
];
|
|
const entries = build({ snapshots, restoreJobs, refreshRuns });
|
|
const roots = entries.filter(({ kind }) => kind === "restore");
|
|
assert.deepEqual(roots.map(({ id }) => id), ["restore-j1"], "only the restore is on its own");
|
|
const [undo] = roots[0].children;
|
|
assert.equal(undo.id, "restore-j2");
|
|
assert.equal(undo.variant, "undo");
|
|
assert.equal(undo.children[0].variant, "redo");
|
|
// the newest one can be put back, and what opening it offers is the opposite of what it did
|
|
assert.equal(undo.opens, null);
|
|
assert.equal(undo.children[0].opens.kind, "undo");
|
|
assert.equal(activityCounts(entries).running, 0);
|
|
});
|
|
|
|
test("a restore sits where its most urgent undo does, and an undo whose restore is missing stands alone", () => {
|
|
const refreshRuns = [
|
|
{ id: "r1", restore_job_id: "j1", relation_id: "r" },
|
|
{ id: "r2", restore_job_id: "j2", relation_id: "r", undo: true, undo_of: "r1" },
|
|
{ id: "r9", restore_job_id: "j9", relation_id: "r", undo: true, undo_of: "gone" },
|
|
];
|
|
const restoreJobs = [
|
|
restore("j1", "completed", "completed", "2026-11-10T03:00:00Z"),
|
|
restore("j2", "running", "safety", "2026-11-14T10:00:00Z"),
|
|
restore("j9", "completed", "completed", "2026-11-12T03:00:00Z"),
|
|
];
|
|
const entries = build({ restoreJobs, refreshRuns });
|
|
assert.deepEqual(entries.map(({ id, slotGroup }) => [id, slotGroup]), [["restore-j1", "running"], ["restore-j9", "history"]]);
|
|
assert.equal(activityCounts(entries).running, 1);
|
|
});
|
|
|
|
test("someone who may only read runs still gets the restore history, with undos under their restores", () => {
|
|
const run = (id, extra = {}) => ({ id, relation_id: "r", source_name: "ledger", target_name: "staging", status: "completed", restore_phase: "completed", snapshot_taken_at: sec("2026-11-14T02:00:00Z"), created_at: sec("2026-11-14T03:00:00Z"), ...extra });
|
|
const refreshRuns = [
|
|
run("r1"),
|
|
run("r2", { undo: true, undo_of: "r1", created_at: sec("2026-11-14T04:00:00Z"), undo_available: false }),
|
|
run("r3", { undo: true, undo_of: "r2", created_at: sec("2026-11-14T05:00:00Z"), undo_available: true }),
|
|
run("r4", { status: "restoring", restore_phase: "safety", created_at: sec("2026-11-14T11:00:00Z") }),
|
|
run("r5", { status: "recovery_required", restore_phase: "apply_failed", created_at: sec("2026-11-13T01:00:00Z") }),
|
|
run("r6", { status: "cancelled", restore_phase: null, created_at: sec("2026-11-12T01:00:00Z") }),
|
|
];
|
|
const entries = build({ refreshRuns });
|
|
assert.deepEqual(entries.map(({ id }) => id), ["restore-run-r5", "restore-run-r4", "restore-run-r1", "restore-run-r6"]);
|
|
const [locked, live, restored] = entries;
|
|
assert.equal(locked.group, "needs");
|
|
assert.equal(live.progress, 2);
|
|
assert.match(restored.title, /^ledger · .+ → staging$/);
|
|
assert.equal(restored.opens, null, "no snapshot id reaches them, and it cannot be undone any more");
|
|
const [undo] = restored.children;
|
|
assert.equal(undo.variant, "undo");
|
|
assert.equal(undo.children[0].variant, "redo");
|
|
assert.equal(undo.children[0].opens.kind, "undo");
|
|
assert.deepEqual(activityCounts(entries), { running: 1, needs: 1 });
|
|
});
|
|
|
|
test("a restore known by its run and its job is one entry, with the job's detail", () => {
|
|
const refreshRuns = [{ id: "r1", restore_job_id: "j1", relation_id: "r", source_name: "x", target_name: "y", status: "completed", created_at: sec("2026-11-14T03:00:00Z") }];
|
|
const entries = build({
|
|
snapshots: [snapshot("s1", "media-vault", "2026-11-14T02:00:00Z")],
|
|
restoreJobs: [restore("j1", "completed", "completed", "2026-11-14T03:00:00Z")],
|
|
refreshRuns,
|
|
}).filter(({ kind }) => kind === "restore");
|
|
assert.equal(entries.length, 1);
|
|
assert.match(entries[0].title, /pg-staging \/ orders$/);
|
|
});
|
|
|
|
test("a restore whose safety backup expired says so instead of leading anywhere", () => {
|
|
const run = (id, extra = {}) => ({ id, relation_id: "r", source_name: "ledger", target_name: "staging", status: "completed", restore_phase: "completed", created_at: sec("2026-11-14T03:00:00Z"), ...extra });
|
|
const gone = build({ refreshRuns: [run("r1", { undo_state: "expired", undo_available: false })] })[0];
|
|
assert.equal(gone.opens, null);
|
|
assert.equal(gone.note, "Safety backup expired");
|
|
// an administrator's restore job would normally open its snapshot; expired, it must not suggest the copy is there
|
|
const withJob = build({
|
|
snapshots: [snapshot("s1", "ledger", "2026-11-14T02:00:00Z")],
|
|
restoreJobs: [restore("j1", "completed", "completed", "2026-11-14T03:00:00Z")],
|
|
refreshRuns: [run("r1", { restore_job_id: "j1", undo_state: "expired" })],
|
|
}).find(({ id }) => id === "restore-j1");
|
|
assert.equal(withJob.opens, null);
|
|
assert.equal(withJob.note, "Safety backup expired");
|
|
// superseded is not expired: its copy may still be there, and it keeps its normal link
|
|
const old = build({
|
|
snapshots: [snapshot("s1", "ledger", "2026-11-14T02:00:00Z")],
|
|
restoreJobs: [restore("j1", "completed", "completed", "2026-11-14T03:00:00Z")],
|
|
refreshRuns: [run("r1", { restore_job_id: "j1", undo_state: "superseded" })],
|
|
}).find(({ id }) => id === "restore-j1");
|
|
assert.deepEqual(old.opens, { kind: "snapshot", id: "s1" });
|
|
});
|
|
|
|
test("an interrupted restore that can be rolled back says so, and a rolled back one is marked", () => {
|
|
const run = (id, extra) => ({ id, relation_id: "r", source_name: "ledger", target_name: "staging", created_at: sec("2026-11-14T03:00:00Z"), ...extra });
|
|
const [locked] = build({ refreshRuns: [run("r1", { status: "recovery_required", restore_phase: "apply_failed", undo_available: true })] });
|
|
assert.equal(locked.group, "needs");
|
|
assert.match(locked.meta, /roll back available/);
|
|
assert.equal(locked.opens.kind, "undo");
|
|
const [done] = build({ refreshRuns: [run("r2", { status: "reconciled", restore_phase: "rolled_back" })] });
|
|
assert.equal(done.state, "Rolled back");
|
|
});
|