test(service-worker): cover the runtime DISABLED kill switch (06-04)

No test used disabledCleanup: true, so a mutation that skipped the unregister
survived. The new cases pin resolveRuntimeCapabilities and the owned/foreign
unregister outcomes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-26 00:11:57 +09:00
co-authored by Claude Opus 5.5
parent e6c683eb4f
commit cebfa67549
+68
View File
@@ -5,6 +5,7 @@ import { createServiceWorkerRuntime } from "../../src/adapters/service-worker/se
import { createServiceWorkerMessage } from "../../src/adapters/service-worker/service-worker-protocol.ts";
import { installStaticAssets } from "../../src/adapters/service-worker/service-worker-static-assets.ts";
import { createServiceWorkerRuntimeHost } from "../../src/bootstrap/register-service-worker.ts";
import { resolveRuntimeCapabilities } from "../../src/contracts/runtime-capabilities.ts";
import {
SERVICE_WORKER_BOUNDS,
SERVICE_WORKER_SCRIPT_PATH,
@@ -459,6 +460,73 @@ describe("service worker exact ownership and truthful removal", () => {
expect(foreign.unregister).not.toHaveBeenCalled();
});
// 06-04. The runtime kill switch: `CAPABILITY_OVERRIDES.SERVICE_WORKER =
// "DISABLED"` over a static ACTIVE worker unregisters the owned registration
// without registering anything.
it("resolves runtime DISABLED over a static ACTIVE worker to disabled cleanup", () => {
expect(
resolveRuntimeCapabilities(
{
realtime: [],
webWorkers: [],
serviceWorker: {
mode: "ACTIVE",
scriptPath: SERVICE_WORKER_SCRIPT_PATH,
handlers: [],
},
offlineCommands: null,
},
{
REALTIME: "DEFAULT",
WEB_WORKER: "DEFAULT",
SERVICE_WORKER: "DISABLED",
OFFLINE_COMMANDS: "DEFAULT",
},
),
).toMatchObject({ serviceWorker: null, serviceWorkerDisabledCleanup: true });
});
it.each([
{ owner: "owned", scriptURL: SCRIPT_URL, unregisters: 1, expected: "DISABLED" },
{
owner: "foreign",
scriptURL: `${ORIGIN}/someone-else.js`,
unregisters: 0,
expected: "INCOMPATIBLE",
},
])(
"runtime DISABLED unregisters only an $owner registration",
async ({ scriptURL, unregisters, expected }) => {
const registration = {
scope: `${ORIGIN}/`,
installing: null,
waiting: null,
active: { scriptURL },
unregister: vi.fn(async () => true),
update: vi.fn(async () => {}),
} as unknown as ServiceWorkerRegistration;
const container = {
controller: null,
register: vi.fn(),
getRegistration: vi.fn(async () => registration),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
};
const controller = createServiceWorkerPageController({
selection: null,
disabledCleanup: true,
routerBasePath: "/",
origin: ORIGIN,
buildId: "page-build",
container: container as unknown as ServiceWorkerContainer,
});
await expect(controller.start()).resolves.toEqual({ kind: expected });
expect(container.register).not.toHaveBeenCalled();
expect(container.getRegistration).toHaveBeenCalledOnce();
expect(registration.unregister).toHaveBeenCalledTimes(unregisters);
},
);
});
describe("service worker page protocol", () => {