Files
clean-architecture-frontend…/tests/unit/optional-runtime-host.test.ts
T
DongHyeonkaandClaude Opus 5.5 cd47516592 fix(bootstrap): report a Service Worker that did not start through telemetry (06-05)
An ACTIVE worker whose install failed left a trace only in health(), which
nothing reads. The optional host now emits service-worker.start.failed on the
application telemetry port, and the recipe doc lists what else to wire.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 00:26:54 +09:00

149 lines
4.5 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import { createOptionalRuntimeHost } from "../../src/bootstrap/optional-runtime-host.ts";
import type { ResolvedRuntimeCapabilities } from "../../src/contracts/runtime-capabilities.ts";
import type {
ServiceWorkerRuntimeHost,
ServiceWorkerStartOutcome,
} from "../../src/contracts/service-worker.ts";
const capabilities: ResolvedRuntimeCapabilities = Object.freeze({
realtime: Object.freeze([]),
webWorkers: Object.freeze([]),
serviceWorker: null,
serviceWorkerDisabledCleanup: false,
offlineCommands: null,
});
function deferred<Value>() {
let resolve!: (value: Value) => void;
const promise = new Promise<Value>((settle) => {
resolve = settle;
});
return { promise, resolve } as const;
}
describe("optional runtime host", () => {
it("keeps stop-before-start terminal without acquiring browser resources", async () => {
const serviceWorker: ServiceWorkerRuntimeHost = {
start: vi.fn(
async (): Promise<ServiceWorkerStartOutcome> => ({
kind: "ACTIVE",
buildId: "build-1",
}),
),
requestActivation: vi.fn(),
resetOwnedCaches: vi.fn(),
stop: vi.fn(async () => {}),
};
const addEventListener = vi.fn();
const runtime = createOptionalRuntimeHost({
capabilities,
routerBasePath: "/",
buildId: "build-1",
serviceWorkerHost: serviceWorker,
browserLifecycleHost: {
addEventListener,
removeEventListener: vi.fn(),
},
});
await runtime.stop();
await runtime.startAfterMount();
expect(runtime.browserLifecycle()).toBeNull();
expect(runtime.health().serviceWorker).toBe("DISABLED");
expect(addEventListener).not.toHaveBeenCalled();
expect(serviceWorker.start).not.toHaveBeenCalled();
expect(serviceWorker.stop).not.toHaveBeenCalled();
});
it("serializes stop behind an in-flight start and suppresses late health", async () => {
const start = deferred<ServiceWorkerStartOutcome>();
const serviceWorker: ServiceWorkerRuntimeHost = {
start: vi.fn(() => start.promise),
requestActivation: vi.fn(),
resetOwnedCaches: vi.fn(),
stop: vi.fn(async () => {}),
};
const runtime = createOptionalRuntimeHost({
capabilities,
routerBasePath: "/",
buildId: "build-1",
serviceWorkerHost: serviceWorker,
browserLifecycleHost: {
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
},
});
const starting = runtime.startAfterMount();
const stopping = runtime.stop();
start.resolve({ kind: "ACTIVE", buildId: "build-1" });
await Promise.all([starting, stopping]);
expect(serviceWorker.stop).toHaveBeenCalledTimes(1);
expect(runtime.health().serviceWorker).toBe("DISABLED");
expect(runtime.browserLifecycle()).toBeNull();
});
// 06-05. An ACTIVE selection whose worker install fails must be visible to
// operators, not only in `health()`, which nothing reads.
it.each([
{
outcome: { kind: "FAILED", code: "INSTALL_FAILED" } as const,
events: [
[
"service-worker.start.failed",
{ error_kind: "INSTALL_FAILED", build_id: "build-1" },
],
],
},
{
outcome: { kind: "INCOMPATIBLE" } as const,
events: [
[
"service-worker.start.failed",
{ error_kind: "INCOMPATIBLE", build_id: "build-1" },
],
],
},
{ outcome: { kind: "ACTIVE", buildId: "build-1" } as const, events: [] },
{ outcome: { kind: "RELOAD_TO_ENABLE" } as const, events: [] },
])(
"records $outcome.kind as $events.length telemetry event(s)",
async ({ outcome, events }) => {
const emit = vi.fn();
const runtime = createOptionalRuntimeHost({
capabilities: {
...capabilities,
serviceWorker: {
mode: "ACTIVE",
scriptPath: "service-worker.js",
handlers: ["PWA_STATIC_ASSETS"],
},
},
routerBasePath: "/",
buildId: "build-1",
serviceWorkerHost: {
start: vi.fn(async (): Promise<ServiceWorkerStartOutcome> => outcome),
requestActivation: vi.fn(),
resetOwnedCaches: vi.fn(),
stop: vi.fn(async () => {}),
},
browserLifecycleHost: {
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
},
telemetry: { emit },
});
await runtime.startAfterMount();
expect(emit.mock.calls).toEqual(events);
await runtime.stop();
},
);
});