Files
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

333 lines
12 KiB
TypeScript

import { readFile } from "node:fs/promises";
import path from "node:path";
import type { Page } from "@playwright/test";
import { addGeneratedFeature } from "../../scripts/lib/generated-feature.ts";
import { testRunPath } from "../../scripts/lib/run-isolation.ts";
// The tracker's own tests run without the strict verdict, on the same
// browser set-up as every other spec.
import { expect, test } from "../support/browser/browser-test.ts";
import { trackPreloadUse } from "../support/browser/preload-use.ts";
import { test as strictTest } from "../support/browser/strict-browser-test.ts";
import {
prepareFixtureBuilds,
removeFixtureBuilds,
type FixtureBuilds,
} from "./support/fixture-builds.ts";
import { startBuildServer } from "./support/service-worker-builds.ts";
/**
* 1103/11-09. The strict browser fixture ignores WebKit's "preloaded using
* link preload but not used within a few seconds" warning. WebKit checks once,
* three seconds after the load event, so a preload whose module the page
* reaches a moment later was reported as wasted, and one the page never runs
* went unreported when the test ended first. The fixture checks instead, in
* every engine, that each preloaded script was run by the end of the test.
* These tests prove that check on a real build: the tracker itself, and
* (1105/11-01) the strict fixture that fails a test with what it records.
*/
type ManifestEntry = Readonly<{
file: string;
imports?: readonly string[];
isEntry?: boolean;
}>;
/**
* The build the preview serves: this run's own under TEST_RUN_ID (1105/12-04),
* or `dist`.
*/
async function buildManifest(
dist = testRunPath("dist"),
): Promise<Readonly<Record<string, ManifestEntry>>> {
return JSON.parse(await readFile(path.join(dist, ".vite/manifest.json"), "utf8")) as Record<
string,
ManifestEntry
>;
}
/** The platform overview has drawn its contract tables, or their empty state. */
async function expectContractTables(page: Page): Promise<void> {
await expect(
page
.getByRole("table", { name: "설치된 HTTP 오퍼레이션" })
.or(page.getByText("설치된 HTTP 오퍼레이션이 없습니다.")),
).toBeVisible();
}
function staticClosure(
manifest: Readonly<Record<string, ManifestEntry>>,
roots: readonly string[],
): Set<string> {
const reached = new Set<string>();
const pending = [...roots];
for (let key = pending.pop(); key !== undefined; key = pending.pop()) {
if (reached.has(key) || !manifest[key]) continue;
reached.add(key);
pending.push(...(manifest[key].imports ?? []));
}
return reached;
}
async function openHome(page: Page): Promise<void> {
await page.goto("/");
await expect(page.getByRole("heading", { level: 1 })).toBeVisible();
}
/**
* Mutation: one build chunk the page has not fetched, preloaded for nothing.
* Returns its path once the browser has fetched it.
*/
async function preloadAChunkThePageNeverRuns(page: Page): Promise<string> {
const scripts = Object.values(await buildManifest())
.map((entry) => `/${entry.file}`)
.filter((file) => file.endsWith(".js"));
const unused = await page.evaluate(async (files) => {
const fetched = new Set(
performance
.getEntriesByType("resource")
.map((entry) => new URL(entry.name).pathname),
);
const file = files.find((candidate) => !fetched.has(candidate));
if (file === undefined) throw new Error("every build chunk was fetched");
const link = document.createElement("link");
link.rel = "modulepreload";
link.href = file;
document.head.append(link);
return file;
}, scripts);
await page.waitForFunction(
(file) =>
performance
.getEntriesByType("resource")
.some((entry) => new URL(entry.name).pathname === file),
unused,
);
return unused;
}
/** The warning text WebKit writes for its three-second guess. */
const webKitUnusedPreloadGuess = (url: string) =>
`The resource ${url} 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.`;
test("reports a modulepreload the page never runs", async ({ page }) => {
const preloads = await trackPreloadUse(page);
await openHome(page);
const unused = await preloadAChunkThePageNeverRuns(page);
expect((await preloads.check({ timeoutMs: 1_000 })).failures).toEqual([
`preloadunused:${unused}`,
]);
});
test("counts the chunks a route preloads and then runs as used", async ({
page,
}) => {
const preloads = await trackPreloadUse(page);
await page.goto("/");
await page
.getByRole("navigation", { name: "주요 탐색" })
.getByRole("link", { name: "플랫폼 구성" })
.click();
await expectContractTables(page);
const result = await preloads.check();
expect(result.preloaded.length).toBeGreaterThan(0);
expect(result.failures).toEqual([]);
});
/**
* The 1103/11-09 failure, made deterministic: the route's runtime chunks are
* preloaded right after load, and the chunk that imports the deepest one
* arrives 3.5 s later. WebKit's check runs in between and warns about a chunk
* the page then runs. The strict fixture must pass. The template installs no
* feature, so this runs on a build of a copy with the one `create:feature`
* writes (scripts/lib/generated-feature.ts).
*/
let featureBuild: FixtureBuilds<"a"> | undefined;
strictTest.describe("with a feature installed", () => {
strictTest.beforeAll(async () => {
strictTest.setTimeout(300_000);
featureBuild = await prepareFixtureBuilds("preload-use", [
{ name: "a", edit: addGeneratedFeature },
]);
});
strictTest.afterAll(async () => {
await removeFixtureBuilds(featureBuild);
});
strictTest(
"passes a route whose deepest preloaded chunk is reached after WebKit's check",
async ({ page }) => {
if (!featureBuild) throw new Error("The feature build was not prepared.");
const manifest = await buildManifest(featureBuild.dist.a);
const initial = staticClosure(manifest, [
...Object.keys(manifest).filter((key) => manifest[key]?.isEntry),
"src/bootstrap/boot.tsx",
]);
// A feature runtime's static import that itself imports a chunk the
// boot has not loaded: the module graph reaches that chunk only once
// this one has arrived.
const parent = Object.entries(manifest)
.filter(([key]) => /^src\/features\/[^/]+\/feature-runtime\.ts$/u.test(key))
.flatMap(([, runtime]) => runtime.imports ?? [])
.find(
(key) =>
!initial.has(key) &&
(manifest[key]?.imports ?? []).some((child) => !initial.has(child)),
);
// 1105/11-01. Without that chunk there is nothing to delay and this would
// pass as a plain visit. Point it at another chunk with the same shape, or
// remove the test with a reason.
expect(
parent,
"no feature runtime chunk imports a chunk the boot has not loaded",
).toBeDefined();
const server = await startBuildServer({ a: featureBuild.dist.a, b: featureBuild.dist.a });
try {
await page.goto(`${server.origin}/`);
const file = `/${manifest[parent ?? ""]?.file}`;
await page.route(
(url) => url.pathname === file,
async (route) => {
await new Promise((resolve) => setTimeout(resolve, 3_500));
await route.fallback();
},
);
await page
.getByRole("navigation", { name: "주요 탐색" })
.getByRole("link", { name: "플랫폼 구성" })
.click();
await expect(
page.getByRole("table", { name: "설치된 HTTP 오퍼레이션" }),
).toBeVisible({ timeout: 15_000 });
} finally {
await server.close();
}
},
);
});
/**
* 1105/11-01. The tests above prove the tracker. These prove the strict
* fixture: what it records after the test body, and that it fails the test for
* it. `fixtureReports` names the lines the fixture must hand its verdict, in
* order, and nothing else; the verdict is read once the `page` fixture has
* ended. One object, not an array: `test.use` reads an array as a
* `[value, options]` fixture tuple.
*/
type FixtureReports = Readonly<{ lines: readonly RegExp[] }>;
const recordedTest = strictTest.extend<{ fixtureReports: FixtureReports }>({
fixtureReports: [{ lines: [] }, { option: true }],
browserFailureVerdict: async ({ allowedBrowserFailure, fixtureReports }, provide) => {
let reported: readonly string[] | undefined;
await provide(
Object.freeze({
judge(failures: readonly string[]) {
reported = failures.filter(
(failure) => !allowedBrowserFailure?.test(failure),
);
},
}),
);
// Torn down after `page`, which hands over its record when it ends.
expect(reported, "the strict fixture handed over no record").toBeDefined();
expect(reported, "what the strict fixture recorded").toHaveLength(
fixtureReports.lines.length,
);
fixtureReports.lines.forEach((line, index) => {
expect(reported?.[index], "what the strict fixture recorded").toMatch(line);
});
},
});
recordedTest.describe("the strict fixture's preload check", () => {
recordedTest.use({
fixtureReports: { lines: [/^preloadunused:\/assets\/[^/]+\.js$/u] },
});
recordedTest(
"records a modulepreload the page never runs",
async ({ page }) => {
await openHome(page);
await preloadAChunkThePageNeverRuns(page);
},
);
});
recordedTest.describe("the strict fixture's console check", () => {
const url = "http://127.0.0.1:4173/assets/preload-use-probe.js";
const guess = webKitUnusedPreloadGuess(url);
recordedTest.use({
fixtureReports: {
lines: [
// Chromium's wording of the same warning.
/^console:warning: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 has an appropriate `as` value and it is preloaded intentionally\.$/u,
/^console:warning:The resource \S+ .* \(and more\)$/u,
/^console:error:The resource \S+ was preloaded using link preload /u,
/^console:warning:\[preload-use\] another warning$/u,
],
},
});
recordedTest(
"ignores WebKit's unused-preload guess word for word and nothing else",
async ({ page }) => {
await openHome(page);
await page.evaluate(
({ text, address }) => {
console.warn(text);
console.warn(
`The resource ${address} was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it has an appropriate \`as\` value and it is preloaded intentionally.`,
);
console.warn(`${text} (and more)`);
console.error(text);
console.warn("[preload-use] another warning");
},
{ text: guess, address: url },
);
},
);
});
// 1112/03-05. The fixture watched its own page only, so a console error in
// a second tab of the same context (the cross-tab capability specs) passed.
recordedTest.describe("the strict fixture's other tabs", () => {
recordedTest.use({
fixtureReports: { lines: [/^console:error:\[preload-use\] second tab$/u] },
});
recordedTest(
"records a console error in another tab of the context",
async ({ page }) => {
await openHome(page);
const second = await page.context().newPage();
await openHome(second);
await second.evaluate(() => console.error("[preload-use] second tab"));
},
);
});
/**
* 1105/11-01. The verdict every other spec gets: a recorded failure fails the
* test. The record is proven above; this proves the verdict.
*/
strictTest(
"fails a test whose page writes a console error",
async ({ page }) => {
strictTest.fail();
await openHome(page);
await page.evaluate(() => console.error("[preload-use] this test must fail"));
},
);