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: "broiler", when: "Nov 15, 11:57 AM" }); assert.equal(done.tone, "good"); assert.match(done.detail, /Development now holds broiler 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("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"); });