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

A preview's toolbar page says what its pull changed and whether its check is green, each link a 44 px tap on touch screens #4214

Parked opened this
Closed without merging

scout/forge:scout/4205-a-preview-s-toolbar-page-says-what-its-p → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
5 files changed +202 −6
docs/DESIGN.md +1 −1
@@ -397,7 +397,7 @@ gone, so this list only shrinks.
397397 - **`style/content.ts`, code view** — the source view and the syntax-highlight token classes `highlight-html.ts` builds as `tok-${kind}`: `.blob-view` `.ln` `.tok-attribute` `.tok-comment` `.tok-constant` `.tok-decorator` `.tok-deleted` `.tok-emphasis` `.tok-function` `.tok-heading` `.tok-inserted` `.tok-keyword` `.tok-link` `.tok-meta` `.tok-number` `.tok-operator` `.tok-plain` `.tok-property` `.tok-punctuation` `.tok-regex` `.tok-string` `.tok-tag` `.tok-type` `.tok-variable`
398398 - **`style/content.ts`, prose (markdown)** — table cell alignment, built by `markdown.ts` as `ta-${align}`: `.ta-c` `.ta-r`
399399 - **`style/content.ts`, diff viewer** — the diff table: its gutters, line kinds, file head and inline review comments: `.add` `.del` `.dh` `.diff` `.diff-body` `.diff-head` `.diff-note` `.diff-path` `.diff-stat` `.diff-stub` `.diff-summary` `.diff-table` `.dl` `.dl-review` `.dn` `.dn-comment` `.nonl` `.rc-hidden` `.rc-resolve` `.rc-resolved` `.wd`
400 - **`style/content.ts`, apps, runs, and the crew** — run pages, their logs and checks (a pull's look is §5's **look**): `.ln-forge` `.ln-pulse` `.ln-say` `.ln-tool` `.ln-you` `.log-tail-row` `.pinned-failures` `.pinned-slot` `.run-jump` `.run-live` `.run-steer` `.tests-bar` `.tests-words` `.txt` `.who`
400 - **`style/content.ts`, apps, runs, and the crew** — run pages, their logs and checks (a pull's look is §5's **look**): `.change-line` (a preview's toolbar page: the diff totals and the check badge, each link ≥36px tall and 44px under `@media (pointer: coarse)`) `.ln-forge` `.ln-pulse` `.ln-say` `.ln-tool` `.ln-you` `.log-tail-row` `.pinned-failures` `.pinned-slot` `.run-jump` `.run-live` `.run-steer` `.tests-bar` `.tests-words` `.txt` `.who`
401401 - **`style/content.ts`, search results** — search results, the inbox's ask cards: `.ask-acts` `.ask-card` `.ask-group` `.ask-kind` `.ask-meta` `.ask-more-acts` `.ask-pick` `.ask-pick-hit` `.ask-short` `.ask-waiting` `.live-toggle` `.needs-you` `.no-hits`
402402 - **`pages/board.ts`** — the board itself is §5's **board**; the spend tables `spend.ts` draws wear §5's `.table` and `.stack`, no class of their own
403403 - **`pages/map.ts`** — the map of what waits for what: `.map-ask` `.map-breath` `.map-canvas` `.map-card` `.map-counts` `.map-dot` `.map-edge` `.map-edge-crit` `.map-edge-done` `.map-empty` `.map-list` `.map-main` `.map-meta` `.map-node` `.map-pillars` `.map-rule` `.map-svg` `.map-title` `.map-tool` `.map-tools` `.map-who` `.map-who-i` `.map-who-n` `.map-word` `.map-wrap`
src/web/diff-view.ts +12
@@ -545,6 +545,18 @@ function fileHasReview(file: FileDiff, opts: DiffViewOptions): boolean {
545545 return false;
546546 }
547547
548 /** The diff's own totals as the summary line words them: "3 files", "+12", "−4". */
549 export function diffTotals(diff: {
550 files: { length: number };
551 additions: number;
552 deletions: number;
553 }): Html {
554 const n = diff.files.length;
555 return html`<strong>${n}</strong> ${n === 1 ? "file" : "files"}
556 <span class="add">+${diff.additions}</span>
557 <span class="del">−${diff.deletions}</span>`;
558 }
559
548560 /**
549561 * The whole diff: a summary line, then one collapsible view per file. Options
550562 * add syntax color and inline review comments; omit them and the diff still
src/web/studio/toolbar.ts +54 −5
@@ -17,23 +17,25 @@
1717
1818 import { addCommentThrough, openIssueThrough } from "../../actions/issues.ts";
1919 import { reportTitle } from "../../actions/page-report.ts";
20 import { getPull, pullDiff, pullDir, type Pull } from "../../core/pulls.ts";
2021 import { getAppByRepo } from "../../core/apps.ts";
2122 import { getIssue, MAX_BODY } from "../../core/issues.ts";
2223 import { canWrite, type Repo } from "../../core/repos.ts";
23 import { listRuns } from "../../core/runs.ts";
24 import { checkSummary, listRuns } from "../../core/runs.ts";
2425 import { mayView, mayViewId } from "../../core/sealed.ts";
2526 import { mayDirectCrew } from "../../crew/direct.ts";
2627 import { isSafeOid } from "../../git/guard.ts";
2728 import { commitLog } from "../../git/read.ts";
2829 import { repoDir } from "../../git/repos.ts";
29 import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts";
30 import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts";
3031 import type { Ctx } from "../../http/context.ts";
3132 import { notFoundPage, pageHead, loadRepo, repoBase } from "../chrome.ts";
3233 import { field, readForm } from "../form.ts";
3334 import { html, when, type Html } from "../html.ts";
3435 import { page } from "../layout.ts";
3536 import { originError } from "../pages/thread.ts";
36 import { btn, card, envFacts } from "../ui.ts";
37 import { diffTotals } from "../diff-view.ts";
38 import { btn, card, envFacts, runStateBadge, badge } from "../ui.ts";
3739
3840 /** The longest page address the bar may carry. */
3941 const PAGE_MAX = 512;
@@ -58,6 +60,49 @@ function parseEnv(raw: string): { env: string; pull?: number } | undefined {
5860 return n === undefined ? undefined : { env: raw, pull: Number(n) };
5961 }
6062
63 interface Stat {
64 readonly files: number;
65 readonly additions: number;
66 readonly deletions: number;
67 }
68
69 /** The diff stat per pull and head oid: git is asked once per head. */
70 const stats = new Map<string, Stat>();
71 const STATS_MAX = 500;
72
73 async function statOf(ctx: Ctx, repo: Parameters<typeof pullDir>[2], pull: Pull): Promise<Stat> {
74 const key = `${pull.id}:${pull.headOid}`;
75 const kept = stats.get(key);
76 if (kept) return kept;
77 let found: Stat = { files: 0, additions: 0, deletions: 0 };
78 try {
79 const dir = await pullDir(ctx.app.db, ctx.app.reposRoot, repo, pull);
80 const { diff } = await pullDiff(dir, pull);
81 found = { files: diff.files.length, additions: diff.additions, deletions: diff.deletions };
82 } catch {
83 return found; // not kept: the next look asks again
84 }
85 if (stats.size >= STATS_MAX) stats.delete(stats.keys().next().value!);
86 stats.set(key, found);
87 return found;
88 }
89
90 /** What the pull changed and where its latest check stands, each a link. */
91 async function changeLine(ctx: Ctx, repo: Parameters<typeof pullDir>[2] & { id: string }, pull: Pull, base: string) {
92 const stat = await statOf(ctx, repo, pull);
93 const check = checkSummary(ctx.app.db, pull.id, pull.headOid);
94 return html`<p class="change-line">
95 <a href="${base}/pulls/${pull.number}/files">${diffTotals({
96 files: { length: stat.files },
97 additions: stat.additions,
98 deletions: stat.deletions,
99 })}</a>
100 ${check.run
101 ? html`<a href="${base}/runs/${check.run.id}">${runStateBadge(check.state as never, check.run.kind)}</a>`
102 : badge("No check", { icon: "clock", variant: "outline" })}
103 </p>`;
104 }
105
61106 /** The toolbar page's own path, for forms to come back to. */
62107 const toolbarPath = (base: string, env: string, address: string): string =>
63108 `${base}/toolbar?env=${env}${address === "" ? "" : `&page=${encodeURIComponent(address)}`}`;
@@ -113,12 +158,16 @@ export async function toolbarPage(ctx: Ctx): Promise<Response> {
113158 const here = html`${repo.name}, ${pull ? html`preview of pull #${pull.number}` : "production"}`;
114159 const liveNote = pull ? html`` : await liveLine(ctx, repo, base);
115160 const home = btn("Home", { href: "/", variant: "ghost" });
161 const joined = pull ? getPull(db, repo.id, pull.number) : undefined;
162 const change = joined
163 ? await timePhaseAsync(request, "change", () => changeLine(ctx, repo, joined, base))
164 : null;
116165 const rendering = performance.now();
117166 if (!user) {
118167 const response = page({
119168 ctx,
120169 title: `${repo.name} · forge`,
121 body: html`<div class="narrow-wide">${pageHead(here)}${liveNote}<div class="form-actions">${home}</div></div>`,
170 body: html`<div class="narrow-wide">${pageHead(here)}${change}${liveNote}<div class="form-actions">${home}</div></div>`,
122171 });
123172 notePhase(request, "render", performance.now() - rendering);
124173 return response;
@@ -138,7 +187,7 @@ export async function toolbarPage(ctx: Ctx): Promise<Response> {
138187 const comment = pull ? "Comment on this pull" : "Report this page";
139188 const body = html`<div class="narrow-wide">
140189 ${pageHead(here)}
141 ${liveNote}
190 ${change}${liveNote}
142191 ${when(
143192 live !== undefined,
144193 () => html`<p class="mono" data-working>
src/web/style/content.ts +5
@@ -614,6 +614,11 @@ form.inline { display: inline; margin-left: auto; }
614614
615615 /* ---- diff viewer ---- */
616616 .diff { display: flex; flex-direction: column; gap: 12px; }
617 .change-line { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; margin: 0 0 12px; font-size: 13px; }
618 .change-line a { display: inline-flex; align-items: center; gap: 6px; min-height: 36px; color: inherit; }
619 .change-line .add { color: var(--add-fg); font-weight: 600; font-variant-numeric: tabular-nums; }
620 .change-line .del { color: var(--del-fg); font-weight: 600; font-variant-numeric: tabular-nums; }
621 @media (pointer: coarse) { .change-line a { min-height: 44px; } }
617622 .diff-summary { padding: 10px 14px; font-size: 13px; }
618623 .diff-summary .add { color: var(--add-fg); font-weight: 600; font-variant-numeric: tabular-nums; }
619624 .diff-summary .del { color: var(--del-fg); font-weight: 600; font-variant-numeric: tabular-nums; }
test/toolbar-change-line.test.ts added +130
@@ -0,0 +1,130 @@
1 /** A preview's toolbar page says what its pull changed and where its check stands (#4205). */
2 import { afterAll, beforeAll, 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, type App } from "../src/app.ts";
7 import { newApp } from "../src/actions/apps.ts";
8 import { openPull } from "../src/actions/pulls.ts";
9 import { commitToBranch } from "../src/actions/repos.ts";
10 import { claimRun, createRun, finishRun } from "../src/core/runs.ts";
11 import { createSession } from "../src/core/sessions.ts";
12 import { createUser } from "../src/core/users.ts";
13 import { getPull } from "../src/core/pulls.ts";
14 import { serve, type Servers } from "../src/http/server.ts";
15 import { quietLogs } from "./helpers/globals.ts";
16
17 quietLogs();
18
19 let dataDir: string;
20 let app: App;
21 let servers: Servers;
22 let cookie = "";
23 let repoId = "";
24 let aliceId = "";
25 const numbers: number[] = [];
26 const cells = process.env["FORGE_CELLS"];
27
28 beforeAll(async () => {
29 process.env["FORGE_CELLS"] = "off";
30 dataDir = await mkdtemp(join(tmpdir(), "forge-toolbar-change-"));
31 app = createApp({ dataDir, port: 0 });
32 servers = await serve(app);
33 const made = await createUser(app.db, {
34 origin: "seed",
35 trusted: true,
36 username: "alice",
37 email: "[email protected]",
38 password: "a-good-passphrase",
39 adminIfFirst: true,
40 });
41 if (!made.ok) throw new Error(String(made.error));
42 const alice = made.value;
43 aliceId = alice.id;
44 cookie = `forge_session=${createSession(app.db, alice.id).token}`;
45 const repo = await newApp(app, alice, { name: "notes", description: "x", visibility: "public" });
46 if (!repo.ok) throw new Error(String(repo.error));
47 repoId = repo.value.repo.id;
48 const base = await commitToBranch(app, alice, repo.value.repo, {
49 message: "base",
50 files: [{ path: "forge.json", content: "{}" }],
51 });
52 if (!base.ok) throw new Error(JSON.stringify(base.error));
53 for (const name of ["green", "red", "none"]) {
54 const done = await commitToBranch(app, alice, repo.value.repo, {
55 branch: name,
56 message: name,
57 files: [
58 { path: `${name}.txt`, content: "a\nb\nc\n" },
59 { path: `${name}2.txt`, content: "x\n" },
60 ],
61 });
62 if (!done.ok) throw new Error(JSON.stringify(done.error));
63 const pull = await openPull(app, alice, repo.value.repo, {
64 title: name,
65 body: "",
66 baseBranch: "main",
67 headBranch: name,
68 });
69 if (!pull.ok) throw new Error(JSON.stringify(pull.error));
70 numbers.push(pull.value.number);
71 }
72 }, 60_000);
73
74 afterAll(async () => {
75 await servers.stop();
76 app.close();
77 if (cells === undefined) delete process.env["FORGE_CELLS"];
78 else process.env["FORGE_CELLS"] = cells;
79 await rm(dataDir, { recursive: true, force: true });
80 });
81
82 const read = (env: string, c = cookie) =>
83 fetch(`${servers.url}/alice/notes/toolbar?env=${env}`, { headers: c ? { cookie: c } : {} });
84
85 function check(number: number, state: "ok" | "failed"): string {
86 const pull = getPull(app.db, repoId, number)!;
87 const run = createRun(app.db, { repoId, kind: "check", issueId: pull.id, commitOid: pull.headOid, actorId: aliceId });
88 claimRun(app.db, run.id);
89 finishRun(app.db, run.id, { state, summary: "" });
90 return run.id;
91 }
92
93 test("a green check: totals link to Files, the badge to the log", async () => {
94 const run = check(numbers[0]!, "ok");
95 const res = await read(`pull-${numbers[0]}`);
96 expect(res.status).toBe(200);
97 const text = await res.text();
98 expect(text).toContain('class="change-line"');
99 expect(text).toContain(`href="/alice/notes/pulls/${numbers[0]}/files"`);
100 expect(text).toContain("<strong>2</strong>");
101 expect(text).toContain('<span class="add">+4</span>');
102 expect(text).toContain('<span class="del">−0</span>');
103 expect(text).toContain(`href="/alice/notes/runs/${run}"`);
104 expect(text).toContain("Passed");
105 });
106
107 test("a red check", async () => {
108 const run = check(numbers[1]!, "failed");
109 const text = await (await read(`pull-${numbers[1]}`)).text();
110 expect(text).toContain(`href="/alice/notes/runs/${run}"`);
111 expect(text).toContain("Failed");
112 });
113
114 test("no check, and the stat is read once per head", async () => {
115 const text = await (await read(`pull-${numbers[2]}`)).text();
116 expect(text).toContain("No check");
117 const t = performance.now();
118 await (await read(`pull-${numbers[2]}`)).text();
119 expect(performance.now() - t).toBeLessThan(50);
120 });
121
122 test("signed out sees it too; production is unchanged", async () => {
123 expect(await (await read(`pull-${numbers[0]}`, "")).text()).toContain("change-line");
124 const prod = await (await read("production")).text();
125 expect(prod).not.toContain('class="change-line"');
126 });
127
128 test("a pull the viewer may not see is a 404", async () => {
129 expect((await read("pull-9999")).status).toBe(404);
130 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs