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 { BASE, startStaticServer, type StaticServer } from './static-server.js'; let server: StaticServer; test.beforeAll(async () => { server = await startStaticServer(); }); test.afterAll(async () => { await server.close(); }); /** Opens the app and records every request and every CSP violation on the way. */ async function open(page: Page): Promise<{ requests: string[]; cspViolations: string[] }> { const requests: string[] = []; const cspViolations: string[] = []; page.on('request', (r) => requests.push(r.url())); page.on('console', (m) => { if (m.type() === 'error' && /Content Security Policy/i.test(m.text())) cspViolations.push(m.text()); }); await page.goto(server.url); await expect(page.getByRole('heading', { level: 1 })).toHaveText(PRODUCT_NAME); // Let the worker register and precache, so its requests are counted too. await page.waitForFunction(() => navigator.serviceWorker.ready.then(() => true), null, { timeout: 20_000 }); return { requests, cspViolations }; } test('the built app renders its name and its one line, under the base path', async ({ page }) => { await open(page); await expect(page.getByText(en.shell.preparing)).toBeVisible(); await expect(page).toHaveTitle(PRODUCT_NAME); }); test('nothing is requested from any other origin', async ({ page }) => { const { requests } = await open(page); expect(requests.length, 'the page must have made requests for this to mean anything').toBeGreaterThan(0); expect(requests.filter((url) => !url.startsWith(server.origin + '/'))).toEqual([]); }); test('nothing is written to cookies or Web Storage', async ({ page }) => { await open(page); const written = await page.evaluate(() => ({ cookie: document.cookie, localStorage: localStorage.length, sessionStorage: sessionStorage.length })); expect(written).toEqual({ cookie: '', localStorage: 0, sessionStorage: 0 }); expect(await page.context().cookies()).toEqual([]); }); test('the built page carries the CSP and no inline script, and nothing violates it', async ({ page }) => { const { cspViolations } = await open(page); const html = await (await fetch(server.url)).text(); // Read from the DOM, not the raw HTML: Vite escapes the quotes as '. const csp = await page.locator('meta[http-equiv="Content-Security-Policy"]').getAttribute('content'); expect(csp, 'CSP meta tag in the built index.html').toBeTruthy(); expect(csp).toContain("default-src 'self'"); expect(csp).not.toContain('unsafe-inline'); expect(csp).not.toContain('unsafe-eval'); const inline = [...html.matchAll(/]*)>([\s\S]*?)<\/script>/g)].filter( ([, attrs, body]) => !/\bsrc=/.test(attrs ?? '') && (body ?? '').trim() !== '' ); expect(inline.map(([tag]) => tag)).toEqual([]); expect(cspViolations).toEqual([]); }); test('manifest scope, start_url and the service worker scope all equal the base path', async ({ page }) => { await open(page); const manifestHref = await page.locator('link[rel="manifest"]').getAttribute('href'); expect(manifestHref, 'manifest link').toBeTruthy(); const manifest = (await (await fetch(new URL(manifestHref!, server.url))).json()) as Record; expect(manifest['scope']).toBe(BASE); expect(manifest['start_url']).toBe(BASE); expect(manifest['name']).toBe(PRODUCT_NAME); const swScope = await page.evaluate(async () => (await navigator.serviceWorker.ready).scope); expect(swScope).toBe(server.url); // The icon the manifest names must exist where the manifest says it is. const icons = manifest['icons'] as { src: string }[]; for (const icon of icons) { const res = await fetch(new URL(icon.src, new URL(manifestHref!, server.url))); expect(res.status, icon.src).toBe(200); } }); test('the shell has no accessibility violations', async ({ page }) => { await open(page); const results = await new AxeBuilder({ page }).analyze(); expect(results.violations.map((v) => `${v.id}: ${v.help}`)).toEqual([]); });