From 51685503d02508b9884fa15056573890f99c4f4c Mon Sep 17 00:00:00 2001 From: donghyeon-ka Date: Sun, 27 Sep 2026 14:05:59 +0900 Subject: [PATCH] test(bootstrap): pin the composition's telemetry wiring for a failed Service Worker start (06-05) Only the optional host was tested, so removing the composition line that hands it the telemetry port kept every test green. The real composition with an ACTIVE selection and a failing first install now must emit one event, and the recipe doc names what the template still leaves unwired. Co-Authored-By: Claude Opus 5.5 --- docs/architecture/optional-adapter-recipes.md | 18 +- ...mposition-service-worker-telemetry.test.ts | 169 ++++++++++++++++++ 2 files changed, 182 insertions(+), 5 deletions(-) create mode 100644 tests/component/runtime-composition-service-worker-telemetry.test.ts diff --git a/docs/architecture/optional-adapter-recipes.md b/docs/architecture/optional-adapter-recipes.md index 434db1d3..0de7551a 100644 --- a/docs/architecture/optional-adapter-recipes.md +++ b/docs/architecture/optional-adapter-recipes.md @@ -106,14 +106,22 @@ type은 facade 밖으로 노출하지 않는다. SSOT다. 문서와 catalog가 다르면 gate가 검사하는 catalog를 우선 고치고 이 표도 같이 갱신한다. -Service Worker를 켤 때 확인할 것(06-05). 템플릿은 1·2·6만 갖고 있다. +Service Worker를 켤 때 확인할 것(06-05). 템플릿은 1·6과 2의 시작 실패 +이벤트만 갖고 있다. 1. `src/features/installed-runtime-capabilities.ts`의 `SERVICE_WORKER`를 `ACTIVE`로 바꾼다. `scripts/build-frontend.ts`가 worker 빌드를 추가로 돈다. -2. 관측: `createRuntimeComposition`이 텔레메트리 포트를 optional host에 넘긴다. - 시작 결과가 `FAILED`나 `INCOMPATIBLE`이면 `service-worker.start.failed`가 1건 - 남는다(`src/bootstrap/optional-runtime-host.ts`). 업데이트 설치 실패와 page - controller의 `observe`는 아직 어디에도 연결되지 않는다. +2. 관측: `createRuntimeComposition`이 텔레메트리 포트를 optional host에 넘긴다 + (`src/bootstrap/create-runtime-composition.ts`). 시작 결과가 `FAILED`나 + `INCOMPATIBLE`이면 `service-worker.start.failed`가 1건 남는다 + (`src/bootstrap/optional-runtime-host.ts`). 실제 composition에서 첫 설치가 + 실패하면 이벤트가 1건 나가는 것은 + `tests/component/runtime-composition-service-worker-telemetry.test.ts`가 + 확인한다. 템플릿이 연결하지 않은 것은 셋이다. 업데이트 설치 실패 + (`watchForUpdates`, `src/adapters/service-worker/service-worker-page-controller.ts`)는 + 이벤트를 남기지 않는다. page controller의 `observe`는 composition이 넘기지 + 않는다. `OptionalRuntimeHost.health()`는 `src` 안에서 읽는 코드가 없다. + 필요하면 다운스트림이 `createRuntimeComposition`에서 연결한다. 3. blockers: 저장하지 않은 편집이 있는 탭이 활성화를 거절하게 `createOptionalRuntimeHost`에 `blockers`를 넘긴다. 템플릿은 넘기지 않는다. 4. 활성화 UI: `ServiceWorkerRuntimeHost.onUpdateWaiting`을 구독하고, 사용자가 diff --git a/tests/component/runtime-composition-service-worker-telemetry.test.ts b/tests/component/runtime-composition-service-worker-telemetry.test.ts new file mode 100644 index 00000000..ebc43225 --- /dev/null +++ b/tests/component/runtime-composition-service-worker-telemetry.test.ts @@ -0,0 +1,169 @@ +// @vitest-environment jsdom + +import { afterEach, describe, expect, it, vi } from "vitest"; + +import { createRuntimeComposition } from "../../src/bootstrap/create-runtime-composition.ts"; + +/** + * 06-05. The optional host reports a Service Worker that did not start, but + * only if the composition hands it the application's telemetry port. This + * test runs the real composition with an ACTIVE selection and a first install + * that fails, and records what reaches the telemetry adapter. + */ + +const emitted = vi.hoisted( + () => [] as Array]>, +); + +vi.mock("../../src/features/installed-runtime-capabilities.ts", async (importOriginal) => { + const original = + await importOriginal(); + return { + INSTALLED_RUNTIME_CAPABILITIES: Object.freeze({ + ...original.INSTALLED_RUNTIME_CAPABILITIES, + serviceWorker: Object.freeze({ + mode: "ACTIVE", + scriptPath: "service-worker.js", + handlers: Object.freeze(["PWA_STATIC_ASSETS"]), + }), + }), + }; +}); + +vi.mock("../../src/adapters/telemetry/index.ts", async (importOriginal) => { + const original = + await importOriginal(); + return { + ...original, + createTelemetryAdapter: ( + options: Parameters[0], + ) => { + const adapter = original.createTelemetryAdapter(options); + return Object.freeze({ + ...adapter, + emit(eventName: string, attributes: Record) { + emitted.push([eventName, attributes]); + adapter.emit(eventName as never, attributes); + }, + }); + }, + }; +}); + +const origin = "http://localhost"; + +const runtimeConfig = { + APP_ENV: "local", + API_BASE_URL: "http://localhost:8080", + REQUEST_TIMEOUT_MS: 10_000, + MAX_RETRY_ATTEMPTS: 2, + TELEMETRY_ENABLED: false, + AUTH_MODE: "demo", + CONFIG_SCHEMA_VERSION: "1", + API_CONTRACT_VERSION: "1", + RELEASE_MANIFEST_URL: "/release-manifest.json", + BUILD_ID: "local-build", + RELEASE_ID: "local-release", +}; + +const releaseManifest = { + schemaVersion: 1, + appVersion: "0.2.0", + buildId: "local-build", + commitSha: "local", + configSchemaVersion: "1", + apiContractVersion: "1", + assetManifestHash: "test-hash", + releaseId: "local-release", + builtAt: "2026-07-26T00:00:00.000Z", + routeChunks: { + "route-home": "assets/home.js", + "route-examples-ui": "assets/ui.js", + "route-examples-states": "assets/states.js", + "route-examples-auth": "assets/auth.js", + "route-not-found": "assets/not-found.js", + }, +}; + +async function fetcher(input: RequestInfo | URL): Promise { + const url = + typeof input === "string" + ? input + : input instanceof URL + ? input.href + : input.url; + return Response.json( + url.includes("release-manifest") ? releaseManifest : runtimeConfig, + ); +} + +/** A first install whose worker turns `redundant` (install event rejected). */ +function failingInstallContainer(): ServiceWorkerContainer { + const register = vi.fn( + async (scriptURL: string | URL, options?: RegistrationOptions) => { + const installing = { + scriptURL: new URL(scriptURL, origin).href, + state: "installing", + addEventListener(type: string, listener: () => void) { + if (type !== "statechange") return; + queueMicrotask(() => { + installing.state = "redundant"; + listener(); + }); + }, + removeEventListener() {}, + }; + return { + scope: new URL(options?.scope ?? "/", origin).href, + installing, + waiting: null, + active: null, + addEventListener() {}, + removeEventListener() {}, + update: vi.fn(async () => {}), + } as unknown as ServiceWorkerRegistration; + }, + ); + return { + controller: null, + register, + getRegistration: vi.fn(async () => undefined), + getRegistrations: vi.fn(async () => []), + addEventListener() {}, + removeEventListener() {}, + } as unknown as ServiceWorkerContainer; +} + +describe("06-05 runtime composition reports a Service Worker install failure", () => { + afterEach(() => { + emitted.length = 0; + }); + + it("emits exactly one service-worker.start.failed through the application telemetry port", async () => { + const container = failingInstallContainer(); + const composition = await createRuntimeComposition({ + fetcher, + host: { + location: { origin }, + navigator: { serviceWorker: container }, + }, + }); + + try { + await composition.optional.startAfterMount(); + + expect(container.register).toHaveBeenCalledTimes(1); + expect(composition.optional.health().serviceWorker).toBe("DEGRADED"); + expect( + emitted.filter(([eventName]) => eventName.startsWith("service-worker.")), + ).toEqual([ + [ + "service-worker.start.failed", + { error_kind: "INSTALL_FAILED", build_id: "local-build" }, + ], + ]); + } finally { + await composition.dispose(); + } + }); +});