Public: anyone can read this; the crew can change it.

The terminal's forge view screen updates when the forge changes, without a timer #13183

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13140-the-terminal-s-forge-view-screen-updates → main 3 commits fork 1 thread · 1 unresolved

  • Merged. scout/13140-the-terminal-s-forge-view-screen-updates was merged into main as af8c6f4.

Review

Sign in to review this change.
3 files changed +71 −5
docs/features/forge-view.md +1 −1
@@ -33,4 +33,4 @@ forge depends on it.
3333 **Terminal.** The palette's **Forge view** opens a screen with the view's counts and the Foundry address (when there
3434 is a door); Enter opens Foundry on the web.
3535
36 Not done: a push stream (`/api/v1/forge/view` is polled), and anything that writes.
36 Not done: a push stream over HTTP (outside callers still poll `/api/v1/forge/view` with ETag; the terminal screen is live: it re-reads when the in-process event feed moves, at most once per refresh gap, with no timer), and anything that writes.
src/tui/screens/forgeview.ts +13 −4
@@ -2,8 +2,9 @@
22 * The forge view (`GET /api/v1/forge/view`) as a screen: the apps, running,
33 * queued and waiting counts it carries, the first apps and runs, and the Foundry address when the
44 * reader has a door to it (`foundryHref`). Enter opens Foundry on the web.
5 * Signed-in readers only; the numbers are the view's own, read once when the
6 * screen opens (not per frame or key), so it is a picture, not a live board.
5 * Signed-in readers only; the numbers are the view's own, read when the
6 * screen opens and again when the forge's event feed says something moved
7 * (`live`: coalesced, no timer of its own), never per frame or key.
78 */
89
910 import { foundryHref } from "../../actions/foundry_door.ts";
@@ -13,7 +14,7 @@ import { binding } from "../app/keys.ts";
1314 import { style } from "../term/style.ts";
1415 import { truncate } from "../term/width.ts";
1516 import type { Session } from "../session.ts";
16 import { openOnWeb } from "./common.ts";
17 import { live, openOnWeb } from "./common.ts";
1718 import type { PaletteEntry } from "./entries.ts";
1819
1920 /** Apps and runs the screen lists below the counts; the rest is `counts`. */
@@ -43,9 +44,17 @@ export function forgeViewLines(f: Session): { lines: string[]; foundry: string |
4344 }
4445
4546 export function forgeViewScreen(f: Session, ctx: Ctx): Screen {
46 const { lines, foundry } = forgeViewLines(f);
47 let { lines, foundry } = forgeViewLines(f);
4748 return {
4849 title: "Forge view",
50 load(signal) {
51 // The screen's one subscription, dropped when it is popped.
52 live(f, signal, {}, () => {
53 ({ lines, foundry } = forgeViewLines(f));
54 ctx.invalidate();
55 });
56 return Promise.resolve();
57 },
4958 keys: [binding(["enter"], "open Foundry on the web")],
5059 render(area, s) {
5160 const c = ctx.theme.c;
test/tui-forgeview-live.test.ts added +57
@@ -0,0 +1,57 @@
1 /** The terminal's forge view re-reads when the event feed moves, and lets go when popped. */
2 import { expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp } from "../src/app.ts";
7 import { createRepo } from "../src/actions/repos.ts";
8 import { createApp as createAppRecord } from "../src/core/apps.ts";
9 import { forgeViewScreen } from "../src/tui/screens/forgeview.ts";
10 import type { Ctx } from "../src/tui/app/app.ts";
11 import type { Session } from "../src/tui/session.ts";
12 import { seedAdmin } from "./helpers/admin.ts";
13
14 test("one event re-reads the counts once; popping leaves no listener", async () => {
15 const dir = await mkdtemp(join(tmpdir(), "fv-live-"));
16 const app = createApp({ dataDir: dir, port: 0 });
17 try {
18 const admin = await seedAdmin(app, { username: "ada", email: "[email protected]" });
19 if (!admin.ok) throw new Error(admin.error);
20 const user = admin.value;
21 const repo = await createRepo(app, user, { name: "thing", visibility: "public" });
22 if (!repo.ok) throw new Error(String(repo.error));
23 let subs = 0;
24 const real = app.bus.subscribe.bind(app.bus);
25 app.bus.subscribe = (...args: Parameters<typeof real>) => {
26 subs++;
27 const off = real(...args);
28 return () => (subs--, off());
29 };
30 let draws = 0;
31 const ctx = { invalidate: () => draws++, theme: { c: {} } } as unknown as Ctx;
32 const screen = forgeViewScreen({ app, user } as unknown as Session, ctx);
33 const abort = new AbortController();
34 await screen.load!(abort.signal);
35 expect(subs).toBe(1);
36 const first = () => {
37 let row = "";
38 (screen.render as (a: unknown, s: unknown) => void)({ x: 0, y: 0, w: 200, h: 1 }, { text: (_x: number, _y: number, t: string) => (row = t) });
39 return row;
40 };
41 expect(first()).toStartWith("0 apps");
42 createAppRecord(app.db, { repoId: repo.value.id, slug: "one", access: "public" });
43 // Two events in a burst: one re-read.
44 app.bus.append({ kind: "run.started" });

Real-time sleep ties the test to LIVE_COALESCE_MS; under a loaded gate this can flake. Poll for draws===1 with a deadline instead of a fixed sleep.

45 app.bus.append({ kind: "run.finished" });
46 await Bun.sleep(200); // real-timer: the view coalesces a burst on its own timer
47 expect(draws).toBe(1);
48 expect(first()).toStartWith("1 apps");
49 abort.abort();
50 expect(subs).toBe(0);
51 app.bus.append({ kind: "run.started" });
52 await Bun.sleep(100); // real-timer: proving nothing fires after pop needs the window to pass
53 expect(draws).toBe(1);
54 } finally {
55 await rm(dir, { recursive: true, force: true });
56 }
57 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs