268 lines
9.5 KiB
TypeScript
268 lines
9.5 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
|
|
import { expect, test, type Page } from "@playwright/test";
|
|
|
|
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";
|
|
|
|
/**
|
|
* 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). */
|
|
async function buildManifest(): Promise<Readonly<Record<string, ManifestEntry>>> {
|
|
return JSON.parse(await readFile(testRunPath("dist/.vite/manifest.json"), "utf8")) as Record<
|
|
string,
|
|
ManifestEntry
|
|
>;
|
|
}
|
|
|
|
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 expect(
|
|
page.getByRole("table", { name: "설치된 HTTP 오퍼레이션" }),
|
|
).toBeVisible();
|
|
|
|
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.
|
|
*/
|
|
strictTest(
|
|
"passes a route whose deepest preloaded chunk is reached after WebKit's check",
|
|
async ({ page }) => {
|
|
const manifest = await buildManifest();
|
|
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();
|
|
|
|
await page.goto("/");
|
|
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 });
|
|
},
|
|
);
|
|
|
|
/**
|
|
* 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"));
|
|
},
|
|
);
|