Public: anyone can read this; the crew can change it.
Each look records whether its page scrolls sideways and how many screens tall it is, and the pull's looks name any phone page that scrolls sideways #9256
scout/forge:scout/9249-each-look-records-whether-its-page-scrol → main 3 commits fork
7 files
changed +139
−11
src/checks/looks.ts +8 −1
| @@ -48,7 +48,14 @@ export function parseLooks(raw: unknown): LookVitals[] { | ||
| 48 | 48 | const skipped = words(rec.skipped); |
| 49 | 49 | if (skipped !== null) out.push({ look, skipped }); |
| 50 | 50 | else if (rec.entries && typeof rec.entries === "object" && !Array.isArray(rec.entries)) |
| 51 | out.push({ look, entries: rec.entries }); | |
| 51 | out.push({ | |
| 52 | look, | |
| 53 | entries: rec.entries, | |
| 54 | ...(typeof rec.sideways === "boolean" ? { sideways: rec.sideways } : {}), | |
| 55 | ...(typeof rec.screens === "number" && Number.isFinite(rec.screens) && rec.screens >= 0 | |
| 56 | ? { screens: rec.screens } | |
| 57 | : {}), | |
| 58 | }); | |
| 52 | 59 | } |
| 53 | 60 | return out; |
| 54 | 61 | } |
src/checks/progress.ts +3 −2
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | |
| 10 | 10 | import type { CheckSummary } from "../core/runs.ts"; |
| 11 | 11 | import { readLooks } from "./looks.ts"; |
| 12 | import { checkSteps, stepsWords, vitalsRows, type LookVitals } from "./summary.ts"; | |
| 12 | import { checkSteps, sidewaysNotices, stepsWords, vitalsRows, type LookVitals } from "./summary.ts"; | |
| 13 | 13 | import { parseTestsRow, testsRowLine } from "./tests-row.ts"; |
| 14 | 14 | import { readRunLog } from "../runs/log.ts"; |
| 15 | 15 | |
| @@ -120,5 +120,6 @@ export function vitalsLine(looks: readonly LookVitals[]): string { | ||
| 120 | 120 | ? `${g.look}: skipped: ${g.skipped}` |
| 121 | 121 | : `${g.look}: ${g.rows.map((r) => `${r.vital} ${r.value}${r.grade === "n/a" ? "" : ` ${r.grade}`}`).join(", ")}`, |
| 122 | 122 | ); |
| 123 | return groups.length === 0 ? "" : `vitals ${groups.join("; ")}`; | |
| 123 | const said = groups.length === 0 ? "" : `vitals ${groups.join("; ")}`; | |
| 124 | return [said, ...sidewaysNotices(looks)].filter((w) => w !== "").join(" · "); | |
| 124 | 125 | } |
src/checks/summary.ts +23 −1
| @@ -220,7 +220,14 @@ export function stepsWords(steps: readonly CheckStep[]): string { | ||
| 220 | 220 | |
| 221 | 221 | /** One look's recorded entries, or why it has none; plain data from the caller. */ |
| 222 | 222 | export type LookVitals = |
| 223 | | { readonly look: string; readonly entries: Entries } | |
| 223 | | { | |
| 224 | readonly look: string; | |
| 225 | readonly entries: Entries; | |
| 226 | /** `scrollWidth > innerWidth`; absent when the page did not say. */ | |
| 227 | readonly sideways?: boolean; | |
| 228 | /** `scrollHeight / innerHeight`, one decimal; absent when unknown. */ | |
| 229 | readonly screens?: number; | |
| 230 | } | |
| 224 | 231 | | { readonly look: string; readonly skipped: string }; |
| 225 | 232 | |
| 226 | 233 | /** One cell row of a look's table: words only, escaped where they are rendered. */ |
| @@ -267,3 +274,18 @@ export function vitalsRows(looks: readonly LookVitals[]): readonly LookRows[] { | ||
| 267 | 274 | return { look: l.look, skipped: "", rows }; |
| 268 | 275 | }); |
| 269 | 276 | } |
| 277 | ||
| 278 | /** | |
| 279 | * One plain line for each phone look whose page scrolls sideways, e.g. | |
| 280 | * `/preview-admin/sample/held scrolls sideways at phone width`. A page that | |
| 281 | * fits, or one that did not say, adds nothing; no verdict. | |
| 282 | */ | |
| 283 | export function sidewaysNotices(looks: readonly LookVitals[]): string[] { | |
| 284 | const out: string[] = []; | |
| 285 | for (const l of looks) { | |
| 286 | if (!("entries" in l) || l.sideways !== true) continue; | |
| 287 | if (l.look !== "phone" && !l.look.startsWith("phone ")) continue; | |
| 288 | out.push(`${l.look.slice(6).trim() || "/"} scrolls sideways at phone width`); | |
| 289 | } | |
| 290 | return out; | |
| 291 | } |
src/looks/capture.ts +30 −1
| @@ -58,8 +58,20 @@ export interface LookInput { | ||
| 58 | 58 | /** What the page recorded while it loaded; a type the browser lacks is an empty list. */ |
| 59 | 59 | export interface Recorded { |
| 60 | 60 | readonly entries: Entries; |
| 61 | /** The page's own size after load; a number the page did not give is absent. */ | |
| 62 | readonly page?: PageSize | undefined; | |
| 61 | 63 | } |
| 62 | 64 | |
| 65 | /** `document.documentElement.scroll*` and the viewport, in whole pixels. */ | |
| 66 | export interface PageSize { | |
| 67 | readonly scrollWidth?: number; | |
| 68 | readonly scrollHeight?: number; | |
| 69 | readonly innerWidth?: number; | |
| 70 | readonly innerHeight?: number; | |
| 71 | } | |
| 72 | ||
| 73 | const PAGE_FIELDS = ["scrollWidth", "scrollHeight", "innerWidth", "innerHeight"] as const; | |
| 74 | ||
| 63 | 75 | /** |
| 64 | 76 | * What takes the picture: the real browser, or a test's stand-in. One that |
| 65 | 77 | * recorded nothing returns empty entries. |
| @@ -137,6 +149,13 @@ const OBSERVER = `(() => { | ||
| 137 | 149 | watch("longtask", "longtask", (e) => ({ startTime: e.startTime, duration: e.duration })); |
| 138 | 150 | })()`; |
| 139 | 151 | |
| 152 | /** Read back after load, in the one evaluation: the entries and the page's size. */ | |
| 153 | const PAGE_READ = `JSON.stringify(window.__forgeVitals ? Object.assign({}, window.__forgeVitals, { page: { | |
| 154 | scrollWidth: document.documentElement.scrollWidth, | |
| 155 | scrollHeight: document.documentElement.scrollHeight, | |
| 156 | innerWidth: window.innerWidth, | |
| 157 | innerHeight: window.innerHeight } }) : null)`; | |
| 158 | ||
| 140 | 159 | const finite = (v: unknown): v is number => |
| 141 | 160 | typeof v === "number" && Number.isFinite(v) && v >= 0; |
| 142 | 161 | |
| @@ -172,7 +191,17 @@ export function parseRecorded(raw: unknown): Recorded { | ||
| 172 | 191 | } |
| 173 | 192 | return out; |
| 174 | 193 | }; |
| 194 | const size: Record<string, number> = {}; | |
| 195 | const given = | |
| 196 | rec["page"] !== null && typeof rec["page"] === "object" | |
| 197 | ? (rec["page"] as Record<string, unknown>) | |
| 198 | : {}; | |
| 199 | for (const f of PAGE_FIELDS) { | |
| 200 | const v = given[f]; | |
| 201 | if (finite(v) && Number.isInteger(v)) size[f] = v; | |
| 202 | } | |
| 175 | 203 | return { |
| 204 | ...(Object.keys(size).length > 0 ? { page: size as PageSize } : {}), | |
| 176 | 205 | entries: { |
| 177 | 206 | lcp: list("lcp", ["startTime", "size"], "startTime"), |
| 178 | 207 | layoutShift: list("layoutShift", ["startTime", "value", "hadRecentInput"], "value"), |
| @@ -470,7 +499,7 @@ async function shoot( | ||
| 470 | 499 | const read = await dt.send( |
| 471 | 500 | "Runtime.evaluate", |
| 472 | 501 | { |
| 473 | expression: "JSON.stringify(window.__forgeVitals ?? null)", | |
| 502 | expression: PAGE_READ, | |
| 474 | 503 | returnByValue: true, |
| 475 | 504 | }, |
| 476 | 505 | sessionId, |
src/looks/service.ts +28 −2
| @@ -69,10 +69,32 @@ import { createLog } from "../kernel/log.ts"; | ||
| 69 | 69 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 70 | 70 | import { writeCommitLooks } from "../checks/looks.ts"; |
| 71 | 71 | import type { LookVitals } from "../checks/summary.ts"; |
| 72 | import { findChromium, isNoBrowser, takeLook, type Capture } from "./capture.ts"; | |
| 72 | import { findChromium, isNoBrowser, takeLook, type Capture, type PageSize } from "./capture.ts"; | |
| 73 | 73 | |
| 74 | 74 | const log = createLog("looks"); |
| 75 | 75 | |
| 76 | const tenth = (n: number): number => Math.round(n * 10) / 10; | |
| 77 | ||
| 78 | /** | |
| 79 | * What the page's size says: whether it scrolls sideways and how many | |
| 80 | * screens tall it is. Each is absent when its numbers were not given. | |
| 81 | */ | |
| 82 | export function pageFacts(page: PageSize | undefined): { | |
| 83 | sideways?: boolean; | |
| 84 | screens?: number; | |
| 85 | } { | |
| 86 | const out: { sideways?: boolean; screens?: number } = {}; | |
| 87 | if (page?.scrollWidth !== undefined && page.innerWidth !== undefined) | |
| 88 | out.sideways = page.scrollWidth > page.innerWidth; | |
| 89 | if ( | |
| 90 | page?.scrollHeight !== undefined && | |
| 91 | page.innerHeight !== undefined && | |
| 92 | page.innerHeight > 0 | |
| 93 | ) | |
| 94 | out.screens = tenth(page.scrollHeight / page.innerHeight); | |
| 95 | return out; | |
| 96 | } | |
| 97 | ||
| 76 | 98 | export interface LooksOptions { |
| 77 | 99 | /** The forge's own listener, plain http: where the browser is pointed. */ |
| 78 | 100 | readonly localUrl: string; |
| @@ -432,7 +454,11 @@ export function startLooks(app: App, options: LooksOptions): LooksHandle { | ||
| 432 | 454 | // The thumb is the desktop page again, as a stranger: it adds no row. |
| 433 | 455 | if (kind !== "thumb" && theme === "light") { |
| 434 | 456 | const look = page === "/" ? kind : `${kind} ${page}`; |
| 435 | if (shot.ok) vitals.push({ look, entries: shot.value.entries }); | |
| 457 | if (shot.ok) vitals.push({ | |
| 458 | look, | |
| 459 | entries: shot.value.entries, | |
| 460 | ...pageFacts(shot.value.page), | |
| 461 | }); | |
| 436 | 462 | else if (isNoBrowser(shot.error)) |
| 437 | 463 | vitals.push({ look, skipped: "no browser" }); |
| 438 | 464 | } |
src/web/studio/vitals.ts +3 −2
| @@ -4,13 +4,14 @@ | ||
| 4 | 4 | * look recorded anything. |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | import { vitalsRows, type LookVitals } from "../../checks/summary.ts"; | |
| 7 | import { sidewaysNotices, vitalsRows, type LookVitals } from "../../checks/summary.ts"; | |
| 8 | 8 | import { html, type Html } from "../html.ts"; |
| 9 | 9 | |
| 10 | 10 | export function vitalsTable(looks: readonly LookVitals[]): Html { |
| 11 | 11 | const groups = vitalsRows(looks); |
| 12 | 12 | if (groups.length === 0) return html``; |
| 13 | return html`<div class="table-scroll" tabindex="0" role="region" aria-label="Web vitals per look"> | |
| 13 | const notices = sidewaysNotices(looks); | |
| 14 | return html`${notices.map((n) => html`<p>${n}</p>`)}<div class="table-scroll" tabindex="0" role="region" aria-label="Web vitals per look"> | |
| 14 | 15 | <table class="table" data-dense> |
| 15 | 16 | <thead><tr><th>Look</th><th>Vital</th><th>Value</th><th>Thresholds</th><th>Grade</th></tr></thead> |
| 16 | 17 | <tbody> |
test/looks.test.ts +44 −2
| @@ -31,7 +31,7 @@ import type { Pull } from "../src/core/pulls.ts"; | ||
| 31 | 31 | import type { Repo } from "../src/core/repos.ts"; |
| 32 | 32 | import { createRun, listRuns, type Run } from "../src/core/runs.ts"; |
| 33 | 33 | import { readLooks } from "../src/checks/looks.ts"; |
| 34 | import { vitalsRows } from "../src/checks/summary.ts"; | |
| 34 | import { sidewaysNotices, vitalsRows } from "../src/checks/summary.ts"; | |
| 35 | 35 | import { createUser, type User } from "../src/core/users.ts"; |
| 36 | 36 | import { serve, type Servers } from "../src/http/server.ts"; |
| 37 | 37 | import { err, ok } from "../src/kernel/result.ts"; |
| @@ -44,7 +44,9 @@ import { | ||
| 44 | 44 | type Capture, |
| 45 | 45 | type LookInput, |
| 46 | 46 | } from "../src/looks/capture.ts"; |
| 47 | import { startLooks, type LooksHandle } from "../src/looks/service.ts"; | |
| 47 | import { vitalsLine } from "../src/checks/progress.ts"; | |
| 48 | import { vitalsTable } from "../src/web/studio/vitals.ts"; | |
| 49 | import { pageFacts, startLooks, type LooksHandle } from "../src/looks/service.ts"; | |
| 48 | 50 | |
| 49 | 51 | quietLogs(); |
| 50 | 52 | |
| @@ -92,6 +94,10 @@ describe("the look", () => { | ||
| 92 | 94 | writeFileSync(input.out, input.width === 1280 ? DESKTOP_PNG : PNG); |
| 93 | 95 | // What the page recorded: a slow paint for the phone, a shift for the desktop. |
| 94 | 96 | return ok({ |
| 97 | page: | |
| 98 | input.width === 390 | |
| 99 | ? { scrollWidth: 500, scrollHeight: 2000, innerWidth: 390, innerHeight: 800 } | |
| 100 | : { scrollWidth: 1280, scrollHeight: 800, innerWidth: 1280, innerHeight: 800 }, | |
| 95 | 101 | entries: |
| 96 | 102 | input.width === 390 |
| 97 | 103 | ? { lcp: [{ startTime: 3100, size: 10 }] } |
| @@ -294,6 +300,10 @@ describe("the look", () => { | ||
| 294 | 300 | expect(recorded).toHaveLength(2); |
| 295 | 301 | expect(recorded.map((l) => l.look).sort()).toEqual(["desktop", "phone"]); |
| 296 | 302 | const graded = vitalsRows(recorded); |
| 303 | // The page's own size is stored beside the vitals. | |
| 304 | expect(recorded.find((l) => l.look === "phone")).toMatchObject({ sideways: true, screens: 2.5 }); | |
| 305 | expect(recorded.find((l) => l.look === "desktop")).toMatchObject({ sideways: false, screens: 1 }); | |
| 306 | expect(sidewaysNotices(recorded)).toEqual(["/ scrolls sideways at phone width"]); | |
| 297 | 307 | expect(graded.find((g) => g.look === "phone")!.rows[0]).toMatchObject({ |
| 298 | 308 | vital: "LCP", |
| 299 | 309 | value: "3100 ms", |
| @@ -562,6 +572,38 @@ describe("the look", () => { | ||
| 562 | 572 | expect(parseRecorded("x").entries.lcp).toEqual([]); |
| 563 | 573 | }); |
| 564 | 574 | |
| 575 | test("the page's size is kept as whole numbers, a missing or malformed one as absent", () => { | |
| 576 | const size = { scrollWidth: 500, scrollHeight: 2000, innerWidth: 390, innerHeight: 800 }; | |
| 577 | expect(parseRecorded({ page: size }).page).toEqual(size); | |
| 578 | expect(parseRecorded({}).page).toBeUndefined(); | |
| 579 | expect(parseRecorded({ page: "x" }).page).toBeUndefined(); | |
| 580 | expect( | |
| 581 | parseRecorded({ page: { scrollWidth: "500", scrollHeight: -1, innerWidth: 390.5, innerHeight: 800 } }).page, | |
| 582 | ).toEqual({ innerHeight: 800 }); | |
| 583 | expect(pageFacts({ scrollWidth: 391, innerWidth: 390, scrollHeight: 1000, innerHeight: 800 })).toEqual({ | |
| 584 | sideways: true, | |
| 585 | screens: 1.3, | |
| 586 | }); | |
| 587 | expect(pageFacts({ scrollWidth: 390, innerWidth: 390 })).toEqual({ sideways: false }); | |
| 588 | expect(pageFacts(undefined)).toEqual({}); | |
| 589 | }); | |
| 590 | ||
| 591 | test("a sideways phone look is named in one plain line, a page that fits says nothing", () => { | |
| 592 | const entries = {}; | |
| 593 | const looks = [ | |
| 594 | { look: "phone /preview-admin/sample/held", entries, sideways: true, screens: 3 }, | |
| 595 | { look: "phone /ok", entries, sideways: false, screens: 1 }, | |
| 596 | { look: "desktop /wide", entries, sideways: true }, | |
| 597 | { look: "phone /unknown", entries }, | |
| 598 | ]; | |
| 599 | expect(sidewaysNotices(looks)).toEqual(["/preview-admin/sample/held scrolls sideways at phone width"]); | |
| 600 | expect(sidewaysNotices(looks.slice(1))).toEqual([]); | |
| 601 | expect(vitalsLine(looks)).toContain("/preview-admin/sample/held scrolls sideways at phone width"); | |
| 602 | expect(vitalsLine(looks.slice(1))).not.toContain("sideways"); | |
| 603 | expect(vitalsTable(looks).text).toContain("/preview-admin/sample/held scrolls sideways at phone width"); | |
| 604 | expect(vitalsTable(looks.slice(1)).text).not.toContain("sideways"); | |
| 605 | }); | |
| 606 | ||
| 565 | 607 | test("a closed pull's pictures go with its preview", async () => { |
| 566 | 608 | const env = `pull-${unpictured.number}`; |
| 567 | 609 | expect(looksForEnv(app.db, record.id, env).length).toBeGreaterThan(0); |
new above
offline