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

The terminal says nothing of a spawned forge's running apps #1544

Parked opened this
Closed without merging

scout/forge:scout/1518-the-terminal-says-nothing-of-a-spawned-f → main 8 commits fork 5 threads · 5 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +375 −0
README.md +7
@@ -152,6 +152,13 @@ The web page has the same *Cost and landed* section on a milestone, and the
152152 milestones API and MCP carry it as `spend` (`usd`, `landed`, `costliest`),
153153 owner only.
154154
155 **Forges you sent.** `:` → *Forges you sent* is the terminal's door to the
156 web `/forges` page, for an admin only (anyone else is told so and nothing is
157 read). Each row names the child, whether it is claimed, who it is for and the
158 apps it was given by name, with the running state first (the `/family` wording:
159 "1 of 2 apps running — bakery asleep"; "running state unknown" when the child
160 does not answer). `enter` opens the full list, `o` opens the forge.
161
155162 Working on the UI itself? `bun run src/tui/main.ts --user alice` runs it over
156163 stdio against your local data directory. That `--user` flag is a development
157164 convenience and exists only there: over SSH, the key decides who you are and
src/tui/forge.ts +8
@@ -42,6 +42,7 @@ import { resilienceLines, resilienceScreen } from "./screens/resilience.ts";
4242 import { flowLines, flowScreen } from "./screens/flow.ts";
4343 import { stalledScreen } from "./screens/stalled.ts";
4444 import { issuesScreen, newIssueScreen } from "./screens/issues.ts";
45 import { forgesScreen } from "./screens/forges.ts";
4546 import { limbsScreen } from "./screens/limbs.ts";
4647 import { newPullScreen } from "./screens/newpull.ts";
4748 import { newForgeScreen } from "./screens/newforge.ts";
@@ -205,6 +206,13 @@ export function forgeCommands(session: Session): (ctx: Ctx) => Command[] {
205206 run: () => openOnWeb(session, ctx, "/settings", "Account settings"),
206207 },
207208 );
209 if (session.user?.isAdmin === true)
210 commands.push({
211 id: "forges",
212 title: "Forges you sent",
213 hint: "apps given, claimed",
214 run: () => ctx.push(forgesScreen(session, ctx)),
215 });
208216 // The forge's own faults, as the board's strip shows them: a site admin's.
209217 if (session.user?.isAdmin === true)
210218 commands.push({
src/tui/screens/forges.ts added +165
@@ -0,0 +1,165 @@
1 /**

The admin path (rows load, enter opens the detail, o opens the forge, r reloads) is untested. Only the refusal is. Make clientRows injectable and cover it.

2 * The forges this one sent (#1518), in the terminal: the `/forges` client
3 * list's rows, an admin's only. The data is `clientRows`, the same answer the
4 * page reads, so a child that does not answer reads "unknown" here too.
5 *
6 * `givenLine` names the apps a child was given; `runningLine` says whether
7 * they run, in the words /family uses, from `ClientRow.running`. A child
8 * whose running state is not known reads "running state unknown". Enter opens
9 * the whole list.
10 */
11
12 import { clientRows, type ClientRow } from "../../spawn/clients.ts";
13 import { appsLine } from "../../spawn/family.ts";
14 import type { Ctx, Screen } from "../app/app.ts";
15 import { binding } from "../app/keys.ts";
16 import { fill, len, split } from "../ui/layout.ts";
17 import { style } from "../term/style.ts";
18 import { truncate } from "../term/width.ts";
19 import type { Session } from "../session.ts";
20 import {
21 ListState,
22 drawFacts,
outdated

Return the app names, not just a count, to match the page: r.apps.join(', '). Truncate to the width, and show the full list when the row is opened.

23 drawList,
24 hitList,
25 metaOf,
26 openLink,
27 plural,
28 relTime,
29 runAsync,
30 type ListRow,
31 } from "./common.ts";
32
33 /** The names of the apps the child was given; the row truncates, the detail shows all. */
34 export function givenLine(r: ClientRow): string {
35 return r.apps.length === 0 ? "no apps given" : r.apps.join(", ");
36 }
37
38 /** Whether the apps run, as /family says it; "" when there are none to run. */
39 export function runningLine(r: ClientRow): string {
40 if (r.running === null) return r.apps.length === 0 ? "" : "running state unknown";
41 return appsLine(r.running) ?? "";
42 }
43
44 function claimLine(r: ClientRow): string {
45 if (!r.made) return "still being made";
46 return r.claimed === null ? "unknown" : r.claimed ? "claimed" : "not claimed yet";
47 }
48
49 /** The facts the detail screen shows for one forge: one pair per line. */
50 export function forgeFacts(r: ClientRow): [string, string][] {
51 const facts: [string, string][] = [
52 ["Forge", r.name],
53 ["Address", r.url],
54 ["State", claimLine(r)],
55 ];
56 if (r.for !== null) facts.push(["For", r.for]);
57 if (r.sentAt !== null) facts.push(["Sent", relTime(r.sentAt)]);
58 if (r.lastActivityAt !== null) facts.push(["Last activity", relTime(r.lastActivityAt)]);
59 const run = runningLine(r);
outdated

The non-admin path (may() false) is untested. Add a test that drives forgesScreen with a non-admin session and asserts that no rows load and that the note reads 'Only an admin...'.

60 if (run !== "") facts.push(["Running", run]);
61 facts.push(["Apps given", r.apps.length === 0 ? "none" : String(r.apps.length)]);
62 for (const a of r.apps) facts.push(["", a]);
63 return facts;
64 }
65
66 export function detailScreen(ctx: Ctx, r: ClientRow): Screen {
67 return {
68 title: r.name,
69 keys: [binding(["o"], "open the forge"), binding(["esc"], "back")],
70 render(area, s) {
71 const c = ctx.theme.c;
72 const [head, body] = split(area, "v", [len(1), fill(1)]);
73 s.text(
74 head!.x,
75 head!.y,
76 truncate("Apps given, and whether they run.", head!.w),
77 style(c.mutedFg, c.bg),
78 head!.x + head!.w,
79 );
80 drawFacts(s, body!, c, forgeFacts(r));
81 },
82 key(e) {
83 if (e.ctrl || e.alt || e.key !== "o") return false;
84 openLink(ctx, r.url);
85 return true;
86 },
87 };
88 }
89
90 export function forgeRows(rows: readonly ClientRow[]): ListRow[] {
91 return rows.map((r) => ({
92 title: r.name,
93 secondary: metaOf(claimLine(r), runningLine(r), givenLine(r), r.for === null ? "" : `for ${r.for}`),
94 meta: r.lastActivityAt === null ? "" : `active ${relTime(r.lastActivityAt)}`,
95 }));
96 }
97
98 export function forgesScreen(
99 f: Session,
100 ctx: Ctx,
101 load: (app: Session["app"]) => Promise<ClientRow[]> = clientRows,
102 ): Screen {
103 const state = new ListState();
104 let rows: ClientRow[] = [];
105 let loaded = false;
106 // Read at use: an admin can stop being one under a session.
107 const may = (): boolean => f.user?.isAdmin === true;
108
109 const reload = (): Promise<void> => {
110 if (!may()) {
111 rows = [];
112 loaded = true;
113 ctx.invalidate();
114 return Promise.resolve();
115 }
116 return load(f.app).then((r) => {
117 rows = r;
118 loaded = true;
119 state.clamp(rows.length);
120 ctx.invalidate();
121 });
122 };
123
124 const open = (): void => {
125 const row = rows[state.index];
126 if (row !== undefined) ctx.push(detailScreen(ctx, row));
127 };
128
129 const screen: Screen = {
130 title: "Forges you sent",
131 keys: [
132 binding(["j", "k"], "move cursor"),
133 binding(["enter"], "show apps"),
134 binding(["o"], "open the forge"),
135 binding(["r"], "refresh"),
136 ],
137 load: () => reload(),
138 render(area, s) {
139 const c = ctx.theme.c;
140 const [head, list] = split(area, "v", [len(1), fill(1)]);
141 const note = !loaded
142 ? "Loading…"
143 : !may()
144 ? "Only an admin sees the forges this one sent."
145 : rows.length === 0
146 ? "No forges sent yet."
147 : `${plural(rows.length, "forge")} sent`;
148 s.text(head!.x, head!.y, truncate(note, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w);
149 drawList(s, list!, c, state, forgeRows(rows));
150 hitList(ctx, list!, state, rows.length, { open });
151 },
152 key(e) {
153 if (state.key(e, rows.length)) return true;
154 if (e.ctrl || e.alt) return false;
155 if (e.key === "enter") open();
156 else if (e.key === "o") {
157 const row = rows[state.index];
158 if (row !== undefined) openLink(ctx, row.url);
159 } else if (e.key === "r") runAsync(ctx, reload);
160 else return false;
161 return true;
162 },
163 };
164 return screen;
165 }
test/tui-forges.test.ts added +195
@@ -0,0 +1,195 @@
1 /** The forges list in the terminal (#1518): an admin's, from the client list's rows. */

Add the admin-positive screen test and a forgeCommands test that the palette entry shows for an admin only.

2 import { describe, expect, test } from "bun:test";
3 import type { ClientRow } from "../src/spawn/clients.ts";
4 import type { Session } from "../src/tui/session.ts";
5 import type { Ctx } from "../src/tui/app/app.ts";
6 import { Screen as Surface } from "../src/tui/term/screen.ts";
7 import { theme } from "../src/tui/ui/theme.ts";
8 import { forgeCommands } from "../src/tui/forge.ts";
9 import { detailScreen, forgeFacts, forgeRows, forgesScreen, givenLine, runningLine } from "../src/tui/screens/forges.ts";
10
11 const row = (over: Partial<ClientRow>): ClientRow => ({
12 name: "acme",
13 url: "https://acme--forge.test",
14 for: null,
15 apps: [],
16 sentAt: null,
17 made: true,
18 claimed: false,
19 lastActivityAt: null,
20 running: null,
21 ...over,
22 });
23
24 describe("forges list rows", () => {
25 test("names the apps given, as the page does", () => {
26 expect(givenLine(row({ apps: ["a/x", "a/y", "a/z"] }))).toBe("a/x, a/y, a/z");
27 expect(givenLine(row({}))).toBe("no apps given");
28 expect(forgeRows([row({ apps: ["a/x", "a/y"] })])[0]!.secondary).toContain("a/x, a/y");
29 });
30
31 test("the detail lists every app on its own line", () => {
32 const facts = forgeFacts(row({ apps: ["a/x", "a/y"] }));
33 expect(facts.map((f) => f[1])).toContain("a/x");
34 expect(facts.map((f) => f[1])).toContain("a/y");
35 const ctx = { theme: theme("dark") } as unknown as Ctx;
36 const s = new Surface(60, 12);
37 detailScreen(ctx, row({ apps: ["a/x", "a/y"] })).render({ x: 0, y: 0, w: 60, h: 12 }, s);
38 expect(s.toText()).toContain("a/x");
39 expect(s.toText()).toContain("a/y");
40 });
41
42 test("a row names the child, its claim and for whom", () => {
43 const [r] = forgeRows([row({ for: "Dana", apps: ["a/x"] })]);
44 expect(r!.title).toBe("acme");
45 expect(r!.secondary).toContain("not claimed yet");
46 expect(r!.secondary).toContain("for Dana");
47 });
48
49 test("a child still being made or silent reads so", () => {
50 expect(forgeRows([row({ made: false })])[0]!.secondary).toContain("still being made");
51 expect(forgeRows([row({ claimed: null })])[0]!.secondary).toContain("unknown");
52 });
53 });
54
55 describe("running state", () => {
56 const up = { total: 2, running: 2, notRunning: [] };
57 const down = { total: 2, running: 1, notRunning: [{ slug: "bakery", why: "asleep" }] };
58
59 test("all apps up reads as running", () => {
60 const r = row({ apps: ["a/x", "a/y"], running: up });
61 expect(runningLine(r)).toBe("2 of 2 apps running");
62 expect(forgeRows([r])[0]!.secondary).toContain("2 of 2 apps running");
63 expect(forgeFacts(r)).toContainEqual(["Running", "2 of 2 apps running"]);
64 });
65
66 test("an app down says which and why", () => {
67 const r = row({ apps: ["a/x", "a/y"], running: down });
68 expect(runningLine(r)).toBe("1 of 2 apps running — bakery asleep");
69 expect(forgeFacts(r)).toContainEqual(["Running", "1 of 2 apps running — bakery asleep"]);
70 });
71
72 test("the running state comes before the app names on the row", () => {
73 const sec = forgeRows([row({ apps: ["a/x", "a/y"], running: down })])[0]!.secondary ?? "";
74 expect(sec.indexOf("running")).toBeLessThan(sec.indexOf("a/x"));
75 });
76
77 test("null running reads unknown when apps were given, nothing when none", () => {
78 expect(runningLine(row({ apps: ["a/x"], running: null }))).toBe("running state unknown");
79 expect(runningLine(row({ running: null }))).toBe("");
80 expect(forgeRows([row({ apps: ["a/x"] })])[0]!.secondary).toContain("running state unknown");
81 });
82
83 test("the detail screen draws the running line", () => {
84 const ctx = { theme: theme("dark") } as unknown as Ctx;
85 const s = new Surface(70, 12);
86 detailScreen(ctx, row({ apps: ["a/x", "a/y"], running: down })).render({ x: 0, y: 0, w: 70, h: 12 }, s);
87 expect(s.toText()).toContain("bakery asleep");
88 });
89 });
90
91 describe("forges screen is admin-gated", () => {
92 test("a non-admin loads no rows, never asks for the list, and is told why", async () => {
93 // Any touch of the app (which `clientRows` needs) fails the test.
94 const app = new Proxy({}, { get: () => { throw new Error("clientRows reached the app"); } });
95 const session = { app, user: { isAdmin: false } } as unknown as Session;
96 let invalidated = 0;
97 const ctx = { theme: theme("dark"), invalidate: () => invalidated++, hit: () => {} } as unknown as Ctx;
98 const screen = forgesScreen(session, ctx);
99 await screen.load!(new AbortController().signal);
100 expect(invalidated).toBe(1);
101 const s = new Surface(60, 6);
102 screen.render({ x: 0, y: 0, w: 60, h: 6 }, s);
103 expect(s.toText()).toContain("Only an admin sees the forges this one sent.");
104 expect(s.toText()).not.toContain("forge sent");
105 });
106
107 test("a guest is refused the same way", async () => {
108 const app = new Proxy({}, { get: () => { throw new Error("clientRows reached the app"); } });
109 const session = { app, user: undefined } as unknown as Session;
110 const ctx = { theme: theme("dark"), invalidate: () => {}, hit: () => {} } as unknown as Ctx;
111 const screen = forgesScreen(session, ctx);
112 await screen.load!(new AbortController().signal);
113 const s = new Surface(60, 6);
114 screen.render({ x: 0, y: 0, w: 60, h: 6 }, s);
115 expect(s.toText()).toContain("Only an admin");
116 });
117 });
118
119 const press = (key: string) => ({ type: "key" as const, key, ctrl: false, alt: false, shift: false });
120
121 function adminScreen(rows: () => ClientRow[]) {
122 const pushed: { title: string }[] = [];
123 const opened: string[] = [];
124 let loads = 0;
125 const session = { app: {}, user: { isAdmin: true } } as unknown as Session;
126 const ctx = {
127 theme: theme("dark"),
128 invalidate: () => {},
129 hit: () => {},
130 push: (s: { title: string }) => pushed.push(s),
131 openUrl: (u: string) => (opened.push(u), true),
132 toast: () => {},
133 track: (p: Promise<unknown>) => p,
134 } as unknown as Ctx;
135 const screen = forgesScreen(session, ctx, async () => (loads++, rows()));
136 return { screen, pushed, opened, loads: () => loads };
137 }
138
139 describe("forges screen for an admin", () => {
140 const rows = [
141 row({ name: "acme", url: "https://acme--forge.test", apps: ["a/x"] }),
142 row({ name: "beta", url: "https://beta--forge.test", apps: [] }),
143 ];
144
145 test("rows render, enter opens the detail, o opens the forge, r reloads", async () => {
146 const h = adminScreen(() => rows);
147 await h.screen.load!(new AbortController().signal);
148 const s = new Surface(70, 8);
149 h.screen.render({ x: 0, y: 0, w: 70, h: 8 }, s);
150 expect(s.toText()).toContain("2 forges sent");
151 expect(s.toText()).toContain("acme");
152 expect(s.toText()).toContain("beta");
153
154 expect(h.screen.key!(press("enter"))).toBe(true);
155 expect(h.pushed.map((p) => p.title)).toEqual(["acme"]);
156
157 expect(h.screen.key!(press("o"))).toBe(true);
158 expect(h.opened).toEqual(["https://acme--forge.test"]);
159
160 expect(h.loads()).toBe(1);
161 expect(h.screen.key!(press("r"))).toBe(true);
162 await Bun.sleep(10);
163 expect(h.loads()).toBe(2);
164 });
165
166 test("the cursor picks the row enter and o act on", async () => {
167 const h = adminScreen(() => rows);
168 await h.screen.load!(new AbortController().signal);
169 h.screen.key!(press("j"));
170 h.screen.key!(press("o"));
171 expect(h.opened).toEqual(["https://beta--forge.test"]);
172 });
173
174 test("no forges sent says so, and enter does nothing", async () => {
175 const h = adminScreen(() => []);
176 await h.screen.load!(new AbortController().signal);
177 const s = new Surface(60, 6);
178 h.screen.render({ x: 0, y: 0, w: 60, h: 6 }, s);
179 expect(s.toText()).toContain("No forges sent yet.");
180 h.screen.key!(press("enter"));
181 expect(h.pushed).toEqual([]);
182 });
183 });
184
185 describe("the palette's Forges you sent", () => {
186 const titles = (user: unknown) => {
187 const session = { user, signedIn: user !== undefined } as unknown as Session;
188 return forgeCommands(session)({} as Ctx).map((c) => c.title);
189 };
190 test("an admin is offered it; a member and a guest are not", () => {
191 expect(titles({ isAdmin: true })).toContain("Forges you sent");
192 expect(titles({ isAdmin: false })).not.toContain("Forges you sent");
193 expect(titles(undefined)).not.toContain("Forges you sent");
194 });
195 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs