Files
clean-architecture-frontend…/tests/component/runtime-composition-service-worker-telemetry.test.ts
T
donghyeon-kaandClaude Opus 5.5 51685503d0 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 <noreply@anthropic.com>
2026-09-27 14:05:59 +09:00

170 lines
5.0 KiB
TypeScript

// @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<readonly [string, Record<string, unknown>]>,
);
vi.mock("../../src/features/installed-runtime-capabilities.ts", async (importOriginal) => {
const original =
await importOriginal<typeof import("../../src/features/installed-runtime-capabilities.ts")>();
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<typeof import("../../src/adapters/telemetry/index.ts")>();
return {
...original,
createTelemetryAdapter: (
options: Parameters<typeof original.createTelemetryAdapter>[0],
) => {
const adapter = original.createTelemetryAdapter(options);
return Object.freeze({
...adapter,
emit(eventName: string, attributes: Record<string, unknown>) {
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<Response> {
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();
}
});
});