Files
clean-architecture-frontend…/tests/e2e/preload-use.spec.ts
DongHyeonka e3c8db48ae chore(features): 참조 기능과 예제 화면을 지우고 레지스트리를 다시 승인한다
소유자 결정(2026-11-07, design-docs repo-structure/README.md 3.4절)으로
템플릿 샘플을 지운다. 샘플이 맡던 시험 대상은 앞 커밋들이 옮겼다.

참조 기능과 예제 화면, 두 context, 그 시험, e2e 3개, 수동 접근성 기록
7개, 런타임 프로필 키, test:reference-feature(명령, 산출물, 게이트
참조), 커버리지 정책 행을 지웠다. removable-template-features 목록은
비우고 장치는 남겼다: test:sample-removal은 인자가 없으면 목록을
하나씩 지우고, 비면 SKIP을 출력하고 보고서에 건너뛴 사례를 남긴다.
기능 스위치 시험과 홈 시작 링크 스위치 시험은 시험 전용 fixture 기능으로
옮겼다. browser soak은 생성 기능을 넣은 빌드에서 돈다. 문서의 샘플
경로를 고쳤다. 날짜가 붙은 설계·리뷰 기록의 본문은 그대로 두었다.
레지스트리 기준선(update:registry-baseline, OWNER DongHyeonka), CI 계약
기준선과 workflow, 초기 JS 기록(194,354B, 17파일, 첫 기능 6,327B, 기능
539B, context 4B)을 다시 받았다.

위험: 레지스트리 행 삭제는 breaking이다. 샘플을 고쳐 쓰는 다운스트림은
수정/삭제 충돌을 만난다. 성능 lab의 기능 라우트 측정은 템플릿에
공개 기능 라우트가 없어 measured:false다. TEMPLATE_FIXTURE 출처는
플랫폼 코드에 남았다.

검증(desktop): check:types, lint, check:architecture, check:registries,
check:ci, verify:documentation, test:unit, test:component,
test:contract, test:integration, test:recipes, test:coverage(Risk
coverage PASS 378/378, 문턱 68개), test:e2e 183, test:a11y, test:visual,
test:storybook, test:browser-capabilities, test:feature-scaffold PASS(76
checks), test:sample-removal SKIP, build, check:bundle.
2026-11-08 14:07:58 +09:00

311 lines
11 KiB
TypeScript

import { readFile } from "node:fs/promises";
import path from "node:path";
import { expect, test, type Page } from "@playwright/test";
import { addGeneratedFeature } from "../../scripts/lib/generated-feature.ts";
import { testRunPath } from "../../scripts/lib/run-isolation.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 },
);
},
);
});
/**
* 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"));
},
);