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 a link #4100
scout/forge:scout/4088-a-preview-s-toolbar-page-says-what-its-p → main 4 commits fork 7 threads · 7 unresolved
README.md +5 −1
| @@ -983,7 +983,11 @@ file), and if you named an agent, the sentence became issue #1, assigned. | ||
| 983 | 983 | a sealed report, are left out); *Report this page* / *Comment on this pull*, |
| 984 | 984 | previewed first, then a receipt; *Tell the crew* for those who may direct it |
| 985 | 985 | (the one `/:owner/:name/tell`); and *Make it yours* for everyone who cannot |
| 986 | write the app. A production comment opens an issue carrying the page address; | |
| 986 | write the app. On a preview, under the head line, it also says what the pull | |
| 987 | changed (*N files, +A -R*, linking to the pull's Files page) and its latest | |
| 988 | check (§5's run-state badge, linking to the log, or *No check*); the terminal | |
| 989 | has no equivalent, since it already shows the pull's diff and check on the | |
| 990 | pull screen. A production comment opens an issue carrying the page address; | |
| 987 | 991 | a preview comment goes on its pull. From a terminal or an agent the same |
| 988 | 992 | actions are, in the SSH terminal, rows of the palette on a repository |
| 989 | 993 | screen: *Working now* (while an agent run is live, left out when it is on work |
docs/DESIGN.md +1 −1
| @@ -397,7 +397,7 @@ gone, so this list only shrinks. | ||
| 397 | 397 | - **`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` |
| 398 | 398 | - **`style/content.ts`, prose (markdown)** — table cell alignment, built by `markdown.ts` as `ta-${align}`: `.ta-c` `.ta-r` |
| 399 | 399 | - **`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**): `.ln-forge` `.ln-pulse` `.ln-say` `.ln-tool` `.ln-you` `.log-tail-row` `.pinned-failures` `.pinned-slot` `.change-line` (a preview toolbar's change and check: `diffTotals` and the run-state badge, each a link, in a wrapping flex row with 36px tap height) `.run-jump` `.run-live` `.run-steer` `.tests-bar` `.tests-words` `.txt` `.who` | |
| 401 | 401 | - **`style/content.ts`, search results** — search results, the inbox's ask cards, the client list and the claim link: `.ask-acts` `.ask-card` `.ask-group` `.ask-kind` `.ask-meta` `.ask-more-acts` `.ask-pick` `.ask-pick-hit` `.ask-short` `.ask-waiting` `.claim-link-box` `.forges-sent` `.live-toggle` `.needs-you` `.no-hits` |
| 402 | 402 | - **`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 |
| 403 | 403 | - **`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 +8 −2
| @@ -545,6 +545,13 @@ function fileHasReview(file: FileDiff, opts: DiffViewOptions): boolean { | ||
| 545 | 545 | return false; |
| 546 | 546 | } |
| 547 | 547 | |
| 548 | /** The totals of a diff as the summary writes them: additions in `.add`, | |
| 549 | * deletions in `.del` with a real minus sign. */ | |
| 550 | export function diffTotals(diff: Pick<Diff, "additions" | "deletions">): Html { | |
| 551 | return html`<span class="add">+${diff.additions}</span> | |
| 552 | <span class="del">−${diff.deletions}</span>`; | |
| 553 | } | |
| 554 | ||
| 548 | 555 | /** |
| 549 | 556 | * The whole diff: a summary line, then one collapsible view per file. Options |
| 550 | 557 | * add syntax color and inline review comments; omit them and the diff still |
| @@ -566,8 +573,7 @@ export function renderDiff( | ||
| 566 | 573 | <strong>${diff.files.length}</strong> ${diff.files.length === 1 |
| 567 | 574 | ? "file" |
| 568 | 575 | : "files"} |
| 569 | changed <span class="add">+${diff.additions}</span> | |
| 570 | <span class="del">−${diff.deletions}</span> | |
| 576 | changed ${diffTotals(diff)} | |
| 571 | 577 | </div> |
| 572 | 578 | ${shown.map((file) => fileView(file, opts))} |
| 573 | 579 | ${when( |
src/web/studio/change_stat.ts added +33
| @@ -0,0 +1,33 @@ | ||
| 1 | /** | |
| 2 | * The size of a pull's change, remembered by the head it was read at. Every | |
| 3 | * preview page fetches the toolbar page, so the merge-base and diff behind | |
| 4 | * "N files, +A −R" are read once per head and then served from memory. The | |
| 5 | * base may move under a head that has not, so an entry lives a minute. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { Diff } from "../../git/diff.ts"; | |
| 9 | ||
| 10 | export type ChangeStat = Pick<Diff, "additions" | "deletions"> & { files: number }; | |
| 11 | ||
| 12 | const TTL_MS = 60_000; | |
| 13 | const MAX = 500; | |
| 14 | const seen = new Map<string, { at: number; stat: ChangeStat }>(); | |
| 15 | ||
| 16 | /** The stat for `pullId` at `headOid`: cached, or `read` once and kept. */ | |
| 17 | export async function changeStat( | |
| 18 | pullId: string, | |
| 19 | headOid: string, | |
| 20 | read: () => Promise<ChangeStat>, | |
| 21 | now = Date.now(), | |
| 22 | ): Promise<ChangeStat> { | |
| 23 | const key = `${pullId}:${headOid}`; | |
| 24 | const hit = seen.get(key); | |
| 25 | if (hit && now - hit.at < TTL_MS) return hit.stat; | |
| 26 | const stat = await read(); | |
| 27 | if (seen.size >= MAX) seen.delete(seen.keys().next().value!); | |
| 28 | seen.set(key, { at: now, stat }); | |
| 29 | return stat; | |
| 30 | } | |
| 31 | ||
| 32 | /** Forget everything: for tests. */ | |
| 33 | export const forgetChangeStats = (): void => seen.clear(); |
src/web/studio/toolbar.ts +36 −6
| @@ -17,20 +17,24 @@ | ||
| 17 | 17 | |
| 18 | 18 | import { addCommentThrough, openIssueThrough } from "../../actions/issues.ts"; |
| 19 | 19 | import { reportTitle } from "../../actions/page-report.ts"; |
| 20 | import { changeStat } from "./change_stat.ts"; | |
| 21 | import { diffTotals } from "../diff-view.ts"; | |
| 20 | 22 | import { getAppByRepo } from "../../core/apps.ts"; |
| 21 | 23 | import { getIssue, MAX_BODY } from "../../core/issues.ts"; |
| 22 | 24 | import { canWrite } from "../../core/repos.ts"; |
| 23 | import { listRuns } from "../../core/runs.ts"; | |
| 25 | import { getPull, pullDiff, pullDir } from "../../core/pulls.ts"; | |
| 26 | import { checkSummary, listRuns } from "../../core/runs.ts"; | |
| 24 | 27 | import { mayView, mayViewId } from "../../core/sealed.ts"; |
| 25 | 28 | import { mayDirectCrew } from "../../crew/direct.ts"; |
| 26 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; | |
| 29 | import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts"; | |
| 27 | 30 | import type { Ctx } from "../../http/context.ts"; |
| 28 | 31 | import { notFoundPage, pageHead, loadRepo, repoBase } from "../chrome.ts"; |
| 29 | 32 | import { field, readForm } from "../form.ts"; |
| 30 | import { html, when } from "../html.ts"; | |
| 33 | import { html, when, type Html } from "../html.ts"; | |
| 31 | 34 | import { page } from "../layout.ts"; |
| 32 | 35 | import { originError } from "../pages/thread.ts"; |
| 33 | import { btn, card } from "../ui.ts"; | |
| 36 | import type { Repo } from "../../core/repos.ts"; | |
| 37 | import { badge, btn, card, runStateBadge } from "../ui.ts"; | |
| 34 | 38 | |
| 35 | 39 | /** The longest page address the bar may carry. */ |
| 36 | 40 | const PAGE_MAX = 512; |
| @@ -59,8 +63,32 @@ function parseEnv(raw: string): { env: string; pull?: number } | undefined { | ||
| 59 | 63 | const toolbarPath = (base: string, env: string, address: string): string => |
| 60 | 64 | `${base}/toolbar?env=${env}${address === "" ? "" : `&page=${encodeURIComponent(address)}`}`; |
| 61 | 65 | |
| 66 | /** What a preview shows of its pull: the size of the change and the verdict | |
| 67 | * of its latest check, each a link. Nothing when the pull has no head to read. */ | |
| 68 | async function changeLine(ctx: Ctx, repo: Repo, number: number): Promise<Html> { | |
| 69 | const db = ctx.app.db; | |
| 70 | const request = ctx.request; | |
| 71 | const found = timePhase(request, "pull-row", () => getPull(db, repo.id, number)); | |
| 72 | if (!found) return html``; | |
| 73 | const base = `${repoBase(repo)}/pulls/${number}`; | |
| 74 | const stat = await timePhaseAsync(request, "diff", () => | |
| 75 | changeStat(found.id, found.headOid, async () => { | |
| 76 | const dir = await pullDir(db, ctx.app.reposRoot, repo, found); | |
| 77 | const { diff } = await pullDiff(dir, found); | |
| 78 | return { files: diff.files.length, additions: diff.additions, deletions: diff.deletions }; | |
| 79 | }), | |
outdated
Hand-rolled mapping departs from the design system. runStateBadge() already owns these words, icons and tones: queued is secondary, running is attn with the loader icon, cancelled is outline. Here 'running' and 'cancelled' are both the default tone with no icon, and the words are lowercase where the system says Passed/Failed/Running. Fix: |
||
| 80 | ); | |
outdated
Bare |
||
| 81 | const { run } = timePhase(request, "check", () => checkSummary(db, found.id, found.headOid)); | |
outdated
Plain |
||
| 82 | return html`<p class="change-line"> | |
| 83 | <a href="${base}/files">${stat.files} ${stat.files === 1 ? "file" : "files"}, <span class="diff-stat">${diffTotals(stat)}</span></a> | |
| 84 | ${run | |
| 85 | ? html`<a href="${repoBase(repo)}/runs/${run.id}" aria-label="Check ${run.state === "ok" ? "passed" : run.state}, open log">${runStateBadge(run.state, run.kind)}</a>` | |
| 86 | : badge("No check", { variant: "outline" })} | |
| 87 | </p>`; | |
| 88 | } | |
outdated
Hand-built |
||
| 89 | ||
| 62 | 90 | // GET /:owner/:name/toolbar |
| 63 | export function toolbarPage(ctx: Ctx): Response { | |
| 91 | export async function toolbarPage(ctx: Ctx): Promise<Response> { | |
| 64 | 92 | const request = ctx.request; |
| 65 | 93 | notePhase(request, "session", takeSessionTime(request)); |
| 66 | 94 | const repo = timePhase(request, "repo", () => loadRepo(ctx)); |
| @@ -80,12 +108,13 @@ export function toolbarPage(ctx: Ctx): Response { | ||
| 80 | 108 | const base = repoBase(repo); |
| 81 | 109 | const here = html`${repo.name}, ${pull ? html`preview of pull #${pull.number}` : "production"}`; |
| 82 | 110 | const home = btn("Home", { href: "/", variant: "ghost" }); |
| 111 | const change = pull ? await changeLine(ctx, repo, pull.number) : html``; | |
| 83 | 112 | const rendering = performance.now(); |
| 84 | 113 | if (!user) { |
| 85 | 114 | const response = page({ |
| 86 | 115 | ctx, |
| 87 | 116 | title: `${repo.name} · forge`, |
| 88 | body: html`<div class="narrow-wide">${pageHead(here)}<div class="form-actions">${home}</div></div>`, | |
| 117 | body: html`<div class="narrow-wide">${pageHead(here)}${change}<div class="form-actions">${home}</div></div>`, | |
| 89 | 118 | }); |
| 90 | 119 | notePhase(request, "render", performance.now() - rendering); |
| 91 | 120 | return response; |
| @@ -105,6 +134,7 @@ export function toolbarPage(ctx: Ctx): Response { | ||
| 105 | 134 | const comment = pull ? "Comment on this pull" : "Report this page"; |
| 106 | 135 | const body = html`<div class="narrow-wide"> |
| 107 | 136 | ${pageHead(here)} |
| 137 | ${change} | |
| 108 | 138 | ${when( |
| 109 | 139 | live !== undefined, |
| 110 | 140 | () => html`<p class="mono" data-working> |
src/web/style/content.ts +2
| @@ -653,6 +653,8 @@ form.inline { display: inline; margin-left: auto; } | ||
| 653 | 653 | } |
| 654 | 654 | .diff-stat .add { color: var(--add-fg); } |
| 655 | 655 | .diff-stat .del { color: var(--del-fg); } |
| 656 | .change-line { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin: 0 0 12px; } | |
| 657 | .change-line a { display: inline-flex; align-items: center; gap: 6px; min-height: 36px; font-family: var(--mono); font-size: 13px; } | |
min-height: 36px is below the 44px touch floor on coarse pointers (DESIGN.md Density). Add a |
||
| 656 | 658 | .diff-note { padding: 20px 16px; text-align: center; color: var(--muted-fg); font-size: 13px; } |
| 657 | 659 | .diff-stub p { margin: 0 0 10px; } |
| 658 | 660 | .diff-stub .diff-stat { display: inline-flex; } |
test/toolbar-change.test.ts added +178
| @@ -0,0 +1,178 @@ | ||
| 1 | /** A preview's toolbar page says what its pull changed and how its check stands (#4088). */ | |
| 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 { newApp } from "../src/actions/apps.ts"; | |
| 7 | import { openPull } from "../src/actions/pulls.ts"; | |
| 8 | import { commitToBranch } from "../src/actions/repos.ts"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { getRepo } from "../src/core/repos.ts"; | |
| 11 | import { cancelRun, claimRun, createRun, finishRun } from "../src/core/runs.ts"; | |
| 12 | import { createSession } from "../src/core/sessions.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { getPull } from "../src/core/pulls.ts"; | |
| 15 | import { changeStat, forgetChangeStats } from "../src/web/studio/change_stat.ts"; | |
| 16 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 17 | import { proveAll } from "./helpers/verified.ts"; | |
| 18 | import { quietLogs } from "./helpers/globals.ts"; | |
| 19 | ||
| 20 | quietLogs(); | |
| 21 | ||
| 22 | let dataDir: string; | |
| 23 | let app: App; | |
| 24 | let servers: Servers; | |
| 25 | let cookie = ""; | |
| 26 | let repoId = ""; | |
| 27 | const pulls: Record<string, number> = {}; | |
| 28 | const cells = process.env["FORGE_CELLS"]; | |
| 29 | ||
| 30 | beforeAll(async () => { | |
| 31 | process.env["FORGE_CELLS"] = "off"; | |
| 32 | dataDir = await mkdtemp(join(tmpdir(), "forge-toolbar-change-")); | |
| 33 | app = createApp({ dataDir, port: 0 }); | |
| 34 | servers = await serve(app); | |
| 35 | const made = await createUser(app.db, { | |
| 36 | origin: "seed", | |
| 37 | trusted: true, | |
| 38 | username: "alice", | |
| 39 | email: "[email protected]", | |
| 40 | password: "a-good-passphrase", | |
| 41 | adminIfFirst: true, | |
| 42 | }); | |
| 43 | if (!made.ok) throw new Error(String(made.error)); | |
| 44 | const alice = made.value; | |
| 45 | proveAll(app); | |
| 46 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 47 | const r = await newApp(app, { id: alice.id, username: "alice" } as never, { | |
| 48 | name: "r", | |
| 49 | description: "x", | |
| 50 | visibility: "public", | |
| 51 | }); | |
| 52 | if (!r.ok) throw new Error(String(r.error)); | |
| 53 | const repo = getRepo(app.db, "alice", "r")!; | |
| 54 | repoId = repo.id; | |
| 55 | const base = await commitToBranch(app, alice, repo, { | |
| 56 | message: "base", | |
| 57 | files: [{ path: "a.txt", content: "one\n" }], | |
| 58 | }); | |
| 59 | if (!base.ok) throw new Error(JSON.stringify(base.error)); | |
| 60 | for (const name of ["green", "red", "none", "queued", "running", "cancelled"]) { | |
| 61 | const done = await commitToBranch(app, alice, repo, { | |
| 62 | branch: name, | |
| 63 | message: name, | |
| 64 | files: [ | |
| 65 | { path: `${name}.txt`, content: "x\ny\n" }, | |
| 66 | { path: "a.txt", content: "two\n" }, | |
| 67 | ], | |
| 68 | }); | |
| 69 | if (!done.ok) throw new Error(JSON.stringify(done.error)); | |
| 70 | const pull = await openPull(app, alice, repo, { title: name, body: "", baseBranch: "main", headBranch: name }); | |
| 71 | if (!pull.ok) throw new Error(JSON.stringify(pull.error)); | |
| 72 | pulls[name] = pull.value.number; | |
| 73 | } | |
| 74 | for (const [name, state] of [["queued", "queued"], ["running", "running"], ["cancelled", "cancelled"]] as const) { | |
| 75 | const p = getPull(app.db, repoId, pulls[name]!)!; | |
| 76 | const run = createRun(app.db, { repoId, kind: "deploy", issueId: p.id, commitOid: p.headOid, env: `pull-${p.number}` }); | |
| 77 | if (state !== "queued") claimRun(app.db, run.id); | |
| 78 | if (state === "cancelled") cancelRun(app.db, run.id); | |
| 79 | } | |
| 80 | for (const [name, state] of [["green", "ok"], ["red", "failed"]] as const) { | |
| 81 | const p = getPull(app.db, repoId, pulls[name]!)!; | |
| 82 | const run = createRun(app.db, { repoId, kind: "deploy", issueId: p.id, commitOid: p.headOid, env: `pull-${p.number}` }); | |
| 83 | claimRun(app.db, run.id); | |
| 84 | finishRun(app.db, run.id, { state, summary: state }); | |
| 85 | } | |
| 86 | }); | |
| 87 | ||
| 88 | afterAll(async () => { | |
| 89 | await servers.stop(); | |
| 90 | app.close(); | |
| 91 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 92 | else process.env["FORGE_CELLS"] = cells; | |
| 93 | await rm(dataDir, { recursive: true, force: true }); | |
| 94 | }); | |
| 95 | ||
| 96 | async function toolbar(env: string): Promise<{ status: number; body: string; ms: number }> { | |
| 97 | const t = performance.now(); | |
| 98 | const res = await fetch(`${servers.url}/alice/r/toolbar?env=${env}`, { headers: { cookie } }); | |
| 99 | const body = await res.text(); | |
| 100 | return { status: res.status, body, ms: performance.now() - t }; | |
| 101 | } | |
| 102 | ||
| 103 | test("a preview says what its pull changed, linking to the files", async () => { | |
| 104 | const got = await toolbar(`pull-${pulls["green"]}`); | |
| 105 | expect(got.status).toBe(200); | |
| 106 | expect(got.body).toMatch(new RegExp(`<a href="/alice/r/pulls/${pulls["green"]}/files">\\s*2 files,`)); | |
| 107 | }); | |
| 108 | ||
| 109 | /** The words of the badges in the change line, in order. */ | |
| 110 | const words = (body: string): string[] => | |
| 111 | [...body.slice(body.indexOf("change-line")).split("</p>")[0]!.matchAll(/>\s*(Passed|Failed|Running|Queued|Cancelled|No check)\s*</g)].map((m) => m[1]!); | |
| 112 | ||
outdated
Regex-matching badge markup couples the test to whitespace in the renderer. Once runStateBadge is used, assert the badge's words (Passed, Failed) and add queued/running and cancelled cases; the 'running' branch currently has no test that would fail without it. |
||
| 113 | test("a green check is Passed and links to its log", async () => { | |
| 114 | const got = await toolbar(`pull-${pulls["green"]}`); | |
| 115 | expect(words(got.body)).toEqual(["Passed"]); | |
| 116 | expect(got.body).toMatch(/href="\/alice\/r\/runs\/[^"]+" aria-label="Check passed, open log"/); | |
| 117 | }); | |
| 118 | ||
| 119 | test("a red check is Failed", async () => { | |
| 120 | expect(words((await toolbar(`pull-${pulls["red"]}`)).body)).toEqual(["Failed"]); | |
| 121 | }); | |
| 122 | ||
| 123 | test("a queued check is Queued, a running one Running (attn), a stopped one Cancelled (outline)", async () => { | |
| 124 | expect(words((await toolbar(`pull-${pulls["queued"]}`)).body)).toEqual(["Queued"]); | |
| 125 | const running = (await toolbar(`pull-${pulls["running"]}`)).body; | |
| 126 | expect(words(running)).toEqual(["Running"]); | |
| 127 | expect(running).toContain('data-variant="attn"'); | |
| 128 | const cancelled = (await toolbar(`pull-${pulls["cancelled"]}`)).body; | |
| 129 | expect(words(cancelled)).toEqual(["Cancelled"]); | |
| 130 | expect(cancelled).toContain('data-variant="outline"'); | |
| 131 | }); | |
| 132 | ||
| 133 | test("a pull with no check says No check, without a link to a log", async () => { | |
| 134 | const got = await toolbar(`pull-${pulls["none"]}`); | |
| 135 | expect(words(got.body)).toEqual(["No check"]); | |
| 136 | expect(got.body).not.toContain("open log"); | |
| 137 | }); | |
| 138 | ||
| 139 | test("production's page has no change line, and the preview answers well inside the 200 ms floor", async () => { | |
| 140 | const prod = await toolbar("production"); | |
| 141 | expect(prod.status).toBe(200); | |
| 142 | expect(prod.body).not.toContain("change-line"); | |
| 143 | const got = await toolbar(`pull-${pulls["green"]}`); | |
| 144 | expect(got.ms).toBeLessThan(200); | |
| 145 | }); | |
| 146 | ||
| 147 | test("a pull that does not exist is a 404", async () => { | |
| 148 | expect((await toolbar("pull-999")).status).toBe(404); | |
| 149 | }); | |
| 150 | ||
| 151 | test("the change is read once per head, not once per request", async () => { | |
| 152 | forgetChangeStats(); | |
| 153 | let reads = 0; | |
| 154 | const read = async () => (reads++, { files: 1, additions: 2, deletions: 3 }); | |
| 155 | await changeStat("p", "h1", read, 0); | |
| 156 | await changeStat("p", "h1", read, 1_000); | |
| 157 | expect(reads).toBe(1); | |
| 158 | await changeStat("p", "h2", read, 1_000); | |
| 159 | expect(reads).toBe(2); | |
| 160 | await changeStat("p", "h1", read, 120_000); | |
| 161 | expect(reads).toBe(3); | |
| 162 | }); | |
| 163 | ||
| 164 | test("a preview's toolbar reads the diff once across requests, and writes the totals as the summary does", async () => { | |
| 165 | forgetChangeStats(); | |
| 166 | const first = await toolbar(`pull-${pulls["green"]}`); | |
| 167 | const t = performance.now(); | |
| 168 | const second = await fetch(`${servers.url}/alice/r/toolbar?env=pull-${pulls["green"]}`, { headers: { cookie } }); | |
| 169 | const body = await second.text(); | |
| 170 | const timing = /diff;dur=([\d.]+)/.exec(second.headers.get("server-timing") ?? ""); | |
| 171 | expect(Number(timing?.[1])).toBeLessThan(1); | |
| 172 | expect(performance.now() - t).toBeLessThan(200); | |
| 173 | for (const html of [first.body, body]) { | |
| 174 | expect(html).toContain('<span class="add">+3</span>'); | |
| 175 | expect(html).toContain('<span class="del">−1</span>'); | |
| 176 | expect(html).toContain('class="change-line"'); | |
| 177 | } | |
| 178 | }); |
A full pullDiff on every toolbar load, which every preview page fetches. Cache the stat by head oid so the common request stays inside the 50 ms the issue asked for.