[app] CalmRefresh: Register the worker in the app and offer the waiting version; e2e publishes a real second one
This commit is contained in:
@@ -7,7 +7,7 @@ import { defineConfig, devices } from '@playwright/test';
|
||||
*/
|
||||
export default defineConfig({
|
||||
testDir: '.',
|
||||
testMatch: /shell\.spec\.ts/,
|
||||
testMatch: /(shell|update)\.spec\.ts/,
|
||||
fullyParallel: false,
|
||||
workers: 1,
|
||||
retries: 0,
|
||||
|
||||
+10
-4
@@ -19,7 +19,7 @@ import { fileURLToPath } from 'node:url';
|
||||
*/
|
||||
|
||||
export const BASE = process.env['ROSEA_BASE'] ?? '/Rosea/';
|
||||
const DIST = fileURLToPath(new URL('../apps/rosea/dist', import.meta.url));
|
||||
export const DIST = fileURLToPath(new URL('../apps/rosea/dist', import.meta.url));
|
||||
|
||||
const MIME: Record<string, string> = {
|
||||
'.html': 'text/html; charset=utf-8',
|
||||
@@ -36,6 +36,8 @@ export interface StaticServer {
|
||||
readonly url: string;
|
||||
readonly origin: string;
|
||||
readonly close: () => Promise<void>;
|
||||
/** Serves another build from now on, at the same URL: a new version being published. */
|
||||
readonly serveFrom: (dir: string) => void;
|
||||
}
|
||||
|
||||
export async function startStaticServer(): Promise<StaticServer> {
|
||||
@@ -45,6 +47,7 @@ export async function startStaticServer(): Promise<StaticServer> {
|
||||
throw new Error(`No built app at ${DIST}. Run \`pnpm build\` first. (${String(error)})`);
|
||||
}
|
||||
|
||||
let root = DIST;
|
||||
const server: Server = createServer((req, res) => {
|
||||
const path = decodeURIComponent((req.url ?? '/').split('?')[0] ?? '/');
|
||||
|
||||
@@ -55,9 +58,9 @@ export async function startStaticServer(): Promise<StaticServer> {
|
||||
}
|
||||
|
||||
const rel = normalize(path.slice(BASE.length) || 'index.html');
|
||||
const file = join(DIST, rel);
|
||||
// Confine to DIST: a test server is still a server.
|
||||
if (!file.startsWith(DIST + sep) && file !== DIST) {
|
||||
const file = join(root, rel);
|
||||
// Confine to the served folder: a test server is still a server.
|
||||
if (!file.startsWith(root + sep) && file !== root) {
|
||||
res.writeHead(403).end();
|
||||
return;
|
||||
}
|
||||
@@ -90,6 +93,9 @@ export async function startStaticServer(): Promise<StaticServer> {
|
||||
return {
|
||||
origin,
|
||||
url: `${origin}${BASE}`,
|
||||
serveFrom: (dir) => {
|
||||
root = dir;
|
||||
},
|
||||
close: () =>
|
||||
new Promise<void>((resolve) => {
|
||||
server.closeAllConnections();
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import AxeBuilder from '@axe-core/playwright';
|
||||
import { expect, test, type Page } from '@playwright/test';
|
||||
import { PRODUCT_NAME } from '@rosea/schema';
|
||||
import { en } from '@rosea/ui/copy';
|
||||
import { appendFileSync, cpSync, mkdtempSync, rmSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { DIST, startStaticServer } from './static-server.js';
|
||||
|
||||
/**
|
||||
* The stale service worker (CLAUDE.md §8, calm-refresh.md). A waiting worker exists
|
||||
* only between two builds, so this publishes a real second one: version A's build
|
||||
* with `sw.js` changed, served at the same URL. Its extra listener answers "which
|
||||
* version are you?", so the test knows for certain which worker controls the page.
|
||||
*/
|
||||
const VERSION_B = `
|
||||
// Added by e2e/update.spec.ts: version B of the worker, and a way to ask.
|
||||
self.addEventListener('message', (event) => {
|
||||
if (event.data === 'rosea-which-version') event.source?.postMessage('B');
|
||||
});
|
||||
`;
|
||||
|
||||
/** "A" unless the controlling worker answers that it is B. */
|
||||
async function whichVersion(page: Page): Promise<string> {
|
||||
return page.evaluate(
|
||||
() =>
|
||||
new Promise<string>((resolve) => {
|
||||
const controller = navigator.serviceWorker.controller;
|
||||
if (!controller) return resolve('none');
|
||||
const timer = setTimeout(() => resolve('A'), 1500);
|
||||
navigator.serviceWorker.addEventListener('message', (e) => {
|
||||
if (e.data === 'B') {
|
||||
clearTimeout(timer);
|
||||
resolve('B');
|
||||
}
|
||||
});
|
||||
controller.postMessage('rosea-which-version');
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
test('a new version waits, is offered in one quiet line, and comes only when the player refreshes', async ({ browser }) => {
|
||||
const server = await startStaticServer();
|
||||
const context = await browser.newContext();
|
||||
const versionB = mkdtempSync(join(tmpdir(), 'rosea-version-b-'));
|
||||
try {
|
||||
const page = await context.newPage();
|
||||
await page.goto(server.url);
|
||||
await expect(page.getByRole('heading', { level: 1 })).toHaveText(PRODUCT_NAME);
|
||||
await page.waitForFunction(() => navigator.serviceWorker.controller !== null, null, { timeout: 20_000 });
|
||||
expect(await whichVersion(page)).toBe('A');
|
||||
await expect(page.getByText(en.update.ready)).toHaveCount(0);
|
||||
|
||||
// Publish version B at the same URL.
|
||||
cpSync(DIST, versionB, { recursive: true });
|
||||
appendFileSync(join(versionB, 'sw.js'), VERSION_B);
|
||||
server.serveFrom(versionB);
|
||||
|
||||
// A navigation makes the browser check for a new worker, as it does for a player.
|
||||
await page.reload();
|
||||
await expect(page.getByText(en.update.ready)).toBeVisible({ timeout: 20_000 });
|
||||
|
||||
// Offered, not swapped: A still controls the page, and B waits.
|
||||
expect(await whichVersion(page)).toBe('A');
|
||||
expect(await page.evaluate(async () => (await navigator.serviceWorker.getRegistration())?.waiting !== null)).toBe(true);
|
||||
expect((await new AxeBuilder({ page }).analyze()).violations.map((v) => `${v.id}: ${v.help}`)).toEqual([]);
|
||||
|
||||
// The player refreshes: B takes over and the page reloads under it.
|
||||
await Promise.all([page.waitForEvent('load'), page.getByRole('button', { name: en.update.refresh }).click()]);
|
||||
await expect(page.getByRole('heading', { level: 1 })).toHaveText(PRODUCT_NAME);
|
||||
await expect.poll(() => whichVersion(page), { timeout: 10_000 }).toBe('B');
|
||||
await expect(page.getByText(en.update.ready)).toHaveCount(0);
|
||||
} finally {
|
||||
// A test run leaves the machine as it found it (CLAUDE.md §5.7).
|
||||
await context.close();
|
||||
await server.close();
|
||||
rmSync(versionB, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user