Files
clean-architecture-frontend…/tests/support/browser/strict-browser-test.ts
DongHyeonka 7679ce6592 revert: #55 되돌림(411c9338)을 되돌려 그룹 C 검사기 변경을 다시 넣는다
This reverts commit 411c9338c6.

문제: #55(b4895894)가 main run 376의 FE-GATE-030(browser file/storage
제거 드릴)을 깨서 411c9338(#64)로 통째로 되돌렸다. #55의 검사기 진단과
시험 빈틈 고침 12커밋이 main에 없다.

고른 방법: 되돌림을 되돌려 #55를 그대로 다시 넣고, 드릴을 깬 원인은
다음 커밋에서 따로 고친다. 리뷰어가 이 커밋이 #55와 같은지 트리로 볼 수
있다. 이 커밋의 트리는 b4895894 트리에 그 뒤 main에 들어온 #63·#65만
더한 것이다(git diff b4895894 <이 커밋>: docs/ledger 두 파일,
cross-context-invalidation adapter 한 파일과 시험 두 파일).
버린 대안: #55 커밋 12개 cherry-pick. 71606ff1 merge의 충돌 해소분을
다시 풀어야 하고 결과 트리를 b4895894와 대조하기 어렵다.

위험·한계: 이 커밋만으로는 main 단계의 FE-GATE-030이 run 376처럼
실패한다(같은 트리 내용). PR 단계 게이트는 돈다.

검증: 다음 커밋 본문에 적는다. 그 커밋의 새 단언을 이 커밋의 검사기에
돌리면 실패한다(RED).

Refs: #55, #64
2026-11-19 16:32:00 +09:00

126 lines
4.9 KiB
TypeScript

import type { ConsoleMessage, Page } from "@playwright/test";
import { expect, test as base } from "./browser-test.ts";
import { trackPreloadUse } from "./preload-use.ts";
/**
* 1103/11-09. WebKit's guess, three seconds after the load event, that a
* preload went unused. It fired for chunks the page ran a moment later and
* missed preloads the page never ran once the test had ended. The fixture
* checks preload use itself, in every engine (`preloadunused:` below).
*/
const WEBKIT_UNUSED_PRELOAD_GUESS =
/^The resource \S+ was preloaded using link preload but not used within a few seconds from the window's load event\. Please make sure it wasn't preloaded for nothing\.$/u;
function ignoredConsole(message: ConsoleMessage) {
return (
message.type() === "warning" &&
(message.text().includes("NO_COLOR") ||
WEBKIT_UNUSED_PRELOAD_GUESS.test(message.text()))
);
}
/**
* 1112/03-05. Records a page's browser failures as `console:…`, `pageerror:…`
* and `requestfailed:…` lines, with the same rules the strict fixture judges
* by. The fixture watches its `page` and every other tab opened in the same
* context; a spec that drives a context of its own watches its pages with
* this. `preloads` also checks that every modulepreload ran (preload-use.ts)
* in the documents open when `stop` runs; attach it before the navigation
* that loads them.
*/
export async function watchBrowserFailures(
page: Page,
options: Readonly<{ preloads?: boolean }> = {},
): Promise<Readonly<{ stop(): Promise<string[]> }>> {
const failures: string[] = [];
const onConsole = (message: ConsoleMessage) => {
if (["error", "warning"].includes(message.type()) && !ignoredConsole(message)) {
failures.push(`console:${message.type()}:${message.text()}`);
}
};
const onPageError = (error: Error) => {
failures.push(`pageerror:${error.name}`);
};
const onRequestFailed = (request: import("@playwright/test").Request) => {
failures.push(
`requestfailed:${request.method()}:${new URL(request.url()).pathname}:${request.failure()?.errorText ?? "unknown"}`,
);
};
page.on("console", onConsole);
page.on("pageerror", onPageError);
page.on("requestfailed", onRequestFailed);
const preloads = options.preloads ? await trackPreloadUse(page) : null;
return Object.freeze({
async stop() {
if (preloads) failures.push(...(await preloads.check()).failures);
page.off("console", onConsole);
page.off("pageerror", onPageError);
page.off("requestfailed", onRequestFailed);
return [...failures];
},
});
}
/**
* 1105/11-01. What the strict `page` fixture does with every failure it
* recorded, once the test has ended.
*/
export type BrowserFailureVerdict = Readonly<{
judge(failures: readonly string[]): void;
}>;
export const test = base.extend<{
/**
* Browser failures a test provokes on purpose, matched against the recorded
* `console:…`, `pageerror:…`, `requestfailed:…` or `preloadunused:…` line
* (for example the browser's log line for a mocked 4xx, or the chunks
* preloaded with a chunk the test removed). Everything else still fails. One
* pattern, not an array: `test.use` reads an array as a `[value, options]`
* fixture tuple.
*/
allowedBrowserFailure: RegExp | undefined;
/**
* Fails the test for every recorded failure `allowedBrowserFailure` does not
* match. The record is made after the test body, where no test can read it,
* so tests/e2e/preload-use.spec.ts replaces this verdict to read what the
* fixture recorded and prove the record itself. No other spec replaces it.
*/
browserFailureVerdict: BrowserFailureVerdict;
}>({
allowedBrowserFailure: [undefined, { option: true }],
browserFailureVerdict: async ({ allowedBrowserFailure }, use) => {
await use(
Object.freeze({
judge(failures: readonly string[]) {
expect(
failures.filter(
(failure) => !allowedBrowserFailure?.test(failure),
),
"unexpected browser console/page errors",
).toEqual([]);
},
}),
);
},
page: async ({ page, browserFailureVerdict }, use) => {
const watched = [await watchBrowserFailures(page, { preloads: true })];
// 1112/03-05. A second tab of the same context (the cross-tab specs)
// went unwatched. Its listeners are attached as the tab opens, before
// the spec can navigate it; preload use is checked on the first page only.
const pending: Promise<void>[] = [];
const onPage = (opened: Page) => {
pending.push(watchBrowserFailures(opened).then((watcher) => void watched.push(watcher)));
};
page.context().on("page", onPage);
await use(page);
page.context().off("page", onPage);
await Promise.all(pending);
const failures = (await Promise.all(watched.map((watcher) => watcher.stop()))).flat();
browserFailureVerdict.judge(failures);
},
});
export { expect };
export type { Page };