// Two host pages on one origin share one Service Worker, and each /~/ URL names its // owning page, so each page's frames and module fetches reach its own runtime. import { test, expect, type Page } from "@playwright/test"; import { appUrl } from "./http-golden"; async function framed(page: Page, text: string): Promise { await expect(page.frameLocator("#app").locator("#hi")).toHaveText(text, { timeout: 20_000 }); return await appUrl(page); } const get = (page: Page, url: string) => page.evaluate(async (url) => { const r = await fetch(url); return { status: r.status, text: await r.text() }; }, url); test("two pages each reach their own runtime, and a page's URL works from the other", async ({ context }) => { const hono = await context.newPage(); const esm = await context.newPage(); // Booted together, so both module graphs load through the SW at the same time. await Promise.all([hono.goto("/demo/hono.html"), esm.goto("/demo/http.html?esm")]); const [honoUrl, esmUrl] = await Promise.all([framed(hono, "Hello from Hono"), framed(esm, "Hello from GET / (esm)")]); expect(honoUrl).not.toBe(esmUrl); expect(honoUrl).toMatch(/^\/~\/@[0-9a-f]{16}\/http\/3000\/$/); expect((await get(esm, honoUrl)).text).toContain("Hello from Hono"); expect((await get(hono, esmUrl)).text).toContain("Hello from GET / (esm)"); await expect(hono.locator("#err")).toHaveText(""); await expect(esm.locator("#err")).toHaveText(""); }); test("closing one page leaves the other working, and the closed page's URL answers 502", async ({ context }) => { const a = await context.newPage(); const b = await context.newPage(); await Promise.all([a.goto("/demo/hono.html"), b.goto("/demo/http.html")]); const [aUrl, bUrl] = await Promise.all([framed(a, "Hello from Hono"), framed(b, "Hello from GET /")]); const token = /@([0-9a-f]{16})\//.exec(aUrl)![1]; await a.close(); const started = Date.now(); expect(await get(b, aUrl)).toEqual({ status: 502, text: `ERR_NIB_NO_HOST: no runtime host page is open for @${token}\n` }); expect(Date.now() - started).toBeLessThan(5_000); expect((await get(b, bUrl)).text).toContain("Hello from GET /"); await b.frameLocator("#app").locator("#about").click(); await expect(b.frameLocator("#app").locator("#about-h")).toHaveText("About"); });