Public: anyone can read this; the crew can change it.
/games says real uptime, memory and CPU, from the cloud tree's containers #17743
Merged 17h 59m ago, live in production
sold/games-up-since → main 0 commits 1 thread · 1 unresolved
7 files
changed +172
−41
docs/features/games.md +1 −1
| @@ -170,4 +170,4 @@ A game's own refusal is a failure: `op` with a name that does not exist ("That p | ||
| 170 | 170 | |
| 171 | 171 | ## On a phone (#17656) |
| 172 | 172 | |
| 173 | `/games/<box>` opens on **How to join** (the box's machine name with Copy, and who is on, server by server), then one row a server: its name, its state in words, players, TPS, memory, CPU, up since and the 24 h sparklines, and a quiet button (Start while it is not running, else Restart); Stop, Restart and Remove are behind **More**. A server's page leads with its state, its main button (the page's one accent), who is on and **Why someone could not join** (each cause with who, how often and "Tell them": what to say). Console, Numbers, Sleep, World, Environment, Settings and Mods follow, each folded and one tap away; nothing was removed (the numbers stayed in the row). A start, stop or restart that was asked shows on the server ("Starting: asked 10 s ago", then "Started" or why not) until the server answers, and the page updates itself. The player form has no default: choose what to do, in the plain word with the game's own beside it in the choice only (Let in (whitelist), Make operator (op), Ban, Lift a ban (pardon)); headings and notes use the plain word. The terminal says the same: the state line, the address, the "Tell them" fixes and the plain words (`src/games/gist.ts`). No address shows when the box has no machine name. | |
| 173 | `/games/<box>` opens on **How to join** (the box's machine name with Copy, and who is on, server by server), then one row a server: its name, its state in words, players, TPS, then memory, CPU and how long it has been up in the cloud tree's words (`containersIn` in `src/cloud/containers.ts`: "not known" for what the box did not say or once it is silent, nothing for a server that is not running) and the 24 h sparklines, and a quiet button (Start while it is not running, else Restart); Stop, Restart and Remove are behind **More**. A server's page leads with its state, the same memory, CPU and uptime line under it, which says so when the server is not running (the terminal's people part too), its main button (the page's one accent), who is on and **Why someone could not join** (each cause with who, how often and "Tell them": what to say). Console, Numbers, Sleep, World, Environment, Settings and Mods follow, each folded and one tap away; nothing was removed (the numbers stayed in the row). A start, stop or restart that was asked shows on the server ("Starting: asked 10 s ago", then "Started" or why not) until the server answers, and the page updates itself. The player form has no default: choose what to do, in the plain word with the game's own beside it in the choice only (Let in (whitelist), Make operator (op), Ban, Lift a ban (pardon)); headings and notes use the plain word. The terminal says the same: the state line, the address, the "Tell them" fixes and the plain words (`src/games/gist.ts`). No address shows when the box has no machine name. |
src/cloud/containers.ts +53 −30
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | /** |
| 2 | * The containers on a machine, for the cloud tree (#17532): today a game box's, as its last full | |
| 2 | * The containers on a machine, for the cloud tree and /games (#17532): today a game box's, as its last full | |
| 3 | 3 | * sync wrote them. Nothing is asked of the box, and nothing here changes it. A person sees the ones |
| 4 | 4 | * their games role covers. What the sync did not say is null or empty, never 0: the agent sends 0 |
| 5 | 5 | * when Docker's figures could not be read, and its `startedAt` is when the container was made, so |
| @@ -11,7 +11,7 @@ import type { Database } from "bun:sqlite"; | ||
| 11 | 11 | import { listPinned } from "../core/pinned.ts"; |
| 12 | 12 | import type { User } from "../core/users.ts"; |
| 13 | 13 | import { nowWords } from "../games/gist.ts"; |
| 14 | import { listBoxes, mayDo, roleOn, seesBox, servicesOf } from "../games/store.ts"; | |
| 14 | import { listBoxes, mayDo, roleOn, seesBox, servicesOf, type GameBox, type GameService } from "../games/store.ts"; | |
| 15 | 15 | import { agoWords } from "../kernel/ago.ts"; |
| 16 | 16 | |
| 17 | 17 | export interface Container { |
| @@ -24,6 +24,8 @@ export interface Container { | ||
| 24 | 24 | readonly cpu: string; |
| 25 | 25 | readonly memory: string; |
| 26 | 26 | readonly uptime: string; |
| 27 | /** Memory, CPU and uptime in one short line, as /games says them. */ | |
| 28 | readonly brief: string; | |
| 27 | 29 | /** Empty for anyone but the box's owner. */ |
| 28 | 30 | readonly image: string; |
| 29 | 31 | /** Its own page, where it is changed. */ |
| @@ -45,6 +47,54 @@ const spanWords = (since: string, now: number): string => agoWords(since, now).r | ||
| 45 | 47 | const limitWords = (n: number | null, words: (n: number) => string): string => (n === null ? ", limit not known" : n === 0 ? ", no limit set" : `, limit ${words(n)}`); |
| 46 | 48 | const cores = (n: number): string => `${Number(n.toFixed(3))} ${n === 1 ? "core" : "cores"}`; |
| 47 | 49 | |
| 50 | /** "900 MiB of 2.0 GiB · 12.5% CPU · up 3 hours", for a row; empty for one that is not running, whose state says it. */ | |
| 51 | function briefOf(s: GameService, silent: boolean, up: string | undefined): string { | |
| 52 | if (silent) return "memory, CPU and uptime not known"; | |
| 53 | if (s.state !== "running") return ""; | |
| 54 | const used = s.memMiB < 1 ? "memory and CPU not known" : `${mib(s.memMiB)}${s.memLimitMiB ? ` of ${mib(s.memLimitMiB)}` : ""} · ${s.cpuPct.toFixed(1)}% CPU`; | |
| 55 | return `${used} · ${up === undefined ? "uptime not known" : `up ${up}`}`; | |
| 56 | } | |
| 57 | ||
| 58 | /** The containers of one box that `viewer` may see, whether or not its machine is known. */ | |
| 59 | export function containersIn(db: Database, viewer: User | undefined, box: GameBox, now = Date.now()): Container[] { | |
| 60 | const all = servicesOf(db, box.id); | |
| 61 | // A row stays when its container is gone; the newest full sync names the ones there are. | |
| 62 | const newest = all.map((s) => s.seenAt).sort().at(-1); | |
| 63 | const silent = newest === undefined || now - Date.parse(newest) > SILENT_MS; | |
| 64 | // The diagnosis names images to the box's owner alone (`diag.ts`). | |
| 65 | const owner = mayDo(roleOn(db, viewer, box.id), "owner"); | |
| 66 | return all | |
| 67 | .filter((s) => s.seenAt === newest && mayDo(roleOn(db, viewer, box.id, s.name), "viewer")) | |
| 68 | .map((s): Container => { | |
| 69 | // Compose names a container `<project>-<service>-1` unless the file does. | |
| 70 | const ps = box.diag.ps?.find((c) => c.name === `game-${s.name}-1`) ?? box.diag.ps?.find((c) => c.name === s.name); | |
| 71 | const figure = (words: string): string => (silent ? UNKNOWN : s.state !== "running" ? STOPPED : words); | |
| 72 | const measured = s.memMiB >= 1; | |
| 73 | const has = box.host["memMiB"] ?? 0; | |
| 74 | const state = nowWords(s, undefined); | |
| 75 | const up = s.upSince !== null ? spanWords(s.upSince, now) : /^Up (.+?)(?: \(.*\))?$/.exec(ps?.status ?? "")?.[1]; | |
| 76 | return { | |
| 77 | box: box.name, | |
| 78 | name: s.name, | |
| 79 | state: silent ? `${UNKNOWN}: ${state} ${agoWords(s.seenAt, now)}` : state, | |
| 80 | health: silent ? "" : s.health, | |
| 81 | cpu: figure(measured || s.cpuLimit !== null ? `${measured ? `${s.cpuPct.toFixed(1)}% of one core` : UNKNOWN}${limitWords(s.cpuLimit, cores)}` : UNKNOWN), | |
| 82 | memory: figure(measured || s.memLimitMiB !== null ? `${measured ? mib(s.memMiB) : UNKNOWN}${limitWords(s.memLimitMiB, mib)}${has > 0 && !s.memLimitMiB ? `; its machine has ${mib(has)}` : ""}` : UNKNOWN), | |
| 83 | uptime: figure(up ?? UNKNOWN), | |
| 84 | brief: briefOf(s, silent, up), | |
| 85 | image: owner ? (ps?.image ?? "") || UNKNOWN : "", | |
| 86 | page: `/games/${box.name}/${s.name}`, | |
| 87 | readAt: s.seenAt, | |
| 88 | }; | |
| 89 | }); | |
| 90 | } | |
| 91 | ||
| 92 | /** The line under a server's state on its own page. It always says something, so what is below it stays put on a start or a stop. */ | |
| 93 | export function figuresOf(db: Database, viewer: User | undefined, box: GameBox, service: string): string { | |
| 94 | const c = containersIn(db, viewer, box).find((x) => x.name === service); | |
| 95 | return c === undefined ? "memory, CPU and uptime not known" : c.brief || "not running: no memory or CPU to show"; | |
| 96 | } | |
| 97 | ||
| 48 | 98 | /** Every container `viewer` may see, by its machine as the cloud tree names it: `<cloud>/<account>/machine:<id>`. */ |
| 49 | 99 | export function containersOf(db: Database, viewer: User | undefined, now = Date.now()): Map<string, readonly Container[]> { |
| 50 | 100 | const out = new Map<string, readonly Container[]>(); |
| @@ -54,34 +104,7 @@ export function containersOf(db: Database, viewer: User | undefined, now = Date. | ||
| 54 | 104 | for (const box of boxes) { |
| 55 | 105 | const m = machines.get(box.pinnedId); |
| 56 | 106 | if (m === undefined) continue; |
| 57 | const all = servicesOf(db, box.id); | |
| 58 | // A row stays when its container is gone; the newest full sync names the ones there are. | |
| 59 | const newest = all.map((s) => s.seenAt).sort().at(-1); | |
| 60 | const silent = newest === undefined || now - Date.parse(newest) > SILENT_MS; | |
| 61 | // The diagnosis names images to the box's owner alone (`diag.ts`). | |
| 62 | const owner = mayDo(roleOn(db, viewer, box.id), "owner"); | |
| 63 | const own = all | |
| 64 | .filter((s) => s.seenAt === newest && mayDo(roleOn(db, viewer, box.id, s.name), "viewer")) | |
| 65 | .map((s): Container => { | |
| 66 | // Compose names a container `<project>-<service>-1` unless the file does. | |
| 67 | const ps = box.diag.ps?.find((c) => c.name === `game-${s.name}-1`) ?? box.diag.ps?.find((c) => c.name === s.name); | |
| 68 | const figure = (words: string): string => (silent ? UNKNOWN : s.state !== "running" ? STOPPED : words); | |
| 69 | const measured = s.memMiB >= 1; | |
| 70 | const has = box.host["memMiB"] ?? 0; | |
| 71 | const state = nowWords(s, undefined); | |
| 72 | return { | |
| 73 | box: box.name, | |
| 74 | name: s.name, | |
| 75 | state: silent ? `${UNKNOWN}: ${state} ${agoWords(s.seenAt, now)}` : state, | |
| 76 | health: silent ? "" : s.health, | |
| 77 | cpu: figure(measured || s.cpuLimit !== null ? `${measured ? `${s.cpuPct.toFixed(1)}% of one core` : UNKNOWN}${limitWords(s.cpuLimit, cores)}` : UNKNOWN), | |
| 78 | memory: figure(measured || s.memLimitMiB !== null ? `${measured ? mib(s.memMiB) : UNKNOWN}${limitWords(s.memLimitMiB, mib)}${has > 0 && !s.memLimitMiB ? `; its machine has ${mib(has)}` : ""}` : UNKNOWN), | |
| 79 | uptime: figure(s.upSince !== null ? spanWords(s.upSince, now) : (/^Up (.+?)(?: \(.*\))?$/.exec(ps?.status ?? "")?.[1] ?? UNKNOWN)), | |
| 80 | image: owner ? (ps?.image ?? "") || UNKNOWN : "", | |
| 81 | page: `/games/${box.name}/${s.name}`, | |
| 82 | readAt: s.seenAt, | |
| 83 | }; | |
| 84 | }); | |
| 107 | const own = containersIn(db, viewer, box, now); | |
| 85 | 108 | if (own.length > 0) out.set(`${m.target}/${m.account}/machine:${m.remote}`, own); |
| 86 | 109 | } |
| 87 | 110 | return out; |
src/http/mcp/tools/games.ts +5 −2
Diff not shown — a line 2,420 characters wide (limit 2,000). +5 −2
View filesrc/tui/screens/games.ts +6 −3
| @@ -19,6 +19,7 @@ import { consoleText, crashOf } from "../../games/console.ts"; | ||
| 19 | 19 | import { commitPolicy, policyOf, proxyPort, STILL_BILLED } from "../../games/sleep.ts"; |
| 20 | 20 | import { SHELL_SERVICE, queueShell } from "../../games/shell.ts"; |
| 21 | 21 | import { watchBox } from "../../games/notify.ts"; |
| 22 | import { containersIn, figuresOf } from "../../cloud/containers.ts"; | |
| 22 | 23 | import { joinAddress, NO_ADDRESS, nowWords, powerNow, PREVIEW_WORDS, privateWords, SAVE_WORDS } from "../../games/gist.ts"; |
| 23 | 24 | import { findingLine, findingsOf as kickFindingsOf } from "../../games/findings.ts"; |
| 24 | 25 | import { eventsOf, eventWords, isBedrock, peopleOf, peopleWords, queuePeople } from "../../games/people.ts"; |
| @@ -68,9 +69,9 @@ import { gameConfigScreen } from "./game_config.ts"; | ||
| 68 | 69 | import { gameModsScreen } from "./game_mods.ts"; |
| 69 | 70 | |
| 70 | 71 | /** "4 online · 19.8 TPS · " from the last hour's numbers, or nothing before any. */ |
| 71 | function playersTps(f: Session, box: { id: string }, service: string): string { | |
| 72 | function playersTps(f: Session, box: { id: string }, service: string): string[] { | |
| 72 | 73 | const last = metricsSeries(f.app.db, box, service, "1h").at(-1); |
| 73 | return last === undefined ? "" : `${last.players} online · ${last.tps === null ? "" : `${last.tps} TPS · `}`; | |
| 74 | return last === undefined ? [] : [`${last.players} online`, ...(last.tps === null ? [] : [`${last.tps} TPS`])]; | |
| 74 | 75 | } |
| 75 | 76 | |
| 76 | 77 | export function gamesScreen(f: Session, ctx: Ctx): Screen { |
| @@ -172,10 +173,11 @@ export function gamesScreen(f: Session, ctx: Ctx): Screen { | ||
| 172 | 173 | ? emptyText("gameServices") |
| 173 | 174 | : joinLine(); |
| 174 | 175 | s.text(head!.x, head!.y, truncate(said, head!.w), style(booting !== undefined && note === "" && bootTroubled(booting) ? c.danger : c.mutedFg, c.bg), head!.x + head!.w); |
| 176 | const brief = new Map([...new Map(all.map(({ box }) => [box.id, box])).values()].flatMap((b) => containersIn(f.app.db, f.user, b).map((c) => [`${b.id}/${c.name}`, c.brief] as const))); | |
| 175 | 177 | const view: ListRow[] = all.map(({ box, service }) => ({ |
| 176 | 178 | title: `${box.name}/${service.name}`, |
| 177 | 179 | ...(service.state === "exited" ? { tone: "danger" as const } : {}), |
| 178 | secondary: `${nowWords(service, powerNow(f.app.db, box.id, service.name))} · ${playersTps(f, box, service.name)}${Math.round(service.memMiB)} MiB · ${service.cpuPct.toFixed(1)}% CPU`, | |
| 180 | secondary: [nowWords(service, powerNow(f.app.db, box.id, service.name)), ...playersTps(f, box, service.name), brief.get(`${box.id}/${service.name}`) ?? ""].filter((x) => x !== "").join(" · "), | |
| 179 | 181 | meta: box.seenAt.slice(11, 19), |
| 180 | 182 | })); |
| 181 | 183 | // The machines after the services: the cursor moves over the services only. |
| @@ -313,6 +315,7 @@ export function gameServiceScreen(f: Session, ctx: Ctx, box: GameBox, service: s | ||
| 313 | 315 | const tag = (n: string) => `${n}${isBedrock(n) ? " (Bedrock)" : ""}${lists.ops.some((o) => o.name === n) ? " · op" : ""}`; |
| 314 | 316 | return [ |
| 315 | 317 | nowWords(servicesOf(f.app.db, box.id).find((x) => x.name === service), powerNow(f.app.db, box.id, service)), |
| 318 | figuresOf(f.app.db, f.user, boxNamed(f.app.db, box.name) ?? box, service), | |
| 316 | 319 | ...findingsOf(f.app.db, box.id, service).flatMap((x) => [x.says, ...x.fix.map((l) => `Fix: ${l}`)]), |
| 317 | 320 | online.length === 0 ? `Nobody is on ${service} right now.` : `Online: ${online.map((p) => tag(p.name)).join(", ")}`, |
| 318 | 321 | `${lists.whitelistOn ? "Only listed players may join" : "Anyone may join"} · ${lists.whitelist.length} listed · ${lists.ops.length} operators · ${lists.bans.length} banned`, |
src/web/pages/games.ts +10 −4
| @@ -73,6 +73,7 @@ import { START_BOX } from "../../core/empty_words.ts"; | ||
| 73 | 73 | import { emptyLine, emptyNote } from "../empty_words.ts"; |
| 74 | 74 | import { findingsOf, type GameFinding } from "../../games/player_events.ts"; |
| 75 | 75 | import { findingsOf as kickFindingsOf } from "../../games/findings.ts"; |
| 76 | import { containersIn, figuresOf } from "../../cloud/containers.ts"; | |
| 76 | 77 | import { joinAddress, mainPower, NO_ADDRESS, PREVIEW_WORDS, privateWords, SAVE_WORDS, nowWords, PEOPLE_BUTTON, PEOPLE_CHOICES, powerNow, whoIsOn } from "../../games/gist.ts"; |
| 77 | 78 | import { metricsSeries, playerEvents, recentErrors, type Point } from "../../games/metrics.ts"; |
| 78 | 79 | import { daySpark } from "../studio/kpi_row.ts"; |
| @@ -274,6 +275,7 @@ export async function gameBoxPage(ctx: Ctx): Promise<Response> { | ||
| 274 | 275 | if (got instanceof Response) return got; |
| 275 | 276 | const { box, role } = got; |
| 276 | 277 | const seen = servicesOf(ctx.app.db, box.id); |
| 278 | const brief = new Map(containersIn(ctx.app.db, ctx.auth.user, box).map((c) => [c.name, c.brief])); | |
| 277 | 279 | const compose = await readCompose(ctx.app, box); |
| 278 | 280 | const held = await heldNotice(ctx, box, compose); |
| 279 | 281 | const names = [...new Set([...seen.map((s) => s.name), ...(compose === undefined ? [] : composeServices(compose.text))])].sort(); |
| @@ -316,7 +318,7 @@ export async function gameBoxPage(ctx: Ctx): Promise<Response> { | ||
| 316 | 318 | return row({ |
| 317 | 319 | href: `/games/${box.name}/${n}`, |
| 318 | 320 | title: n, |
| 319 | meta: html`${nowWords(s, powerNow(ctx.app.db, box.id, n))}${players === null ? "" : ` · ${players} online`}${tps === null ? "" : ` · ${tps.toFixed(1)} TPS`}${s === undefined ? "" : ` · ${mib(s.memMiB)} · ${s.cpuPct.toFixed(1)}% CPU`}${s?.startedAt ? html` · up since ${time(s.startedAt)}` : ""} | |
| 321 | meta: html`${nowWords(s, powerNow(ctx.app.db, box.id, n))}${players === null ? "" : ` · ${players} online`}${tps === null ? "" : ` · ${tps.toFixed(1)} TPS`}${brief.get(n) ? ` · ${brief.get(n)!}` : ""} | |
| 320 | 322 | ${day.length === 0 ? "" : html`<span class="bar muted">${sparkOf(day, (p) => p.players, `${n}: players over 24 hours`)}${sparkOf(day, (p) => p.tps, `${n}: TPS over 24 hours`)}${sparkOf(day, (p) => p.memMiB, `${n}: memory over 24 hours`)}</span>`}`, |
| 321 | 323 | ...(act |
| 322 | 324 | ? { after: power(box, n, s, removable(n) ? btn("Remove", { size: "sm", variant: "ghost", href: `/games/${box.name}?remove=${encodeURIComponent(n)}`, label: `Remove ${n}` }) : null, "ghost") } |
| @@ -502,6 +504,8 @@ export const gamesConsoleJs = /* js */ ` | ||
| 502 | 504 | if (stick) log.scrollTop = log.scrollHeight; |
| 503 | 505 | var now = document.getElementById('game-now'); |
| 504 | 506 | if (now && d.now) now.textContent = d.now; |
| 507 | var figures = document.getElementById('game-figures'); | |
| 508 | if (figures && typeof d.figures === 'string') figures.textContent = d.figures; | |
| 505 | 509 | (d.results || []).forEach(function (r) { |
| 506 | 510 | var el = document.getElementById('cmd-' + r.id); |
| 507 | 511 | if (el) el.textContent = r.says; |
| @@ -574,6 +578,7 @@ export async function gameServicePage(ctx: Ctx): Promise<Response> { | ||
| 574 | 578 | scriptJs: gamesConsoleJs, |
| 575 | 579 | body: html`${pageHead(service, { sub: `on ${box.name}`, actions: act ? power(box, service, seen) : undefined })} |
| 576 | 580 | <p class="game-now" id="game-now" aria-live="polite">${nowWords(seen, powerNow(ctx.app.db, box.id, service))}</p> |
| 581 | <p class="muted small" id="game-figures">${figuresOf(ctx.app.db, ctx.auth.user, box, service)}</p> | |
| 577 | 582 | ${held} |
| 578 | 583 | ${findingsBlock(findings)} |
| 579 | 584 | ${joinTrouble(ctx, box, service)} |
| @@ -975,10 +980,11 @@ export function gameServiceStream(ctx: Ctx): Response { | ||
| 975 | 980 | after = got.last; |
| 976 | 981 | const results = answers(); |
| 977 | 982 | const now = nowWords(servicesOf(ctx.app.db, box.id).find((x) => x.name === service), powerNow(ctx.app.db, box.id, service)); |
| 983 | const figures = figuresOf(ctx.app.db, ctx.auth.user, boxNamed(ctx.app.db, box.name) ?? box, service); | |
| 978 | 984 | const shownBefore = shown; |
| 979 | shown = now; | |
| 980 | if (got.lines.length > 0 || results.length > 0 || now !== shownBefore) | |
| 981 | emit("lines", { now, html: consoleHtml(got.lines), lines: got.lines, results: results.map((r) => ({ id: r.id, ok: r.ok, output: r.output, says: commandSays(r) })) }); | |
| 985 | shown = `${now}\n${figures}`; | |
| 986 | if (got.lines.length > 0 || results.length > 0 || shown !== shownBefore) | |
| 987 | emit("lines", { now, figures, html: consoleHtml(got.lines), lines: got.lines, results: results.map((r) => ({ id: r.id, ok: r.ok, output: r.output, says: commandSays(r) })) }); | |
| 982 | 988 | }), |
| 983 | 989 | ); |
| 984 | 990 | } |
test/cloud-containers.test.ts +95
| @@ -17,9 +17,11 @@ import { createSession } from "../src/core/sessions.ts"; | ||
| 17 | 17 | import { aboutContext, aboutLine } from "../src/core/talk_about.ts"; |
| 18 | 18 | import { createToken } from "../src/core/tokens.ts"; |
| 19 | 19 | import { createUser, type User } from "../src/core/users.ts"; |
| 20 | import { synced } from "../src/games/notify.ts"; | |
| 20 | 21 | import { boxNamed, createGameBox, grant, recordSync, type SyncBody } from "../src/games/store.ts"; |
| 21 | 22 | import { serve, type Servers } from "../src/http/server.ts"; |
| 22 | 23 | import { cloudLines, cloudTreeScreen } from "../src/tui/screens/cloudtree.ts"; |
| 24 | import { gameServiceScreen, gamesScreen } from "../src/tui/screens/games.ts"; | |
| 23 | 25 | import { createForgeApp } from "../src/tui/forge.ts"; |
| 24 | 26 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; |
| 25 | 27 | import { mcpTool } from "./helpers/mcp.ts"; |
| @@ -159,6 +161,99 @@ test("the box is not trusted: a field out of its range is not known, and never 0 | ||
| 159 | 161 | expect((row({ upSince: new Date(Date.now() + 60_000).toISOString() }) as { up_since: string | null }).up_since).not.toBeNull(); |
| 160 | 162 | }); |
| 161 | 163 | |
| 164 | test("/games says the same, short, on the web and in the terminal, and never when the container was made", async () => { | |
| 165 | const web = async (): Promise<string> => (await fetch(`${servers.url}/games/mc`, { headers: { cookie: `forge_session=${createSession(app.db, alice.id).token}` } })).text(); | |
| 166 | const tty = async (): Promise<string> => { | |
| 167 | const { app: tui, session } = createForgeApp(app, { sshUsername: "alice", remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user: alice }, { terminal: headlessTerminal(160, 40), exitHooks: false }); | |
| 168 | tui.start(); | |
| 169 | tui.ctx.push(gamesScreen(session, tui.ctx)); | |
| 170 | await settleApp(tui); | |
| 171 | const text = tui.screen.toText(); | |
| 172 | tui.stop?.(); | |
| 173 | return text; | |
| 174 | }; | |
| 175 | const both = async (): Promise<string[]> => [await web(), await tty()]; | |
| 176 | // An older agent: the diagnosis' own words. | |
| 177 | for (const text of await both()) { | |
| 178 | expect(text).toContain("900 MiB · 12.5% CPU · up 3 hours"); | |
| 179 | expect(text).toContain("memory and CPU not known · uptime not known"); | |
| 180 | for (const wrong of ["up since", "2026-11-01", " 0 MiB", "0.0% CPU"]) expect(text).not.toContain(wrong); | |
| 181 | } | |
| 182 | sync({ ...SYNC, services: [{ ...service("hub", "running", 12.5, 900), upSince: new Date(Date.now() - 26 * 3_600_000).toISOString(), memLimitMiB: 2048, cpuLimit: 2 }] }); | |
| 183 | for (const text of await both()) expect(text).toContain("900 MiB of 2.0 GiB · 12.5% CPU · up 26 hours"); | |
| 184 | app.db.run("DELETE FROM game_service"); | |
| 185 | sync(SYNC, new Date(Date.now() - 3 * 3_600_000)); | |
| 186 | for (const text of await both()) expect(text).toContain("memory, CPU and uptime not known"); | |
| 187 | }); | |
| 188 | ||
| 189 | test("a server's own page says the same line under its state, live too, and the terminal under its state", async () => { | |
| 190 | const cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 191 | const web = async (name: string): Promise<string | undefined> => /id="game-now"[^>]*>[^<]*<\/p>\s*<p[^>]*id="game-figures"[^>]*>([^<]*)<\/p>/.exec(await (await fetch(`${servers.url}/games/mc/${name}`, { headers: { cookie } })).text())?.[1]; | |
| 192 | const tty = async (): Promise<string> => { | |
| 193 | const terminal = headlessTerminal(160, 40); | |
| 194 | const { app: tui, session } = createForgeApp(app, { sshUsername: "alice", remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user: alice }, { terminal, exitHooks: false }); | |
| 195 | tui.start(); | |
| 196 | tui.ctx.push(gameServiceScreen(session, tui.ctx, boxNamed(app.db, "mc")!, "hub")); | |
| 197 | await settleApp(tui); | |
| 198 | terminal.feed("\t\t"); | |
| 199 | await settleApp(tui); | |
| 200 | const text = tui.screen.toText(); | |
| 201 | tui.stop?.(); | |
| 202 | return text; | |
| 203 | }; | |
| 204 | app.db.run("DELETE FROM game_service"); | |
| 205 | sync(); | |
| 206 | expect(await web("hub")).toBe("900 MiB · 12.5% CPU · up 3 hours"); | |
| 207 | const none = "not running: no memory or CPU to show"; | |
| 208 | expect(await web("survival")).toBe(none); | |
| 209 | expect(await tty()).toMatch(/running[^\n]*\n[^\n]*900 MiB · 12\.5% CPU · up 3 hours/); | |
| 210 | // The open page: only the figures changed. | |
| 211 | const res = await fetch(`${servers.url}/games/mc/hub/stream`, { headers: { cookie } }); | |
| 212 | const reader = res.body!.getReader(); | |
| 213 | const next = async (): Promise<string> => { | |
| 214 | for (;;) { | |
| 215 | const got = await Promise.race([reader.read(), new Promise<never>((_, no) => setTimeout(() => no(new Error("no event")), 3000))]); | |
| 216 | const text = new TextDecoder().decode(got.value); | |
| 217 | if (text.includes("event: lines")) return text; | |
| 218 | } | |
| 219 | }; | |
| 220 | const id = boxNamed(app.db, "mc")!.id; | |
| 221 | synced(app, id); | |
| 222 | expect(await next()).toContain('"figures":"900 MiB · 12.5% CPU · up 3 hours"'); | |
| 223 | sync({ ...SYNC, services: [service("hub", "running", 12.5, 1_000)] }); | |
| 224 | synced(app, id); | |
| 225 | expect(await next()).toContain('"figures":"1000 MiB · 12.5% CPU · up 3 hours"'); | |
| 226 | // Stopped, then started: the line keeps its place both ways. | |
| 227 | sync({ ...SYNC, services: [service("hub", "exited", 0, 0)] }); | |
| 228 | synced(app, id); | |
| 229 | expect(await next()).toContain(`"figures":"${none}"`); | |
| 230 | expect(await tty()).toContain(none); | |
| 231 | sync({ ...SYNC, services: [{ ...service("hub", "running", 3, 500), upSince: new Date().toISOString() }] }); | |
| 232 | synced(app, id); | |
| 233 | expect(await next()).toContain('"figures":"500 MiB · 3.0% CPU · up less than a minute"'); | |
| 234 | // Silent: the figures go, on the open page as well. | |
| 235 | app.db.run("DELETE FROM game_service"); | |
| 236 | sync(SYNC, new Date(Date.now() - 3 * 3_600_000)); | |
| 237 | synced(app, id); | |
| 238 | expect(await next()).toContain('"figures":"memory, CPU and uptime not known"'); | |
| 239 | await reader.cancel(); | |
| 240 | expect(await web("hub")).toBe("memory, CPU and uptime not known"); | |
| 241 | expect(await tty()).toContain("memory, CPU and uptime not known"); | |
| 242 | }); | |
| 243 | ||
| 244 | test("the games tool says when a server last started, never when its container was made", async () => { | |
| 245 | const token = createToken(app.db, alice.id, "up", "admin").token; | |
| 246 | const hub = async () => (await mcpTool(servers.url, token, "games", { action: "list" })).value.items[0].services.find((s: { name: string }) => s.name === "hub"); | |
| 247 | app.db.run("DELETE FROM game_service"); | |
| 248 | sync(); | |
| 249 | expect(await hub()).toMatchObject({ state: "running", memMiB: 900, upSince: null }); | |
| 250 | const upSince = new Date(Date.now() - 3_600_000).toISOString(); | |
| 251 | sync({ ...SYNC, services: [{ ...service("hub", "running", 12.5, 900), upSince }] }); | |
| 252 | expect((await hub()).upSince).toBe(upSince); | |
| 253 | expect(await hub()).not.toHaveProperty("startedAt"); | |
| 254 | expect(JSON.stringify(await hub())).not.toContain("2026-11-01"); | |
| 255 | }); | |
| 256 | ||
| 162 | 257 | test("a silent box: what runs now is not known, and what it last said is told as that", () => { |
| 163 | 258 | app.db.run("DELETE FROM game_service"); |
| 164 | 259 | sync(SYNC, new Date(Date.now() - 3 * 3_600_000)); |
test/game-gist.test.ts +2 −1
| @@ -123,7 +123,8 @@ describe("on a forge", () => { | ||
| 123 | 123 | expect(box).toContain("no address to give yet"); |
| 124 | 124 | expect(box).toContain("game-menu"); |
| 125 | 125 | expect(box.indexOf('aria-label="start survival"')).toBeLessThan(box.indexOf('class="game-menu"')); |
| 126 | expect(box).toContain("% CPU"); | |
| 126 | // Exited: its state says it, and no figure is made up for it. | |
| 127 | expect(box).not.toContain("% CPU"); | |
| 127 | 128 | expect(box).toContain("players over 24 hours"); |
| 128 | 129 | expect(box).not.toContain('data-variant="primary"'); |
| 129 | 130 | const page = await get("/games/mc/survival"); |
new above
offline
Principles 3 and 1 (rhythm, no layout shift). This
<p>is always rendered, andbriefis empty for any server that is not running. The line is then a collapsed empty paragraph. When the stream setsfigurestext on a start, the page jumps by a line, and a stopped server leaves a stray gap. Fix: render the element only whenbriefis non-empty, or keep it always rendered but reserve its height, and make the stream update handle both cases.