test(service-worker): 살아남던 워커 보호 장치 변이 4건을 테스트로 잡는다

문제
워커 보호 장치 넷은 지워도 테스트가 모두 통과한다(1101/06-04, 이번
리뷰에서 다시 생존). install fetch의 credentials: "omit"·redirect:
"error"(M1), activate nonce 한 번만 쓰기(M3), 요청받지 않은 client의
CLIENT_DRAINED 거절(M9), 첫 설치를 "업데이트 대기"로 알리지 않기(N8).

고른 방법
- service-worker-runtime.test.ts에 테스트 3건을 더한다.
  - install이 자산마다 cache no-store, credentials omit, redirect error로
    요청한다(M1).
  - 같은 nonce의 두 번째 ACTIVATE_REQUEST는 REJECTED, 요청받지 않은
    client의 drain 응답은 세지 않는다(M3, M9).
  - active 워커가 없는 첫 설치는 onUpdateWaiting을 부르지 않는다(N8).
- 동작은 바꾸지 않는다.

위험·한계
- N8 테스트는 시작 뒤 registration.active를 null로 바꿔 첫 설치를
  흉내 낸다. 실제 브라우저 첫 설치 순서는 e2e가 본다.

검증
- service-worker-runtime 64건 통과.
- 변이 4건(M1·M3·M9·N8)을 하나씩 넣어 각각 1건 실패를 확인하고
  되돌렸다.
- 바꾼 파일 eslint, check:types:test 통과.

Refs: docs/reviews/2026-11-03-maturity-review/06-adapters-service-worker-push.md 2절 1101/06-04
This commit is contained in:
DongHyeonka
2026-11-04 20:40:11 +09:00
parent c3aa5eba35
commit 4f19cd9e8b
+99
View File
@@ -768,6 +768,45 @@ describe("service worker worker-side protocol", () => {
expect(fixture.scope.skipWaiting).toHaveBeenCalledTimes(1);
});
// 1101/06-04 (M3, M9). An activation nonce is single-use, and only a client
// the worker asked to drain can acknowledge it.
it("refuses a reused nonce and a drain reply from a client it did not ask", async () => {
const fixture = workerScope();
const runtime = createServiceWorkerRuntime(fixture.scope as never, {
identity,
handlers: [],
manifest: null,
runtimeConfigUrl: "/runtime-config.json",
releaseManifestUrl: "/release-manifest.json",
});
const message = (kind: "ACTIVATE_REQUEST" | "CLIENT_DRAINED") =>
createServiceWorkerMessage({
kind,
sourceBuildId: "page-build",
targetBuildId: "worker-build",
nonce: "activation-1",
});
const activation = runtime.onActivateRequest(message("ACTIVATE_REQUEST"));
let settled = false;
void activation.then(() => {
settled = true;
});
await Promise.resolve();
runtime.onClientMessage(message("CLIENT_DRAINED"), "client-a");
runtime.onClientMessage(message("CLIENT_DRAINED"), "client-unasked");
for (let turn = 0; turn < 10; turn += 1) await Promise.resolve();
expect(settled).toBe(false);
expect(fixture.scope.skipWaiting).not.toHaveBeenCalled();
runtime.onClientMessage(message("CLIENT_DRAINED"), "client-b");
await expect(activation).resolves.toBe("ACCEPTED");
await expect(
runtime.onActivateRequest(message("ACTIVATE_REQUEST")),
).resolves.toBe("REJECTED");
expect(fixture.scope.skipWaiting).toHaveBeenCalledTimes(1);
});
it("enumerates uncontrolled window clients before an activation drain", async () => {
const fixture = workerScope();
const runtime = createServiceWorkerRuntime(fixture.scope as never, {
@@ -1039,6 +1078,41 @@ describe("service worker static asset install", () => {
return { result, put };
}
// 1101/06-04 (M1). The precache holds only public assets: no cookie or
// credential goes with the request, a redirect is refused rather than
// followed to another resource, and the HTTP cache is bypassed.
it("fetches each asset without credentials, redirects or the HTTP cache", async () => {
const fetcher = vi.fn(
async (_url: RequestInfo | URL, _init?: RequestInit) =>
new Response(new Uint8Array([1]), {
status: 200,
headers: { "content-type": "text/javascript" },
}),
);
await expect(
installStaticAssets(
{
...manifest,
assets: [{ ...manifest.assets[0]!, contentType: "text/javascript" }],
},
{
caches: {
open: vi.fn(async () => ({ put: vi.fn(async () => {}) }) as unknown as Cache),
delete: vi.fn(async () => true),
},
fetcher,
digest: vi.fn(async () => manifest.assets[0]!.sha256),
},
),
).resolves.toMatchObject({ kind: "INSTALLED" });
expect(fetcher).toHaveBeenCalledOnce();
expect(fetcher.mock.calls[0]![1]).toMatchObject({
cache: "no-store",
credentials: "omit",
redirect: "error",
});
});
it("installs JavaScript served under a JavaScript MIME synonym (R-6.5)", async () => {
const { result, put } = installOne(
new Response(new Uint8Array([1]), {
@@ -1339,6 +1413,31 @@ describe("service worker static asset install", () => {
await controller.stop();
});
// 1101/06-04 (N8). A first install has no active worker to replace. It
// activates on its own, so it is never announced as an update waiting.
it("does not announce a first install as an update waiting", async () => {
const browser = pageContainer({ controlled: true });
const controller = pageController(browser.container);
await expect(controller.start()).resolves.toMatchObject({ kind: "ACTIVE" });
const updateWaiting = vi.fn();
controller.onUpdateWaiting?.(updateWaiting);
const registration = browser.registration as unknown as EventTarget & {
installing: ServiceWorker | null;
waiting: ServiceWorker | null;
active: ServiceWorker | null;
};
registration.active = null;
const first = fakeWorker([], "installing");
registration.installing = first as unknown as ServiceWorker;
registration.dispatchEvent(new Event("updatefound"));
registration.installing = null;
registration.waiting = first as unknown as ServiceWorker;
setWorkerState(first, "installed");
expect(updateWaiting).not.toHaveBeenCalled();
await controller.stop();
});
it("accepts a post-commit reply after the waiting worker was promoted (R-6.1)", async () => {
const browser = pageContainer({ waiting: true });
const controller = pageController(browser.container);