Public: anyone can read this; the crew can change it.
A hosted forge's own owner sees when their forge was last backed up, on their forge's settings, in plain words #14035
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13918-a-hosted-forge-s-own-owner-sees-when-the → main 9 commits fork 2 threads · 2 unresolved
9 files
changed +313
−9
docs/features/README.md +1
| @@ -453,6 +453,7 @@ Read with the guide page [Backups and moving](../guide-backups-moving.md). | ||
| 453 | 453 | |
| 454 | 454 | - [](backup-growth.md) |
| 455 | 455 | - [](backup-age.md) |
| 456 | - [](hosted-backup-line.md) | |
| 456 | 457 | - [](disk-free.md) |
| 457 | 458 | - [](backup-refs.md) |
| 458 | 459 | - [](forge-origin.md) |
docs/features/hosted-backup-line.md added +34
| @@ -0,0 +1,34 @@ | ||
| 1 | # A hosted forge's own backup line | |
| 2 | ||
| 3 | A forge another forge hosts says when its host last backed it up (#13918). Its admins read it | |
| 4 | in two places, in the same words: | |
| 5 | ||
| 6 | - **On /settings**, in the block under the plan, as one row: who backed it up as the label, | |
| 7 | how long ago as the value. | |
| 8 | - **In the terminal**, on the **Plan** screen (palette, admins), as the same sentence on one | |
| 9 | line. | |
| 10 | ||
| 11 | A forge nobody hosts has no such row, and neither does a preview. | |
| 12 | ||
| 13 | ## The three wordings | |
| 14 | ||
| 15 | - **Backed up by forge.example, 5 h ago**: the host's newest nightly snapshot kept this forge, that long | |
| 16 | ago. | |
| 17 | - **Backed up by forge.example, 40 h ago, more than 36 hours**: the same, past 36 hours (a nightly, a | |
| 18 | missed night's slack, and a retry). Plain words only: no red, no banner. | |
| 19 | - **Not backed up yet**: the host has not answered since this forge started, or its newest snapshot did | |
| 20 | not keep this forge (a forge it left out counts as not kept). | |
| 21 | ||
| 22 | ## Where it comes from | |
| 23 | ||
| 24 | The host records what each nightly snapshot kept of every forge it hosts | |
| 25 | ([](hosted-backup.md), `snapshot/hosted.json`). It answers | |
| 26 | each signed beat with that forge's own entry, the time and size, and nothing about any other | |
| 27 | forge. The hosted forge keeps the answer as `parent-backup.json` in its own data directory; it | |
| 28 | never reads its host's disk. | |
| 29 | ||
| 30 | A forge beats every 10 minutes, so a new backup, or a first one, shows within 10 minutes of the | |
| 31 | host's snapshot. The host reads its small record on each beat; it keeps no copy between beats. | |
| 32 | ||
| 33 | Code: `src/snapshot/hosted_backup.ts`. Tests: `test/hosted-backup-line.test.ts`, | |
| 34 | `test/tui-plan-row.test.ts`. |
src/snapshot/hosted_backup.ts added +102
| @@ -0,0 +1,102 @@ | ||
| 1 | /** | |
| 2 | * A hosted forge's own line on when its host last backed it up (#13918). The parent already | |
| 3 | * records what its newest snapshot kept of each forge (`hosted.ts`, `snapshot/hosted.json`); it | |
| 4 | * answers each signed beat with that forge's entry (`backupFor`), the child keeps the answer in its | |
| 5 | * own data directory (`noteParentBackup`), and the child's /settings says it in one line | |
| 6 | * (`parentBackupWords`). The child never reads the parent's disk, and the parent tells a forge only | |
| 7 | * of itself. | |
| 8 | */ | |
| 9 | ||
| 10 | import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { ageWords, readHosted } from "./hosted.ts"; | |
| 13 | ||
| 14 | /** What the parent says of one forge's newest backup. */ | |
| 15 | export interface HostedBackup { | |
| 16 | readonly at: string; | |
| 17 | readonly bytes: number; | |
| 18 | } | |
| 19 | ||
| 20 | /** What the child keeps: who hosts it, and the backup, or null when the host has none of it. */ | |
| 21 | export interface ParentBackup { | |
| 22 | readonly host: string; | |
| 23 | readonly backup: HostedBackup | null; | |
| 24 | } | |
| 25 | ||
| 26 | /** Past this long without a backup the line says so. */ | |
| 27 | export const BACKUP_LATE_MS = 36 * 3_600_000; | |
| 28 | export const NOT_BACKED_UP = "Not backed up yet"; | |
| 29 | /** What a late line adds, from `BACKUP_LATE_MS`. */ | |
| 30 | export const LATE_WORDS = `more than ${BACKUP_LATE_MS / 3_600_000} hours`; | |
| 31 | const HOST = /^[A-Za-z0-9.-]{1,253}(:\d{1,5})?$/; | |
| 32 | ||
| 33 | /** Parent side: the newest backup that kept `name`, or undefined when none did. */ | |
| 34 | export function backupFor(dataDir: string, name: string): HostedBackup | undefined { | |
| 35 | const h = readHosted(dataDir)?.find((e) => e.name === name && e.why === undefined); | |
| 36 | return h === undefined ? undefined : { at: h.at, bytes: h.bytes }; | |
| 37 | } | |
| 38 | ||
| 39 | /** A backup as the beat's answer carries it, or null when it does not read whole. */ | |
| 40 | export function parseBackup(v: unknown): HostedBackup | null { | |
| 41 | if (typeof v !== "object" || v === null) return null; | |
| 42 | const { at, bytes } = v as Record<string, unknown>; | |
| 43 | if (typeof at !== "string" || !Number.isFinite(Date.parse(at))) return null; | |
| 44 | if (typeof bytes !== "number" || !Number.isFinite(bytes) || bytes < 0) return null; | |
| 45 | return { at: new Date(Date.parse(at)).toISOString(), bytes }; | |
| 46 | } | |
| 47 | ||
| 48 | const keptFile = (dataDir: string): string => join(dataDir, "parent-backup.json"); | |
| 49 | ||
| 50 | /** Child side: keep what the host's answer said, replacing what it said before. */ | |
| 51 | export function noteParentBackup(dataDir: string, host: string, said: unknown): void { | |
| 52 | if (!HOST.test(host)) return; | |
| 53 | mkdirSync(dataDir, { recursive: true }); | |
| 54 | const tmp = `${keptFile(dataDir)}.${process.pid}.${crypto.randomUUID()}.tmp`; | |
| 55 | writeFileSync(tmp, JSON.stringify({ host, backup: parseBackup(said) } satisfies ParentBackup)); | |
| 56 | renameSync(tmp, keptFile(dataDir)); | |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Whether a host beats for this forge: its `parent.json` names the upstream it reports to. The | |
| 61 | * preview's seeded `parent.json` names only an organisation (#11285) and is not hosted. | |
| 62 | */ | |
| 63 | export function hasHost(dataDir: string): boolean { | |
| 64 | try { | |
| 65 | const v = JSON.parse(readFileSync(join(dataDir, "parent.json"), "utf8")) as Record<string, unknown>; | |
| 66 | return typeof v["upstream"] === "string" && v["upstream"] !== ""; | |
| 67 | } catch { | |
| 68 | return false; | |
| 69 | } | |
| 70 | } | |
| 71 | ||
| 72 | /** What `noteParentBackup` kept, or undefined when the host has not answered yet. */ | |
| 73 | export function parentBackupOf(dataDir: string): ParentBackup | undefined { | |
| 74 | try { | |
| 75 | const v = JSON.parse(readFileSync(keptFile(dataDir), "utf8")) as Record<string, unknown>; | |
| 76 | if (typeof v["host"] !== "string" || !HOST.test(v["host"])) return undefined; | |
| 77 | return { host: v["host"], backup: parseBackup(v["backup"]) }; | |
| 78 | } catch { | |
| 79 | return undefined; | |
| 80 | } | |
| 81 | } | |
| 82 | ||
| 83 | /** | |
| 84 | * The line in two parts: who and how long ago. `said` is "Backed up by <host>" or "Not backed up | |
| 85 | * yet"; `age` is "5 h ago", with ", more than 36 hours" when late, or "" when there is no backup. | |
| 86 | * The settings row puts `said` in its term and `age` in its value, so nothing is said twice. | |
| 87 | */ | |
| 88 | export function parentBackupParts(kept: ParentBackup | undefined, now: number): { said: string; age: string } { | |
| 89 | if (kept?.backup == null) return { said: NOT_BACKED_UP, age: "" }; | |
| 90 | const age = Math.max(0, now - Date.parse(kept.backup.at)); | |
| 91 | const ago = `${ageWords(age)} ago`; | |
| 92 | return { said: `Backed up by ${kept.host}`, age: age > BACKUP_LATE_MS ? `${ago}, ${LATE_WORDS}` : ago }; | |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * The whole line: "Backed up by <host>, 5 h ago", ", more than 36 hours" when late, or "Not backed | |
| 97 | * up yet". | |
| 98 | */ | |
| 99 | export function parentBackupWords(kept: ParentBackup | undefined, now: number): string { | |
| 100 | const { said, age } = parentBackupParts(kept, now); | |
| 101 | return age === "" ? said : `${said}, ${age}`; | |
| 102 | } |
src/spawn/heartbeat.ts +16 −3
| @@ -32,6 +32,7 @@ import { during } from "../perf/jobs.ts"; | ||
| 32 | 32 | import { forgeHasNoKey, NO_KEY_HELD } from "../crew/no_key.ts"; |
| 33 | 33 | |
| 34 | 34 | import { SIGNATURE_HEADER } from "./beat_wire.ts"; |
| 35 | import { backupFor, noteParentBackup } from "../snapshot/hosted_backup.ts"; | |
| 35 | 36 | export { SIGNATURE_HEADER }; |
| 36 | 37 | /** A child reports this often; one silent for three beats reads as quiet. */ |
| 37 | 38 | export const BEAT_MS = 10 * 60_000; |
| @@ -243,12 +244,18 @@ const turns = new Map<string, Promise<void>>(); | ||
| 243 | 244 | /** |
| 244 | 245 | * What a kept beat is answered with: ok, and the tip each app the child follows now stands at on |
| 245 | 246 | * this forge (`spawn/app_links.ts`), so a child in a cell of its own looks at an app that landed |
| 246 | * without waiting for its clock. Nothing secret; only for a child this forge made. | |
| 247 | * without waiting for its clock, and when this forge's newest backup kept that child, its time and | |
| 248 | * size (`snapshot/hosted_backup.ts`). Nothing secret; only for a child this forge made. | |
| 247 | 249 | */ |
| 248 | 250 | export async function beatAnswer(app: App, raw: string): Promise<Record<string, unknown>> { |
| 249 | 251 | const name = parseBeat(raw)?.name; |
| 250 | 252 | const tips = name === undefined ? {} : await appTipsFor(app, name).catch(() => ({})); |
| 251 | return Object.keys(tips).length === 0 ? { ok: true } : { ok: true, follow: tips }; | |
| 253 | const backup = name === undefined ? undefined : backupFor(app.config.dataDir, name); | |
| 254 | return { | |
| 255 | ok: true, | |
| 256 | ...(Object.keys(tips).length === 0 ? {} : { follow: tips }), | |
| 257 | ...(backup === undefined ? {} : { backup }), | |
| 258 | }; | |
| 252 | 259 | } |
| 253 | 260 | |
| 254 | 261 | async function takeBeat( |
| @@ -416,7 +423,13 @@ export async function sendBeat( | ||
| 416 | 423 | if (res.ok) { |
| 417 | 424 | await refreshFunding(app, fetcher); |
| 418 | 425 | // The parent says where the apps this forge follows stand: look at one that moved. |
| 419 | const said = (await res.json().catch(() => undefined)) as { follow?: unknown } | undefined; | |
| 426 | const said = (await res.json().catch(() => undefined)) as { follow?: unknown; backup?: unknown } | undefined; | |
| 427 | // When the host last backed this forge up, for its own settings page. | |
| 428 | try { | |
| 429 | noteParentBackup(app.config.dataDir, new URL(link.upstream).host, said?.backup); | |
| 430 | } catch (error) { | |
| 431 | app.log.warn("the host's word on this forge's backup was not kept", { reason: String(error) }); | |
| 432 | } | |
| 420 | 433 | const { hearLandings, parseHints } = await import("./app_follow.ts"); |
| 421 | 434 | await hearLandings(app, parseHints(said?.follow)).catch(() => {}); |
| 422 | 435 | // What this forge asked its host to let it reach is looked at with every beat, so an answer |
src/tui/screens/planrow.ts +15 −3
| @@ -2,12 +2,15 @@ | ||
| 2 | 2 | * The Plan row in the terminal (#11141): the plan line a forge's admins see |
| 3 | 3 | * at the top of /settings (`planLine`, the page's own words) and, for an |
| 4 | 4 | * admin who is a person, the billing portal address with Enter to set it, |
| 5 | * through `changePortalUrl` so the gate and the checks are the page's. | |
| 5 | * through `changePortalUrl` so the gate and the checks are the page's. On a | |
| 6 | * hosted forge a second line says when its host last backed it up, the | |
| 7 | * settings page's backup row in its words (`parentBackupWords`, #13918). | |
| 6 | 8 | */ |
| 7 | 9 | |
| 8 | 10 | import { changePortalUrl } from "../../actions/signup.ts"; |
| 9 | 11 | import { getPortalUrl } from "../../settings/billing.ts"; |
| 10 | 12 | import { planLine } from "../../web/plan_row.ts"; |
| 13 | import { hasHost, parentBackupOf, parentBackupWords } from "../../snapshot/hosted_backup.ts"; | |
| 11 | 14 | import type { Ctx, Screen } from "../app/app.ts"; |
| 12 | 15 | import { binding } from "../app/keys.ts"; |
| 13 | 16 | import { style } from "../term/style.ts"; |
| @@ -17,6 +20,13 @@ import { fill, len, split } from "../ui/layout.ts"; | ||
| 17 | 20 | import { ListState, drawList, promptScreen } from "./common.ts"; |
| 18 | 21 | import type { PaletteEntry } from "./entries.ts"; |
| 19 | 22 | |
| 23 | /** | |
| 24 | * `Backed up by <host>, 5 h ago` or `Not backed up yet`; undefined on a forge no host beats for. | |
| 25 | */ | |
| 26 | export function backupScreenLine(dataDir: string, now = Date.now()): string | undefined { | |
| 27 | return hasHost(dataDir) ? parentBackupWords(parentBackupOf(dataDir), now) : undefined; | |
| 28 | } | |
| 29 | ||
| 20 | 30 | const person = (f: Session): boolean => f.user?.isAdmin === true && f.user.kind === "user"; |
| 21 | 31 | |
| 22 | 32 | export function planRowScreen(f: Session, ctx: Ctx): Screen { |
| @@ -27,9 +37,11 @@ export function planRowScreen(f: Session, ctx: Ctx): Screen { | ||
| 27 | 37 | keys: [binding(["enter"], "set the billing portal address")], |
| 28 | 38 | render(area, s) { |
| 29 | 39 | const c = ctx.theme.c; |
| 30 | const [head, body] = split(area, "v", [len(1), fill(1)]); | |
| 40 | const backup = backupScreenLine(f.app.config.dataDir); | |
| 41 | const [head, body] = split(area, "v", [len(backup === undefined ? 1 : 2), fill(1)]); | |
| 31 | 42 | const text = note !== "" ? note : (planLine(f.app.config.dataDir) ?? "This forge was told no plan."); |
| 32 | 43 | s.text(head!.x, head!.y, truncate(text, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); |
| 44 | if (backup !== undefined) s.text(head!.x, head!.y + 1, truncate(backup, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); | |
| 33 | 45 | drawList(s, body!, c, state, [{ title: "Billing portal", secondary: getPortalUrl(f.app.db) || "no link" }]); |
| 34 | 46 | }, |
| 35 | 47 | key(e) { |
| @@ -59,7 +71,7 @@ export function planRowScreen(f: Session, ctx: Ctx): Screen { | ||
| 59 | 71 | export const entry: PaletteEntry = { |
| 60 | 72 | id: "plan-row", |
| 61 | 73 | title: "Plan", |
| 62 | hint: "this forge's plan, and where its owner manages billing", | |
| 74 | hint: "this forge's plan, its last backup, and where its owner manages billing", | |
| 63 | 75 | after: ["billing-links", "limits"], |
| 64 | 76 | offered: (session) => session.user?.isAdmin === true, |
| 65 | 77 | run: (session, ctx) => ctx.push(planRowScreen(session, ctx)), |
src/web/pages/account.ts +14 −1
| @@ -66,6 +66,7 @@ import { html, when, type Html } from "../html.ts"; | ||
| 66 | 66 | import { mailCappedBlocked, noMailBlocked } from "../../actions/blocked_words.ts"; |
| 67 | 67 | import { blockedFlash } from "../blocked.ts"; |
| 68 | 68 | import { planRow } from "../plan_row.ts"; |
| 69 | import { hasHost, parentBackupOf, parentBackupParts } from "../../snapshot/hosted_backup.ts"; | |
| 69 | 70 | import { coresRow } from "../cores_row.ts"; |
| 70 | 71 | import { coresLine } from "../../spawn/cores.ts"; |
| 71 | 72 | import { flashTo, page } from "../layout.ts"; |
| @@ -578,6 +579,17 @@ export async function shellsStopIdle(ctx: Ctx): Promise<Response> { | ||
| 578 | 579 | return flashTo(ctx, "/settings/shells", { message: stoppedIdleSays(stopIdleShells(ctx.app, ctx.auth.user.id)) }); |
| 579 | 580 | } |
| 580 | 581 | |
| 582 | /** | |
| 583 | * A hosted forge's backup row (#13918): what its host last said of backing it up, kept from the | |
| 584 | * beat's answer (`snapshot/hosted_backup.ts`). Null on a forge nobody hosts, the preview's seeded | |
| 585 | * one included. | |
| 586 | */ | |
| 587 | function hostedBackupRow(dataDir: string): Html | null { | |
Both doors: this row has no terminal equivalent in src/tui/. Add one and a test. Principle 1/8: dt "Backup" plus dd "Backed up by ..." says the same thing twice, and the 36-hour wording is stilted. |
||
| 588 | if (!hasHost(dataDir)) return null; | |
| 589 | const { said, age } = parentBackupParts(parentBackupOf(dataDir), Date.now()); | |
| 590 | return html`<div class="status-row"><dt>${said}</dt><dd>${age}</dd></div>`; | |
| 591 | } | |
| 592 | ||
| 581 | 593 | // GET /settings |
| 582 | 594 | export function accountPage(ctx: Ctx): Response { |
| 583 | 595 | if (!ctx.auth.user) return signInFirst(ctx); |
| @@ -593,8 +605,9 @@ export function accountPage(ctx: Ctx): Response { | ||
| 593 | 605 | const admin = user.isAdmin === true; |
| 594 | 606 | const made = madeFromOf(ctx.app.config.dataDir); |
| 595 | 607 | const madeRow = made === undefined ? null : html`<div class="status-row"><dt>Started from</dt><dd>${madeFromHtml(ctx.app.db, made, user)}</dd></div>`; |
| 608 | const backupRow = admin ? hostedBackupRow(ctx.app.config.dataDir) : null; | |
| 596 | 609 | const plan = |
| 597 | planRow(ctx.app.config.dataDir, parentIssuerOf(ctx.app.config), admin, html`${madeRow}${coresRow(admin, coresLine())}`) ?? | |
| 610 | planRow(ctx.app.config.dataDir, parentIssuerOf(ctx.app.config), admin, html`${madeRow}${backupRow}${coresRow(admin, coresLine())}`) ?? | |
| 598 | 611 | (madeRow === null ? null : html`<dl class="status-rows">${madeRow}</dl>`); |
| 599 | 612 | const response = accountShell( |
| 600 | 613 | ctx, |
test/hosted-backup-line.test.ts added +116
| @@ -0,0 +1,116 @@ | ||
| 1 | /** | |
| 2 | * #13918: a hosted forge's own settings say when its host last backed it up, carried by the beat's | |
| 3 | * answer from the record the parent's nightly keeps (`snapshot/hosted.json`). | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { createToken } from "../src/core/tokens.ts"; | |
| 11 | import { createUser } from "../src/core/users.ts"; | |
| 12 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 13 | import { beatAnswer, sendBeat } from "../src/spawn/heartbeat.ts"; | |
| 14 | import { noteHosted } from "../src/snapshot/hosted.ts"; | |
| 15 | import { backupFor, hasHost, NOT_BACKED_UP, parentBackupOf, parentBackupWords } from "../src/snapshot/hosted_backup.ts"; | |
| 16 | import { backupScreenLine } from "../src/tui/screens/planrow.ts"; | |
| 17 | import { quietLogs } from "./helpers/globals.ts"; | |
| 18 | ||
| 19 | quietLogs(); | |
| 20 | ||
| 21 | const HOUR = 3_600_000; | |
| 22 | ||
| 23 | describe("the words", () => { | |
| 24 | const now = Date.parse("2026-11-16T12:00:00Z"); | |
| 25 | const at = (h: number) => ({ host: "forge.test", backup: { at: new Date(now - h * HOUR).toISOString(), bytes: 10 } }); | |
| 26 | test("a recent backup names the host and how long ago", () => { | |
| 27 | expect(parentBackupWords(at(5), now)).toBe("Backed up by forge.test, 5 h ago"); | |
| 28 | }); | |
| 29 | test("past 36 hours it says so in words", () => { | |
| 30 | expect(parentBackupWords(at(37), now)).toBe("Backed up by forge.test, 37 h ago, more than 36 hours"); | |
| 31 | }); | |
| 32 | test("none kept, or no answer yet, is not backed up yet", () => { | |
| 33 | expect(parentBackupWords({ host: "forge.test", backup: null }, now)).toBe(NOT_BACKED_UP); | |
| 34 | expect(parentBackupWords(undefined, now)).toBe(NOT_BACKED_UP); | |
| 35 | expect(NOT_BACKED_UP).toBe("Not backed up yet"); | |
| 36 | }); | |
| 37 | }); | |
| 38 | ||
| 39 | test("only a parent.json naming an upstream is hosted, not the preview's seeded one", async () => { | |
| 40 | const dir = await mkdtemp(join(tmpdir(), "forge-hosted-backup-host-")); | |
| 41 | try { | |
| 42 | expect(hasHost(dir)).toBe(false); | |
| 43 | expect(backupScreenLine(dir)).toBeUndefined(); | |
| 44 | await Bun.write(join(dir, "parent.json"), JSON.stringify({ org: "preview-newcomer" })); | |
| 45 | expect(hasHost(dir)).toBe(false); | |
| 46 | await Bun.write(join(dir, "parent.json"), JSON.stringify({ name: "kid", upstream: "https://forge.test" })); | |
| 47 | expect(hasHost(dir)).toBe(true); | |
| 48 | expect(backupScreenLine(dir)).toBe("Not backed up yet"); | |
| 49 | } finally { | |
| 50 | await rm(dir, { recursive: true, force: true }); | |
| 51 | } | |
| 52 | }); | |
| 53 | ||
| 54 | describe("parent to child", () => { | |
| 55 | let parentDir: string; | |
| 56 | let dataDir: string; | |
| 57 | let app: App; | |
| 58 | let servers: Servers; | |
| 59 | let base: string; | |
| 60 | const auth = { authorization: "" }; | |
| 61 | ||
| 62 | beforeAll(async () => { | |
| 63 | parentDir = await mkdtemp(join(tmpdir(), "forge-hosted-backup-parent-")); | |
| 64 | dataDir = await mkdtemp(join(tmpdir(), "forge-hosted-backup-child-")); | |
| 65 | app = createApp({ dataDir, port: 0 }); | |
| 66 | servers = await serve(app); | |
| 67 | base = servers.url; | |
| 68 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 69 | if (!a.ok) throw new Error("user"); | |
| 70 | auth.authorization = `Bearer ${createToken(app.db, a.value.id, "rw", "rw").token}`; | |
| 71 | await Bun.write(join(dataDir, "parent.json"), JSON.stringify({ name: "kid", upstream: "https://forge.test", beatKey: "k" })); | |
| 72 | }, 30_000); | |
| 73 | ||
| 74 | afterAll(async () => { | |
| 75 | await servers.stop(); | |
| 76 | app.close(); | |
| 77 | await rm(dataDir, { recursive: true, force: true }); | |
| 78 | await rm(parentDir, { recursive: true, force: true }); | |
| 79 | }); | |
| 80 | ||
| 81 | const settings = async () => (await (await fetch(`${base}/settings`, { headers: auth })).text()); | |
| 82 | const beatOnce = (answer: Record<string, unknown>) => sendBeat(app, async (url) => (url.endsWith("/heartbeat") ? Response.json(answer) : new Response("{}"))); | |
| 83 | ||
| 84 | test("the parent answers a kept forge's beat with its record, and nothing for one it did not keep", async () => { | |
| 85 | const at = new Date(Date.now() - 2 * HOUR).toISOString(); | |
| 86 | noteHosted(parentDir, [ | |
| 87 | { name: "kid", at, repos: 1, bytes: 2048, key: "wrapped" }, | |
| 88 | { name: "other", at, repos: 1, bytes: 1, key: "none", why: "too big" }, | |
| 89 | ]); | |
| 90 | expect(backupFor(parentDir, "kid")).toEqual({ at, bytes: 2048 }); | |
| 91 | expect(backupFor(parentDir, "other")).toBeUndefined(); | |
| 92 | expect(backupFor(parentDir, "nobody")).toBeUndefined(); | |
| 93 | const parent = { config: { dataDir: parentDir } } as unknown as App; | |
| 94 | const answer = await beatAnswer(parent, JSON.stringify({ name: "kid", at: Date.now(), version: "1", openProposals: 0, children: 0 })); | |
| 95 | expect(answer["backup"]).toEqual({ at, bytes: 2048 }); | |
| 96 | // The record is read once and held until the file changes: the next night's is seen. | |
| 97 | const later = new Date(Date.now() - HOUR).toISOString(); | |
| 98 | noteHosted(parentDir, [{ name: "kid", at: later, repos: 2, bytes: 40960, key: "wrapped" }]); | |
| 99 | expect(backupFor(parentDir, "kid")).toEqual({ at: later, bytes: 40960 }); | |
| 100 | }); | |
| 101 | ||
| 102 | test("a child with no word from its host says none yet", async () => { | |
| 103 | expect(await settings()).toContain("<dt>Not backed up yet</dt><dd></dd>"); | |
| 104 | }); | |
| 105 | ||
| 106 | test("a child whose host kept it shows the time", async () => { | |
| 107 | expect(await beatOnce({ ok: true, backup: { at: new Date(Date.now() - 3 * HOUR).toISOString(), bytes: 2048 } })).toBe(true); | |
| 108 | expect(parentBackupOf(dataDir)?.host).toBe("forge.test"); | |
| 109 | expect(await settings()).toContain("<dt>Backed up by forge.test</dt><dd>3 h ago</dd>"); | |
| 110 | }); | |
| 111 | ||
| 112 | test("a host that answers with no record turns it back to none yet", async () => { | |
| 113 | expect(await beatOnce({ ok: true })).toBe(true); | |
| 114 | expect(await settings()).toContain("<dt>Not backed up yet</dt><dd></dd>"); | |
| 115 | }); | |
| 116 | }); |
test/palette-roster.test.ts +2 −2
| @@ -182,7 +182,7 @@ test("a site-admin person is offered the whole roster", () => { | ||
| 182 | 182 | ["flows-all", "Flows across the forge", "every repository's flow lines, by kind and stale"], |
| 183 | 183 | ["teams", "Crew teams", "each team's charter, dial and members"], |
| 184 | 184 | ["billing-links", "Buy links", "where each hosted plan is paid for, shown on the home page"], |
| 185 | ["plan-row", "Plan", "this forge's plan, and where its owner manages billing"], | |
| 185 | ["plan-row", "Plan", "this forge's plan, its last backup, and where its owner manages billing"], | |
| 186 | 186 | ["agents", "Crew agents", "each agent's model, budget and brief"], |
| 187 | 187 | ["invites", "Invite links", "make a single-use link, see which are open, revoke one"], |
| 188 | 188 | ["claim-start", "Take an agent's work", "claim the repositories an agent started, from its one-time link"], |
| @@ -310,7 +310,7 @@ test("a site-admin agent is offered the roster without the person-only entries", | ||
| 310 | 310 | ["audit", "Who can do what", "each app's doors from its contract.json: yes, own or no"], |
| 311 | 311 | ["archived", "Archived repositories", "put away, not gone: open one or restore it"], |
| 312 | 312 | ["apps-crew", "What the crew is doing", "one plain sentence per app: who is on what"], |
| 313 | ["plan-row", "Plan", "this forge's plan, and where its owner manages billing"], | |
| 313 | ["plan-row", "Plan", "this forge's plan, its last backup, and where its owner manages billing"], | |
| 314 | 314 | ["cloud", "Cloud accounts", "provider tokens, what they are used for, today's calls"], |
| 315 | 315 | ["pinned", "Pinned forges", "deploy, update and roll back a forge on your cloud"], |
| 316 | 316 | ["feed", "Feed", "what happened, ranked; r replies on a post about an issue or pull, l likes, h who liked, p its permalink, o a run's log"], |
test/tui-plan-row.test.ts +13
| @@ -11,6 +11,7 @@ import { createApp, type App as Forge } from "../src/app.ts"; | ||
| 11 | 11 | import { keepNotice } from "../src/billing/notice.ts"; |
| 12 | 12 | import { createUser, getUserByName } from "../src/core/users.ts"; |
| 13 | 13 | import { getPortalUrl } from "../src/settings/billing.ts"; |
| 14 | import { noteParentBackup } from "../src/snapshot/hosted_backup.ts"; | |
| 14 | 15 | import { createForgeApp } from "../src/tui/forge.ts"; |
| 15 | 16 | import type { TuiIdentity } from "../src/tui/session.ts"; |
| 16 | 17 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; |
| @@ -85,3 +86,15 @@ test("a member is not offered the screen", async () => { | ||
| 85 | 86 | expect(h.text()).not.toContain("Billing portal"); |
| 86 | 87 | h.stop(); |
| 87 | 88 | }); |
| 89 | ||
| 90 | test("a hosted forge's admin reads the backup line the settings page shows (#13918)", async () => { | |
| 91 | const h = boot("alice"); | |
| 92 | await h.open(); | |
| 93 | expect(h.text()).toContain("Not backed up yet"); | |
| 94 | h.stop(); | |
| 95 | noteParentBackup(dir, "host.example", { at: new Date(Date.now() - 3 * 3_600_000).toISOString(), bytes: 2048 }); | |
| 96 | const again = boot("alice"); | |
| 97 | await again.open(); | |
| 98 | expect(again.text()).toContain("Backed up by host.example, 3 h ago"); | |
| 99 | again.stop(); | |
| 100 | }); |
new above
offline
Box 2/4: the issue says "Not backed up yet"; this says "None yet". Use the issue words, and carry the same wording into the tests and docs.