Public: anyone can read this; the crew can change it.
Take updates from trevato/forge-upstream #1376
mason/forge:mason/1303-take-updates-from-trevato-forge-upstream → main 4 commits fork
10 files
changed +329
−41
scripts/gate.ts +6
| @@ -48,6 +48,12 @@ export const GATE: readonly string[] = [ | ||
| 48 | 48 | "test/review-route.test.ts", |
| 49 | 49 | "test/review-lines.test.ts", |
| 50 | 50 | "test/review-gate.test.ts", |
| 51 | "test/review-from-check.test.ts", | |
| 52 | "test/themes.test.ts", | |
| 53 | "test/chaos.test.ts", | |
| 54 | "test/fuzz-found.test.ts", | |
| 55 | "test/fuzz-parsers.test.ts", | |
| 56 | "test/health.test.ts", | |
| 51 | 57 | "test/crew-wrapup.test.ts", |
| 52 | 58 | "test/crew-limits.test.ts", |
| 53 | 59 | "test/crew-parks.test.ts", |
src/core/releases.json +16
| @@ -1,6 +1,22 @@ | ||
| 1 | 1 | { |
| 2 | 2 | "note": "The migration count each release shipped with, newest first. The release agent adds the new release here when it cuts one. Every migration past the older of the two newest counts must be additive (test/schema-additive.test.ts, notAdditive in src/kernel/migrate.ts): the old process keeps serving on the same database while the new one rolls in.", |
| 3 | 3 | "releases": [ |
| 4 | { | |
| 5 | "version": "0.7.69", | |
| 6 | "migrations": 72 | |
| 7 | }, | |
| 8 | { | |
| 9 | "version": "0.7.68", | |
| 10 | "migrations": 72 | |
| 11 | }, | |
| 12 | { | |
| 13 | "version": "0.7.67", | |
| 14 | "migrations": 72 | |
| 15 | }, | |
| 16 | { | |
| 17 | "version": "0.7.66", | |
| 18 | "migrations": 72 | |
| 19 | }, | |
| 4 | 20 | { |
| 5 | 21 | "version": "0.7.65", |
| 6 | 22 | "migrations": 72 |
src/crew/review.ts +89 −20
| @@ -84,8 +84,16 @@ const REVIEW_TRIES = 2; | ||
| 84 | 84 | export interface ReviewAttempt { |
| 85 | 85 | readonly createdAt: string; |
| 86 | 86 | readonly active: boolean; |
| 87 | /** The reviewer it went to, when known. */ | |
| 88 | readonly actorId?: string | null; | |
| 87 | 89 | } |
| 88 | 90 | |
| 91 | /** | |
| 92 | * A head that went green this long after the lapse, with no review yet, | |
| 93 | * is still sent once, late; past it, nothing was ever going to send. | |
| 94 | */ | |
| 95 | const LATE_SEND_MS = REVIEW_LAPSE_MS; | |
| 96 | ||
| 89 | 97 | /** |
| 90 | 98 | * Where review stands on a green head that has no verdict: `send` a |
| 91 | 99 | * review run (the first, or the one retry), `wait` for the one that is |
| @@ -119,27 +127,71 @@ export function reviewStage( | ||
| 119 | 127 | export function reviewAttempts( |
| 120 | 128 | db: Database, |
| 121 | 129 | pull: Pull, |
| 122 | ): { greenAt: string; attempts: ReviewAttempt[] } | undefined { | |
| 123 | const green = db | |
| 124 | .query<{ at: string | null }, [string, string]>( | |
| 125 | `SELECT MIN(finished_at) AS at FROM run WHERE issue_id = ?1 AND commit_oid = ?2 | |
| 126 | AND kind IN ('check','deploy') AND state = 'ok'`, | |
| 130 | ): ReviewHistory | undefined { | |
| 131 | // Review is owed from the check's green: a preview deploy finishes in | |
| 132 | // seconds, a check may queue for an hour. A deploy counts only where the | |
| 133 | // head has no check at all. | |
| 134 | const hasCheck = | |
| 135 | db | |
| 136 | .query<{ n: number }, [string, string]>( | |
| 137 | `SELECT COUNT(*) AS n FROM run WHERE issue_id = ?1 AND commit_oid = ?2 AND kind = 'check'`, | |
| 138 | ) | |
| 139 | .get(pull.id, pull.headOid)!.n > 0; | |
| 140 | const greenRun = db | |
| 141 | .query<{ at: string | null; created: string | null }, [string, string, string]>( | |
| 142 | `SELECT MIN(finished_at) AS at, created_at AS created FROM run | |
| 143 | WHERE issue_id = ?1 AND commit_oid = ?2 AND kind = ?3 AND state = 'ok'`, | |
| 127 | 144 | ) |
| 128 | .get(pull.id, pull.headOid)?.at; | |
| 145 | .get(pull.id, pull.headOid, hasCheck ? "check" : "deploy"); | |
| 146 | const green = greenRun?.at; | |
| 129 | 147 | if (green === null || green === undefined) return undefined; |
| 148 | const checkMs = hasCheck | |
| 149 | ? Date.parse(green) - Date.parse(greenRun?.created ?? green) | |
| 150 | : 0; | |
| 130 | 151 | const released = lastRelease(db, pull.id); |
| 131 | 152 | const since = released !== undefined && released > green ? released : green; |
| 132 | 153 | const attempts = db |
| 133 | .query<{ created_at: string; state: string }, [string, string]>( | |
| 134 | `SELECT created_at, state FROM run WHERE issue_id = ?1 AND kind = 'agent' | |
| 154 | .query<{ created_at: string; state: string; actor_id: string | null }, [string, string]>( | |
| 155 | `SELECT created_at, state, actor_id FROM run WHERE issue_id = ?1 AND kind = 'agent' | |
| 135 | 156 | AND reason = 'review' AND created_at >= ?2 ORDER BY created_at, id`, |
| 136 | 157 | ) |
| 137 | 158 | .all(pull.id, since) |
| 138 | 159 | .map((r) => ({ |
| 139 | 160 | createdAt: r.created_at, |
| 140 | 161 | active: r.state === "queued" || r.state === "running", |
| 162 | actorId: r.actor_id, | |
| 141 | 163 | })); |
| 142 | return { greenAt: since, attempts }; | |
| 164 | const first = attempts[0]; | |
| 165 | const lateAt = | |
| 166 | first !== undefined && Date.parse(first.createdAt) - Date.parse(since) >= REVIEW_LAPSE_MS | |
| 167 | ? first.createdAt | |
| 168 | : undefined; | |
| 169 | return { greenAt: since, attempts, lateAt, checkMinutes: Math.round(checkMs / 60_000) }; | |
| 170 | } | |
| 171 | ||
| 172 | /** What the run table says about review on a head. */ | |
| 173 | export interface ReviewHistory { | |
| 174 | readonly greenAt: string; | |
| 175 | readonly attempts: ReviewAttempt[]; | |
| 176 | /** When the first review was sent, if it was sent past the lapse: the lapse clock restarts there. */ | |
| 177 | readonly lateAt: string | undefined; | |
| 178 | /** How long the check took, from queued to green. */ | |
| 179 | readonly checkMinutes: number; | |
| 180 | } | |
| 181 | ||
| 182 | /** | |
| 183 | * `reviewStage` for a history: a head that went green and was never sent | |
| 184 | * is sent once, late, before it lapses to a person; a review sent late | |
| 185 | * has the lapse counted from its sending. | |
| 186 | */ | |
| 187 | export function stageOf( | |
| 188 | history: ReviewHistory, | |
| 189 | now: Date, | |
| 190 | ): "send" | "wait" | "lapsed" | "late" { | |
| 191 | const age = now.getTime() - Date.parse(history.greenAt); | |
| 192 | if (history.attempts.length === 0 && age >= REVIEW_LAPSE_MS) | |
| 193 | return age < REVIEW_LAPSE_MS + LATE_SEND_MS ? "late" : "lapsed"; | |
| 194 | return reviewStage(history.lateAt ?? history.greenAt, history.attempts, now); | |
| 143 | 195 | } |
| 144 | 196 | |
| 145 | 197 | /** Rounds of changes the crew's reviewer asks on one pull before it is handed to a person. */ |
| @@ -193,6 +245,8 @@ export interface ReviewWait { | ||
| 193 | 245 | readonly why: "no reviewer" | "lapsed" | "rounds"; |
| 194 | 246 | /** When review became owed: the head went green, or the pull was let go. */ |
| 195 | 247 | readonly since: string; |
| 248 | /** Minutes the check took, when the review was sent late because of it. */ | |
| 249 | readonly lateCheckMinutes?: number; | |
| 196 | 250 | } |
| 197 | 251 | |
| 198 | 252 | /** |
| @@ -218,8 +272,14 @@ export function waitsForPerson( | ||
| 218 | 272 | if (history === undefined) return undefined; |
| 219 | 273 | if (reviewersOf(db, repoId).length === 0) |
| 220 | 274 | return { why: "no reviewer", since: history.greenAt }; |
| 221 | return reviewStage(history.greenAt, history.attempts, now) === "lapsed" | |
| 222 | ? { why: "lapsed", since: history.greenAt } | |
| 275 | return stageOf(history, now) === "lapsed" | |
| 276 | ? { | |
| 277 | why: "lapsed", | |
| 278 | since: history.greenAt, | |
| 279 | ...(history.lateAt === undefined | |
| 280 | ? {} | |
| 281 | : { lateCheckMinutes: history.checkMinutes }), | |
| 282 | } | |
| 223 | 283 | : undefined; |
| 224 | 284 | } |
| 225 | 285 | |
| @@ -245,8 +305,9 @@ export async function sendToReview( | ||
| 245 | 305 | if (handedToPerson(app.db, repo.id, pull)) return "held"; |
| 246 | 306 | const history = reviewAttempts(app.db, pull); |
| 247 | 307 | if (history === undefined) return "wait"; |
| 248 | const stage = reviewStage(history.greenAt, history.attempts, now); | |
| 249 | if (stage !== "send") return stage; | |
| 308 | const stage = stageOf(history, now); | |
| 309 | if (stage === "wait" || stage === "lapsed") return stage; | |
| 310 | const late = stage === "late"; | |
| 250 | 311 | const issue = getIssueById(app.db, pull.id); |
| 251 | 312 | if (!issue) return "wait"; |
| 252 | 313 | const { diff } = await pullDiff( |
| @@ -265,6 +326,7 @@ export async function sendToReview( | ||
| 265 | 326 | pull: pull.number, |
| 266 | 327 | reviewer: reviewer.user.username, |
| 267 | 328 | attempt: history.attempts.length + 1, |
| 329 | ...(late ? { late: true, checkMinutes: history.checkMinutes } : {}), | |
| 268 | 330 | }); |
| 269 | 331 | return "sent"; |
| 270 | 332 | } |
| @@ -323,13 +385,7 @@ export async function sweepReviews(app: App): Promise<number> { | ||
| 323 | 385 | const key = `${pull.headOid}:${result}`; |
| 324 | 386 | if (said.get(pull.id) === key) continue; |
| 325 | 387 | said.set(pull.id, key); |
| 326 | const since = reviewAttempts(app.db, pull)?.greenAt; | |
| 327 | log.info( | |
| 328 | result === "lapsed" | |
| 329 | ? `review: lapsed since ${since}` | |
| 330 | : "review: not sent yet", | |
| 331 | { repo: `${repo.ownerName}/${repo.name}`, pull: pull.number }, | |
| 332 | ); | |
| 388 | skipped(app, repo, pull, result); | |
| 333 | 389 | } |
| 334 | 390 | } |
| 335 | 391 | } |
| @@ -341,3 +397,16 @@ function forgetSettled(stalled: Set<string>, queued: number): number { | ||
| 341 | 397 | for (const id of said.keys()) if (!stalled.has(id)) said.delete(id); |
| 342 | 398 | return queued; |
| 343 | 399 | } |
| 400 | ||
| 401 | /** One line for a pull the sweep leaves be, and why, so a stall reads in the log. */ | |
| 402 | function skipped(app: App, repo: Repo, pull: Pull, why: "wait" | "lapsed"): void { | |
| 403 | const history = reviewAttempts(app.db, pull); | |
| 404 | if (history === undefined) return; | |
| 405 | let line = `review: lapsed since ${history.greenAt.slice(11, 16)}`; | |
| 406 | if (why === "wait") { | |
| 407 | const out = history.attempts.at(-1)?.actorId; | |
| 408 | const who = reviewersOf(app.db, repo.id).find((r) => r.user.id === out); | |
| 409 | line = `review: waiting on ${who?.user.username ?? "the reviewer"}`; | |
| 410 | } | |
| 411 | log.info(line, { repo: `${repo.ownerName}/${repo.name}`, pull: pull.number }); | |
| 412 | } |
src/design/themes.ts +14 −12
| @@ -429,16 +429,18 @@ export function metaThemeColor(name: ThemeName): string { | ||
| 429 | 429 | * while Safari often keeps the prior paint. Matching `html`/`body` to the |
| 430 | 430 | * theme before the external stylesheet arrives kills that flash. Kept tiny |
| 431 | 431 | * and nonce'd — CSP forbids unnonce'd inline style. |
| 432 | * | |
| 433 | * The same for every page, whatever theme it was rendered with: the theme | |
| 434 | * menu flips `data-theme` on the root without a reload, so the ground must | |
| 435 | * follow the attribute, not the theme the server saw. With no attribute the | |
| 436 | * system pair follows the OS; each `html[data-theme]` rule outranks it. | |
| 432 | 437 | */ |
| 433 | export function bootCss(theme: ThemeName | undefined): string { | |
| 434 | if (theme !== undefined) { | |
| 435 | const t = THEMES[theme]; | |
| 436 | return `html,body{background-color:${t.tokens.bg};color-scheme:${t.appearance}}`; | |
| 437 | } | |
| 438 | const light = THEMES.light; | |
| 439 | const dark = THEMES.dark; | |
| 440 | return ( | |
| 441 | `html,body{background-color:${light.tokens.bg};color-scheme:light}` + | |
| 442 | `@media (prefers-color-scheme: dark){html,body{background-color:${dark.tokens.bg};color-scheme:dark}}` | |
| 443 | ); | |
| 444 | } | |
| 438 | export const bootCss = [ | |
| 439 | `html,body{background-color:${THEMES.light.tokens.bg};color-scheme:light}`, | |
| 440 | `@media (prefers-color-scheme: dark){html,body{background-color:${THEMES.dark.tokens.bg};color-scheme:dark}}`, | |
| 441 | ...THEME_NAMES.map((name) => { | |
| 442 | const t = THEMES[name]; | |
| 443 | const root = `html[data-theme="${name}"]`; | |
| 444 | return `${root},${root} body{background-color:${t.tokens.bg};color-scheme:${t.appearance}}`; | |
| 445 | }), | |
| 446 | ].join(""); |
src/web/layout.ts +1 −1
| @@ -620,7 +620,7 @@ export function page(options: PageOptions, status = 200): Response { | ||
| 620 | 620 | rel="icon" |
| 621 | 621 | href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cdefs%3E%3ClinearGradient id='g' x1='0' y1='0' x2='1' y2='1'%3E%3Cstop offset='0' stop-color='%232563eb'/%3E%3Cstop offset='1' stop-color='%237c3aed'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect x='3' y='3' width='26' height='26' rx='8' fill='url(%23g)'/%3E%3C/svg%3E" |
| 622 | 622 | /> |
| 623 | ${raw(`<style nonce="${nonce}">${bootCss(theme)}</style>`)} | |
| 623 | ${raw(`<style nonce="${nonce}">${bootCss}</style>`)} | |
| 624 | 624 | <link rel="stylesheet" href="${stylesheetAsset().path}" /> |
| 625 | 625 | ${when( |
| 626 | 626 | options.scriptAsset, |
src/web/studio/asks.ts +2
| @@ -649,6 +649,8 @@ function lineOf(c: AskCandidate): string { | ||
| 649 | 649 | const reviewWhy = (wait: ReviewWait | undefined): string => |
| 650 | 650 | wait?.why === "rounds" |
| 651 | 651 | ? `the reviewer asked for changes three times, since ${wait.since.slice(0, 10)}` |
| 652 | : wait?.why === "lapsed" && wait.lateCheckMinutes !== undefined | |
| 653 | ? `review ran late: the check took ${wait.lateCheckMinutes} min, and the reviewer did not answer, since ${wait.since.slice(0, 10)}` | |
| 652 | 654 | : wait?.why === "lapsed" |
| 653 | 655 | ? `the reviewer did not answer, since ${wait.since.slice(0, 10)}` |
| 654 | 656 | : `no reviewer on the crew, since ${wait?.since.slice(0, 10) ?? "it went green"}`; |
test/checks.test.ts +7
| @@ -633,6 +633,13 @@ describe("checks", () => { | ||
| 633 | 633 | files: [{ path: "QUIET.md", content: "quiet\n" }], |
| 634 | 634 | }); |
| 635 | 635 | if (!branch.ok) throw new Error(JSON.stringify(branch.error)); |
| 636 | // The pull's check is the base's (#1275): give main a valid one. | |
| 637 | const base = await commitToBranch(app, alice, repo, { | |
| 638 | branch: "main", | |
| 639 | message: "The base's check", | |
| 640 | files: [{ path: "forge.json", content: checkJson("test -f QUIET.md") }], | |
| 641 | }); | |
| 642 | if (!base.ok) throw new Error(JSON.stringify(base.error)); | |
| 636 | 643 | // Opened while no checks service was listening: nothing asked for a check. |
| 637 | 644 | const opened = await openPull(app, alice, repo, { |
| 638 | 645 | title: "Quiet", |
test/review-from-check.test.ts added +140
| @@ -0,0 +1,140 @@ | ||
| 1 | /** | |
| 2 | * Review is owed from the check's green, not the preview deploy's (#1216): | |
| 3 | * a slow check queue must not lapse a pull before review was ever sent. | |
| 4 | */ | |
| 5 | ||
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 11 | import { openPull } from "../src/actions/pulls.ts"; | |
| 12 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 13 | import { createApp, type App } from "../src/app.ts"; | |
| 14 | import { setCrewRole } from "../src/core/agents.ts"; | |
| 15 | import type { Pull } from "../src/core/pulls.ts"; | |
| 16 | import { getRepoById, type Repo } from "../src/core/repos.ts"; | |
| 17 | import { claimRun, createRun, finishRun, listRuns } from "../src/core/runs.ts"; | |
| 18 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 19 | import { sendToReview, waitsForPerson } from "../src/crew/review.ts"; | |
| 20 | import { quietLogs } from "./helpers/globals.ts"; | |
| 21 | ||
| 22 | quietLogs(); | |
| 23 | ||
| 24 | const T0 = Date.parse("2026-10-13T03:00:00.000Z"); | |
| 25 | const at = (min: number) => new Date(T0 + min * 60_000); | |
| 26 | ||
| 27 | describe("review is owed from the check's green", () => { | |
| 28 | let dataDir = ""; | |
| 29 | let app: App; | |
| 30 | let alice: User; | |
| 31 | let repo: Repo; | |
| 32 | let n = 0; | |
| 33 | ||
| 34 | const fresh = () => getRepoById(app.db, repo.id)!; | |
| 35 | const reviewRuns = (pull: Pull) => | |
| 36 | listRuns(app.db, repo.id, { issueId: pull.id }).filter( | |
| 37 | (r) => r.reason === "review", | |
| 38 | ); | |
| 39 | const pullOn = async (): Promise<Pull> => { | |
| 40 | const branch = `b${++n}`; | |
| 41 | const c = await commitToBranch(app, alice, repo, { | |
| 42 | branch, | |
| 43 | base: "main", | |
| 44 | message: branch, | |
| 45 | files: [{ path: `${branch}.md`, content: "x\n" }], | |
| 46 | }); | |
| 47 | if (!c.ok) throw new Error(JSON.stringify(c.error)); | |
| 48 | const o = await openPull(app, alice, repo, { | |
| 49 | title: branch, | |
| 50 | body: "", | |
| 51 | baseBranch: "main", | |
| 52 | headBranch: branch, | |
| 53 | }); | |
| 54 | if (!o.ok) throw new Error(String(o.error)); | |
| 55 | return o.value; | |
| 56 | }; | |
| 57 | const green = (pull: Pull, kind: "check" | "deploy", queued: number, done: number) => { | |
| 58 | const run = createRun(app.db, { | |
| 59 | repoId: repo.id, | |
| 60 | kind, | |
| 61 | issueId: pull.id, | |
| 62 | env: `pull-${pull.number}`, | |
| 63 | commitOid: pull.headOid, | |
| 64 | reason: "pull", | |
| 65 | }); | |
| 66 | claimRun(app.db, run.id); | |
| 67 | finishRun(app.db, run.id, { state: "ok", summary: "ok" }); | |
| 68 | app.db | |
| 69 | .query("UPDATE run SET created_at = ?2, finished_at = ?3 WHERE id = ?1") | |
| 70 | .run(run.id, at(queued).toISOString(), at(done).toISOString()); | |
| 71 | }; | |
| 72 | const agentPull = async () => { | |
| 73 | const pull = await pullOn(); | |
| 74 | return { ...pull, authorIsAgent: true }; | |
| 75 | }; | |
| 76 | ||
| 77 | beforeAll(async () => { | |
| 78 | dataDir = await mkdtemp(join(tmpdir(), "forge-review-check-")); | |
| 79 | app = createApp({ dataDir, port: 0 }); | |
| 80 | const made = await createUser(app.db, { | |
| 81 | trusted: true, | |
| 82 | username: "alice", | |
| 83 | email: "[email protected]", | |
| 84 | password: "a-good-passphrase", | |
| 85 | adminIfFirst: true, | |
| 86 | }); | |
| 87 | if (!made.ok) throw new Error(made.error); | |
| 88 | alice = made.value; | |
| 89 | const r = await createRepo(app, alice, { name: "garden", visibility: "private" }); | |
| 90 | if (!r.ok) throw new Error(String(r.error)); | |
| 91 | repo = r.value; | |
| 92 | const seeded = await commitToBranch(app, alice, repo, { | |
| 93 | message: "seed", | |
| 94 | files: [{ path: "README.md", content: "# garden\n" }], | |
| 95 | }); | |
| 96 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 97 | const agent = await createAgent(app, alice, { username: "quill" }); | |
| 98 | if (!agent.ok) throw new Error(String(agent.error)); | |
| 99 | addToCrew(app, alice, repo, "quill"); | |
| 100 | setCrewRole(app.db, repo.id, getUserByName(app.db, "quill")!.id, "reviewer"); | |
| 101 | }, 30_000); | |
| 102 | ||
| 103 | afterAll(async () => { | |
| 104 | app.close(); | |
| 105 | await rm(dataDir, { recursive: true, force: true }); | |
| 106 | }); | |
| 107 | ||
| 108 | test("a deploy green at t=0 and a check green at t=70 min: sent at t=70, not lapsed", async () => { | |
| 109 | const pull = await agentPull(); | |
| 110 | green(pull, "deploy", 0, 0); | |
| 111 | green(pull, "check", 0, 70); | |
| 112 | expect(waitsForPerson(app.db, repo.id, pull, at(70))).toBeUndefined(); | |
| 113 | expect(await sendToReview(app, fresh(), pull, at(70))).toBe("sent"); | |
| 114 | expect(reviewRuns(pull)).toHaveLength(1); | |
| 115 | }); | |
| 116 | ||
| 117 | test("a head with no check is reviewed from its deploy, as before", async () => { | |
| 118 | const pull = await agentPull(); | |
| 119 | green(pull, "deploy", 0, 0); | |
| 120 | expect(await sendToReview(app, fresh(), pull, at(1))).toBe("sent"); | |
| 121 | expect(await sendToReview(app, fresh(), pull, at(1))).toBe("wait"); | |
| 122 | }); | |
| 123 | ||
| 124 | test("a head that went green long ago and was never sent is sent once, late, and says so if it lapses", async () => { | |
| 125 | const pull = await agentPull(); | |
| 126 | green(pull, "check", 0, 72); | |
| 127 | // Green at 72; the first look comes at 140, past the lapse. | |
| 128 | expect(await sendToReview(app, fresh(), pull, at(72 + 70))).toBe("sent"); | |
| 129 | const run = reviewRuns(pull)[0]!; | |
| 130 | app.db | |
| 131 | .query("UPDATE run SET created_at = ?2 WHERE id = ?1") | |
| 132 | .run(run.id, at(72 + 70).toISOString()); | |
| 133 | // The late review is out: the lapse counts from its sending. | |
| 134 | expect(waitsForPerson(app.db, repo.id, pull, at(72 + 80))).toBeUndefined(); | |
| 135 | app.db.query("UPDATE run SET state = 'failed' WHERE id = ?1").run(run.id); | |
| 136 | const wait = waitsForPerson(app.db, repo.id, pull, at(72 + 70 + 61)); | |
| 137 | expect(wait?.why).toBe("lapsed"); | |
| 138 | expect(wait?.lateCheckMinutes).toBe(72); | |
| 139 | }); | |
| 140 | }); |
test/themes.test.ts added +29
| @@ -0,0 +1,29 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { bootCss, THEME_NAMES, THEMES } from "../src/design/themes.ts"; | |
| 3 | ||
| 4 | describe("bootCss: the ground before the stylesheet arrives (#1240)", () => { | |
| 5 | test("with no data-theme, the system pair follows the OS", () => { | |
| 6 | expect(bootCss).toStartWith( | |
| 7 | "html,body{background-color:#fafafa;color-scheme:light}" + | |
| 8 | "@media (prefers-color-scheme: dark){html,body{background-color:#09090b;color-scheme:dark}}", | |
| 9 | ); | |
| 10 | }); | |
| 11 | ||
| 12 | test("every theme paints its own ground from the attribute, after the media rule", () => { | |
| 13 | const media = bootCss.indexOf("@media"); | |
| 14 | for (const name of THEME_NAMES) { | |
| 15 | const t = THEMES[name]; | |
| 16 | const rule = `html[data-theme="${name}"],html[data-theme="${name}"] body{background-color:${t.tokens.bg};color-scheme:${t.appearance}}`; | |
| 17 | expect(bootCss).toContain(rule); | |
| 18 | expect(bootCss.indexOf(rule)).toBeGreaterThan(media); | |
| 19 | } | |
| 20 | }); | |
| 21 | ||
| 22 | test("names no theme on its own, so a toggle never leaves a stale ground", () => { | |
| 23 | // Every rule outside the system pair is keyed on data-theme. | |
| 24 | const rest = bootCss.slice(bootCss.indexOf("}}") + 2); | |
| 25 | for (const rule of rest.split("}").filter(Boolean)) { | |
| 26 | expect(rule).toStartWith("html[data-theme="); | |
| 27 | } | |
| 28 | }); | |
| 29 | }); |
test/web.test.ts +25 −8
| @@ -601,15 +601,26 @@ describe("themes", () => { | ||
| 601 | 601 | |
| 602 | 602 | test("no cookie means system: no data-theme, switcher present", async () => { |
| 603 | 603 | const text = await (await fetch(base)).text(); |
| 604 | expect(text).not.toContain("data-theme="); | |
| 604 | expect(text).not.toMatch(/<html[^>]*\sdata-theme=/); | |
| 605 | 605 | expect(text).toContain('action="/theme"'); |
| 606 | 606 | for (const name of THEME_NAMES) { |
| 607 | 607 | expect(text).toContain(`value="${name}"`); |
| 608 | 608 | } |
| 609 | // Critical paint before the stylesheet: system pair (light default + dark media). | |
| 610 | expect(text).toMatch( | |
| 611 | /<style nonce="[^"]+">html,body\{background-color:#fafafa;color-scheme:light\}@media \(prefers-color-scheme: dark\)\{html,body\{background-color:#09090b;color-scheme:dark\}\}<\/style>/, | |
| 609 | // Critical paint before the stylesheet: system pair (light default + dark | |
| 610 | // media), then a rule per theme so a toggle repaints without a reload. | |
| 611 | const boot = text.match(/<style nonce="[^"]+">([^<]*)<\/style>/)?.[1]; | |
| 612 | expect(boot).toStartWith( | |
| 613 | "html,body{background-color:#fafafa;color-scheme:light}@media (prefers-color-scheme: dark){html,body{background-color:#09090b;color-scheme:dark}}", | |
| 612 | 614 | ); |
| 615 | expect(boot).toContain( | |
| 616 | 'html[data-theme="dark"],html[data-theme="dark"] body{background-color:#09090b;color-scheme:dark}', | |
| 617 | ); | |
| 618 | expect(boot).toContain( | |
| 619 | 'html[data-theme="light"],html[data-theme="light"] body{background-color:#fafafa;color-scheme:light}', | |
| 620 | ); | |
| 621 | for (const name of THEME_NAMES) { | |
| 622 | expect(boot).toContain(`html[data-theme="${name}"] body{`); | |
| 623 | } | |
| 613 | 624 | }); |
| 614 | 625 | |
| 615 | 626 | test("choosing a theme sets the cookie and stamps the page", async () => { |
| @@ -629,10 +640,16 @@ describe("themes", () => { | ||
| 629 | 640 | expect(text).toContain('data-theme="midnight"'); |
| 630 | 641 | // The switcher shows the active choice. |
| 631 | 642 | expect(text).toMatch(/value="midnight"\s+checked/); |
| 632 | // Boot paint matches midnight, not the system pair. | |
| 633 | expect(text).toMatch( | |
| 634 | /<style nonce="[^"]+">html,body\{background-color:#16161e;color-scheme:dark\}<\/style>/, | |
| 643 | // Boot paint follows the attribute: midnight's ground for this page, and | |
| 644 | // the same rules as a system page, so switching away repaints at once. | |
| 645 | const boot = text.match(/<style nonce="[^"]+">([^<]*)<\/style>/)?.[1]; | |
| 646 | expect(boot).toContain( | |
| 647 | 'html[data-theme="midnight"],html[data-theme="midnight"] body{background-color:#16161e;color-scheme:dark}', | |
| 635 | 648 | ); |
| 649 | const system = (await (await fetch(base)).text()).match( | |
| 650 | /<style nonce="[^"]+">([^<]*)<\/style>/, | |
| 651 | )?.[1]; | |
| 652 | expect(boot).toBe(system!); | |
| 636 | 653 | }); |
| 637 | 654 | |
| 638 | 655 | test("system clears the cookie; junk never sticks", async () => { |
| @@ -654,7 +671,7 @@ describe("themes", () => { | ||
| 654 | 671 | const text = await ( |
| 655 | 672 | await fetch(base, { headers: { cookie: "forge_theme=evil" } }) |
| 656 | 673 | ).text(); |
| 657 | expect(text).not.toContain("data-theme="); | |
| 674 | expect(text).not.toMatch(/<html[^>]*\sdata-theme=/); | |
| 658 | 675 | }); |
| 659 | 676 | |
| 660 | 677 | test("theme POST refuses cross-origin and foreign referers", async () => { |
new above
offline