Files
clean-architecture-frontend…/tests/e2e/preload-use.spec.ts
T
DongHyeonka f81dda4913
frontend-quality-gates / merge / static checks (pull_request) Successful in 1m23s
frontend-quality-gates / merge / browser tests (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-014 / config-compatibility (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-028 / manual-accessibility-review (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-026 / lab-performance (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-015 / immutable-release-candidate (pull_request) Canceled after 0s
frontend-quality-gates / external-vulnerability-provider (pull_request) Canceled after 0s
frontend-quality-gates / external-provenance-provider (pull_request) Canceled after 0s
frontend-quality-gates / promote-verified-immutable-candidate (pull_request) Canceled after 0s
frontend-quality-gates / deploy-promoted-release (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-019 / hosting-header (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-021 / runbook-boot-config (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-022 / runbook-chunk-mismatch (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-023 / runbook-api-degradation (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-024 / runbook-telemetry (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-025 / runbook-release-rollback (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-018 / field-web-vitals (pull_request) Canceled after 0s
frontend-quality-gates / FE-GATE-017 / diagram-review (pull_request) Canceled after 0s
frontend-quality-gates / main / self-tests and removal drills (pull_request) Canceled after 0s
frontend-quality-gates / merge / tests (pull_request) Canceled after 11m31s
test(e2e): Firefox 브라우저 프로필을 RAM 파일시스템에 둔다
FE-GATE-008의 Firefox 시험이 가끔 첫 화면 5초 대기에서 실패한다
(old-tab-chunk-recovery.spec.ts, preload-use.spec.ts,
platform-overview.spec.ts). runner run 127과 desktop merge 검증에서
실패했고, 다시 돌리면 통과해 묻혔다.
Playwright는 Firefox 프로필을 os.tmpdir()에 만든다. desktop과 CI job
모두 /tmp가 같은 SATA SSD 위에 있다. Firefox는 시험 context의
localStorage·IndexedDB를 그 프로필(디스크)에 둔다. 그래서 디스크가
붐비면 context마다 첫 localStorage 접근이 디스크 일을 기다린다.
Chromium·WebKit은 시험 context 저장소를 메모리에 둔다. 앱은 첫 렌더
전에 색 구성 설정을 localStorage에서 읽어, 그 대기가 첫 화면 대기에
들어간다. desktop e2e 전체 8회(2026-11-13)에서 Firefox 앱 부팅 463회
중 19회가 0.7~3.8초 멈췄고, Chromium·WebKit은 16 ms를 넘지 않았다.

tests/support/browser/browser-test.ts가 Firefox worker의 프로필을 RAM
파일시스템에 둔다. os.tmpdir()가 이미 tmpfs면 그대로 쓴다. 아니면
/dev/shm에 worker 수 x 64 MB 자리가 있을 때 그 안에 폴더를 만든다.
Playwright의 browser fixture가 launch하는 동안만 TMPDIR를 그 폴더로
돌리고 바로 되돌린다. 그래서 fixture build 같은 다른 임시 파일은
디스크에 그대로 남는다. 프로필이 그 폴더에 생기지 않으면 시험을
실패시킨다. 자리가 없으면 이유를 한 줄 알리고 디스크에 둔다. strict
fixture와 strict를 쓰지 않는 spec 둘(preload-use, chunk-load-error)이
이 test를 바탕으로 쓴다. docs/work/reference/testing.md 5절 표에 원인을
적었다.

버린 대안:
- worker마다 localStorage를 미리 한 번 건드리는 예열: 브라우저 프로세스당
  한 번 드는 준비 비용으로 봤다. 예열 뒤에도 19/463이 멈췄다. 예열
  context를 바로 닫는 판은 21/464로 더 나빴다.
- toolkit.storage.synchronous=0(SQLite fsync 끔): 15/224로 그대로였다.
- 첫 화면 대기 timeout 늘리기, worker 줄이기: 원인을 덮는다.
- 시험 실행 전체에 TMPDIR 걸기: fixture build까지 RAM에 들어간다.

위험·한계:
- CI job 컨테이너는 /dev/shm이 64 MB다(2026-11-13 runner 확인). /tmp는
  같은 SSD 위에 있다. 그래서 CI에서는 프로필이 디스크에 남고 로그에
  이유가 한 줄 찍힌다. CI까지 고치려면 runner가 job 컨테이너에 RAM
  자리를 줘야 한다(--shm-size 또는 --tmpfs /tmp). runner 설정이라 이
  커밋에 넣지 않았다.
- 64 MB/worker는 Firefox worker 6개 프로필 합이 가장 컸을 때 211 MB(1개
  35 MB)인 것을 보고 잡은 여유다.
- Playwright의 두 동작에 기댄다. fixture 의존성을 적힌 순서로 준비하는
  것과, launch할 때 os.tmpdir()를 읽는 것이다. 어느 쪽이 바뀌어도
  폴더가 비므로 시험이 실패해 알린다.

검증(desktop):
- 재현: Firefox 6개를 동시에 띄우고 context 8개씩 localStorage 첫 접근을
  쟀다. 프로필 /tmp에서 최대 8,157 ms(384회), /dev/shm에서 최대 17 ms
  (384회).
- 수정본 e2e 전체 10회: 매번 189 passed. Firefox 부팅 576회 중 0.5초를
  넘는 멈춤 0회(최대 36 ms), 부하 15.9~31.2. 프로필은 매번 /dev/shm에
  5~6개, 디스크 0개, 끝난 뒤 남은 폴더 0개.
- 변이: launch 전에 TMPDIR를 돌리지 않게 하면 theme.spec.ts(firefox)가
  2 failed, 안전장치 메시지로 실패한다.
- check:types:test, eslint(바뀐 파일), check:test-evidence:source 통과.
- scripts/verify.sh gate FE-GATE-008(CI=true, main ee95573f 위): rc=0 205초,
  e2e 189 passed, browser-capabilities 69, storybook 11, visual 7.
- scripts/verify.sh docs, desktop verify:documentation 통과.
2026-11-13 18:13:17 +09:00

314 lines
11 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 },
);
},
);
});
/**
* 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"));
},
);