diff --git a/web/scripts/activity.test.mjs b/web/scripts/activity.test.mjs index 1f086a4..e79e4c7 100644 --- a/web/scripts/activity.test.mjs +++ b/web/scripts/activity.test.mjs @@ -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"); +}); diff --git a/web/scripts/restoreRun.test.mjs b/web/scripts/restoreRun.test.mjs index 6f06481..f599745 100644 --- a/web/scripts/restoreRun.test.mjs +++ b/web/scripts/restoreRun.test.mjs @@ -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); +}); diff --git a/web/src/App.tsx b/web/src/App.tsx index e95165a..8b4ce78 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -3,7 +3,7 @@ import { AnimatePresence, useMotionValue } from "motion/react"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { fetchCaptureJobs, fetchHealth, fetchOrbits, fetchPreferences, fetchRefreshRelations, fetchSnapshots, savePreferences, - enqueueCapture, runOrbit, type Orbit, + enqueueCapture, runOrbit, type Orbit, type RefreshRun, } from "./api"; import { OrbitInspector, OrbitRegister, TelemetryStrip } from "./components/hud/DiegeticHUD"; import { HUDPanel } from "./components/hud/HUDPanel"; @@ -91,7 +91,7 @@ function VaultApp({ actor, onLogout, caps }: { actor?: string; onLogout?: () => const [pairing, setPairing] = useState<{ code: string; expires_at: string } | null>(null); const [play, setPlay] = useState(true); const [restoreBusy, setRestoreBusy] = useState(false); - const [restoreTarget, setRestoreTarget] = useState<{ id: string | null }>(); + const [restoreTarget, setRestoreTarget] = useState<{ id: string | null; undo?: RefreshRun }>(); const reducedMotion = useReducedMotion(); const tools = useRef(null); useScreenTransition(tools, bigScreen, reducedMotion); @@ -237,7 +237,7 @@ function VaultApp({ actor, onLogout, caps }: { actor?: string; onLogout?: () => ? { orbitId: captureOrbitId, key: capture.submittedAt, snapshotId: capture.data ?? undefined } : undefined, [captureOrbitId, capture.submittedAt, capture.isError, capture.data]); const captureReady = !!captureEffect?.snapshotId && !!snaps.data?.some(({ id }) => id === captureEffect.snapshotId); - const activity = useActivity(snaps.data, orbits.data, caps.admin); + const activity = useActivity(snaps.data, orbits.data, { admin: caps.admin, refresh: caps.refresh }); // Consoles a non-administrator never sees: managing orbits and Outposts, restores, and access. const railItems = useMemo( () => (caps.admin ? [...hudItems, relationsItem, accessItem] : hudItems.filter((item) => item.id === "scene" || item.id === "activity")), @@ -293,6 +293,12 @@ function VaultApp({ actor, onLogout, caps }: { actor?: string; onLogout?: () => setMode(next); } + /** Open the dialog that puts back a restore (or undo) shown in the Activity log. */ + function openUndo(run: RefreshRun) { + if (!restoreBusy) setRestoreTarget({ id: null, undo: run }); + setMode("reconstitute"); + } + function selectSearchResult(result: SearchResult) { if (result.kind === "navigation") openConsole(result.id); else if (result.kind === "snapshot") openSnapshot(result.id); @@ -424,7 +430,8 @@ function VaultApp({ actor, onLogout, caps }: { actor?: string; onLogout?: () => entries={activity.entries} counts={activity.counts} updatedAt={activity.updatedAt} restoresUnavailable={activity.restoresUnavailable} capturesUnavailable={activity.capturesUnavailable} onOpen={(target) => { - if (target.kind === "snapshot") openSnapshot(target.id); + if ("run" in target) openUndo(target.run); + else if (target.kind === "snapshot") openSnapshot(target.id); else { selectOrbit(target.id); setMode("scene"); @@ -454,7 +461,18 @@ function VaultApp({ actor, onLogout, caps }: { actor?: string; onLogout?: () => )} - {restoreSnapshot + {restoreTarget?.undo + ? ( + r.id === restoreTarget.undo!.relation_id)} + admin={caps.admin} + onBusyChange={setRestoreBusy} + onDone={() => setMode("activity")} + /> + ) + : restoreSnapshot ? ( void }) { +function Row({ entry, now, onOpen, depth = 0 }: { entry: ActivityEntry; now: number; onOpen: (target: ActivityTarget) => void; depth?: number }) { const opens = entry.opens; const content = ( <> {entry.group === "running" && entry.id.startsWith("orbit-") ? "now" : entry.at === null ? "—" : clock(entry.at)}