import { expect, test } from '@playwright/test'; import { PRODUCT_NAME } from '@rosea/schema'; import { en } from '@rosea/ui/copy'; import { startStaticServer } from './static-server.js'; /** * The offline suite. CLAUDE.md §3 describes its full shape: tend the rose, walk * a planet, watch a sunset, close the tab, reopen, assert everything survived. * Phase 0 has no rose, so this proves the part everything else will stand on: * the app opens with no server anywhere, under its real base path. * * The server is genuinely CLOSED, not emulated: `context.setOffline` acts before * the service worker and would test the harness instead (imported from Ludus). */ test('the shell opens from the service worker with the server gone', async ({ browser }) => { const server = await startStaticServer(); const context = await browser.newContext(); let serverOpen = true; try { const page = await context.newPage(); await page.goto(server.url); await expect(page.getByRole('heading', { level: 1 })).toHaveText(PRODUCT_NAME); // `clientsClaim` makes the FIRST worker control the page that registered it. // Without it, a player who opens the link once and loses signal has nothing. await page.waitForFunction(() => navigator.serviceWorker.controller !== null, null, { timeout: 20_000 }); await page.waitForFunction( async () => { for (const name of await caches.keys()) { const cache = await caches.open(name); if ((await cache.keys()).some((r) => r.url.includes('index.html'))) return true; } return false; }, null, { timeout: 20_000 } ); await server.close(); serverOpen = false; // Proof the network is really gone, from outside the browser. await expect(fetch(server.url)).rejects.toThrow(); // 1. Reload the open tab. await page.reload(); await expect(page.getByRole('heading', { level: 1 })).toHaveText(PRODUCT_NAME); await expect(page.getByText(en.shell.preparing)).toBeVisible(); // 2. Close the tab and open a fresh one: a cold start, the shape of a player // coming back days later with no signal. await page.close(); const again = await context.newPage(); await again.goto(server.url); await expect(again.getByRole('heading', { level: 1 })).toHaveText(PRODUCT_NAME); // 3. A deep link inside the scope falls back to the shell, not an error page. await again.goto(`${server.url}somewhere/deeper`); await expect(again.getByRole('heading', { level: 1 })).toHaveText(PRODUCT_NAME); } finally { // A test run leaves the machine as it found it (CLAUDE.md §5.7). await context.close(); if (serverOpen) await server.close(); } });