Public: anyone can read this; the crew can change it.
The forge toolbar in a preview shows the pull's check as one word #14868
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14858-the-forge-toolbar-in-a-preview-shows-the → main 5 commits fork 1 thread · 1 unresolved
6 files
changed +103
−7
src/apps/pill_check.ts +3
| @@ -32,3 +32,6 @@ export function pillCheck(db: Database, issueId: string, headOid: string): PillC | ||
| 32 | 32 | if (row.state === "cancelled") return "cancelled"; |
| 33 | 33 | return "none"; |
| 34 | 34 | } |
| 35 | ||
| 36 | /** Width in ch the word's slot reserves: the longest word, so a change of state never reflows. */ | |
| 37 | export const CHECK_WIDTH = Math.max(...Object.values(CHECK_WORDS).map((w) => w.length)); |
src/apps/toolbar.ts +14 −3
| @@ -19,7 +19,7 @@ import { TELL_TEXT_MAX } from "../actions/tell_from.ts"; | ||
| 19 | 19 | import { sha256 } from "../kernel/hash.ts"; |
| 20 | 20 | import { TOUR_ASSET_PATH } from "./tour.ts"; |
| 21 | 21 | import type { ReviewStep } from "../core/review_steps.ts"; |
| 22 | import { CHECK_WORDS, type PillCheck } from "./pill_check.ts"; | |
| 22 | import { CHECK_WIDTH, CHECK_WORDS, type PillCheck } from "./pill_check.ts"; | |
| 23 | 23 | import { NOTE_EMPTY, NOTE_FAILED, NOTE_OFFLINE, NOTE_WAIT } from "./note_words.ts"; |
| 24 | 24 | import { COMMENT_ASSET_PATH } from "./toolbar_comment.ts"; |
| 25 | 25 | |
| @@ -119,7 +119,17 @@ const toolbarSource = `(function () { | ||
| 119 | 119 | try { pullHref = new URL("/" + d.repo + "/pulls/" + d.pull, fo).href; } catch (e) {} |
| 120 | 120 | } |
| 121 | 121 | var cw = ${JSON.stringify(CHECK_WORDS)}; |
| 122 | var pt = "Pull #" + d.pull + (d.check && Object.prototype.hasOwnProperty.call(cw, d.check) ? ": " + cw[d.check] : ""); | |
| 122 | var cword = d.check && Object.prototype.hasOwnProperty.call(cw, d.check) ? cw[d.check] : ""; | |
| 123 | var pt = "Pull #" + d.pull + (cword ? ": " : ""); | |
| 124 | // The word's own slot is as wide as the longest word: checking -> green never reflows the row. | |
| 125 | function slot(a) { | |
| 126 | if (!cword) return; | |
| 127 | var cs = document.createElement("span"); | |
| 128 | cs.textContent = cword; | |
| 129 | cs.style.minWidth = "${CHECK_WIDTH}ch"; | |
| 130 | cs.style.display = "inline-block"; | |
| 131 | a.appendChild(cs); | |
| 132 | } | |
| 123 | 133 | if (pullHref) { |
| 124 | 134 | var pl = document.createElement("a"); |
| 125 | 135 | pl.textContent = pt; |
| @@ -127,8 +137,9 @@ const toolbarSource = `(function () { | ||
| 127 | 137 | pl.href = pullHref; |
| 128 | 138 | pl.rel = "noopener"; |
| 129 | 139 | pl.style.cssText = LS + ";overflow-wrap:anywhere"; |
| 140 | slot(pl); | |
| 130 | 141 | panel.appendChild(pl); |
| 131 | } else if (pv) line(pt); | |
| 142 | } else if (pv) slot(line(pt)); | |
| 132 | 143 | // The comment box is its own script, fetched on the first open (toolbar_comment.ts); it gets |
| 133 | 144 | // the proof chain only while that exact script runs, and only once. |
| 134 | 145 | var cm = null, cx = null; |
test/apps-pill-check.test.ts +8 −2
| @@ -84,8 +84,14 @@ describe("the pill's check line", () => { | ||
| 84 | 84 | |
| 85 | 85 | test("the panel script draws the line for a pull, never for production or an unknown key", () => { |
| 86 | 86 | const pull = { app: "notes", env: "pull-3", pull: "3" }; |
| 87 | expect(drawn({ ...pull, check: "failed" })).toContain(`Pull #3: ${CHECK_WORDS.failed}`); | |
| 88 | expect(drawn({ ...pull, check: "cancelled" })).toContain("Pull #3: cancelled"); | |
| 87 | // The word sits in its own slot, so it is its own text beside "Pull #3: ". | |
| 88 | const line = (set: Record<string, string>) => { | |
| 89 | const t = drawn(set); | |
| 90 | const i = t.indexOf("Pull #3: "); | |
| 91 | return i < 0 ? null : `${t[i]}${t[i + 1]}`; | |
| 92 | }; | |
| 93 | expect(line({ ...pull, check: "failed" })).toBe(`Pull #3: ${CHECK_WORDS.failed}`); | |
| 94 | expect(line({ ...pull, check: "cancelled" })).toBe("Pull #3: cancelled"); | |
| 89 | 95 | expect(drawn({ ...pull, check: "bogus" }).filter((t) => t.startsWith("Pull"))).toEqual(["Pull #3"]); |
| 90 | 96 | expect(drawn({ ...pull, check: "constructor" }).filter((t) => t.startsWith("Pull"))).toEqual(["Pull #3"]); |
| 91 | 97 | expect(drawn(pull).some((t) => /^(Checks|No checks)/.test(t))).toBe(false); |
test/apps-toolbar-state.test.ts +3 −1
| @@ -31,9 +31,11 @@ test("no landed change leaves the attribute out and the pill says only since whe | ||
| 31 | 31 | // the growth on top, allowed here by name; anything beyond that still has to stay under 300. |
| 32 | 32 | const BASE_SCRIPT_BYTES = 22918; |
| 33 | 33 | const DISMISSAL_ALLOWANCE = 1150; |
| 34 | // The check word's reserved-width slot (#14858), allowed by name: 228 bytes. | |
| 35 | const CHECK_SLOT_ALLOWANCE = 230; | |
| 34 | 36 | |
| 35 | 37 | test("the addition stays under 300 bytes", () => { |
| 36 | expect(Buffer.byteLength(toolbarJs) - BASE_SCRIPT_BYTES).toBeLessThan(300 + DISMISSAL_ALLOWANCE); | |
| 38 | expect(Buffer.byteLength(toolbarJs) - BASE_SCRIPT_BYTES).toBeLessThan(300 + DISMISSAL_ALLOWANCE + CHECK_SLOT_ALLOWANCE); | |
| 37 | 39 | const tag = toolbarTag("https://forge.test", where("x".repeat(CHANGE_MAX)), "p", "n"); |
| 38 | 40 | const bare = toolbarTag("https://forge.test", where(), "p", "n"); |
| 39 | 41 | expect(Buffer.byteLength(tag) - Buffer.byteLength(bare)).toBeLessThan(300); |
test/toolbar-check.test.ts added +74
| @@ -0,0 +1,74 @@ | ||
| 1 | /** | |
| 2 | * The preview toolbar's pull line says green, red or checking from the pull's stored check run, and | |
| 3 | * plain "Pull #N" when it has none. Already true on main (#14060); this proves it end to end: the | |
| 4 | * stored run row, then the word (#14858). | |
| 5 | */ | |
| 6 | import { Database } from "bun:sqlite"; | |
| 7 | import { expect, test } from "bun:test"; | |
| 8 | import { CHECK_WIDTH, CHECK_WORDS, pillCheck, type PillCheck } from "../src/apps/pill_check.ts"; | |
| 9 | import { toolbarJs, withToolbar } from "../src/apps/toolbar.ts"; | |
| 10 | ||
| 11 | const page = new TextEncoder().encode("<html><body><p>hi</p></body></html>"); | |
| 12 | ||
| 13 | function db(): Database { | |
| 14 | const d = new Database(":memory:"); | |
| 15 | d.run(`CREATE TABLE run (id INTEGER PRIMARY KEY, kind TEXT, issue_id TEXT, commit_oid TEXT, state TEXT)`); | |
| 16 | return d; | |
| 17 | } | |
| 18 | ||
| 19 | function row(check: PillCheck | undefined): { text: string; slot?: any } { | |
| 20 | const out = new TextDecoder().decode( | |
| 21 | withToolbar(page, "https://forge.test", { app: "notes", repo: "alice/notes", env: "pull-12", pull: 12, ...(check ? { check } : {}) }), | |
| 22 | ); | |
| 23 | const data: Record<string, string> = {}; | |
| 24 | for (const m of /<script ([^>]*)>/.exec(out)![1]!.matchAll(/data-([a-z]+)="([^"]*)"/g)) data[m[1]!] = m[2]!; | |
| 25 | const made: any[] = []; | |
| 26 | const el = (): any => ({ | |
| 27 | tag: "", | |
| 28 | style: {}, dataset: {}, children: [], textContent: "", | |
| 29 | appendChild(c: any) { this.textContent += c.textContent ?? ""; return c; }, setAttribute() {}, addEventListener() {}, classList: { add() {}, remove() {} }, | |
| 30 | attachShadow() { return el(); }, | |
| 31 | }); | |
| 32 | const doc: any = { | |
| 33 | referrer: "", currentScript: { src: "/__forge/t.js", dataset: data }, | |
| 34 | createElement: (t: string) => { const e = el(); e.tag = t; made.push(e); return e; }, | |
| 35 | addEventListener() {}, body: el(), documentElement: el(), | |
| 36 | }; | |
| 37 | const win: any = { location: { origin: "https://app.test", pathname: "/" }, addEventListener() {}, matchMedia: () => ({ matches: false }) }; | |
| 38 | new Function("window", "document", "location", "localStorage", "fetch", "URL", "setTimeout", "setInterval", "requestAnimationFrame", toolbarJs)( | |
| 39 | win, doc, win.location, { getItem: () => null, setItem() {} }, () => new Promise(() => {}), URL, () => 0, () => 0, () => 0); | |
| 40 | const link = made.find((e) => /^Pull #\d+/.test(e.textContent)); | |
| 41 | return { text: link.textContent, slot: made.find((e) => e.tag === "span" && e.textContent === CHECK_WORDS[check as PillCheck]) }; | |
| 42 | } | |
| 43 | const word = (check: PillCheck | undefined) => row(check).text; | |
| 44 | ||
| 45 | test("stored ok, failed and running runs read green, red and checking", () => { | |
| 46 | const d = db(); | |
| 47 | for (const [i, s] of ["ok", "failed", "running"].entries()) | |
| 48 | d.run(`INSERT INTO run (kind, issue_id, commit_oid, state) VALUES ('check', ?, 'abc', ?)`, [`p${i}`, s]); | |
| 49 | expect(word(pillCheck(d, "p0", "abc"))).toBe("Pull #12: green"); | |
| 50 | expect(word(pillCheck(d, "p1", "abc"))).toBe("Pull #12: red"); | |
| 51 | expect(word(pillCheck(d, "p2", "abc"))).toBe("Pull #12: checking"); | |
| 52 | }); | |
| 53 | ||
| 54 | test("the newest run of the head wins; another commit's run is not read", () => { | |
| 55 | const d = db(); | |
| 56 | d.run(`INSERT INTO run (kind, issue_id, commit_oid, state) VALUES ('check','p','abc','failed'), ('check','p','abc','ok'), ('check','p','old','failed')`); | |
| 57 | expect(pillCheck(d, "p", "abc")).toBe("passed"); | |
| 58 | }); | |
| 59 | ||
| 60 | test("a pull with no check run says no check yet, and the pill draws plain Pull #N when none is sent", () => { | |
| 61 | expect(pillCheck(db(), "p", "abc")).toBe("none"); | |
| 62 | expect(word("none")).toBe("Pull #12: no check yet"); | |
| 63 | expect(word(undefined)).toBe("Pull #12"); | |
| 64 | }); | |
| 65 | ||
| 66 | test("the word sits in a slot as wide as the longest word, the same for every state", () => { | |
| 67 | expect(CHECK_WIDTH).toBe("no check yet".length); | |
| 68 | for (const check of Object.keys(CHECK_WORDS) as PillCheck[]) { | |
| 69 | const slot = row(check).slot; | |
| 70 | expect(slot.style.minWidth).toBe(`${CHECK_WIDTH}ch`); | |
| 71 | expect(slot.style.display).toBe("inline-block"); | |
| 72 | } | |
| 73 | expect(row(undefined).slot).toBeUndefined(); | |
| 74 | }); |
test/toolbar-pull-state.test.ts +1 −1
| @@ -20,7 +20,7 @@ function line(markup: string): { textContent: string; href?: string } | undefine | ||
| 20 | 20 | const made: any[] = []; |
| 21 | 21 | const el = (t: string): any => ({ |
| 22 | 22 | tag: t, style: {}, dataset: {}, children: [], textContent: "", |
| 23 | appendChild(c: any) { return c; }, setAttribute() {}, addEventListener() {}, classList: { add() {}, remove() {} }, | |
| 23 | appendChild(c: any) { this.textContent += c.textContent ?? ""; return c; }, setAttribute() {}, addEventListener() {}, classList: { add() {}, remove() {} }, | |
| 24 | 24 | attachShadow() { return el("shadow"); }, |
| 25 | 25 | }); |
| 26 | 26 | const doc: any = { |
new above
offline
Add a case asserting the word sits in a reserved-width element, so box 2 has a test that fails without it.