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"); });