Public: anyone can read this; the crew can change it.
A pull's check summary carries the phone look of /preview-admin/sample/settings#move #10346
mason/forge:mason/10336-a-pull-s-check-summary-carries-the-phone → main 10 commits fork 2 threads · 2 unresolved
16 files
changed +321
−33
docs/features/pull-looks.md +6
| @@ -31,3 +31,9 @@ they may enter, and not those of a sealed pull. A look that failed (no | ||
| 31 | 31 | picture) is never listed. Only the pull's own preview environment is read, so |
| 32 | 32 | another pull's or app's looks never appear. A pull with no app or no looks |
| 33 | 33 | answers `items: []`. |
| 34 | ||
| 35 | ## The check's vitals looks | |
| 36 | ||
| 37 | Looks are taken for this repository's pulls, as for every app's: each time a pull's preview starts serving a head (`app.serving`), the looks service on the forge queues that head and pictures it, one environment at a time. A preview of the forge's own repository takes every page forge.json lists, then every named owner look (`PREVIEW_OWNER_LOOKS` in `src/spawn/preview-looks.ts`), at desk and phone width; `phone /preview-admin/sample/settings#move` is one of them. A head is not pictured when its preview has moved on or gone to sleep before its turn, or when it waited more than ten minutes in the queue (memory pressure holds the queue): the next deploy of the pull queues it again. | |
| 38 | ||
| 39 | Each look's vitals and size are filed under the commit as it is taken (`writeCommitLooks`, `src/checks/looks.ts`), so `looks` of `GET …/pulls/:number/checks` (fields in [](vitals-screens.md)) lists what is taken so far, whether the check went green before the pictures or after; the list is complete once they are. The file holds every look one preview takes (`FILED_LOOKS_MAX`, `src/core/look_limits.ts`); before #10336 it kept the first twenty, the forge.json pages, and no owner look was ever listed. |
docs/features/vitals-screens.md +5 −1
| @@ -7,6 +7,10 @@ A check's per-look vitals say how many screens tall each look is (`scrollHeight | ||
| 7 | 7 | |
| 8 | 8 | Report-only: neither can turn a check red. Skipped looks show no screens. |
| 9 | 9 | |
| 10 | API: `GET /api/v1/repos/:owner/:name/commits/:oid/check` and `GET /api/v1/repos/:owner/:name/pulls/:number/checks` carry `looks: [{look, screens, sideways}]`, from the same file as the run page (a run with no file of its own falls back to the commit's filed looks). `screens` is the one-decimal string (`""` without a size), `sideways` is a boolean; `[]` when there are none or the reader may not read runs. | |
| 10 | API: `GET /api/v1/repos/:owner/:name/commits/:oid/check` and `GET /api/v1/repos/:owner/:name/pulls/:number/checks` carry `looks: [{look, screens, sideways, size?}]`, from the same file as the run page (a run with no file of its own falls back to the commit's filed looks). `screens` is the one-decimal string (`""` without a size), `sideways` is a boolean; `[]` when there are none or the reader may not read runs. | |
| 11 | 11 | |
| 12 | 12 | The held page (`/preview-admin/sample/held`) is measured as an owner look (`PREVIEW_OWNER_LOOKS`), not a visitor look, because the visitor cap (`LOOKS_MAX`, 24) is spent; the check lists it at desktop and phone with its screens (#9508). |
| 13 | ||
| 14 | `size` is `{scrollWidth, innerWidth}` as the browser measured it (absent when none was recorded), so a pull can quote the width of a phone look, e.g. `phone /preview-admin/sample/settings#move` (#10336). The terminal line carries it too, on each phone look: `, 390/390 px wide` (page width / window width). | |
| 15 | ||
| 16 | Which looks a check lists, and when, is in [](pull-looks.md#the-checks-vitals-looks). |
src/actions/runs.ts +8 −1
| @@ -248,6 +248,8 @@ export interface LookFigures { | ||
| 248 | 248 | readonly look: string; |
| 249 | 249 | readonly screens: string; |
| 250 | 250 | readonly sideways: boolean; |
| 251 | /** The width the page needed against the window it was taken in; absent when none was recorded. */ | |
| 252 | readonly size?: { readonly scrollWidth: number; readonly innerWidth: number }; | |
| 251 | 253 | } |
| 252 | 254 | |
| 253 | 255 | /** |
| @@ -262,7 +264,12 @@ export function looksOfCheck( | ||
| 262 | 264 | ): LookFigures[] { |
| 263 | 265 | if (!mayReadRuns(app, actor, repo)) return []; |
| 264 | 266 | const looks = readLooks(app.config.dataDir, { id: run.id, repoId: repo.id, commitOid: run.commitOid }); |
| 265 | return vitalsRows(looks).map((g) => ({ look: g.look, screens: g.screens, sideways: g.sideways })); | |
| 267 | return vitalsRows(looks).map((g) => ({ | |
| 268 | look: g.look, | |
| 269 | screens: g.screens, | |
| 270 | sideways: g.sideways, | |
| 271 | ...(g.width === undefined ? {} : { size: g.width }), | |
| 272 | })); | |
| 266 | 273 | } |
| 267 | 274 | |
| 268 | 275 | /** The own green or red verdicts of a page of commits, for whoever may read the runs; empty otherwise. */ |
src/apps/manifest.ts +1 −4
| @@ -17,6 +17,7 @@ import type { DeclaredLimits } from "./contain.ts"; | ||
| 17 | 17 | import { isPublicAddress } from "./egress.ts"; |
| 18 | 18 | import { isLaunch, LAUNCHES, type Launch } from "../git/builds.ts"; |
| 19 | 19 | import { parseAdapter, type AdapterSpec } from "./adapter.ts"; |
| 20 | import { LOOKS_MAX } from "../core/look_limits.ts"; | |
| 20 | 21 | |
| 21 | 22 | export interface AppManifest { |
| 22 | 23 | /** The command that serves on `$PORT`. Run through `sh -c`, `exec`-ed. */ |
| @@ -93,10 +94,6 @@ export type ManifestError = | ||
| 93 | 94 | const READY_DEFAULT = 30; |
| 94 | 95 | const READY_MAX = 600; |
| 95 | 96 | const COMMAND_MAX = 1_000; |
| 96 | /** More than this many pages is a manifest error: a browser start per | |
| 97 | * picture per width is the heaviest thing the forge does. Room for every | |
| 98 | * top-level page of the forge itself. */ | |
| 99 | const LOOKS_MAX = 24; | |
| 100 | 97 | const LOOK_PATH_MAX = 200; |
| 101 | 98 | |
| 102 | 99 | /** |
src/checks/looks.ts +11 −7
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs"; |
| 12 | 12 | import { join } from "node:path"; |
| 13 | 13 | import type { LookVitals, PageSize } from "./summary.ts"; |
| 14 | import { FILED_LOOKS_MAX } from "../core/look_limits.ts"; | |
outdated
A checks module importing from apps/manifest and spawn/preview-looks couples layers just for two constants. Move the constants to a small shared module. |
||
| 14 | 15 | import { runsRoot } from "../runs/log.ts"; |
| 15 | 16 | |
| 16 | 17 | export const looksPath = (dataDir: string, runId: string): string => |
| @@ -29,8 +30,8 @@ export const commitLooksPath = ( | ||
| 29 | 30 | |
| 30 | 31 | const SAFE = /^[0-9A-Za-z]{1,64}$/; |
| 31 | 32 | |
| 32 | const MAX_BYTES = 1024 * 1024; | |
| 33 | const MAX_LOOKS = 20; | |
| 33 | const MAX_LOOKS = FILED_LOOKS_MAX; | |
| 34 | const MAX_BYTES = MAX_LOOKS * 64 * 1024; | |
| 34 | 35 | const MAX_WORDS = 80; |
| 35 | 36 | |
| 36 | 37 | const words = (v: unknown): string | null => |
| @@ -94,10 +95,11 @@ export function readLooks(dataDir: string, run: LooksOf): LookVitals[] { | ||
| 94 | 95 | } |
| 95 | 96 | |
| 96 | 97 | /** |
| 97 | * File a commit's looks, replacing any earlier set for it: written whole | |
| 98 | * beside the log and moved in, so a reader never sees half of it. What | |
| 99 | * `readLooks` would drop is not written, and a set with nothing left removes | |
| 100 | * the earlier file, so a stale table never outlives its replacement. | |
| 98 | * File a commit's looks, written whole beside the log and moved in, so a | |
| 99 | * reader never sees half of it. A look filed before keeps its place unless | |
| 100 | * `looks` names it again, so the set can be filed as it grows and a run that | |
| 101 | * resumes adds to it. What `readLooks` would drop is not written; a set with | |
| 102 | * nothing to keep removes the file. | |
| 101 | 103 | */ |
| 102 | 104 | export function writeCommitLooks( |
| 103 | 105 | dataDir: string, |
| @@ -107,7 +109,9 @@ export function writeCommitLooks( | ||
| 107 | 109 | ): void { |
| 108 | 110 | if (!SAFE.test(commitOid) || !SAFE.test(repoId)) return; |
| 109 | 111 | const path = commitLooksPath(dataDir, repoId, commitOid); |
| 110 | const kept = parseLooks(looks); | |
| 112 | const fresh = parseLooks(looks); | |
| 113 | const named = new Set(fresh.map((l) => l.look)); | |
| 114 | const kept = parseLooks([...readFile(path).filter((l) => !named.has(l.look)), ...fresh]); | |
| 111 | 115 | if (kept.length === 0) { |
| 112 | 116 | rmSync(path, { force: true }); |
| 113 | 117 | return; |
src/checks/progress.ts +7 −3
| @@ -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, sidewaysLine, stepsWords, vitalsRows, type LookVitals } from "./summary.ts"; | |
| 12 | import { checkSteps, isPhoneLook, sidewaysLine, 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 | |
| @@ -113,13 +113,17 @@ export function stepsLine( | ||
| 113 | 113 | return [steps, tests, vitalsLine(readLooks(dataDir, run))].filter((w) => w !== "").join(" · "); |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | /** `vitals home: LCP 3100 ms needs-improvement, CLS 0.3 poor, TBT 100 ms good; mobile: skipped: no browser` */ | |
| 116 | /** | |
| 117 | * `vitals home: LCP 3100 ms needs-improvement, CLS 0.3 poor, TBT 100 ms good; mobile: skipped: no browser`; | |
| 118 | * a phone look with a size adds `, 390/390 px wide` (page width / window width). | |
| 119 | */ | |
| 117 | 120 | export function vitalsLine(looks: readonly LookVitals[]): string { |
| 118 | 121 | const rows = vitalsRows(looks); |
| 119 | 122 | const groups = rows.map((g) => { |
| 120 | 123 | if (g.skipped !== "") return `${g.look}: skipped: ${g.skipped}`; |
| 121 | 124 | const vitals = g.rows.map((r) => `${r.vital} ${r.value}${r.grade === "n/a" ? "" : ` ${r.grade}`}`); |
| 122 | return `${g.look}: ${vitals.join(", ")}${g.screens === "" ? "" : `, ${g.screens} screens`}`; | |
| 125 | const wide = g.width === undefined || !isPhoneLook(g.look) ? "" : `, ${g.width.scrollWidth}/${g.width.innerWidth} px wide`; | |
| 126 | return `${g.look}: ${vitals.join(", ")}${g.screens === "" ? "" : `, ${g.screens} screens`}${wide}`; | |
| 123 | 127 | }); |
| 124 | 128 | if (groups.length === 0) return ""; |
| 125 | 129 | const sideways = sidewaysLine(rows); |
src/checks/summary.ts +7 −3
| @@ -243,9 +243,12 @@ export interface LookRows { | ||
| 243 | 243 | readonly screens: string; |
| 244 | 244 | /** A phone look whose page is wider than its window; false without a size. */ |
| 245 | 245 | readonly sideways: boolean; |
| 246 | /** The width the page needed against its window, from this look's own size; absent without one. */ | |
| 247 | readonly width?: { readonly scrollWidth: number; readonly innerWidth: number }; | |
| 246 | 248 | } |
| 247 | 249 | |
| 248 | const isPhone = (look: string): boolean => look === "phone" || look.startsWith("phone "); | |
| 250 | /** A look taken at phone width: `phone`, or `phone <page>`. */ | |
| 251 | export const isPhoneLook = (look: string): boolean => look === "phone" || look.startsWith("phone "); | |
| 249 | 252 | |
| 250 | 253 | /** `phone /x scrolls sideways` for each phone look wider than its window; empty when none. */ |
| 251 | 254 | export function sidewaysLine(groups: readonly LookRows[]): string { |
| @@ -283,7 +286,8 @@ export function vitalsRows(looks: readonly LookVitals[]): readonly LookRows[] { | ||
| 283 | 286 | const z = l.size; |
| 284 | 287 | const tall = z === undefined || z.innerHeight <= 0 ? null : z.scrollHeight / z.innerHeight; |
| 285 | 288 | const screens = tall === null ? "" : (Math.round(tall * 10) / 10).toFixed(1); |
| 286 | const sideways = z !== undefined && isPhone(l.look) && z.scrollWidth > z.innerWidth; | |
| 287 | return { look: l.look, skipped: "", rows, screens, sideways }; | |
| 289 | const sideways = z !== undefined && isPhoneLook(l.look) && z.scrollWidth > z.innerWidth; | |
| 290 | const width = z === undefined ? {} : { width: { scrollWidth: z.scrollWidth, innerWidth: z.innerWidth } }; | |
| 291 | return { look: l.look, skipped: "", rows, screens, sideways, ...width }; | |
| 288 | 292 | }); |
| 289 | 293 | } |
src/core/look_limits.ts added +23
| @@ -0,0 +1,23 @@ | ||
| 1 | /** | |
| 2 | * How many looks there may be: the pages forge.json may name, and the looks | |
| 3 | * one commit may file for its check. Plain numbers, no imports, so the | |
| 4 | * manifest parser and the check's looks file read one value each without | |
| 5 | * reaching into each other's layer. | |
| 6 | */ | |
| 7 | ||
| 8 | /** | |
| 9 | * More than this many pages in `app.looks` is a manifest error: a browser | |
| 10 | * start per picture per width is the heaviest thing the forge does. Room | |
| 11 | * for every top-level page of the forge itself. | |
| 12 | */ | |
| 13 | export const LOOKS_MAX = 24; | |
| 14 | ||
| 15 | /** | |
| 16 | * The most looks one commit files for its check's summary. A preview of the | |
| 17 | * forge takes every `LOOKS_MAX` page and every named owner look | |
| 18 | * (`spawn/preview-looks.ts`), each at desk and phone, so this must hold | |
| 19 | * both; `test/look-limits.test.ts` fails when the named list outgrows it. | |
| 20 | * Owner looks are taken after the forge.json pages, so a cap below the sum | |
| 21 | * silently drops them, the move look among them (#10336). | |
| 22 | */ | |
| 23 | export const FILED_LOOKS_MAX = 128; |
src/looks/service.ts +14 −8
| @@ -410,6 +410,17 @@ export function startLooks(app: App, options: LooksOptions): LooksHandle { | ||
| 410 | 410 | let allOk = true; |
| 411 | 411 | // What each look's page recorded, for the check summary's vitals table. |
| 412 | 412 | const vitals: LookVitals[] = []; |
| 413 | // Filed under the commit: the check of it shows them whenever it exists. | |
| 414 | let filed = 0; | |
| 415 | const fileVitals = (): void => { | |
| 416 | if (vitals.length === filed) return; | |
| 417 | filed = vitals.length; | |
| 418 | try { | |
| 419 | writeCommitLooks(dataDir, record.repoId, item.commitOid, vitals); | |
| 420 | } catch (error) { | |
| 421 | log.warn("vitals not recorded", { commit: item.commitOid, error: String(error) }); | |
| 422 | } | |
| 423 | }; | |
| 413 | 424 | for (const { page, kind, theme } of wanted) { |
| 414 | 425 | if (stopped) return; |
| 415 | 426 | const token = signGrant( |
| @@ -446,6 +457,9 @@ export function startLooks(app: App, options: LooksOptions): LooksHandle { | ||
| 446 | 457 | vitals.push({ look, entries: shot.value.entries, ...size }); |
| 447 | 458 | } else if (isNoBrowser(shot.error)) |
| 448 | 459 | vitals.push({ look, skipped: "no browser" }); |
| 460 | // Filed as each look is taken: the check turns green long before the | |
| 461 | // last of fifty pictures, and its summary lists what is taken so far. | |
| 462 | fileVitals(); | |
| 449 | 463 | } |
| 450 | 464 | const taken = |
| 451 | 465 | shot.ok && kind === "thumb" ? reduceToThumb(staged) : shot; |
| @@ -491,14 +505,6 @@ export function startLooks(app: App, options: LooksOptions): LooksHandle { | ||
| 491 | 505 | recordLook(app.db, { ...row, kind, path: "", error: taken.error }); |
| 492 | 506 | } |
| 493 | 507 | } |
| 494 | // Filed under the commit: the check of it shows them whenever it exists. | |
| 495 | if (vitals.length > 0) { | |
| 496 | try { | |
| 497 | writeCommitLooks(dataDir, record.repoId, item.commitOid, vitals); | |
| 498 | } catch (error) { | |
| 499 | log.warn("vitals not recorded", { commit: item.commitOid, error: String(error) }); | |
| 500 | } | |
| 501 | } | |
| 502 | 508 | // A full set at this head is the environment's look now; the pictures |
| 503 | 509 | // of heads before it have nothing left to say. |
| 504 | 510 | if (allOk) |
test/check-looks-api.test.ts +32 −3
| @@ -7,10 +7,11 @@ import { join } from "node:path"; | ||
| 7 | 7 | import { createApp, type App } from "../src/app.ts"; |
| 8 | 8 | import { openPull } from "../src/actions/pulls.ts"; |
| 9 | 9 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; |
| 10 | import { looksPath } from "../src/checks/looks.ts"; | |
| 10 | import { looksPath, writeCommitLooks } from "../src/checks/looks.ts"; | |
| 11 | 11 | import { claimRun, createRun, finishRun } from "../src/core/runs.ts"; |
| 12 | 12 | import { createSession } from "../src/core/sessions.ts"; |
| 13 | 13 | import { createUser } from "../src/core/users.ts"; |
| 14 | import { PREVIEW_OWNER_LOOKS } from "../src/spawn/preview-looks.ts"; | |
| 14 | 15 | import { serve, type Servers } from "../src/http/server.ts"; |
| 15 | 16 | import { quietLogs } from "./helpers/globals.ts"; |
| 16 | 17 | |
| @@ -93,7 +94,7 @@ test("the commit's check carries screens and sideways; [] without a file", async | ||
| 93 | 94 | const body = await get(`/api/v1/repos/alice/r/commits/${OID}/check`); |
| 94 | 95 | expect(body.runId).toBe(run.id); |
| 95 | 96 | expect(body.looks).toEqual([ |
| 96 | { look: "phone /x", screens: "2.5", sideways: true }, | |
| 97 | { look: "phone /x", screens: "2.5", sideways: true, size: { scrollWidth: 500, innerWidth: 390 } }, | |
| 97 | 98 | { look: "desktop", screens: "", sideways: false }, |
| 98 | 99 | ]); |
| 99 | 100 | }); |
| @@ -107,7 +108,35 @@ test("the pull's checks carry the same looks beside run", async () => { | ||
| 107 | 108 | const body = await get(`/api/v1/repos/alice/r/pulls/${number}/checks`); |
| 108 | 109 | expect(body.run.id).toBe(run.id); |
| 109 | 110 | expect(body.looks).toEqual([ |
| 110 | { look: "phone /x", screens: "1.3", sideways: false }, | |
| 111 | { look: "phone /x", screens: "1.3", sideways: false, size: { scrollWidth: 390, innerWidth: 390 } }, | |
| 111 | 112 | { look: "desktop", screens: "", sideways: false }, |
| 112 | 113 | ]); |
| 113 | 114 | }); |
| 115 | ||
| 116 | test("a green check lists the move look with its size from the commit's filed looks, whole preview set or as it grows", async () => { | |
| 117 | const move = "phone /preview-admin/sample/settings#move"; | |
| 118 | const size = { scrollWidth: 390, scrollHeight: 900, innerWidth: 390, innerHeight: 844 }; | |
| 119 | const oid = "c".repeat(40); | |
| 120 | const run = createRun(app.db, { repoId, kind: "check", commitOid: oid, env: "main", branch: "main" }); | |
| 121 | claimRun(app.db, run.id); | |
| 122 | finishRun(app.db, run.id, { state: "ok", summary: "check passed" }); | |
| 123 | // The check is green while looks are still being taken: what is filed so far is listed. | |
| 124 | writeCommitLooks(dataDir, repoId, oid, [{ look: "desktop", entries: {} }]); | |
| 125 | expect((await get(`/api/v1/repos/alice/r/commits/${oid}/check`)).looks.map((l: any) => l.look)).toEqual(["desktop"]); | |
| 126 | // The preview's whole named set, the move look last, filed one look at a time. | |
| 127 | const set = PREVIEW_OWNER_LOOKS.map((l) => ({ | |
| 128 | look: `${l.kind === "phone" ? "phone" : "desktop"} ${l.page}`, | |
| 129 | entries: {}, | |
| 130 | ...(l.kind === "phone" && l.page.endsWith("#move") ? { size } : {}), | |
| 131 | })); | |
| 132 | set.sort((a, b) => Number(a.look.endsWith("#move")) - Number(b.look.endsWith("#move"))); | |
| 133 | for (const one of set) writeCommitLooks(dataDir, repoId, oid, [one]); | |
| 134 | const body = await get(`/api/v1/repos/alice/r/commits/${oid}/check`); | |
| 135 | expect(body.looks).toHaveLength(set.length + 1); | |
| 136 | expect(body.looks.find((l: any) => l.look === move)).toEqual({ | |
| 137 | look: move, | |
| 138 | screens: "1.1", | |
| 139 | sideways: false, | |
| 140 | size: { scrollWidth: 390, innerWidth: 390 }, | |
| 141 | }); | |
| 142 | }); |
test/check-looks-held.test.ts +1
| @@ -89,6 +89,7 @@ test("a forge preview's check lists the held page at desktop and phone with scre | ||
| 89 | 89 | look: `${kind} ${PREVIEW_HELD_PAGE}`, |
| 90 | 90 | screens: "2.5", |
| 91 | 91 | sideways: false, |
| 92 | size: { scrollWidth: 390, innerWidth: 390 }, | |
| 92 | 93 | }); |
| 93 | 94 | } |
| 94 | 95 | }); |
test/check-looks-preview.test.ts added +149
| @@ -0,0 +1,149 @@ | ||
| 1 | /** | |
| 2 | * A preview of the forge's own repository, end to end (#10336): its deploy | |
| 3 | * serves, the looks service takes every forge.json page and every named owner | |
| 4 | * look through a stand-in browser, the check of its head goes green, and | |
| 5 | * `GET …/pulls/N/checks` lists `phone /preview-admin/sample/settings#move` | |
| 6 | * with its size. Owner looks are taken after the forge.json pages, so a cap | |
| 7 | * on the filed looks smaller than the whole set drops this one. | |
| 8 | */ | |
| 9 | ||
| 10 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 11 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 12 | import { writeFileSync } from "node:fs"; | |
| 13 | import { tmpdir } from "node:os"; | |
| 14 | import { join } from "node:path"; | |
| 15 | import { createApp, type App } from "../src/app.ts"; | |
| 16 | import { newApp } from "../src/actions/apps.ts"; | |
| 17 | import { openPull } from "../src/actions/pulls.ts"; | |
| 18 | import { commitToBranch } from "../src/actions/repos.ts"; | |
| 19 | import { previewEnv } from "../src/apps/data.ts"; | |
| 20 | import { AppsRuntime } from "../src/apps/runtime.ts"; | |
| 21 | import { LOOKS_MAX } from "../src/core/look_limits.ts"; | |
| 22 | import { claimRun, createRun, finishRun, listRuns } from "../src/core/runs.ts"; | |
| 23 | import { createSession } from "../src/core/sessions.ts"; | |
| 24 | import { createUser, type User } from "../src/core/users.ts"; | |
| 25 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 26 | import { ok } from "../src/kernel/result.ts"; | |
| 27 | import type { Capture } from "../src/looks/capture.ts"; | |
| 28 | import { startLooks, type LooksHandle } from "../src/looks/service.ts"; | |
| 29 | import { PREVIEW_MOVE_LOOK, PREVIEW_OWNER_LOOKS } from "../src/spawn/preview-looks.ts"; | |
| 30 | import { quietLogs } from "./helpers/globals.ts"; | |
| 31 | ||
| 32 | quietLogs(); | |
| 33 | ||
| 34 | const PNG = Buffer.from( | |
| 35 | "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==", | |
| 36 | "base64", | |
| 37 | ); | |
| 38 | const PHONE_SIZE = { scrollWidth: 390, scrollHeight: 1688, innerWidth: 390, innerHeight: 844 }; | |
| 39 | ||
| 40 | let dataDir: string; | |
| 41 | let app: App; | |
| 42 | let servers: Servers; | |
| 43 | let runtime: AppsRuntime; | |
| 44 | let looks: LooksHandle; | |
| 45 | let cookie = ""; | |
| 46 | let alice: User; | |
| 47 | ||
| 48 | /** The stand-in browser: a picture, and a size for every phone-width look. */ | |
| 49 | const fake: Capture = async (input) => { | |
| 50 | writeFileSync(input.out, PNG); | |
| 51 | return ok({ entries: {}, ...(input.width === 390 ? { size: PHONE_SIZE } : {}) }); | |
| 52 | }; | |
| 53 | ||
| 54 | async function until<T>(read: () => T | undefined, timeoutMs = 60_000): Promise<T> { | |
| 55 | const deadline = Date.now() + timeoutMs; | |
| 56 | while (Date.now() < deadline) { | |
| 57 | const value = read(); | |
| 58 | if (value !== undefined) return value; | |
| 59 | await Bun.sleep(10); | |
| 60 | } | |
| 61 | throw new Error("did not happen in time"); | |
| 62 | } | |
| 63 | ||
| 64 | beforeAll(async () => { | |
| 65 | dataDir = await mkdtemp(join(tmpdir(), "forge-check-looks-preview-")); | |
| 66 | // This forge's own repository is alice/forge: its previews take the named owner looks. | |
| 67 | app = createApp({ dataDir, port: 0, forgeRepo: "alice/forge" }); | |
| 68 | servers = await serve(app); | |
| 69 | runtime = new AppsRuntime(app, { | |
| 70 | domain: "apps.test", | |
| 71 | scheme: "http", | |
| 72 | publicPort: servers.port, | |
| 73 | forgeUrl: servers.url, | |
| 74 | }); | |
| 75 | await runtime.start(); | |
| 76 | looks = startLooks(app, { localUrl: servers.url, capture: fake }); | |
| 77 | const made = await createUser(app.db, { | |
| 78 | origin: "seed", | |
| 79 | trusted: true, | |
| 80 | username: "alice", | |
| 81 | email: "[email protected]", | |
| 82 | password: "a-good-passphrase", | |
| 83 | adminIfFirst: true, | |
| 84 | }); | |
| 85 | if (!made.ok) throw new Error(String(made.error)); | |
| 86 | alice = made.value; | |
| 87 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 88 | }, 60_000); | |
| 89 | ||
| 90 | afterAll(async () => { | |
| 91 | await looks.stop(); | |
| 92 | await runtime.stop(); | |
| 93 | servers.stop(); | |
| 94 | app.close(); | |
| 95 | await rm(dataDir, { recursive: true, force: true }); | |
| 96 | }); | |
| 97 | ||
| 98 | test("a green check on a preview of the forge lists the move look with its size", async () => { | |
| 99 | const made = await newApp(app, alice, { | |
| 100 | name: "forge", | |
| 101 | visibility: "private", | |
| 102 | }); | |
| 103 | if (!made.ok) throw new Error(String(made.error)); | |
| 104 | const { repo } = made.value; | |
| 105 | // forge.json names as many pages as it may: every one of them is filed before the owner looks. | |
| 106 | const pages = Array.from({ length: LOOKS_MAX }, (_, i) => `/page-${i}`); | |
| 107 | const committed = await commitToBranch(app, alice, repo, { | |
| 108 | branch: "move", | |
| 109 | base: repo.defaultBranch, | |
| 110 | message: "Every page", | |
| 111 | files: [{ path: "forge.json", content: `${JSON.stringify({ app: { run: "bun run src/server.ts", looks: pages } }, null, 2)}\n` }], | |
| 112 | }); | |
| 113 | if (!committed.ok) throw new Error(JSON.stringify(committed.error)); | |
| 114 | const head = committed.value.commitOid; | |
| 115 | const pull = await openPull(app, alice, repo, { title: "Every page", baseBranch: repo.defaultBranch, headBranch: "move" }); | |
| 116 | if (!pull.ok) throw new Error(String(pull.error)); | |
| 117 | const env = previewEnv(pull.value.number); | |
| 118 | ||
| 119 | // The preview deploys and serves; the looks service pictures it. | |
| 120 | await until(() => { | |
| 121 | const run = listRuns(app.db, repo.id, { kind: "deploy", limit: 50 }).find((r) => r.env === env && r.commitOid === head); | |
| 122 | return run && run.state !== "queued" && run.state !== "running" ? run : undefined; | |
| 123 | }); | |
| 124 | await until(() => (runtime.envStatus(made.value.app, env)?.state === "running" ? true : undefined)); | |
| 125 | await looks.idle(); | |
| 126 | ||
| 127 | // The check of that head goes green. | |
| 128 | const check = createRun(app.db, { repoId: repo.id, kind: "check", issueId: pull.value.id, commitOid: head, branch: "move" }); | |
| 129 | claimRun(app.db, check.id); | |
| 130 | finishRun(app.db, check.id, { state: "ok", summary: "check passed" }); | |
| 131 | ||
| 132 | const res = await fetch(`${servers.url}/api/v1/repos/alice/forge/pulls/${pull.value.number}/checks`, { headers: { cookie } }); | |
| 133 | expect(res.status).toBe(200); | |
| 134 | const body = (await res.json()) as { head: string; state: string; looks: { look: string; screens: string; sideways: boolean; size?: unknown }[] }; | |
| 135 | expect(body.head).toBe(head); | |
| 136 | expect(body.state).toBe("ok"); | |
| 137 | const listed = body.looks.map((l) => l.look); | |
| 138 | // Every forge.json page and every owner look, at desk and phone, deduped by page. | |
| 139 | const ownerPages = new Set(PREVIEW_OWNER_LOOKS.map((l) => l.page)); | |
| 140 | const expected = new Set([...pages, ...ownerPages]); | |
| 141 | expect(listed).toHaveLength(2 * expected.size); | |
| 142 | const move = body.looks.find((l) => l.look === `phone ${PREVIEW_MOVE_LOOK}`); | |
| 143 | expect(move).toEqual({ | |
| 144 | look: `phone ${PREVIEW_MOVE_LOOK}`, | |
| 145 | screens: "2.0", | |
| 146 | sideways: false, | |
| 147 | size: { scrollWidth: 390, innerWidth: 390 }, | |
| 148 | }); | |
| 149 | }, 120_000); |
test/check-vitals-size.test.ts +18
| @@ -43,3 +43,21 @@ test("the terminal line carries screens and the sideways note", () => { | ||
| 43 | 43 | expect(line).toContain("desktop: skipped: no browser · phone /x scrolls sideways"); |
| 44 | 44 | expect(vitalsLine([{ look: "phone", entries: {} }])).not.toContain("screens"); |
| 45 | 45 | }); |
| 46 | ||
| 47 | test("the terminal line carries a phone look's width beside its window, as the API's size does (#10336)", () => { | |
| 48 | const move = { look: "phone /preview-admin/sample/settings#move", entries: {}, size: size(390, 1600) }; | |
| 49 | const desk = { look: "desktop /x", entries: {}, size: size(1280, 1600) }; | |
| 50 | const line = vitalsLine([desk, move]); | |
| 51 | expect(line).toContain("phone /preview-admin/sample/settings#move: LCP n/a, CLS 0 good, TBT 0 ms good, 2.0 screens, 390/390 px wide"); | |
| 52 | expect(line).not.toContain("1280/"); | |
| 53 | expect(vitalsLine([{ look: "phone", entries: {} }])).not.toContain("px wide"); | |
| 54 | }); | |
| 55 | ||
| 56 | test("each row carries its own look's width, so nothing pairs rows with looks by place", () => { | |
| 57 | const rows = vitalsRows([ | |
| 58 | { look: "desktop", skipped: "no browser" }, | |
| 59 | { look: "phone /x", entries: {}, size: size(500, 800) }, | |
| 60 | ]); | |
| 61 | expect(rows[0]!.width).toBeUndefined(); | |
| 62 | expect(rows[1]!.width).toEqual({ scrollWidth: 500, innerWidth: 390 }); | |
| 63 | }); |
test/look-limits.test.ts added +20
| @@ -0,0 +1,20 @@ | ||
| 1 | /** The look caps (#10336): a commit's filed looks hold every look a preview of the forge takes. */ | |
| 2 | import { expect, test } from "bun:test"; | |
| 3 | import { FILED_LOOKS_MAX, LOOKS_MAX } from "../src/core/look_limits.ts"; | |
| 4 | import { parseLooks } from "../src/checks/looks.ts"; | |
| 5 | import { PREVIEW_MOVE_LOOK, PREVIEW_OWNER_LOOKS } from "../src/spawn/preview-looks.ts"; | |
| 6 | ||
| 7 | test("the filed cap holds every forge.json page and every named owner look, at desk and phone", () => { | |
| 8 | expect(FILED_LOOKS_MAX).toBeGreaterThanOrEqual(2 * LOOKS_MAX + PREVIEW_OWNER_LOOKS.length); | |
| 9 | }); | |
| 10 | ||
| 11 | test("a full preview set, forge.json pages first, keeps the move look", () => { | |
| 12 | const pages = Array.from({ length: LOOKS_MAX }, (_, i) => `/p${i}`); | |
| 13 | const set = [ | |
| 14 | ...pages.flatMap((p) => [`desktop ${p}`, `phone ${p}`]), | |
| 15 | ...PREVIEW_OWNER_LOOKS.map((l) => `${l.kind} ${l.page}`), | |
| 16 | ].map((look) => ({ look, entries: {} })); | |
| 17 | const kept = parseLooks(set).map((l) => l.look); | |
| 18 | expect(kept).toHaveLength(set.length); | |
| 19 | expect(kept).toContain(`phone ${PREVIEW_MOVE_LOOK}`); | |
| 20 | }); |
test/looks.test.ts +14
| @@ -94,7 +94,12 @@ describe("the look", () => { | ||
| 94 | 94 | const darkCalls: LookInput[] = []; |
| 95 | 95 | /** The light pictures taken of one of `hello`'s environments. */ |
| 96 | 96 | const callsAt = (env: string) => calls.filter((c) => hostOf(c) === helloHost(env)); |
| 97 | /** How many looks were already filed under the commit when a light picture of it was begun. */ | |
| 98 | const filedAtCapture: { commit: string; filed: number }[] = []; | |
| 97 | 99 | const fake: Capture = async (input) => { |
| 100 | const commit = /[0-9a-f]{40}/.exec(input.out)?.[0] ?? ""; | |
| 101 | if (commit !== "" && input.dark !== true) | |
| 102 | filedAtCapture.push({ commit, filed: readLooks(dataDir, { id: "none", repoId: repo.id, commitOid: commit }).length }); | |
| 98 | 103 | (input.dark === true ? darkCalls : calls).push(input); |
| 99 | 104 | const mode = modes.get(hostOf(input)); |
| 100 | 105 | if (mode === "fail") return err("the browser fell over"); |
| @@ -893,6 +898,15 @@ describe("the look", () => { | ||
| 893 | 898 | ); |
| 894 | 899 | }); |
| 895 | 900 | |
| 901 | test("a commit's looks are filed as each is taken, so a check that is green before the last picture lists them", async () => { | |
| 902 | // Invariant: the vitals file grows look by look; it is not written only once the whole set is done. | |
| 903 | const mine = filedAtCapture.filter((c) => c.commit === twoPages.commitOid); | |
| 904 | expect(mine.length).toBeGreaterThanOrEqual(4); | |
| 905 | expect(Math.max(...mine.map((c) => c.filed))).toBeGreaterThanOrEqual(3); | |
| 906 | const check = { id: "none", repoId: repo.id, commitOid: twoPages.commitOid }; | |
| 907 | expect(readLooks(dataDir, check).map((l) => l.look).sort()).toEqual(["desktop", "desktop /board", "phone", "phone /board"]); | |
| 908 | }); | |
| 909 | ||
| 896 | 910 | test("app.looks naming two pages pictures both, distinctly, and the pull page groups them", async () => { |
| 897 | 911 | // Invariant: each page `app.looks` names gets its own light and dark slots and files, grouped by page on the pull, dark folded away. |
| 898 | 912 | const opened = { value: twoPages.pull }; |
test/preview-spec.test.ts +5 −3
| @@ -6,7 +6,7 @@ import { tmpdir } from "node:os"; | ||
| 6 | 6 | import { join } from "node:path"; |
| 7 | 7 | import { createApp, type App } from "../src/app.ts"; |
| 8 | 8 | import { AppsRuntime } from "../src/apps/runtime.ts"; |
| 9 | import { previewSpec } from "../src/apps/preview_spec.ts"; | |
| 9 | import { PREVIEW_SPEC_TIMEOUT_MS, previewSpec } from "../src/apps/preview_spec.ts"; | |
| 10 | 10 | import { newApp } from "../src/actions/apps.ts"; |
| 11 | 11 | import { openPull } from "../src/actions/pulls.ts"; |
| 12 | 12 | import { commitToBranch } from "../src/actions/repos.ts"; |
| @@ -91,7 +91,9 @@ test("a deployed preview's findings are stored under its run, and the run still | ||
| 91 | 91 | const r = listRuns(app.db, repo.id, { kind: "deploy", limit: 50 }).find((x) => x.env === env); |
| 92 | 92 | return r && ["ok", "failed"].includes(r.state) ? r : null; |
| 93 | 93 | }, 60_000); |
| 94 | const found = await until(() => previewFindings(app.db, run.id)); | |
| 94 | // previewSpec asks a page that missed once more, each pass on its own clock: | |
| 95 | // on a busy machine the findings can take two of them to land. | |
| 96 | const found = await until(() => previewFindings(app.db, run.id), 2 * PREVIEW_SPEC_TIMEOUT_MS + 5_000); | |
| 95 | 97 | expect(found.some((f) => f.check === "etag")).toBe(true); |
| 96 | 98 | expect(listRuns(app.db, repo.id, { kind: "deploy", limit: 50 }).find((x) => x.id === run.id)?.state).toBe("ok"); |
| 97 | 99 | expect(specFindings(found).text).toContain("finding"); |
| @@ -104,7 +106,7 @@ test("a deployed preview's findings are stored under its run, and the run still | ||
| 104 | 106 | .then((r) => r.text()); |
| 105 | 107 | expect(page).toMatch(/Website spec: \d+ findings?/); |
| 106 | 108 | expect(page).toContain("https://specification.website/#caching"); |
| 107 | }, 90_000); | |
| 109 | }, 150_000); | |
| 108 | 110 | |
| 109 | 111 | test("a webspec that cannot finish is one finding, and is stored", async () => { |
| 110 | 112 | // Nothing listens on port 1: every fetch fails or the clock runs out. |
new above
offline
Index pairing between vitalsRows output and looks[i] is fragile. Carry size on the row itself or look it up by name.