feat: restore history, undo and redo in the Activity log for everyone who may refresh
Some checks failed
ci / rust (push) Has been cancelled
ci / supply-chain (push) Has been cancelled
ci / web (push) Has been cancelled

- 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>
This commit is contained in:
2026-11-16 20:37:21 +07:00
parent 62f0c68e89
commit d356c313ce
10 changed files with 418 additions and 79 deletions

View File

@@ -78,3 +78,112 @@ test("labels days relative to now", () => {
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");
});

View File

@@ -59,3 +59,10 @@ test("restore points are named by environment, and by target only when two share
);
assert.equal(relationLabels([rel("a", null, "odd")])[0].label, "odd");
});
test("an interrupted restore offers a rollback only when its safety copy can be used", () => {
const names = { target: "Staging" };
const stuck = { status: "recovery_required", restore_phase: "apply_failed" };
assert.match(outcomeOf({ ...stuck, undo_available: true }, names).detail, /roll it back/i);
assert.match(outcomeOf(stuck, names).detail, /administrator has to recover/i);
});