refactor(e2e): Service Worker 빌드 helper가 fixture 빌드의 준비 함수를 쓴다
문제: `tests/e2e/support/service-worker-builds.ts`가 checkout 복사, node modules 연결, 빌드 실행(`run`), 한 번 치환(`replaceOnce`)을 `fixture-builds.ts`와 따로 가졌다. 한쪽을 고친 변경(로컬 빌드 env 1103/11-08, 복사 제외 목록 1105/12-04)을 두 번 해야 했다. 고친 것: `fixture-builds.ts`에 `buildEditedCopies` 하나를 두고(복사 → `prepare` 한 번 → 빌드마다 `edit`·빌드·`dist-<name>`·`collect`), `prepareFixtureBuilds`와 `prepareServiceWorkerBuilds`가 그것을 쓴다. `replaceOnce`는 `editOnce`를 가리킨다. 준비 중 실패한 사본은 전처럼 `sharedFixtureBuild`가 지운다. 검증: desktop `tests/unit/e2e-build-helpers.test.ts` 통과, SW update·recovery e2e와 fixture 빌드 e2e는 FE-GATE-008에서 본다. Refs: 0929/11-07
This commit is contained in:
@@ -65,17 +65,41 @@ function run(
|
||||
}
|
||||
}
|
||||
|
||||
export async function prepareFixtureBuilds<Name extends string>(
|
||||
prefix: string,
|
||||
builds: readonly FixtureBuild<Name>[],
|
||||
): Promise<FixtureBuilds<Name>> {
|
||||
// An edit's source text names the build (two specs never share a prefix).
|
||||
const fingerprint = JSON.stringify(
|
||||
builds.map((build) => [build.name, build.env ?? {}, build.edit?.toString() ?? ""]),
|
||||
);
|
||||
export type EditedCopyBuild<Name extends string> = Readonly<{
|
||||
name: Name;
|
||||
/** VITE_BUILD_ID and RELEASE_ID of this build. */
|
||||
buildId: string;
|
||||
edit?: (root: string) => Promise<void>;
|
||||
env?: Readonly<Record<string, string>>;
|
||||
}>;
|
||||
|
||||
/**
|
||||
* 0929/11-07. The one way an e2e spec gets real production builds of an
|
||||
* edited copy of the checkout; prepareFixtureBuilds and
|
||||
* prepareServiceWorkerBuilds (service-worker-builds.ts) are its two shapes.
|
||||
* It copies the checkout (without `checkout-copy-skip`), links node_modules,
|
||||
* runs `prepare` once, then for each build runs `edit`, builds, moves `dist`
|
||||
* to `dist-<name>` and asks `collect` what the spec needs from that build.
|
||||
* `key` and `fingerprint` name the result in the e2e run
|
||||
* (shared-fixture-builds.ts), which removes the copy when preparing fails.
|
||||
*/
|
||||
export async function buildEditedCopies<Name extends string, Collected = null>(
|
||||
input: Readonly<{
|
||||
key: string;
|
||||
fingerprint: string;
|
||||
prepare?: (root: string) => Promise<void>;
|
||||
builds: readonly EditedCopyBuild<Name>[];
|
||||
collect?: (name: Name, root: string) => Promise<Collected>;
|
||||
}>,
|
||||
): Promise<Readonly<{
|
||||
root: string;
|
||||
dist: Readonly<Record<Name, string>>;
|
||||
collected: Readonly<Record<Name, Collected>>;
|
||||
shared: boolean;
|
||||
}>> {
|
||||
const { root, value, shared } = await sharedFixtureBuild(
|
||||
`fixture-${prefix}`,
|
||||
fingerprint,
|
||||
input.key,
|
||||
input.fingerprint,
|
||||
async (root) => {
|
||||
await cp(repoRoot, root, {
|
||||
recursive: true,
|
||||
@@ -83,19 +107,36 @@ export async function prepareFixtureBuilds<Name extends string>(
|
||||
!skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!),
|
||||
});
|
||||
await linkFixtureNodeModules(root, repoRoot);
|
||||
await input.prepare?.(root);
|
||||
const dist = {} as Record<Name, string>;
|
||||
for (const build of builds) {
|
||||
const collected = {} as Record<Name, Collected>;
|
||||
for (const build of input.builds) {
|
||||
await build.edit?.(root);
|
||||
const buildId = `${prefix}-${build.name}`;
|
||||
run(root, "scripts/generate-product-feature-index.ts", buildId, build.env);
|
||||
run(root, "scripts/build-frontend.ts", buildId, build.env);
|
||||
run(root, "scripts/generate-product-feature-index.ts", build.buildId, build.env);
|
||||
run(root, "scripts/build-frontend.ts", build.buildId, build.env);
|
||||
dist[build.name] = path.join(root, `dist-${build.name}`);
|
||||
await rename(path.join(root, "dist"), dist[build.name]);
|
||||
collected[build.name] = input.collect ? await input.collect(build.name, root) : (null as Collected);
|
||||
}
|
||||
return dist;
|
||||
return { dist, collected };
|
||||
},
|
||||
);
|
||||
return { root, dist: value, shared };
|
||||
return { root, ...value, shared };
|
||||
}
|
||||
|
||||
export async function prepareFixtureBuilds<Name extends string>(
|
||||
prefix: string,
|
||||
builds: readonly FixtureBuild<Name>[],
|
||||
): Promise<FixtureBuilds<Name>> {
|
||||
// An edit's source text names the build (two specs never share a prefix).
|
||||
const { root, dist, shared } = await buildEditedCopies({
|
||||
key: `fixture-${prefix}`,
|
||||
fingerprint: JSON.stringify(
|
||||
builds.map((build) => [build.name, build.env ?? {}, build.edit?.toString() ?? ""]),
|
||||
),
|
||||
builds: builds.map((build) => ({ ...build, buildId: `${prefix}-${build.name}` })),
|
||||
});
|
||||
return { root, dist, shared };
|
||||
}
|
||||
|
||||
export async function removeFixtureBuilds(
|
||||
|
||||
@@ -1,12 +1,5 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { existsSync } from "node:fs";
|
||||
import {
|
||||
cp,
|
||||
readFile,
|
||||
rename,
|
||||
rm,
|
||||
writeFile,
|
||||
} from "node:fs/promises";
|
||||
import { readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { createServer, type Server } from "node:http";
|
||||
import type { AddressInfo } from "node:net";
|
||||
import path from "node:path";
|
||||
@@ -14,11 +7,8 @@ import { pathToFileURL } from "node:url";
|
||||
import { gzipSync } from "node:zlib";
|
||||
|
||||
import { SERVICE_WORKER_ASSETS_OUTPUT } from "../../../scripts/generate-service-worker-assets.ts";
|
||||
import { localBuildEnvironment } from "../../../scripts/lib/build-environment.ts";
|
||||
import { linkFixtureNodeModules } from "../../../scripts/lib/fixture-node-modules.ts";
|
||||
import { selectRepositoryPaths } from "../../../scripts/lib/repository-layout.ts";
|
||||
import type { StaticAssetManifestV1 } from "../../../src/contracts/service-worker.ts";
|
||||
import { sharedFixtureBuild } from "./shared-fixture-builds.ts";
|
||||
import { buildEditedCopies, editOnce } from "./fixture-builds.ts";
|
||||
|
||||
/**
|
||||
* R3-6 test helper: two real production builds with the Service Worker on,
|
||||
@@ -54,11 +44,6 @@ import { sharedFixtureBuild } from "./shared-fixture-builds.ts";
|
||||
* text it expected.
|
||||
*/
|
||||
|
||||
const repoRoot = path.resolve(import.meta.dirname, "../../..");
|
||||
// 1105/12-04. `.tmp` holds other runs' builds and results (.tmp/runs/<id>),
|
||||
// which change while this copy is made; fixture-builds.ts skips it too.
|
||||
const skipped = new Set(selectRepositoryPaths("checkout-copy-skip"));
|
||||
|
||||
export type BuildName = "a" | "b";
|
||||
export type ServiceWorkerBuilds = Readonly<{
|
||||
root: string;
|
||||
@@ -101,17 +86,8 @@ export function createServiceWorkerRuntimeHost(
|
||||
}
|
||||
`;
|
||||
|
||||
export async function replaceOnce(
|
||||
file: string,
|
||||
search: string,
|
||||
replacement: string,
|
||||
): Promise<void> {
|
||||
const source = await readFile(file, "utf8");
|
||||
if (source.split(search).length !== 2) {
|
||||
throw new Error(`Expected exactly one "${search}" in ${file}.`);
|
||||
}
|
||||
await writeFile(file, source.replace(search, replacement));
|
||||
}
|
||||
/** 0929/11-07. The same one-occurrence edit as fixture-builds.ts, not a copy of it. */
|
||||
export const replaceOnce = editOnce;
|
||||
|
||||
/**
|
||||
* Premise 1. Replaces the value of the one `SERVICE_WORKER` declaration in
|
||||
@@ -167,25 +143,12 @@ async function precachedUrls(root: string, name: BuildName): Promise<string[]> {
|
||||
return SERVICE_WORKER_ASSETS.assets.map((asset) => asset.url).sort();
|
||||
}
|
||||
|
||||
function run(cwd: string, script: string, buildId: string): void {
|
||||
const result = spawnSync("node", [script], {
|
||||
cwd,
|
||||
encoding: "utf8",
|
||||
// 1103/11-08. A local build, not the CI job's release build.
|
||||
env: { ...localBuildEnvironment(), VITE_BUILD_ID: buildId, RELEASE_ID: buildId },
|
||||
});
|
||||
if (result.status !== 0) {
|
||||
throw new Error(
|
||||
`${script} failed for ${buildId}:\n${result.stdout}\n${result.stderr}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* `beforeBuild` edits the temp copy right before build `name` (06-03: a
|
||||
* broken worker A, then a `REMOVE_REGISTRATION` release B). Edits persist
|
||||
* into the later build. `key` names this pair of builds in the e2e run
|
||||
* (shared-fixture-builds.ts); each spec passes its own.
|
||||
* (shared-fixture-builds.ts); each spec passes its own. The copy and the
|
||||
* builds are fixture-builds.ts's `buildEditedCopies` (0929/11-07).
|
||||
*/
|
||||
export async function prepareServiceWorkerBuilds(
|
||||
options: Readonly<{
|
||||
@@ -193,38 +156,27 @@ export async function prepareServiceWorkerBuilds(
|
||||
beforeBuild?: (name: BuildName, root: string) => Promise<void>;
|
||||
}>,
|
||||
): Promise<ServiceWorkerBuilds> {
|
||||
const { root, value, shared } = await sharedFixtureBuild(
|
||||
`sw-${options.key}`,
|
||||
options.beforeBuild?.toString() ?? "",
|
||||
async (root) => {
|
||||
await cp(repoRoot, root, {
|
||||
recursive: true,
|
||||
filter: (source) =>
|
||||
!skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!),
|
||||
});
|
||||
await linkFixtureNodeModules(root, repoRoot);
|
||||
const { root, dist, collected, shared } = await buildEditedCopies({
|
||||
key: `sw-${options.key}`,
|
||||
fingerprint: options.beforeBuild?.toString() ?? "",
|
||||
prepare: async (root) => {
|
||||
await turnOnServiceWorker(root);
|
||||
const hostFile = path.join(root, "src/bootstrap/register-service-worker.ts");
|
||||
await replaceOnce(
|
||||
await editOnce(
|
||||
hostFile,
|
||||
"export function createServiceWorkerRuntimeHost(",
|
||||
"function createServiceWorkerRuntimeHostUnobserved(",
|
||||
);
|
||||
await writeFile(hostFile, (await readFile(hostFile, "utf8")) + HOST_PROBE);
|
||||
|
||||
const dist = { a: path.join(root, "dist-a"), b: path.join(root, "dist-b") };
|
||||
const precached: Record<BuildName, readonly string[]> = { a: [], b: [] };
|
||||
for (const name of ["a", "b"] as const) {
|
||||
await options.beforeBuild?.(name, root);
|
||||
run(root, "scripts/generate-product-feature-index.ts", `sw-e2e-${name}`);
|
||||
run(root, "scripts/build-frontend.ts", `sw-e2e-${name}`);
|
||||
await rename(path.join(root, "dist"), dist[name]);
|
||||
precached[name] = await precachedUrls(root, name);
|
||||
}
|
||||
return { dist, precached };
|
||||
},
|
||||
);
|
||||
return { root, ...value, shared };
|
||||
builds: (["a", "b"] as const).map((name) => ({
|
||||
name,
|
||||
buildId: `sw-e2e-${name}`,
|
||||
edit: async (root: string) => options.beforeBuild?.(name, root),
|
||||
})),
|
||||
collect: (name, root) => precachedUrls(root, name),
|
||||
});
|
||||
return { root, dist, precached: collected, shared };
|
||||
}
|
||||
|
||||
export async function removeServiceWorkerBuilds(
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { readFile, readdir } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
const SUPPORT = "tests/e2e/support";
|
||||
|
||||
// 0929/11-07. The Service Worker helper kept its own copy of the checkout
|
||||
// copy, the build runner and the one-occurrence edit, so a fix to one (the
|
||||
// local build env of 1103/11-08, the copy skip list of 1105/12-04) had to be
|
||||
// made twice. Both helpers now go through fixture-builds.ts.
|
||||
describe("e2e build helpers have one copy-and-build path (0929/11-07)", () => {
|
||||
it("builds Service Worker fixtures through buildEditedCopies", async () => {
|
||||
const source = await readFile(path.join(SUPPORT, "service-worker-builds.ts"), "utf8");
|
||||
|
||||
expect(source).toMatch(/import \{[^}]*\bbuildEditedCopies\b[^}]*\} from "\.\/fixture-builds\.ts";/u);
|
||||
expect(source).toContain("buildEditedCopies({");
|
||||
for (const copy of ["spawnSync", "sharedFixtureBuild", "linkFixtureNodeModules", "await cp(", "function run("]) {
|
||||
expect(source, copy).not.toContain(copy);
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps the build runner and the one-occurrence edit in fixture-builds.ts only", async () => {
|
||||
const files = (await readdir(SUPPORT)).filter((file) => file.endsWith(".ts"));
|
||||
const defining = async (pattern: RegExp) => {
|
||||
const found: string[] = [];
|
||||
for (const file of files) {
|
||||
if (pattern.test(await readFile(path.join(SUPPORT, file), "utf8"))) found.push(file);
|
||||
}
|
||||
return found;
|
||||
};
|
||||
|
||||
expect(await defining(/^function run\(/mu)).toEqual(["fixture-builds.ts"]);
|
||||
expect(await defining(/split\(search\)\.length !== 2/u)).toEqual(["fixture-builds.ts"]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user