Public: anyone can read this; the crew can change it.
Red after landing: test/home-frame.test.ts fails on main #15300
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15299-red-after-landing-test-home-frame-test-t → main 0 commits fork 2 threads · 2 unresolved
5 files
changed +83
−43
docs/features/home-frame.md +3 −2
| @@ -43,8 +43,9 @@ home shows them. | ||
| 43 | 43 | **Terminal.** The terminal's home (`ssh`, `FORGE_APP_SSH`) is unchanged: it draws |
| 44 | 44 | its own screen from the same reads, not from this memory. |
| 45 | 45 | |
| 46 | Tests: `test/home-frame.test.ts` (thousands of issues; p95 of the server's own | |
| 47 | time after a write under 50 ms; first response under 100 KB uncompressed), | |
| 46 | Tests: `test/home-frame.test.ts` (thousands of issues; after a write, the median | |
| 47 | look runs at most 50 SQL statements, fewer than a cold draw; first response under 100 KB | |
| 48 | uncompressed), | |
| 48 | 49 | `test/home-memory.test.ts`. Tests that write and then read home use |
| 49 | 50 | `test/helpers/home-fresh.ts`, which does what the page's script does. |
| 50 | 51 |
scripts/test-honesty-allow.json +1 −1
| @@ -231,7 +231,7 @@ | ||
| 231 | 231 | "test/helpers/tui.ts": {"wait":1}, |
| 232 | 232 | "test/holds.test.ts": {"wait":1}, |
| 233 | 233 | "test/home-cache-ttl.test.ts": {"wait":2}, |
| 234 | "test/home-frame.test.ts": {"wait":2}, | |
| 234 | "test/home-frame.test.ts": {"wait":1}, | |
| 235 | 235 | "test/home-memory.test.ts": {"wait":1}, |
| 236 | 236 | "test/hook-ping.test.ts": {"wait":1}, |
| 237 | 237 | "test/hooks-etag.test.ts": {"wait":1}, |
test/behaviors-rule.test.ts +36 −2
| @@ -107,15 +107,49 @@ const WIDTH_READS: Record<string, number> = { | ||
| 107 | 107 | |
| 108 | 108 | const WIDTH = /innerWidth|clientWidth|offsetWidth|matchMedia\(['"]\((min|max)-/g; |
| 109 | 109 | |
| 110 | /** | |
| 111 | * Whether the file sets a script of its own. Handing one on to `documentHeaders` (the 304 a held | |
| 112 | * page is owed carries the page's CSP hash) forwards a script; it does not own one. | |
| 113 | */ | |
| 114 | function setsOwnScript(text: string): boolean { | |
| 115 | return /\bscriptJs:/.test(withoutForwards(text)); | |
| 116 | } | |
| 117 | ||
| 118 | /** The text with every `documentHeaders(…)` call cut out, its parentheses matched across lines. */ | |
| 119 | function withoutForwards(text: string): string { | |
| 120 | let out = ""; | |
| 121 | let at = 0; | |
| 122 | for (const m of text.matchAll(/\bdocumentHeaders\(/g)) { | |
| 123 | if (m.index < at) continue; | |
| 124 | out += text.slice(at, m.index); | |
| 125 | let depth = 0; | |
| 126 | let i = m.index + m[0].length - 1; | |
| 127 | for (; i < text.length; i++) { | |
| 128 | if (text[i] === "(") depth++; | |
| 129 | else if (text[i] === ")" && --depth === 0) break; | |
| 130 | } | |
| 131 | at = i + 1; | |
| 132 | } | |
| 133 | return out + text.slice(at); | |
| 134 | } | |
| 135 | ||
| 136 | test("a file that only forwards a script to documentHeaders does not own one", () => { | |
| 137 | expect(setsOwnScript("const h = documentHeaders({ ctx, scriptJs: script });")).toBe(false); | |
| 138 | expect(setsOwnScript("const h = documentHeaders({\n ctx,\n ...(s === undefined ? {} : { scriptJs: s }),\n});")).toBe(false); | |
| 139 | expect(setsOwnScript("return page({ ctx, title, scriptJs: SCRIPT, body });")).toBe(true); | |
| 140 | expect(setsOwnScript("const h = documentHeaders({ ctx });\nreturn page({ scriptJs: S });")).toBe(true); | |
| 141 | expect(setsOwnScript("documentHeaders({ ctx }); return page({ scriptJs: S });")).toBe(true); | |
| 142 | }); | |
| 143 | ||
| 110 | 144 | test("a page that sets its own script is on the list or composes the behaviors", () => { |
| 111 | 145 | const strangers = files |
| 112 | .filter((f) => /\bscriptJs:/.test(f.text) && !OWN_SCRIPT.has(f.path) && !f.text.includes("behaviorsJs")) | |
| 146 | .filter((f) => setsOwnScript(f.text) && !OWN_SCRIPT.has(f.path) && !f.text.includes("behaviorsJs")) | |
| 113 | 147 | .map((f) => f.path); |
| 114 | 148 | expect(strangers).toEqual([]); |
| 115 | 149 | }); |
| 116 | 150 | |
| 117 | 151 | test("the list only names pages that still set a script", () => { |
| 118 | const live = new Set(files.filter((f) => /\bscriptJs:/.test(f.text)).map((f) => f.path)); | |
| 152 | const live = new Set(files.filter((f) => setsOwnScript(f.text)).map((f) => f.path)); | |
| 119 | 153 | expect([...OWN_SCRIPT].filter((p) => !live.has(p))).toEqual([]); |
| 120 | 154 | }); |
| 121 | 155 |
test/home-frame.test.ts +39 −34
| @@ -1,18 +1,23 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * Home paints its frame first (#7660): signed in, on a forge with thousands |
| 3 | 3 | * of issues, a write between two looks leaves the next look answered from |
| 4 | * memory, within the budget, with the stale regions marked for the page to | |
| 4 | * memory, within its statement budget, with the stale regions marked for the page to | |
| 5 | 5 | * swap, and each late region has a fragment of its own. |
| 6 | 6 | */ |
| 7 | 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; |
| 8 | import { snappyFixture, type SnappyFixture } from "../bench/snappy.ts"; | |
| 8 | import { medianCount, pageStatements, snappyFixture, type SnappyFixture } from "../bench/snappy.ts"; | |
| 9 | 9 | import { forgetHome } from "../src/web/studio/home_last.ts"; |
| 10 | 10 | |
| 11 | /** The server's own time for `/` after a write, median of a sample, in ms: what the request did before sending a byte. */ | |
| 12 | const APP_BUDGET_MS = 50; | |
| 13 | /** Looks in a sample, and how many samples may be taken: a sample starved by the machine is taken once more, a slow code is slow in both. */ | |
| 11 | /** | |
| 12 | * The SQL statements `/` may run after a write, median of the looks: speed counted in work, never | |
| 13 | * in a clock (docs/PRINCIPLES.md). Answered from memory it runs 43 on this forge, and drawing it | |
| 14 | * cold runs 82; the median is the same on an idle machine and a loaded one, where the 50 ms this | |
| 15 | * test once timed went red with no change to the code (#15299: 67 ms with every core busy). | |
| 16 | */ | |
| 17 | const STATEMENT_BUDGET = 50; | |
| 18 | /** Looks after a write, each counted alone, and cold draws to compare them with. */ | |
| 14 | 19 | const LOOKS = 20; |
| 15 | const SAMPLES = 2; | |
| 20 | const COLD_LOOKS = 5; | |
| 16 | 21 | /** The first response, uncompressed, on a forge this size. */ |
| 17 | 22 | const FIRST_BYTES = 100_000; |
| 18 | 23 | |
| @@ -25,10 +30,10 @@ afterAll(async () => { | ||
| 25 | 30 | }); |
| 26 | 31 | |
| 27 | 32 | const look = async (cookie: string, path = "/feed") => { |
| 33 | f.counter.reset(); | |
| 28 | 34 | const res = await fetch(`${f.base}${path}`, { headers: { cookie } }); |
| 29 | 35 | const text = await res.text(); |
| 30 | const app = /app;dur=([\d.]+)/.exec(res.headers.get("server-timing") ?? ""); | |
| 31 | return { res, text, ms: app === null ? NaN : Number(app[1]) }; | |
| 36 | return { res, text, statements: pageStatements(f.counter) }; | |
| 32 | 37 | }; |
| 33 | 38 | const write = (n: number) => |
| 34 | 39 | f.app.db.query("UPDATE repo SET description = ?1 WHERE id = ?2").run(`moved ${n}`, f.repos[0]!.id); |
| @@ -39,33 +44,33 @@ test("the data is realistic: thousands of issues", () => { | ||
| 39 | 44 | }); |
| 40 | 45 | |
| 41 | 46 | test("after a write, signed-in home is answered from memory within the budget and says what to swap", async () => { |
| 42 | forgetHome(f.app.db); | |
| 43 | const cold = await look(f.ownerCookie); | |
| 44 | expect(cold.res.status).toBe(200); | |
| 45 | expect(cold.text).not.toContain(`data-refresh="/`); | |
| 46 | let marked: string; | |
| 47 | // The first look after a write schedules the one-off redraw of the five regions, which runs on this process's loop for the looks that follow; let it finish, so the sample measures the steady cost of a look. | |
| 48 | write(-1); | |
| 49 | marked = (await look(f.ownerCookie)).text; | |
| 50 | await Bun.sleep(1_000); | |
| 51 | let median = Infinity; | |
| 52 | for (let sample = 0; sample < SAMPLES && !(median < APP_BUDGET_MS); sample++) { | |
| 53 | const times: number[] = []; | |
| 54 | for (let i = 0; i < LOOKS; i++) { | |
| 55 | write(sample * LOOKS + i); | |
| 56 | const got = await look(f.ownerCookie); | |
| 57 | expect(got.res.status).toBe(200); | |
| 58 | times.push(got.ms); | |
| 59 | // Answered from memory on every look, not only the first: this is the verdict that depends on the code alone. | |
| 60 | expect(got.text).toContain('data-refresh="/live/home/feed"'); | |
| 61 | await Bun.sleep(1); | |
| 62 | } | |
| 63 | // Every look measured a time; a missing one is a broken header, not a fast look. | |
| 64 | expect(times.every((t) => Number.isFinite(t))).toBe(true); | |
| 65 | times.sort((a, b) => a - b); | |
| 66 | median = times[Math.floor(times.length / 2)]!; | |
| 47 | const colds: number[] = []; | |
| 48 | for (let i = 0; i < COLD_LOOKS; i++) { | |
| 49 | forgetHome(f.app.db); | |
| 50 | const cold = await look(f.ownerCookie); | |
| 51 | expect(cold.res.status).toBe(200); | |
| 52 | expect(cold.text).not.toContain(`data-refresh="/`); | |
| 53 | colds.push(cold.statements); | |
| 67 | 54 | } |
| 68 | expect(median).toBeLessThan(APP_BUDGET_MS); | |
| 55 | // The first look after a write schedules the one-off redraw of the five regions, which runs on | |
| 56 | // this process's loop for the looks that follow; let it finish, so the looks count the steady | |
| 57 | // work of a look. | |
| 58 | write(-1); | |
| 59 | const marked = (await look(f.ownerCookie)).text; | |
| 60 | await Bun.sleep(1_000); | |
| 61 | const counts: number[] = []; | |
| 62 | for (let i = 0; i < LOOKS; i++) { | |
| 63 | write(i); | |
| 64 | const got = await look(f.ownerCookie); | |
| 65 | expect(got.res.status).toBe(200); | |
| 66 | counts.push(got.statements); | |
| 67 | // Answered from memory on every look, not only the first. | |
| 68 | expect(got.text).toContain('data-refresh="/live/home/feed"'); | |
| 69 | } | |
| 70 | const warm = medianCount(counts); | |
| 71 | expect(warm).toBeLessThanOrEqual(STATEMENT_BUDGET); | |
| 72 | // A look from memory is cheaper than a cold draw of the same page, so the budget means something. | |
| 73 | expect(warm).toBeLessThan(medianCount(colds)); | |
| 69 | 74 | for (const name of ["waiting", "panels", "asked", "feed"]) { |
| 70 | 75 | expect(marked).toContain(`id="home-${name}"`); |
| 71 | 76 | } |
test/security-sweep.test.ts +4 −4
| @@ -204,7 +204,7 @@ test("every registered route and verb, asked anonymously, answers with the floor | ||
| 204 | 204 | expect(statuses).toContain(404); |
| 205 | 205 | expect(statuses.some((s) => s >= 400 && s < 500 && s !== 404)).toBe(true); |
| 206 | 206 | expect(anonymous.some((s) => s.type.startsWith("text/event-stream"))).toBe(true); |
| 207 | }, 6_000); | |
| 207 | }); | |
| 208 | 208 | |
| 209 | 209 | test("an API path asked with a verb it does not take answers 405, naming the ones it does, with the floor", async () => { |
| 210 | 210 | const api = buildApiRouter(); |
| @@ -231,7 +231,7 @@ test("an API path asked with a verb it does not take answers 405, naming the one | ||
| 231 | 231 | .map(({ s }) => `${s.label} allow=${s.allow}`); |
| 232 | 232 | expect(wrongAnswers).toEqual([]); |
| 233 | 233 | expect(wrongMethod.at(-1)!.status).toBe(405); // the home page takes GET, so a PATCH is a wrong method, not a miss |
| 234 | }, 2_000); | |
| 234 | }); | |
| 235 | 235 | |
| 236 | 236 | test("a cross-origin mutation with a session cookie is refused before any handler, and the session lives on", async () => { |
| 237 | 237 | const mutations = routes().filter((r) => MUTATING.has(r.method)); |
| @@ -243,7 +243,7 @@ test("a cross-origin mutation with a session cookie is refused before any handle | ||
| 243 | 243 | expect(seen.filter((s) => s.cookies.length > 0).map((s) => `${s.label} sets ${s.cookies.map(cookieName).join(", ")}`)).toEqual([]); |
| 244 | 244 | const still = sessionOf(app.db, token); |
| 245 | 245 | expect(still.ok && still.value.user.username).toBe(USERNAME); |
| 246 | }, 15_000); | |
| 246 | }); | |
| 247 | 247 | |
| 248 | 248 | test("signing in from the forge's own page sets the session cookie HttpOnly, with the floor", async () => { |
| 249 | 249 | const form = await send( |
| @@ -261,7 +261,7 @@ test("signing in from the forge's own page sets the session cookie HttpOnly, wit | ||
| 261 | 261 | expect(notHttpOnly([s])).toEqual([]); |
| 262 | 262 | expect(s.missing).toEqual([]); |
| 263 | 263 | } |
| 264 | }, 1_000); | |
| 264 | }); | |
| 265 | 265 | |
| 266 | 266 | test("every named exception is still an exception, each with its reason", () => { |
| 267 | 267 | const all = [...anonymous, ...wrongMethod]; |
new above
offline
Nit: the detector is per line. A documentHeaders({\n scriptJs: s }) call split over lines is still read as owning, and a line that has both page({scriptJs}) and documentHeaders( is read as forwarding. Add a test for the multi-line form or match the scriptJs value inside the documentHeaders call. Also drop
export: nothing imports it from a test file.