diff --git a/tests/e2e/service-worker-update.spec.ts b/tests/e2e/service-worker-update.spec.ts index 585df28c..3562825f 100644 --- a/tests/e2e/service-worker-update.spec.ts +++ b/tests/e2e/service-worker-update.spec.ts @@ -237,12 +237,10 @@ test("announces update-waiting and reload-required through the host when build B } }); -test("serves A's removed chunk from the previous cache once B controls the page", async ({ - page, - browserName, -}) => { - test.skip(browserName === "firefox", NO_CONTROLLED_PAGES); - const { dist } = requireBuilds(); +/** The route chunk of A that B no longer has: `CHANGED_ROUTE_SOURCE`'s. */ +async function removedRouteChunk( + dist: ServiceWorkerBuilds["dist"], +): Promise { const routeChunks = Object.values( ( JSON.parse( @@ -255,6 +253,16 @@ test("serves A's removed chunk from the previous cache once B controls the page" .map((chunk) => `/${chunk}`) .find((chunk) => !inB.has(chunk)); if (!removedChunk) throw new Error("Build B kept every route chunk of A."); + return removedChunk; +} + +test("serves A's removed chunk from the previous cache once B controls the page", async ({ + page, + browserName, +}) => { + test.skip(browserName === "firefox", NO_CONTROLLED_PAGES); + const { dist } = requireBuilds(); + const removedChunk = await removedRouteChunk(dist); const server = await startBuildServer(dist); try { @@ -279,6 +287,68 @@ test("serves A's removed chunk from the previous cache once B controls the page" } }); +type SameDocumentProbe = typeof globalThis & { __swE2eSameDocument?: boolean }; + +// 1105/06-03. The old tab's real path, not only `fetch()`: A's page, now +// controlled by B's worker, opens the route whose chunk B removed +// (`CHANGED_ROUTE_SOURCE` is the not-found page, so any unmatched path +// imports it). Under the worker the preload fixture cannot judge preloads +// (the 11-09 allowance above), so this test asserts the outcome: the route +// renders in the same document, the chunk never reaches the server, and the +// strict fixture sees no failure outside the allowance. A worker that cannot +// answer the chunk makes the app's chunk recovery reload into B, which also +// renders the not-found page; only the same-document check tells them apart +// (review 06-R10). +test("opens A's removed route in the old tab without a reload once B controls it", async ({ + page, + browserName, +}) => { + test.skip(browserName === "firefox", NO_CONTROLLED_PAGES); + const { dist } = requireBuilds(); + const removedChunk = await removedRouteChunk(dist); + + const server = await startBuildServer(dist); + try { + const { controlled } = await updateFromAToB(page, server); + expect(controlled).toBe(true); + const entryScript = () => + page.evaluate( + () => + document.querySelector('script[type="module"][src]') + ?.src ?? null, + ); + const entryBefore = await entryScript(); + await page.evaluate(() => { + (globalThis as SameDocumentProbe).__swE2eSameDocument = true; + }); + const servedBefore = server.served.length; + + // A client-side navigation, as a link inside the app makes. + await page.evaluate(() => { + history.pushState({}, "", "/sw-e2e-removed-route"); + dispatchEvent(new PopStateEvent("popstate")); + }); + + await expect( + page.getByRole("heading", { name: /페이지를 찾을 수 없습니다|Page not found/u }), + ).toBeVisible({ timeout: 30_000 }); + const after = await page.evaluate((chunk) => ({ + sameDocument: + (globalThis as SameDocumentProbe).__swE2eSameDocument === true, + chunkRequested: + performance.getEntriesByName(new URL(chunk, location.origin).href) + .length > 0, + }), removedChunk); + expect(after).toEqual({ sameDocument: true, chunkRequested: true }); + expect(await entryScript()).toBe(entryBefore); + expect( + server.served.slice(servedBefore).map((response) => response.path), + ).not.toContain(removedChunk); + } finally { + await server.close(); + } +}); + // `beforeAll` already ran `scripts/build-frontend.ts` unmodified (default // `bundle` config loader); this pins that both builds emitted the worker. test("builds the ACTIVE worker with the repo's unmodified build script", async () => {