Skip to content

Commit ebb057f

Browse files
fix(cli): a check that hits its deadline keeps the findings it already made (#6190)
1 parent 0b8f5ec commit ebb057f

5 files changed

Lines changed: 88 additions & 3 deletions

File tree

‎packages/cli/src/commands/check.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,50 @@ describe("check pipeline", () => {
17861786
}
17871787
});
17881788

1789+
it("a deadline keeps the layout findings the finished samples already made", async () => {
1790+
vi.useFakeTimers();
1791+
try {
1792+
let aborted!: AbortSignal;
1793+
const driver = fakeDriver({
1794+
collectLayout: vi.fn(async (time: number) => [
1795+
layoutIssue("warning", { time, code: "container_overflow" }),
1796+
]),
1797+
collectOverlapGrid: vi.fn(
1798+
() =>
1799+
new Promise<never>((_, reject) =>
1800+
aborted.addEventListener("abort", () => reject(aborted.reason)),
1801+
),
1802+
),
1803+
});
1804+
const { deps } = dependencies(driver);
1805+
deps.runBrowserCheck = async (_project, options, motion, deadline) => {
1806+
aborted = deadline!.signal;
1807+
deadline!.start();
1808+
return runAuditGrid(
1809+
driver,
1810+
options,
1811+
motion,
1812+
undefined,
1813+
deadline!.enter,
1814+
undefined,
1815+
deadline!.keepPartial,
1816+
);
1817+
};
1818+
const pending = runCheckPipeline(PROJECT, DEFAULT_CHECK_OPTIONS, deps);
1819+
await vi.advanceTimersByTimeAsync(120_000);
1820+
const report = await pending;
1821+
expect(report.layout.findings.some((finding) => finding.code === "container_overflow")).toBe(
1822+
true,
1823+
);
1824+
expect(report.browserSkipped).toBe(false);
1825+
expect(
1826+
report.runtime.findings.some((finding) => finding.code === "check_runtime_failure"),
1827+
).toBe(true);
1828+
} finally {
1829+
vi.useRealTimers();
1830+
}
1831+
});
1832+
17891833
it("--strict flips a warnings-only result from exit 0 to exit 1", async () => {
17901834
const warningDriver = () =>
17911835
fakeDriver({

‎packages/cli/src/utils/checkBrowser.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ export async function runBrowserCheck(
313313
await waitForPreferredSeekTarget(page);
314314

315315
const rootAnchor = await resolveRootAnchor(page);
316+
deadline?.keepPartial(() => ({
317+
runtimeFindings: dropFailedProxyEchoes(
318+
keepBrokenImageAborts(drafts, new Set()),
319+
failedPaths,
320+
).map((draft) => runtimeFinding(draft, rootAnchor)),
321+
}));
316322
const launchSettleMs = Date.now() - launchSettleStart;
317323
// validate's per-media-element audit, kept in the consolidation: a clip
318324
// whose intrinsic duration is meaningfully shorter than its data-duration
@@ -338,6 +344,7 @@ export async function runBrowserCheck(
338344
helpers,
339345
deadline?.enter,
340346
logCheckProgress,
347+
deadline?.keepPartial,
341348
);
342349
helperSession?.stop();
343350
const helper = await helperOpen?.catch(() => undefined);

‎packages/cli/src/utils/checkDeadline.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Browser, Page } from "puppeteer-core";
22
import { normalizeErrorMessage } from "./errorMessage.js";
33
import { withRemainingBudget } from "../capture/captureTimeout.js";
44
import { BrowserCleanupError, closeManagedBrowser } from "../browser/launch.js";
5+
import type { CheckBrowserResult } from "./checkTypes.js";
56

67
const CHECK_IDLE_MS = 120_000;
78
const SCRIPT_START = "[hyperframes:script:start] ";
@@ -46,13 +47,22 @@ export class CheckDeadline {
4647
private phase: "waiting" | "started" | "closed" = "waiting";
4748
private work: { phase: CheckPhase; since: number } | undefined;
4849
private readonly spent = new Map<CheckPhase, number>();
50+
private readonly partials: Array<() => Partial<CheckBrowserResult>> = [];
4951

5052
constructor() {
5153
this.expiry = new Promise((_, reject) => {
5254
this.rejectExpiry = reject;
5355
});
5456
}
5557

58+
keepPartial = (read: () => Partial<CheckBrowserResult>): void => {
59+
this.partials.push(read);
60+
};
61+
62+
partialResult(): Partial<CheckBrowserResult> {
63+
return Object.assign({}, ...this.partials.map((read) => read()));
64+
}
65+
5666
enter = (phase: CheckPhase): void => {
5767
if (this.expired) return;
5868
const now = Date.now();

‎packages/cli/src/utils/checkPipeline.ts‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ async function collectGridSamples(
517517
helpers: Promise<CheckAuditDriver[]>,
518518
enter: (phase: CheckPhase) => void,
519519
report: CheckProgress,
520+
keep: (read: () => Partial<CheckBrowserResult>) => void,
520521
): Promise<GridSamples> {
521522
const geometryEnabled = grid.captionSamples.length > 0 || grid.frameSamples.length > 0;
522523
const sets: SampleSets = {
@@ -528,6 +529,17 @@ async function collectGridSamples(
528529
const times = mergeSampleTimes(grid.layoutSamples, motion.times);
529530
const pending = times.map((_, index) => index);
530531
const perTime: GridSamples[] = [];
532+
keep(() => {
533+
const so = emptyGridSamples();
534+
for (const samples of perTime) if (samples) appendGridSamples(so, samples);
535+
const contrast = buildContrastResults(so.contrastEntries);
536+
return {
537+
layoutIssues: so.layoutIssues,
538+
contrastFindings: contrast.findings,
539+
contrastChecked: so.contrastEntries.length,
540+
contrastPassed: contrast.passed,
541+
};
542+
});
531543
let finished = 0;
532544
const work = async (worker: CheckAuditDriver, taken: number[] = []) => {
533545
for (let index = pending.shift(); index !== undefined; index = pending.shift()) {
@@ -1466,6 +1478,7 @@ export async function runAuditGrid(
14661478
helpers: Promise<CheckAuditDriver[]> = Promise.resolve([]),
14671479
enter: (phase: CheckPhase) => void = () => {},
14681480
report: CheckProgress = () => {},
1481+
keep: (read: () => Partial<CheckBrowserResult>) => void = () => {},
14691482
): Promise<CheckBrowserResult> {
14701483
enter("samples");
14711484
await driver.initialize(options.contrast);
@@ -1476,7 +1489,16 @@ export async function runAuditGrid(
14761489
grid.duration,
14771490
);
14781491
const seekLoopStart = Date.now();
1479-
const collected = await collectGridSamples(driver, options, grid, plan, helpers, enter, report);
1492+
const collected = await collectGridSamples(
1493+
driver,
1494+
options,
1495+
grid,
1496+
plan,
1497+
helpers,
1498+
enter,
1499+
report,
1500+
keep,
1501+
);
14801502
const seekLoopMs = Date.now() - seekLoopStart;
14811503

14821504
let motionIssues = plan.preflightIssues;
@@ -1642,8 +1664,9 @@ export async function runCheckPipeline(
16421664
dependencies.runBrowserCheck(project, options, motion, deadline, browserAhead),
16431665
);
16441666
} catch (error) {
1645-
browser = emptyBrowserResult();
1646-
browser.runtimeFindings.push(runtimeFailure(error));
1667+
const found = deadline.partialResult();
1668+
browser = { ...emptyBrowserResult(), ...found, skipped: Object.keys(found).length === 0 };
1669+
browser.runtimeFindings = [...browser.runtimeFindings, runtimeFailure(error)];
16471670
}
16481671

16491672
const snapshotFiles = options.snapshots

‎packages/cli/src/utils/checkTypes.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ export type RunAuditGrid = (
329329
helpers?: Promise<CheckAuditDriver[]>,
330330
enter?: (phase: CheckPhase) => void,
331331
report?: CheckProgress,
332+
keep?: (read: () => Partial<CheckBrowserResult>) => void,
332333
) => Promise<CheckBrowserResult>;
333334

334335
export interface CheckSection<T extends CheckFinding = CheckFinding> {

0 commit comments

Comments
 (0)