Public: anyone can read this; the crew can change it.
Every wait names itself: a test that ends while a wait waits says what it waited for and what it saw #17308
Merged 40h 25m ago, live in production
fix/waits-name-themselves → main 0 commits
13 files
changed +390
−93
- scripts/test-times.json
- test/helpers/apps-forge.ts
- test/helpers/awaiting.ts
- test/helpers/checks-forge.ts
- test/helpers/crew-standin.ts
- test/helpers/engine_split.ts
- test/helpers/poll.ts
- test/helpers/preload.ts
- test/helpers/tui.ts
- test/surfaces-adversarial.test.ts
- test/tui-apps.test.ts
- test/tui-workflows.test.ts
- test/waits-name-themselves.test.ts
scripts/test-times.json +1
test/helpers/apps-forge.ts +35 −22
| @@ -19,6 +19,7 @@ import { createUser, type User } from "../../src/core/users.ts"; | ||
| 19 | 19 | import { serve, type Servers } from "../../src/http/server.ts"; |
| 20 | 20 | import "./roomy-disk.ts"; |
| 21 | 21 | import { LOOPBACK_PROXIES } from "./loopback-proxy.ts"; |
| 22 | import { describeWait, noted } from "./awaiting.ts"; | |
| 22 | 23 | |
| 23 | 24 | export const APPS_DOMAIN = "apps.test"; |
| 24 | 25 | |
| @@ -135,21 +136,27 @@ export async function deploySettled( | ||
| 135 | 136 | where: (run: Run) => boolean, |
| 136 | 137 | timeoutMs = 60_000, |
| 137 | 138 | ): Promise<Run> { |
| 138 | const deadline = Date.now() + timeoutMs; | |
| 139 | while (Date.now() < deadline) { | |
| 140 | const run = listRuns(app.db, repoId, { kind: "deploy", limit: 50 }).find( | |
| 141 | where, | |
| 142 | ); | |
| 143 | if ( | |
| 144 | run && | |
| 145 | (run.state === "ok" || | |
| 146 | run.state === "failed" || | |
| 147 | run.state === "cancelled") | |
| 148 | ) | |
| 149 | return run; | |
| 150 | await Bun.sleep(10); | |
| 151 | } | |
| 152 | throw new Error("run did not settle in time"); | |
| 139 | return noted( | |
| 140 | `a deploy to settle: ${describeWait(where)}`, | |
| 141 | async () => { | |
| 142 | const deadline = Date.now() + timeoutMs; | |
| 143 | while (Date.now() < deadline) { | |
| 144 | const run = listRuns(app.db, repoId, { kind: "deploy", limit: 50 }).find( | |
| 145 | where, | |
| 146 | ); | |
| 147 | if ( | |
| 148 | run && | |
| 149 | (run.state === "ok" || | |
| 150 | run.state === "failed" || | |
| 151 | run.state === "cancelled") | |
| 152 | ) | |
| 153 | return run; | |
| 154 | await Bun.sleep(10); | |
| 155 | } | |
| 156 | throw new Error("run did not settle in time"); | |
| 157 | }, | |
| 158 | () => listRuns(app.db, repoId, { kind: "deploy", limit: 50 }).map((r) => `${r.env} ${r.state} ${r.summary}`).join("\n"), | |
| 159 | ); | |
| 153 | 160 | } |
| 154 | 161 | |
| 155 | 162 | /** |
| @@ -162,13 +169,19 @@ export async function askForPreview(app: App, repoId: string, number: number, ti | ||
| 162 | 169 | const record = getAppByRepo(app.db, repoId); |
| 163 | 170 | if (runtime === undefined || record === undefined) throw new Error("no app to ask a preview of"); |
| 164 | 171 | const deadline = Date.now() + timeoutMs; |
| 165 | while (Date.now() < deadline) { | |
| 166 | const status = runtime.envStatus(record, `pull-${number}`); | |
| 167 | if (status?.state === "deploying" || status?.state === "running") return; | |
| 168 | if (status?.dormant !== undefined) return runtime.rouse(record, `pull-${number}`); | |
| 169 | await Bun.sleep(10); | |
| 170 | } | |
| 171 | throw new Error(`pull ${number}'s preview was never recorded`); | |
| 172 | return noted( | |
| 173 | `pull ${number}'s preview to be recorded`, | |
| 174 | async () => { | |
| 175 | while (Date.now() < deadline) { | |
| 176 | const status = runtime.envStatus(record, `pull-${number}`); | |
| 177 | if (status?.state === "deploying" || status?.state === "running") return; | |
| 178 | if (status?.dormant !== undefined) return runtime.rouse(record, `pull-${number}`); | |
| 179 | await Bun.sleep(10); | |
| 180 | } | |
| 181 | throw new Error(`pull ${number}'s preview was never recorded`); | |
| 182 | }, | |
| 183 | () => JSON.stringify(runtime.envStatus(record, `pull-${number}`) ?? null), | |
| 184 | ); | |
| 172 | 185 | } |
| 173 | 186 | |
| 174 | 187 | /** |
test/helpers/awaiting.ts added +58
| @@ -0,0 +1,58 @@ | ||
| 1 | /** | |
| 2 | * What a test is waiting for, so a hang names its step (#17279). A wait helper whose own deadline | |
| 3 | * is at or past its test's timeout never raises its error: bun's timeout ends the test first, and | |
| 4 | * the log reads only "timed out after 30000ms". Every wait helper says here what it waits for, | |
| 5 | * and the suite's `afterEach` (`preload.ts`) prints what a test ended still waiting for, with the | |
| 6 | * state it saw, whichever deadline won. | |
| 7 | */ | |
| 8 | ||
| 9 | interface Waiting { | |
| 10 | readonly what: string; | |
| 11 | readonly since: number; | |
| 12 | readonly state?: () => string; | |
| 13 | } | |
| 14 | ||
| 15 | const waiting = new Set<Waiting>(); | |
| 16 | ||
| 17 | /** Notes a wait for `what` (and how to read the state it waits on); the answer ends it. */ | |
| 18 | export function awaiting(what: string, state?: () => string): () => void { | |
| 19 | const w: Waiting = { what, since: performance.now(), ...(state === undefined ? {} : { state }) }; | |
| 20 | waiting.add(w); | |
| 21 | return () => { | |
| 22 | waiting.delete(w); | |
| 23 | }; | |
| 24 | } | |
| 25 | ||
| 26 | /** Runs `wait`, noted as waiting for `what` until it settles either way. */ | |
| 27 | export async function noted<T>(what: string, wait: () => Promise<T>, state?: () => string): Promise<T> { | |
| 28 | const done = awaiting(what, state); | |
| 29 | try { | |
| 30 | return await wait(); | |
| 31 | } finally { | |
| 32 | done(); | |
| 33 | } | |
| 34 | } | |
| 35 | ||
| 36 | /** A condition's own words, for a wait that was given none: its source, on one line, bounded. */ | |
| 37 | export const describeWait = (done: (...args: never[]) => unknown): string => | |
| 38 | done.toString().replace(/\s+/g, " ").slice(0, 200); | |
| 39 | ||
| 40 | /** | |
| 41 | * What the test that just ended was still waiting for, one line each, with the state each saw; | |
| 42 | * forgotten once said. The waits themselves may still be running: a timed-out test's promise is | |
| 43 | * not cancelled, and ends them when it settles. | |
| 44 | */ | |
| 45 | export function stillWaiting(now = performance.now()): string[] { | |
| 46 | const lines: string[] = []; | |
| 47 | for (const w of waiting) { | |
| 48 | let state = ""; | |
| 49 | try { | |
| 50 | state = w.state === undefined ? "" : `\n${w.state()}`; | |
| 51 | } catch (error) { | |
| 52 | state = `\n(its state could not be read: ${String(error)})`; | |
| 53 | } | |
| 54 | lines.push(`the test ended still waiting ${((now - w.since) / 1000).toFixed(1)} s for: ${w.what}${state}`); | |
| 55 | } | |
| 56 | waiting.clear(); | |
| 57 | return lines; | |
| 58 | } |
test/helpers/checks-forge.ts +23 −16
| @@ -27,6 +27,7 @@ import { serve, type Servers } from "../../src/http/server.ts"; | ||
| 27 | 27 | import { listEvents } from "../../src/core/events.ts"; |
| 28 | 28 | import { promote } from "./members.ts"; |
| 29 | 29 | import "./roomy-disk.ts"; |
| 30 | import { describeWait, noted } from "./awaiting.ts"; | |
| 30 | 31 | |
| 31 | 32 | /** A forge.json declaring `run` as the check, with no install step. */ |
| 32 | 33 | export const checkJson = (run: string, extra = ""): string => |
| @@ -61,23 +62,29 @@ export type ChecksForge = { | ||
| 61 | 62 | export function checksForge(prefix = "forge-checks-", extra: Partial<ChecksOptions> = {}): ChecksForge { |
| 62 | 63 | const f = {} as ChecksForge; |
| 63 | 64 | |
| 64 | f.settled = async (where, timeoutMs = 4_000) => { | |
| 65 | const deadline = Date.now() + timeoutMs; | |
| 66 | while (Date.now() < deadline) { | |
| 67 | const run = listRuns(f.app.db, f.repo.id, { kind: "check", limit: 50 }).find(where); | |
| 68 | if (run && (run.state === "ok" || run.state === "failed")) return run; | |
| 69 | await Bun.sleep(10); | |
| 70 | } | |
| 71 | throw new Error("check did not settle"); | |
| 72 | }; | |
| 65 | const checkStates = () => | |
| 66 | listRuns(f.app.db, f.repo.id, { kind: "check", limit: 50 }) | |
| 67 | .map((r) => `${r.env} ${r.state} ${r.commitOid.slice(0, 7)}`) | |
| 68 | .join("\n"); | |
| 69 | f.settled = (where, timeoutMs = 4_000) => | |
| 70 | noted(`a check to settle: ${describeWait(where)}`, async () => { | |
| 71 | const deadline = Date.now() + timeoutMs; | |
| 72 | while (Date.now() < deadline) { | |
| 73 | const run = listRuns(f.app.db, f.repo.id, { kind: "check", limit: 50 }).find(where); | |
| 74 | if (run && (run.state === "ok" || run.state === "failed")) return run; | |
| 75 | await Bun.sleep(10); | |
| 76 | } | |
| 77 | throw new Error(`check did not settle; the checks:\n${checkStates()}`); | |
| 78 | }, checkStates); | |
| 73 | 79 | |
| 74 | f.until = async (done, what, timeoutMs = 4_000) => { | |
| 75 | const deadline = Date.now() + timeoutMs; | |
| 76 | while (!done()) { | |
| 77 | if (Date.now() > deadline) throw new Error(`never came: ${what}`); | |
| 78 | await Bun.sleep(10); | |
| 79 | } | |
| 80 | }; | |
| 80 | f.until = (done, what, timeoutMs = 4_000) => | |
| 81 | noted(what, async () => { | |
| 82 | const deadline = Date.now() + timeoutMs; | |
| 83 | while (!done()) { | |
| 84 | if (Date.now() > deadline) throw new Error(`never came: ${what}`); | |
| 85 | await Bun.sleep(10); | |
| 86 | } | |
| 87 | }, checkStates); | |
| 81 | 88 | |
| 82 | 89 | f.held = (name, then = "test -f OK.md") => |
| 83 | 90 | `while [ ! -f '${f.dataDir}/go-${name}' ]; do sleep 0.01; done; ${then}`; |
test/helpers/crew-standin.ts +20 −13
| @@ -25,6 +25,7 @@ import { createUser, type User } from "../../src/core/users.ts"; | ||
| 25 | 25 | import type { InstallRunner } from "../../src/crew/workdir.ts"; |
| 26 | 26 | import { serve, type Servers } from "../../src/http/server.ts"; |
| 27 | 27 | import "./roomy-disk.ts"; |
| 28 | import { describeWait, noted } from "./awaiting.ts"; | |
| 28 | 29 | |
| 29 | 30 | export interface StandInCrew { |
| 30 | 31 | readonly dataDir: string; |
| @@ -197,19 +198,25 @@ export async function agentRunSettled( | ||
| 197 | 198 | timeoutMs = 30_000, |
| 198 | 199 | pollMs = 100, |
| 199 | 200 | ): Promise<Run> { |
| 200 | const deadline = Date.now() + timeoutMs; | |
| 201 | while (Date.now() < deadline) { | |
| 202 | const run = listRuns(app.db, repo.id, { kind: "agent" }).find(where); | |
| 203 | if ( | |
| 204 | run && | |
| 205 | (run.state === "ok" || | |
| 206 | run.state === "failed" || | |
| 207 | run.state === "cancelled") | |
| 208 | ) | |
| 209 | return run; | |
| 210 | await Bun.sleep(pollMs); | |
| 211 | } | |
| 212 | throw new Error("agent run did not settle"); | |
| 201 | return noted( | |
| 202 | `an agent run to settle: ${describeWait(where)}`, | |
| 203 | async () => { | |
| 204 | const deadline = Date.now() + timeoutMs; | |
| 205 | while (Date.now() < deadline) { | |
| 206 | const run = listRuns(app.db, repo.id, { kind: "agent" }).find(where); | |
| 207 | if ( | |
| 208 | run && | |
| 209 | (run.state === "ok" || | |
| 210 | run.state === "failed" || | |
| 211 | run.state === "cancelled") | |
| 212 | ) | |
| 213 | return run; | |
| 214 | await Bun.sleep(pollMs); | |
| 215 | } | |
| 216 | throw new Error("agent run did not settle"); | |
| 217 | }, | |
| 218 | () => listRuns(app.db, repo.id, { kind: "agent" }).map((r) => `${r.id} ${r.state} ${r.summary}`).join("\n"), | |
| 219 | ); | |
| 213 | 220 | } |
| 214 | 221 | |
| 215 | 222 | /** The stand-in's paths: the binary, and where it writes its last argv and prompt. */ |
test/helpers/engine_split.ts +2 −1
| @@ -10,6 +10,7 @@ import { createApp, type App } from "../../src/app.ts"; | ||
| 10 | 10 | import type { EventTail } from "../../src/core/event_tail.ts"; |
| 11 | 11 | import type { Engine } from "../../src/engine/host.ts"; |
| 12 | 12 | import { waitFor } from "./poll.ts"; |
| 13 | import { describeWait } from "./awaiting.ts"; | |
| 13 | 14 | |
| 14 | 15 | /** The engine thread's test duties (`engine_writer.ts`), by `ENGINE_TEST`. */ |
| 15 | 16 | export const WRITER = new URL("./engine_writer.ts", import.meta.url).href; |
| @@ -39,5 +40,5 @@ export async function forge(dir?: string): Promise<App> { | ||
| 39 | 40 | |
| 40 | 41 | /** Polls `ok` every 5 ms (`waitFor`) until it holds; throws "timed out" after `ms`. */ |
| 41 | 42 | export const until = async (ok: () => boolean, ms = 10_000): Promise<void> => { |
| 42 | if (!(await waitFor(ok, { every: 5, within: ms }))) throw new Error("timed out"); | |
| 43 | if (!(await waitFor(ok, { every: 5, within: ms }))) throw new Error(`timed out waiting for: ${describeWait(ok)}`); | |
| 43 | 44 | }; |
test/helpers/poll.ts +14 −7
| @@ -1,17 +1,24 @@ | ||
| 1 | import { describeWait, noted } from "./awaiting.ts"; | |
| 2 | ||
| 1 | 3 | /** |
| 2 | 4 | * Waits until `done` holds, polling every `every` ms for at most `within` ms; says whether it held. |
| 3 | 5 | * For work that runs on its own pace (a child process, a timer in the code under test) and has no |
| 4 | 6 | * event to await: one wait in one place, so the fixed-wait ratchet (#14321) counts it once, not at |
| 5 | 7 | * every test that polls (#14620: ~90 `for (let i = 0; i < N && cond; i++) await Bun.sleep(M)`). |
| 6 | 8 | */ |
| 7 | export async function waitFor(done: () => boolean | Promise<boolean>, { every = 50, within = 30_000 } = {}): Promise<boolean> { | |
| 9 | export async function waitFor( | |
| 10 | done: () => boolean | Promise<boolean>, | |
| 11 | { every = 50, within = 30_000, what = describeWait(done) }: { every?: number; within?: number; what?: string } = {}, | |
| 12 | ): Promise<boolean> { | |
| 8 | 13 | // Counted in polls, as the loops it replaced were, not against the clock: a loaded machine |
| 9 | 14 | // stretches each sleep, and a wall-clock deadline would then give up sooner than they did. |
| 10 | for (let polls = Math.ceil(within / every); polls > 0; polls--) { | |
| 11 | if (await done()) return true; | |
| 12 | await Bun.sleep(every); | |
| 13 | } | |
| 14 | return await done(); | |
| 15 | return noted(what, async () => { | |
| 16 | for (let polls = Math.ceil(within / every); polls > 0; polls--) { | |
| 17 | if (await done()) return true; | |
| 18 | await Bun.sleep(every); | |
| 19 | } | |
| 20 | return await done(); | |
| 21 | }); | |
| 15 | 22 | } |
| 16 | 23 | |
| 17 | 24 | /** |
| @@ -19,5 +26,5 @@ export async function waitFor(done: () => boolean | Promise<boolean>, { every = | ||
| 19 | 26 | * `timeoutMs`. |
| 20 | 27 | */ |
| 21 | 28 | export async function pollUntil(done: () => boolean | Promise<boolean>, what: string, timeoutMs = 30_000): Promise<void> { |
| 22 | if (!(await waitFor(done, { within: timeoutMs }))) throw new Error(`never came: ${what}`); | |
| 29 | if (!(await waitFor(done, { within: timeoutMs, what }))) throw new Error(`never came: ${what}`); | |
| 23 | 30 | } |
test/helpers/preload.ts +8
| @@ -14,6 +14,8 @@ import { provideEmptyImagesForTest } from "../../src/kernel/db.ts"; | ||
| 14 | 14 | import { emptyImagesIn } from "./empty-images.ts"; |
| 15 | 15 | import { SCRATCH_ROOT } from "./scratch.ts"; |
| 16 | 16 | import { join } from "node:path"; |
| 17 | import { afterEach } from "bun:test"; | |
| 18 | import { stillWaiting } from "./awaiting.ts"; | |
| 17 | 19 | |
| 18 | 20 | installCheapHash(); |
| 19 | 21 | // Fakes in the suite write wherever they like; the Landlock floor (#16835) is proved in |
| @@ -62,3 +64,9 @@ provideMemoryForTest(() => 64 * 1024 ** 3); | ||
| 62 | 64 | // A new child of the suite answers its first look, so its place among the births is given back. |
| 63 | 65 | const { useWatchProbeByDefault } = await import("../../src/spawn/watch_probe_default.ts"); |
| 64 | 66 | useWatchProbeByDefault(() => async () => true); |
| 67 | ||
| 68 | // A test that ended (a timeout above all) while a wait helper still waited says what it waited for | |
| 69 | // and the state it saw (`awaiting.ts`, #17279): a hang names its step on its first failure. | |
| 70 | afterEach(() => { | |
| 71 | for (const line of stillWaiting()) console.error(line); | |
| 72 | }); |
test/helpers/tui.ts +16 −10
| @@ -27,6 +27,7 @@ | ||
| 27 | 27 | */ |
| 28 | 28 | |
| 29 | 29 | import type { App } from "../../src/tui/app/app.ts"; |
| 30 | import { awaiting } from "./awaiting.ts"; | |
| 30 | 31 | |
| 31 | 32 | /** |
| 32 | 33 | * Consecutive quiet turns before the screen is called settled. Three, not |
| @@ -89,17 +90,22 @@ export async function settleUntil( | ||
| 89 | 90 | ): Promise<void> { |
| 90 | 91 | const timeoutMs = options.timeoutMs ?? 20_000; |
| 91 | 92 | const deadline = Date.now() + timeoutMs; |
| 92 | for (;;) { | |
| 93 | await settleApp(app, { | |
| 94 | timeoutMs: Math.max(1_000, deadline - Date.now()), | |
| 95 | }); | |
| 96 | if (app.screen.toText().includes(what)) return; | |
| 97 | if (Date.now() > deadline) { | |
| 98 | throw new Error( | |
| 99 | `never saw ${JSON.stringify(what)} on screen:\n${app.screen.toText()}`, | |
| 100 | ); | |
| 93 | const done = awaiting(`${JSON.stringify(what)} on screen`, () => app.screen.toText()); | |
| 94 | try { | |
| 95 | for (;;) { | |
| 96 | await settleApp(app, { | |
| 97 | timeoutMs: Math.max(1_000, deadline - Date.now()), | |
| 98 | }); | |
| 99 | if (app.screen.toText().includes(what)) return; | |
| 100 | if (Date.now() > deadline) { | |
| 101 | throw new Error( | |
| 102 | `never saw ${JSON.stringify(what)} on screen:\n${app.screen.toText()}`, | |
| 103 | ); | |
| 104 | } | |
| 105 | await turn(); | |
| 101 | 106 | } |
| 102 | await turn(); | |
| 107 | } finally { | |
| 108 | done(); | |
| 103 | 109 | } |
| 104 | 110 | } |
| 105 | 111 |
test/surfaces-adversarial.test.ts +14 −8
| @@ -39,6 +39,7 @@ import { headlessTerminal } from "../src/tui/term/terminal.ts"; | ||
| 39 | 39 | import { statements } from "./helpers/statements.ts"; |
| 40 | 40 | import { settleApp } from "./helpers/tui.ts"; |
| 41 | 41 | import { adminCookie } from "./helpers/admin.ts"; |
| 42 | import { awaiting } from "./helpers/awaiting.ts"; | |
| 42 | 43 | |
| 43 | 44 | quietLogs(); |
| 44 | 45 | |
| @@ -684,14 +685,19 @@ describe("surfaces adversarial: tui", () => { | ||
| 684 | 685 | settle, |
| 685 | 686 | async until(what: string, ms = 15_000) { |
| 686 | 687 | const deadline = Date.now() + ms; |
| 687 | for (;;) { | |
| 688 | await settle(); | |
| 689 | if (app.screen.toText().includes(what)) return; | |
| 690 | if (Date.now() > deadline) | |
| 691 | throw new Error( | |
| 692 | `never saw ${JSON.stringify(what)} on screen:\n${app.screen.toText()}`, | |
| 693 | ); | |
| 694 | await new Promise((r) => setTimeout(r, 20)); | |
| 688 | const done = awaiting(`${JSON.stringify(what)} on screen`, () => app.screen.toText()); | |
| 689 | try { | |
| 690 | for (;;) { | |
| 691 | await settle(); | |
| 692 | if (app.screen.toText().includes(what)) return; | |
| 693 | if (Date.now() > deadline) | |
| 694 | throw new Error( | |
| 695 | `never saw ${JSON.stringify(what)} on screen:\n${app.screen.toText()}`, | |
| 696 | ); | |
| 697 | await new Promise((r) => setTimeout(r, 20)); // real-timer: the harness polls the screen between settles | |
| 698 | } | |
| 699 | } finally { | |
| 700 | done(); | |
| 695 | 701 | } |
| 696 | 702 | }, |
| 697 | 703 | stop: () => app.stop(), |
test/tui-apps.test.ts +14 −8
| @@ -52,6 +52,7 @@ import { | ||
| 52 | 52 | type HeadlessTerminal, |
| 53 | 53 | } from "../src/tui/term/terminal.ts"; |
| 54 | 54 | import { settleApp } from "./helpers/tui.ts"; |
| 55 | import { awaiting } from "./helpers/awaiting.ts"; | |
| 55 | 56 | |
| 56 | 57 | quietLogs(); |
| 57 | 58 | |
| @@ -130,14 +131,19 @@ function boot( | ||
| 130 | 131 | // read only "timed out after 30000ms", and the step it hung on was lost). |
| 131 | 132 | async until(what, ms = 20_000) { |
| 132 | 133 | const deadline = Date.now() + ms; |
| 133 | for (;;) { | |
| 134 | await settle(); | |
| 135 | if (app.screen.toText().includes(what)) return; | |
| 136 | if (Date.now() > deadline) | |
| 137 | throw new Error( | |
| 138 | `never saw ${JSON.stringify(what)} on screen:\n${app.screen.toText()}`, | |
| 139 | ); | |
| 140 | await new Promise((r) => setTimeout(r, 20)); | |
| 134 | const done = awaiting(`${JSON.stringify(what)} on screen`, () => app.screen.toText()); | |
| 135 | try { | |
| 136 | for (;;) { | |
| 137 | await settle(); | |
| 138 | if (app.screen.toText().includes(what)) return; | |
| 139 | if (Date.now() > deadline) | |
| 140 | throw new Error( | |
| 141 | `never saw ${JSON.stringify(what)} on screen:\n${app.screen.toText()}`, | |
| 142 | ); | |
| 143 | await new Promise((r) => setTimeout(r, 20)); // real-timer: the harness polls the screen between settles | |
| 144 | } | |
| 145 | } finally { | |
| 146 | done(); | |
| 141 | 147 | } |
| 142 | 148 | }, |
| 143 | 149 | async select(name) { |
test/tui-workflows.test.ts +14 −8
| @@ -45,6 +45,7 @@ import { | ||
| 45 | 45 | import { settleApp } from "./helpers/tui.ts"; |
| 46 | 46 | import { fixtureSlot } from "./helpers/slot.ts"; |
| 47 | 47 | import { seedAdmin } from "./helpers/admin.ts"; |
| 48 | import { awaiting } from "./helpers/awaiting.ts"; | |
| 48 | 49 | |
| 49 | 50 | quietLogs(); |
| 50 | 51 | |
| @@ -143,14 +144,19 @@ function boot( | ||
| 143 | 144 | opened: () => [...opened], |
| 144 | 145 | async until(what, ms = 30_000) { |
| 145 | 146 | const deadline = Date.now() + ms; |
| 146 | for (;;) { | |
| 147 | await settle(); | |
| 148 | if (app.screen.toText().includes(what)) return; | |
| 149 | if (Date.now() > deadline) | |
| 150 | throw new Error( | |
| 151 | `never saw ${JSON.stringify(what)} on screen:\n${app.screen.toText()}`, | |
| 152 | ); | |
| 153 | await new Promise((r) => setTimeout(r, 20)); | |
| 147 | const done = awaiting(`${JSON.stringify(what)} on screen`, () => app.screen.toText()); | |
| 148 | try { | |
| 149 | for (;;) { | |
| 150 | await settle(); | |
| 151 | if (app.screen.toText().includes(what)) return; | |
| 152 | if (Date.now() > deadline) | |
| 153 | throw new Error( | |
| 154 | `never saw ${JSON.stringify(what)} on screen:\n${app.screen.toText()}`, | |
| 155 | ); | |
| 156 | await new Promise((r) => setTimeout(r, 20)); // real-timer: the harness polls the screen between settles | |
| 157 | } | |
| 158 | } finally { | |
| 159 | done(); | |
| 154 | 160 | } |
| 155 | 161 | }, |
| 156 | 162 | async walk(key, what, limit = 40) { |
test/waits-name-themselves.test.ts added +171
| @@ -0,0 +1,171 @@ | ||
| 1 | /** | |
| 2 | * Every wait in the suite names itself, so a hang says its step on its first failure (#17279). | |
| 3 | * A wait helper whose own deadline is at or past its test's timeout never raises its error: bun's | |
| 4 | * timeout ends the test first, and the log reads only "timed out after 30000ms". A wait that | |
| 5 | * notes itself (`helpers/awaiting.ts`: `awaiting`, `noted`, or through `waitFor`, `pollUntil`, | |
| 6 | * `settleUntil`) is printed by the suite's `afterEach` with the state it saw, whichever deadline | |
| 7 | * won. | |
| 8 | * | |
| 9 | * A wait helper is a function named for waiting (until, settled, …Settled, pollUntil, …) that | |
| 10 | * polls (`Date.now()`, `Bun.sleep`, `setTimeout`). The ones that did not note themselves when this | |
| 11 | * rule came are listed; the list only shrinks. | |
| 12 | */ | |
| 13 | import { expect, test } from "bun:test"; | |
| 14 | import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; | |
| 15 | import { tmpdir } from "node:os"; | |
| 16 | import { join, resolve } from "node:path"; | |
| 17 | import { Glob } from "bun"; | |
| 18 | ||
| 19 | const NAME = String.raw`(?:until|waitUntil|pollUntil|eventually|settleUntil|settled|[a-z][A-Za-z]*Settled)`; | |
| 20 | const DEF = new RegExp( | |
| 21 | String.raw`(?:^|\n)[ \t]*(?:export\s+)?(?:(?:async\s+)?function\s+(${NAME})\s*[<(]|(?:const|let)\s+(${NAME})\s*=\s*async\b|(?:async\s+)?(${NAME})\s*\([^)\n]*\)\s*(?::[^{\n]*)?\{|(${NAME})\s*:\s*async\b|f\.(${NAME})\s*=\s*)`, | |
| 22 | "g", | |
| 23 | ); | |
| 24 | const NOTES = /\b(?:awaiting|noted|waitFor|pollUntil|settleUntil)\s*\(/; | |
| 25 | const POLLS = /Date\.now\(\)|Bun\.sleep|setTimeout/; | |
| 26 | ||
| 27 | /** The wait helpers that do not note what they wait for, as `file:name`. */ | |
| 28 | function silentWaits(): string[] { | |
| 29 | const out = new Set<string>(); | |
| 30 | for (const file of new Glob("test/**/*.ts").scanSync(".")) { | |
| 31 | const text = readFileSync(file, "utf8"); | |
| 32 | for (const m of text.matchAll(DEF)) { | |
| 33 | const name = m.slice(1).find((x) => x !== undefined)!; | |
| 34 | const body = text.slice(m.index!, m.index! + 900); | |
| 35 | if (POLLS.test(body) && !NOTES.test(body)) out.add(`${file}:${name}`); | |
| 36 | } | |
| 37 | } | |
| 38 | return [...out].sort(); | |
| 39 | } | |
| 40 | ||
| 41 | /** The silent waits there were when this rule came (2026-12-01); none is added. */ | |
| 42 | const KNOWN: ReadonlySet<string> = new Set([ | |
| 43 | "test/app-domains.test.ts:settled", | |
| 44 | "test/app-variants.test.ts:until", | |
| 45 | "test/apps-adversarial-grants.test.ts:settled", | |
| 46 | "test/apps-adversarial-rules.test.ts:settled", | |
| 47 | "test/apps-adversarial-teardown.test.ts:settled", | |
| 48 | "test/apps-adversarial.test.ts:settled", | |
| 49 | "test/apps-build-preview.test.ts:until", | |
| 50 | "test/apps-busy.test.ts:until", | |
| 51 | "test/apps-closed-pull-no-preview.test.ts:settled", | |
| 52 | "test/apps-idle.test.ts:until", | |
| 53 | "test/apps-rollout.test.ts:settled", | |
| 54 | "test/apps-sleep-slider.test.ts:until", | |
| 55 | "test/apps-sleep.test.ts:until", | |
| 56 | "test/apps-tests-clause.test.ts:settled", | |
| 57 | "test/apps-token.test.ts:settled", | |
| 58 | "test/apps-toolbar.test.ts:settled", | |
| 59 | "test/apps-ws.test.ts:settled", | |
| 60 | "test/apps.test.ts:bootSettled", | |
| 61 | "test/boards-stream.test.ts:until", | |
| 62 | "test/boundary-pull-egress.test.ts:checkSettled", | |
| 63 | "test/boundary-tokens-crew.test.ts:settled", | |
| 64 | "test/build-journey.test.ts:until", | |
| 65 | "test/carried-deploy.test.ts:until", | |
| 66 | "test/cell-outlives-host.test.ts:until", | |
| 67 | "test/cell-stopped-dies-with-forge.test.ts:until", | |
| 68 | "test/check-base-moved.test.ts:settled", | |
| 69 | "test/check-budget.test.ts:until", | |
| 70 | "test/check-disk-hold.test.ts:until", | |
| 71 | "test/check-disk-used.test.ts:until", | |
| 72 | "test/check-install.test.ts:settled", | |
| 73 | "test/check-tiers.test.ts:settled", | |
| 74 | "test/check-verify-rerun.test.ts:until", | |
| 75 | "test/check-verify.test.ts:until", | |
| 76 | "test/checks-placement.test.ts:settled", | |
| 77 | "test/contain.test.ts:settled", | |
| 78 | "test/contribute-check-text.test.ts:until", | |
| 79 | "test/credentials.test.ts:until", | |
| 80 | "test/crew-check-cpu-stop.test.ts:until", | |
| 81 | "test/crew-check-failed.test.ts:until", | |
| 82 | "test/crew-direction.test.ts:until", | |
| 83 | "test/crew-early.test.ts:until", | |
| 84 | "test/crew-interrupted.test.ts:settled", | |
| 85 | "test/crew-merge-conflict.test.ts:until", | |
| 86 | "test/crew-proposal.test.ts:settled", | |
| 87 | "test/crew-red-outside-diff.test.ts:until", | |
| 88 | "test/crew-remotes.test.ts:settled", | |
| 89 | "test/crew-wrapup.test.ts:settled", | |
| 90 | "test/deploy-hold-runtime.test.ts:until", | |
| 91 | "test/egress-leak.test.ts:until", | |
| 92 | "test/following.test.ts:until", | |
| 93 | "test/fork-follow-conflict.test.ts:until", | |
| 94 | "test/fork-pull-approval.test.ts:until", | |
| 95 | "test/git-ssh.test.ts:settled", | |
| 96 | "test/guest-pull-env.test.ts:checkSettled", | |
| 97 | "test/helpers/celld_serve.ts:until", | |
| 98 | "test/helpers/real-spawn-e2e.ts:settled", | |
| 99 | "test/helpers/real-stranger-e2e.ts:until", | |
| 100 | "test/landing.test.ts:until", | |
| 101 | "test/look-pages-comment.test.ts:settled", | |
| 102 | "test/looks.test.ts:until", | |
| 103 | "test/machine-shell.test.ts:until", | |
| 104 | "test/mcp-restore-drill.test.ts:settled", | |
| 105 | "test/meter.test.ts:until", | |
| 106 | "test/one-core-hold.test.ts:until", | |
| 107 | "test/outsiders-member.test.ts:settled", | |
| 108 | "test/pace-floor.test.ts:until", | |
| 109 | "test/preview-spec.test.ts:until", | |
| 110 | "test/probe.test.ts:until", | |
| 111 | "test/probes.test.ts:until", | |
| 112 | "test/pull-tui-check-tick.test.ts:until", | |
| 113 | "test/pull-variants.test.ts:until", | |
| 114 | "test/quotas-running.test.ts:until", | |
| 115 | "test/repo-transfer-follow.test.ts:until", | |
| 116 | "test/service-run.test.ts:until", | |
| 117 | "test/snapshot-one-core.test.ts:until", | |
| 118 | "test/spawn-app-links.test.ts:until", | |
| 119 | "test/spawn-events.test.ts:until", | |
| 120 | "test/steer.test.ts:until", | |
| 121 | "test/subscriptions.test.ts:until", | |
| 122 | "test/team-dispatch.test.ts:until", | |
| 123 | "test/tui-about-default-branch.test.ts:until", | |
| 124 | "test/tui-about-description.test.ts:until", | |
| 125 | "test/tui-about-detach.test.ts:until", | |
| 126 | "test/tui-about-outsiders.test.ts:until", | |
| 127 | "test/tui-about-visibility.test.ts:until", | |
| 128 | "test/tui-app-templates.test.ts:until", | |
| 129 | "test/tui-apps.test.ts:settled", | |
| 130 | "test/tui-backups.test.ts:until", | |
| 131 | "test/tui-migrate.test.ts:until", | |
| 132 | "test/tui-newapp.test.ts:until", | |
| 133 | "test/tui-transfer-repo.test.ts:until", | |
| 134 | "test/usage.test.ts:until", | |
| 135 | "test/week-pump.test.ts:until", | |
| 136 | ]); | |
| 137 | ||
| 138 | test("a wait helper notes what it waits for: none is added silent, and the list only shrinks", () => { | |
| 139 | const silent = silentWaits(); | |
| 140 | expect(silent.filter((w) => !KNOWN.has(w))).toEqual([]); | |
| 141 | // One that notes itself now, or is gone, comes off the list. | |
| 142 | expect([...KNOWN].filter((w) => !silent.includes(w))).toEqual([]); | |
| 143 | }); | |
| 144 | ||
| 145 | test("a test that times out while a wait waits says what it waited for, and the state it saw", async () => { | |
| 146 | const dir = mkdtempSync(join(tmpdir(), "waits-name-")); | |
| 147 | try { | |
| 148 | const poll = resolve("test/helpers/poll.ts"); | |
| 149 | const awaitingTs = resolve("test/helpers/awaiting.ts"); | |
| 150 | writeFileSync( | |
| 151 | join(dir, "hang.test.ts"), | |
| 152 | `import { afterEach, test } from "bun:test";\n` + | |
| 153 | `import { pollUntil } from ${JSON.stringify(poll)};\n` + | |
| 154 | `import { awaiting, stillWaiting } from ${JSON.stringify(awaitingTs)};\n` + | |
| 155 | `afterEach(() => { for (const line of stillWaiting()) console.error(line); });\n` + | |
| 156 | `test("hangs", async () => {\n` + | |
| 157 | ` const done = awaiting("the screen to say hello", () => "the screen: goodbye");\n` + | |
| 158 | ` await pollUntil(() => false, "the moon to rise");\n` + | |
| 159 | ` done();\n` + | |
| 160 | `}, 200);\n`, | |
| 161 | ); | |
| 162 | const child = Bun.spawn(["bun", "test", "./hang.test.ts"], { cwd: dir, stdout: "pipe", stderr: "pipe" }); | |
| 163 | const [out, err] = await Promise.all([new Response(child.stdout).text(), new Response(child.stderr).text()]); | |
| 164 | expect(await child.exited).not.toBe(0); | |
| 165 | const said = out + err; | |
| 166 | expect(said).toMatch(/the test ended still waiting [0-9.]+ s for: the moon to rise/); | |
| 167 | expect(said).toMatch(/the test ended still waiting [0-9.]+ s for: the screen to say hello\nthe screen: goodbye/); | |
| 168 | } finally { | |
| 169 | rmSync(dir, { recursive: true, force: true }); | |
| 170 | } | |
| 171 | }, 20_000); |
new above
offline