test(service-worker): SW 픽스처가 현재 선택과 무관하게 워커를 켠다 (0929/06-04)
문제: SW e2e 도우미(prepareServiceWorkerBuilds)는 사본의 `const SERVICE_WORKER: InstalledServiceWorkerSelection | null = null;` 줄을 글자 그대로 찾아 ACTIVE로 바꿨다. 이미 SW를 켠 제품 트리에는 이 줄이 없어서 beforeAll이 throw하고 update·recovery spec이 모두 멈춘다. 이 spec이 실제로 필요한 곳이 바로 SW를 켠 제품이다. setup이 실패하면 spec이 root를 받지 못해 /tmp/ca-sw-builds-* 사본도 남았다. service-worker-build-output.test.ts도 같은 치환을 했다. 고른 방법: selectActiveServiceWorker(root)를 도우미에 두고, SERVICE_WORKER 선언 전체를 값과 무관하게(정규식, 정확히 1개) ACTIVE_SELECTION으로 바꾼다. 두 픽스처가 이 함수를 쓴다. recovery spec이 ACTIVE_SELECTION을 다시 찾는 방식은 그대로 동작한다. setup 실패 때는 fixture-builds.ts처럼 사본을 지우고 다시 throw한다. 버린 대안: 리뷰가 함께 적은 "install spec 기대값을 dist/assets 전체 대신 생성된 service-worker-assets.ts 목록과 비교". ACTIVE 트리에서도 두 값이 같아 이번 결함과 무관하다. 제품이 사전 캐시 대상을 바꿀 때(0929/06-03) 다시 본다. 위험·한계: - 선택 선언의 값에 `;`가 들어가면 정규식이 짧게 끊겨 "정확히 1개" 검사나 이후 타입 검사에서 실패한다(조용히 틀리지는 않는다). - e2e는 chromium만 돌렸다. webkit·firefox는 미실행. 검증: - RED: 작업 트리 선택을 ACTIVE로 바꾸고 SW e2e 2 spec(chromium, webServer 없는 임시 설정)을 돌림 -> `Expected exactly one "const SERVICE_WORKER: ... = null;"`로 2 failed, 4 did not run, 사본 2개 잔류. - GREEN: 같은 ACTIVE 트리에서 6 passed. 선택을 null로 되돌린 템플릿 트리에서도 6 passed. 잔류 사본 0. - 새 단위 테스트(null·ACTIVE·REMOVE_REGISTRATION 선택 -> ACTIVE) 포함 `node scripts/run-vitest.ts run tests/unit/service-worker-build-output.test.ts` -> 5 passed(ACTIVE 트리와 null 트리 모두). - `npx tsc --project tsconfig.test.json --noEmit` -> 오류 0. - `npx eslint <바꾼 파일 2개> --max-warnings=0` -> 통과. Refs: docs/reviews/2026-09-29-maturity-review/06-adapters-service-worker-push.md 06-04, docs/reviews/2026-11-01-maturity-review/06-adapters-service-worker-push.md 2.1절
This commit is contained in:
@@ -58,6 +58,23 @@ export const ACTIVE_SELECTION = `const SERVICE_WORKER: InstalledServiceWorkerSel
|
||||
handlers: ["PWA_STATIC_ASSETS"],
|
||||
};`;
|
||||
|
||||
const SELECTION_DECLARATION =
|
||||
/const SERVICE_WORKER: InstalledServiceWorkerSelection \| null = [^;]*;/gu;
|
||||
|
||||
/**
|
||||
* 0929/06-04. Writes `ACTIVE_SELECTION` over the copy's selection whatever its
|
||||
* current value. Matching only the template's `= null;` line stopped every
|
||||
* worker spec in a product that already ships the worker.
|
||||
*/
|
||||
export async function selectActiveServiceWorker(root: string): Promise<void> {
|
||||
const file = path.join(root, "src/features/installed-runtime-capabilities.ts");
|
||||
const source = await readFile(file, "utf8");
|
||||
if (source.match(SELECTION_DECLARATION)?.length !== 1) {
|
||||
throw new Error(`Expected exactly one SERVICE_WORKER selection in ${file}.`);
|
||||
}
|
||||
await writeFile(file, source.replace(SELECTION_DECLARATION, ACTIVE_SELECTION));
|
||||
}
|
||||
|
||||
const HOST_PROBE = `
|
||||
export function createServiceWorkerRuntimeHost(
|
||||
input: ServiceWorkerHostInput,
|
||||
@@ -116,33 +133,36 @@ export async function prepareServiceWorkerBuilds(
|
||||
}> = {},
|
||||
): Promise<ServiceWorkerBuilds> {
|
||||
const root = await mkdtemp(path.join(tmpdir(), "ca-sw-builds-"));
|
||||
await cp(repoRoot, root, {
|
||||
recursive: true,
|
||||
filter: (source) =>
|
||||
!skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!),
|
||||
});
|
||||
await linkFixtureNodeModules(root, repoRoot);
|
||||
await replaceOnce(
|
||||
path.join(root, "src/features/installed-runtime-capabilities.ts"),
|
||||
"const SERVICE_WORKER: InstalledServiceWorkerSelection | null = null;",
|
||||
ACTIVE_SELECTION,
|
||||
);
|
||||
const hostFile = path.join(root, "src/bootstrap/register-service-worker.ts");
|
||||
await replaceOnce(
|
||||
hostFile,
|
||||
"export function createServiceWorkerRuntimeHost(",
|
||||
"function createServiceWorkerRuntimeHostUnobserved(",
|
||||
);
|
||||
await writeFile(hostFile, (await readFile(hostFile, "utf8")) + HOST_PROBE);
|
||||
try {
|
||||
await cp(repoRoot, root, {
|
||||
recursive: true,
|
||||
filter: (source) =>
|
||||
!skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!),
|
||||
});
|
||||
await linkFixtureNodeModules(root, repoRoot);
|
||||
await selectActiveServiceWorker(root);
|
||||
const hostFile = path.join(root, "src/bootstrap/register-service-worker.ts");
|
||||
await replaceOnce(
|
||||
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") };
|
||||
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]);
|
||||
const dist = { a: path.join(root, "dist-a"), b: path.join(root, "dist-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]);
|
||||
}
|
||||
return { root, dist };
|
||||
} catch (error) {
|
||||
// The spec never receives `root` from a failed setup, so afterAll cannot
|
||||
// remove it.
|
||||
await rm(root, { recursive: true, force: true });
|
||||
throw error;
|
||||
}
|
||||
return { root, dist };
|
||||
}
|
||||
|
||||
export async function removeServiceWorkerBuilds(
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
import { access, readFile } from "node:fs/promises";
|
||||
import { access, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
|
||||
import { generateRuntimeConfig } from "../../scripts/generate-runtime-config.ts";
|
||||
import {
|
||||
editOnce,
|
||||
type FixtureBuilds,
|
||||
prepareFixtureBuilds,
|
||||
removeFixtureBuilds,
|
||||
} from "../e2e/support/fixture-builds.ts";
|
||||
import { ACTIVE_SELECTION } from "../e2e/support/service-worker-builds.ts";
|
||||
import {
|
||||
ACTIVE_SELECTION,
|
||||
selectActiveServiceWorker,
|
||||
} from "../e2e/support/service-worker-builds.ts";
|
||||
|
||||
/**
|
||||
* 0929/06-01. Step 6 of `scripts/build-frontend.ts`, the Service Worker Vite
|
||||
@@ -39,12 +42,7 @@ beforeAll(async () => {
|
||||
{
|
||||
name: "active",
|
||||
env: PROFILE,
|
||||
edit: (root) =>
|
||||
editOnce(
|
||||
path.join(root, "src/features/installed-runtime-capabilities.ts"),
|
||||
"const SERVICE_WORKER: InstalledServiceWorkerSelection | null = null;",
|
||||
ACTIVE_SELECTION,
|
||||
),
|
||||
edit: selectActiveServiceWorker,
|
||||
},
|
||||
]);
|
||||
}, 180_000);
|
||||
@@ -95,3 +93,49 @@ describe("Service Worker build output (0929/06-01, 0929/06-02)", () => {
|
||||
).toBe("/app/config.json");
|
||||
});
|
||||
});
|
||||
|
||||
// 0929/06-04. The worker fixtures must also run in a product that already
|
||||
// ships the worker, so the selection is replaced whatever its value.
|
||||
describe("selectActiveServiceWorker (0929/06-04)", () => {
|
||||
it.each([
|
||||
["null", "null"],
|
||||
[
|
||||
"ACTIVE",
|
||||
`{
|
||||
mode: "ACTIVE",
|
||||
scriptPath: "service-worker.js",
|
||||
handlers: ["PWA_STATIC_ASSETS", "OFFLINE_SYNC_WAKEUP"],
|
||||
}`,
|
||||
],
|
||||
[
|
||||
"REMOVE_REGISTRATION",
|
||||
`{
|
||||
mode: "REMOVE_REGISTRATION",
|
||||
scriptPath: "service-worker.js",
|
||||
}`,
|
||||
],
|
||||
])("turns a %s selection into the active one", async (_label, value) => {
|
||||
const root = await mkdtemp(path.join(tmpdir(), "sw-select-"));
|
||||
try {
|
||||
const file = path.join(
|
||||
root,
|
||||
"src/features/installed-runtime-capabilities.ts",
|
||||
);
|
||||
await mkdir(path.dirname(file), { recursive: true });
|
||||
const around = (selection: string) =>
|
||||
`const WEB_WORKERS = Object.freeze([]);\n\n${selection}\n\nconst OFFLINE_COMMANDS: X | null = null;\n`;
|
||||
await writeFile(
|
||||
file,
|
||||
around(
|
||||
`const SERVICE_WORKER: InstalledServiceWorkerSelection | null = ${value};`,
|
||||
),
|
||||
);
|
||||
|
||||
await selectActiveServiceWorker(root);
|
||||
|
||||
expect(await readFile(file, "utf8")).toBe(around(ACTIVE_SELECTION));
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user