Public: anyone can read this; the crew can change it.
The terminal's Adapter screen lists the last pull runs #10151
mason/forge:mason/10128-the-terminal-s-adapter-screen-lists-the → main 1 commit fork
2 files
changed +58
−3
src/tui/screens/adapter.ts +27 −3
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | import { adapterFactsOf, adapterRows, type AdapterFacts } from "../../apps/adapter_facts.ts"; |
| 10 | import { pullRunLines, type PullRunLine } from "../../apps/pull.ts"; | |
| 10 | 11 | import { getAppByRepo, type AppRecord } from "../../core/apps.ts"; |
| 11 | 12 | import { canSeeOwnerFacts, type Repo } from "../../core/repos.ts"; |
| 12 | 13 | import type { Ctx, Screen } from "../app/app.ts"; |
| @@ -14,7 +15,8 @@ import type { Command } from "../app/palette.ts"; | ||
| 14 | 15 | import type { Session } from "../session.ts"; |
| 15 | 16 | import { style } from "../term/style.ts"; |
| 16 | 17 | import { truncate } from "../term/width.ts"; |
| 17 | import { drawFacts } from "./common.ts"; | |
| 18 | import { binding } from "../app/keys.ts"; | |
| 19 | import { Scroll, drawFacts, drawLines } from "./common.ts"; | |
| 18 | 20 | |
| 19 | 21 | const mayRead = (f: Session, repo: Repo): boolean => canSeeOwnerFacts(f.db, repo, f.user); |
| 20 | 22 | |
| @@ -31,13 +33,24 @@ export function adapterCommands(f: Session, ctx: Ctx, repo: Repo | undefined): C | ||
| 31 | 33 | ]; |
| 32 | 34 | } |
| 33 | 35 | |
| 36 | /** One run as a line: when, state, the three counts, and a failed run's error. */ | |
| 37 | export function runText(run: PullRunLine): string { | |
| 38 | const when = run.at.slice(0, 16).replace("T", " "); | |
| 39 | const counts = `${run.written} written ${run.skipped} skipped ${run.failed} failed`; | |
| 40 | const head = `${when} ${run.state.padEnd(6)} ${counts}`; | |
| 41 | return run.error === null ? head : `${head} ${run.error}`; | |
| 42 | } | |
| 43 | ||
| 34 | 44 | /** `facts` is the read; a test passes its own so the gate is checked without a git repository. */ |
| 35 | 45 | export function adapterScreen( |
| 36 | 46 | f: Session, |
| 37 | 47 | ctx: Ctx, |
| 38 | 48 | repo: Repo, |
| 39 | 49 | facts: (record: AppRecord) => Promise<AdapterFacts | undefined> = (record) => adapterFactsOf(f.app, record), |
| 50 | runsOf: (appId: string) => PullRunLine[] = (appId) => pullRunLines(f.db, appId), | |
| 40 | 51 | ): Screen { |
| 52 | const scroll = new Scroll(); | |
| 53 | let runs: PullRunLine[] = []; | |
| 41 | 54 | let rows: Array<[string, string]> | undefined; |
| 42 | 55 | let loaded = false; |
| 43 | 56 | const load = (): void => { |
| @@ -46,6 +59,7 @@ export function adapterScreen( | ||
| 46 | 59 | loaded = true; |
| 47 | 60 | return; |
| 48 | 61 | } |
| 62 | runs = runsOf(record.id); | |
| 49 | 63 | void facts(record).then( |
| 50 | 64 | (read) => { |
| 51 | 65 | rows = read === undefined ? undefined : adapterRows(read); |
| @@ -61,7 +75,7 @@ export function adapterScreen( | ||
| 61 | 75 | load(); |
| 62 | 76 | return { |
| 63 | 77 | title: `${repo.ownerName}/${repo.name} — Adapter`, |
| 64 | keys: [], | |
| 78 | keys: [binding(["j", "k"], "scroll runs")], | |
| 65 | 79 | render(area, s) { |
| 66 | 80 | const c = ctx.theme.c; |
| 67 | 81 | if (rows === undefined) { |
| @@ -70,7 +84,17 @@ export function adapterScreen( | ||
| 70 | 84 | return; |
| 71 | 85 | } |
| 72 | 86 | drawFacts(s, area, c, rows); |
| 87 | const top = rows.length + 1; | |
| 88 | if (top >= area.h) return; | |
| 89 | const below = { ...area, y: area.y + top, h: area.h - top }; | |
| 90 | const lines = [[{ text: "Last pulls", style: style(c.mutedFg, c.bg) }]]; | |
| 91 | if (runs.length === 0) lines.push([{ text: "No pulls yet.", style: style(c.mutedFg, c.bg) }]); | |
| 92 | for (const run of runs) { | |
| 93 | const text = truncate(runText(run), Math.max(0, area.w - 1)); | |
| 94 | lines.push([{ text, style: style(run.state === "failed" ? c.fg : c.mutedFg, c.bg) }]); | |
| 95 | } | |
| 96 | drawLines(s, below, c, scroll, lines); | |
| 73 | 97 | }, |
| 74 | key: () => false, | |
| 98 | key: (e) => rows !== undefined && scroll.key(e, runs.length + 1), | |
| 75 | 99 | }; |
| 76 | 100 | } |
test/adapter-tui.test.ts +31
| @@ -107,3 +107,34 @@ test("a guest gets no entry, and the screen reads nothing", async () => { | ||
| 107 | 107 | expect(seen.read).toBe(0); |
| 108 | 108 | expect(seen.text).not.toContain("c-secret-9"); |
| 109 | 109 | }); |
| 110 | ||
| 111 | const good = { id: "r1", at: "2026-11-09T10:19:00.000Z", durationMs: 40, state: "ok", error: null, written: 3, skipped: 2, failed: 1, cursor: "c1", repeat: false } as const; | |
| 112 | const bad = { ...good, id: "r2", at: "2026-11-09T09:19:00.000Z", state: "failed", error: "the source answered 500", written: 0, skipped: 0, failed: 0, cursor: null } as const; | |
| 113 | ||
| 114 | async function lookRuns(user: User, runs: Array<typeof good | typeof bad>) { | |
| 115 | const terminal = headlessTerminal(100, 12); | |
| 116 | const { app: tui, session } = createForgeApp( | |
| 117 | app, | |
| 118 | { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user }, | |
| 119 | { terminal, exitHooks: false }, | |
| 120 | ); | |
| 121 | tui.start(); | |
| 122 | tui.ctx.push(adapterScreen(session, tui.ctx, repo, async () => facts, () => [...runs])); | |
| 123 | await settleApp(tui); | |
| 124 | await settleApp(tui); | |
| 125 | const text = tui.screen.toText(); | |
| 126 | tui.stop?.(); | |
| 127 | return text; | |
| 128 | } | |
| 129 | ||
| 130 | test("the screen lists a good and a failed run below the facts", async () => { | |
| 131 | const text = await lookRuns(olive, [good, bad]); | |
| 132 | expect(text).toContain("Last pulls"); | |
| 133 | expect(text).toContain("2026-11-09 10:19 ok 3 written 2 skipped 1 failed"); | |
| 134 | expect(text).toContain("failed 0 written 0 skipped 0 failed the source answered 500"); | |
| 135 | }); | |
| 136 | ||
| 137 | test("with no runs it says so, and a stranger sees no runs", async () => { | |
| 138 | expect(await lookRuns(olive, [])).toContain("No pulls yet."); | |
| 139 | expect(await lookRuns(otto, [good])).not.toContain("Last pulls"); | |
| 140 | }); |
new above
offline