Files
tessera/web/scripts/restoreRun.test.mjs
Adnan Zahir 648c8c6a2a
Some checks failed
ci / rust (push) Has been cancelled
ci / supply-chain (push) Has been cancelled
ci / web (push) Has been cancelled
feat: undo a restore from the safety copy it took
- POST /refresh-runs accepts undo_of: restores the safety copy of a completed restore back onto the same
  target through the normal run engine (checks, countdown, progress, and a new safety copy so the undo can be
  undone); refused when something was restored after it or the copy is gone
- a safety copy is accepted as a source only for the target it was taken from (hub and agent both check)
- run views carry undo and undo_available; audited as refresh.undo_started
- the modal offers Undo last restore and, after a restore, Undo this restore
- proven with a real agent and PostgreSQL: restore, undo back to the original contents, undo of the undo

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 16:47:02 +07:00

62 lines
3.9 KiB
JavaScript

import assert from "node:assert/strict";
import test from "node:test";
import { STAGES, isFinal, outcomeOf, progressOf, relationLabels } from "../src/lib/restoreRun.ts";
const run = (status, restore_phase = null, blocked_reason = null) => ({ status, restore_phase, blocked_reason });
test("the four stages follow the restore job's phase", () => {
assert.deepEqual(STAGES, ["Checking", "Safety copy", "Applying", "Completed"]);
// the run is created first; the restore job appears a moment later
assert.equal(progressOf(run("capturing")).index, 0);
for (const phase of ["queued", "claimed", "checking"]) assert.equal(progressOf(run("restoring", phase)).index, 0, phase);
for (const phase of ["safety", "ready"]) assert.equal(progressOf(run("restoring", phase)).index, 1, phase);
assert.equal(progressOf(run("restoring", "applying")).index, 2);
assert.deepEqual(progressOf(run("completed", "completed")), { index: 4, state: "done" });
assert.equal(progressOf(run("restoring", "applying")).state, "running");
// the restore job finishes a moment before the run is marked completed: that gap must not look like a restart
assert.deepEqual(progressOf(run("restoring", "completed")), { index: 4, state: "done" });
assert.equal(isFinal(run("restoring")), false);
assert.equal(isFinal(run("completed")), true);
});
test("a run that stopped says at which stage and why it counts as failed or merely cancelled", () => {
assert.deepEqual(progressOf(run("restore_failed", "invalid_payload")), { index: 0, state: "failed" });
assert.deepEqual(progressOf(run("restore_failed", "safety_failed")), { index: 1, state: "failed" });
assert.deepEqual(progressOf(run("recovery_required", "apply_failed")), { index: 2, state: "failed" });
assert.deepEqual(progressOf(run("blocked", "checking", "access_revoked")), { index: 0, state: "failed" });
assert.deepEqual(progressOf(run("cancelled", "safety")), { index: 1, state: "cancelled" });
assert.equal(progressOf(run("expired")).state, "failed");
});
test("what the person is told never contains the database's own words", () => {
const done = outcomeOf(run("completed"), { target: "Development", orbit: "ledger", when: "Nov 15, 11:57 AM" });
assert.equal(done.tone, "good");
assert.match(done.detail, /Development now holds ledger as of Nov 15, 11:57 AM/);
assert.match(outcomeOf(run("restore_failed", "apply_failed"), { target: "Development" }).detail, /ask an administrator/i);
assert.match(outcomeOf(run("recovery_required"), { target: "Staging" }).title, /interrupted/i);
assert.match(outcomeOf(run("blocked", null, "access_revoked"), { target: "Staging" }).detail, /access to this restore was removed/);
assert.equal(outcomeOf(run("cancelled"), { target: "Staging" }).tone, "plain");
assert.equal(outcomeOf(run("restoring", "safety"), { target: "Staging" }).tone, "busy");
});
test("an undo is worded as putting the target back, and its own safety copy keeps it undoable", () => {
const done = outcomeOf({ ...run("completed"), undo: true }, { target: "Staging", orbit: "ledger", when: "Nov 15, 11:57 AM" });
assert.equal(done.tone, "good");
assert.match(done.title, /Staging was rolled back/);
assert.match(done.detail, /holds what it held before that restore/);
assert.match(outcomeOf({ ...run("restore_failed", "apply_failed"), undo: true }, { target: "Staging" }).title, /undo failed/i);
});
test("restore points are named by environment, and by target only when two share one", () => {
const rel = (id, env, name) => ({ id, target_environment: env, target_name: name, usable: true });
assert.deepEqual(
relationLabels([rel("a", "staging", "s1"), rel("b", "development", "d1")]).map((r) => r.label),
["Staging", "Development"],
);
assert.deepEqual(
relationLabels([rel("a", "staging", "s1"), rel("b", "staging", "s2")]).map((r) => r.label),
["Staging · s1", "Staging · s2"],
);
assert.equal(relationLabels([rel("a", null, "odd")])[0].label, "odd");
});