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 #9264
torque/forge:torque/9249-each-look-records-whether-its-page-scrol → main 2 commits fork
7 files
changed +142
−12
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 | if (groups.length === 0) return ""; | |
| 124 | return [`vitals ${groups.join("; ")}`, ...sidewaysNotices(looks)].join(" · "); | |
| 124 | 125 | } |
src/checks/summary.ts +24 −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 its size. */ | |
| 227 | readonly sideways?: boolean; | |
| 228 | /** scrollHeight / innerHeight, one decimal; absent likewise. */ | |
| 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,19 @@ 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 per phone look that scrolls sideways, none for a page that | |
| 280 | * fits: `/preview-admin/sample/held scrolls sideways at phone width`. Words | |
| 281 | * only; no verdict, so nothing here can turn a check red. | |
| 282 | */ | |
| 283 | export function sidewaysNotices(looks: readonly LookVitals[]): readonly string[] { | |
| 284 | const out: string[] = []; | |
| 285 | for (const l of looks) { | |
| 286 | if ("skipped" in l || l.sideways !== true) continue; | |
| 287 | if (l.look !== "phone" && !l.look.startsWith("phone ")) continue; | |
| 288 | const page = l.look.slice("phone".length).trim() || "/"; | |
| 289 | out.push(`${page} scrolls sideways at phone width`); | |
| 290 | } | |
| 291 | return out; | |
| 292 | } |
src/looks/capture.ts +27 −1
| @@ -58,8 +58,23 @@ 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 once loaded; absent when the page did not say. */ | |
| 62 | readonly size?: PageSize; | |
| 61 | 63 | } |
| 62 | 64 | |
| 65 | /** The page's scroll size and the viewport's, in CSS pixels; each may be missing. */ | |
| 66 | export interface PageSize { | |
| 67 | readonly scrollWidth?: number; | |
| 68 | readonly scrollHeight?: number; | |
| 69 | readonly innerWidth?: number; | |
| 70 | readonly innerHeight?: number; | |
| 71 | } | |
| 72 | ||
| 73 | const SIZE_FIELDS = ["scrollWidth", "scrollHeight", "innerWidth", "innerHeight"] as const; | |
| 74 | ||
| 75 | /** Read after load, in the same evaluation that reads the entries back. */ | |
| 76 | const READ_BACK = `JSON.stringify({ ...(window.__forgeVitals ?? {}), size: (() => { const d = document.documentElement; return { scrollWidth: d.scrollWidth, scrollHeight: d.scrollHeight, innerWidth: window.innerWidth, innerHeight: window.innerHeight }; })() })`; | |
| 77 | ||
| 63 | 78 | /** |
| 64 | 79 | * What takes the picture: the real browser, or a test's stand-in. One that |
| 65 | 80 | * recorded nothing returns empty entries. |
| @@ -172,7 +187,18 @@ export function parseRecorded(raw: unknown): Recorded { | ||
| 172 | 187 | } |
| 173 | 188 | return out; |
| 174 | 189 | }; |
| 190 | const size: Record<string, number> = {}; | |
| 191 | const from = | |
| 192 | rec["size"] !== null && typeof rec["size"] === "object" | |
| 193 | ? (rec["size"] as Record<string, unknown>) | |
| 194 | : {}; | |
| 195 | for (const f of SIZE_FIELDS) { | |
| 196 | const v = from[f]; | |
| 197 | // Whole, non-negative numbers; anything else is absent, not a failure. | |
| 198 | if (finite(v) && Number.isInteger(v)) size[f] = v; | |
| 199 | } | |
| 175 | 200 | return { |
| 201 | ...(Object.keys(size).length > 0 ? { size: size as PageSize } : {}), | |
| 176 | 202 | entries: { |
| 177 | 203 | lcp: list("lcp", ["startTime", "size"], "startTime"), |
| 178 | 204 | layoutShift: list("layoutShift", ["startTime", "value", "hadRecentInput"], "value"), |
| @@ -470,7 +496,7 @@ async function shoot( | ||
| 470 | 496 | const read = await dt.send( |
| 471 | 497 | "Runtime.evaluate", |
| 472 | 498 | { |
| 473 | expression: "JSON.stringify(window.__forgeVitals ?? null)", | |
| 499 | expression: READ_BACK, | |
| 474 | 500 | returnByValue: true, |
| 475 | 501 | }, |
| 476 | 502 | sessionId, |
src/looks/service.ts +22 −2
| @@ -69,7 +69,7 @@ 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 | |
| @@ -207,6 +207,25 @@ interface Pending { | ||
| 207 | 207 | readonly swept?: true; |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | /** | |
| 211 | * Whether the page scrolls sideways and how many screens tall it is, from | |
| 212 | * what it measured; nothing when it did not say (or the viewport is zero). | |
| 213 | */ | |
| 214 | export function pageReach( | |
| 215 | size: PageSize | undefined, | |
| 216 | ): { sideways?: boolean; screens?: number } { | |
| 217 | const out: { sideways?: boolean; screens?: number } = {}; | |
| 218 | if (size?.scrollWidth !== undefined && size.innerWidth !== undefined) | |
| 219 | out.sideways = size.scrollWidth > size.innerWidth; | |
| 220 | if ( | |
| 221 | size?.scrollHeight !== undefined && | |
| 222 | size.innerHeight !== undefined && | |
| 223 | size.innerHeight > 0 | |
| 224 | ) | |
| 225 | out.screens = Math.round((size.scrollHeight / size.innerHeight) * 10) / 10; | |
| 226 | return out; | |
| 227 | } | |
| 228 | ||
| 210 | 229 | export function startLooks(app: App, options: LooksOptions): LooksHandle { |
| 211 | 230 | const { dataDir } = app.config; |
| 212 | 231 | const port = new URL(options.localUrl).port; |
| @@ -432,7 +451,8 @@ export function startLooks(app: App, options: LooksOptions): LooksHandle { | ||
| 432 | 451 | // The thumb is the desktop page again, as a stranger: it adds no row. |
| 433 | 452 | if (kind !== "thumb" && theme === "light") { |
| 434 | 453 | const look = page === "/" ? kind : `${kind} ${page}`; |
| 435 | if (shot.ok) vitals.push({ look, entries: shot.value.entries }); | |
| 454 | if (shot.ok) | |
| 455 | vitals.push({ look, entries: shot.value.entries, ...pageReach(shot.value.size) }); | |
| 436 | 456 | else if (isNoBrowser(shot.error)) |
| 437 | 457 | vitals.push({ look, skipped: "no browser" }); |
| 438 | 458 | } |
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 +55 −3
| @@ -30,8 +30,10 @@ import { setAppMembers } from "../src/core/app_members.ts"; | ||
| 30 | 30 | 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 | import { readLooks } from "../src/checks/looks.ts"; | |
| 34 | import { vitalsRows } from "../src/checks/summary.ts"; | |
| 33 | import { parseLooks, readLooks } from "../src/checks/looks.ts"; | |
| 34 | import { vitalsLine } from "../src/checks/progress.ts"; | |
| 35 | import { sidewaysNotices, vitalsRows } from "../src/checks/summary.ts"; | |
| 36 | import { vitalsTable } from "../src/web/studio/vitals.ts"; | |
| 35 | 37 | import { createUser, type User } from "../src/core/users.ts"; |
| 36 | 38 | import { serve, type Servers } from "../src/http/server.ts"; |
| 37 | 39 | import { err, ok } from "../src/kernel/result.ts"; |
| @@ -44,7 +46,7 @@ import { | ||
| 44 | 46 | type Capture, |
| 45 | 47 | type LookInput, |
| 46 | 48 | } from "../src/looks/capture.ts"; |
| 47 | import { startLooks, type LooksHandle } from "../src/looks/service.ts"; | |
| 49 | import { pageReach, startLooks, type LooksHandle } from "../src/looks/service.ts"; | |
| 48 | 50 | |
| 49 | 51 | quietLogs(); |
| 50 | 52 | |
| @@ -96,6 +98,10 @@ describe("the look", () => { | ||
| 96 | 98 | input.width === 390 |
| 97 | 99 | ? { lcp: [{ startTime: 3100, size: 10 }] } |
| 98 | 100 | : { layoutShift: [{ startTime: 100, value: 0.3 }] }, |
| 101 | size: | |
| 102 | input.width === 390 | |
| 103 | ? { scrollWidth: 500, scrollHeight: 2000, innerWidth: 390, innerHeight: 844 } | |
| 104 | : { scrollWidth: 1280, scrollHeight: 800, innerWidth: 1280, innerHeight: 800 }, | |
| 99 | 105 | }); |
| 100 | 106 | }; |
| 101 | 107 | |
| @@ -293,6 +299,8 @@ describe("the look", () => { | ||
| 293 | 299 | const recorded = readLooks(dataDir, check); |
| 294 | 300 | expect(recorded).toHaveLength(2); |
| 295 | 301 | expect(recorded.map((l) => l.look).sort()).toEqual(["desktop", "phone"]); |
| 302 | expect(recorded.find((l) => l.look === "phone")).toMatchObject({ sideways: true, screens: 2.4 }); | |
| 303 | expect(recorded.find((l) => l.look === "desktop")).toMatchObject({ sideways: false, screens: 1 }); | |
| 296 | 304 | const graded = vitalsRows(recorded); |
| 297 | 305 | expect(graded.find((g) => g.look === "phone")!.rows[0]).toMatchObject({ |
| 298 | 306 | vital: "LCP", |
| @@ -562,6 +570,50 @@ describe("the look", () => { | ||
| 562 | 570 | expect(parseRecorded("x").entries.lcp).toEqual([]); |
| 563 | 571 | }); |
| 564 | 572 | |
| 573 | test("the page's own size is kept as whole numbers, a missing or malformed one as absent", () => { | |
| 574 | expect( | |
| 575 | parseRecorded({ size: { scrollWidth: 500, scrollHeight: 1688, innerWidth: 390, innerHeight: 844 } }).size, | |
| 576 | ).toEqual({ scrollWidth: 500, scrollHeight: 1688, innerWidth: 390, innerHeight: 844 }); | |
| 577 | expect(parseRecorded({}).size).toBeUndefined(); | |
| 578 | expect(parseRecorded({ size: "wide" }).size).toBeUndefined(); | |
| 579 | expect( | |
| 580 | parseRecorded({ size: { scrollWidth: -1, scrollHeight: 1.5, innerWidth: "390", innerHeight: 844 } }).size, | |
| 581 | ).toEqual({ innerHeight: 844 }); | |
| 582 | }); | |
| 583 | ||
| 584 | test("sideways and screens are worked out from the size, one decimal", () => { | |
| 585 | expect(pageReach({ scrollWidth: 500, innerWidth: 390, scrollHeight: 2000, innerHeight: 844 })).toEqual({ | |
| 586 | sideways: true, | |
| 587 | screens: 2.4, | |
| 588 | }); | |
| 589 | expect(pageReach({ scrollWidth: 390, innerWidth: 390, scrollHeight: 844, innerHeight: 844 })).toEqual({ | |
| 590 | sideways: false, | |
| 591 | screens: 1, | |
| 592 | }); | |
| 593 | expect(pageReach(undefined)).toEqual({}); | |
| 594 | expect(pageReach({ scrollWidth: 400 })).toEqual({}); | |
| 595 | }); | |
| 596 | ||
| 597 | test("a phone look that scrolls sideways is named in one line, a page that fits says nothing", () => { | |
| 598 | const entries = { lcp: [], layoutShift: [], longtask: [] }; | |
| 599 | const looks = [ | |
| 600 | { look: "phone /preview-admin/sample/held", entries, sideways: true, screens: 1.2 }, | |
| 601 | { look: "phone", entries, sideways: false, screens: 1 }, | |
| 602 | { look: "desktop", entries, sideways: true, screens: 1 }, | |
| 603 | { look: "phone /x", entries }, | |
| 604 | ]; | |
| 605 | expect(sidewaysNotices(looks)).toEqual([ | |
| 606 | "/preview-admin/sample/held scrolls sideways at phone width", | |
| 607 | ]); | |
| 608 | expect(vitalsLine(looks)).toContain("/preview-admin/sample/held scrolls sideways at phone width"); | |
| 609 | expect(String(vitalsTable(looks))).toContain("scrolls sideways at phone width"); | |
| 610 | expect(sidewaysNotices(looks.slice(1))).toEqual([]); | |
| 611 | expect(vitalsLine(looks.slice(1))).not.toContain("sideways"); | |
| 612 | expect(String(vitalsTable(looks.slice(1)))).not.toContain("sideways"); | |
| 613 | // Survives the file a check reads back. | |
| 614 | expect(parseLooks(JSON.parse(JSON.stringify(looks)))[0]).toMatchObject({ sideways: true, screens: 1.2 }); | |
| 615 | }); | |
| 616 | ||
| 565 | 617 | test("a closed pull's pictures go with its preview", async () => { |
| 566 | 618 | const env = `pull-${unpictured.number}`; |
| 567 | 619 | expect(looksForEnv(app.db, record.id, env).length).toBeGreaterThan(0); |
new above
offline