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 { parseLetter } from '@rosea/ui/letter'; import { nextInvitation } from '@rosea/engine'; import { searchTowns } from '@rosea/content/places'; import { readFileSync } from 'node:fs'; import { BASE, startStaticServer, type StaticServer } from './static-server.js'; import { storedTown } from './stored.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([]); // The CSP is injected FIRST in , ahead of , so it governs // every script. The charset must still fall in the first 1024 bytes or the // browser may guess the encoding; a CSP that grows can push it out. const charsetAt = Buffer.from(html).indexOf(' { 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([]); }); // Tests whichever state docs/LETTER.md is in: empty now, the author's words later. test('the Letter: no way to it while empty, and it opens from the garden once written', async ({ page }) => { const written = parseLetter(readFileSync(new URL('../docs/LETTER.md', import.meta.url), 'utf8')).length > 0; await open(page); const link = page.getByRole('link', { name: en.letter.title }); if (!written) { await expect(link).toHaveCount(0); await page.goto(server.url + '#letter'); await expect(page.getByRole('heading', { level: 1 })).toHaveText(PRODUCT_NAME); return; } await link.click(); await expect(page.getByRole('heading', { level: 1 })).toHaveText(en.letter.title); const results = await new AxeBuilder({ page }).analyze(); expect(results.violations.map((v) => `${v.id}: ${v.help}`)).toEqual([]); await page.getByRole('link', { name: en.letter.back }).click(); await expect(page.getByRole('heading', { level: 1 })).toHaveText(PRODUCT_NAME); }); // --- Where the sunset is (.agents/features/sunset-place.md) ------------------ test.describe('the sunset place, in Prague\'s time zone at noon on 29 March 2026', () => { test.use({ timezoneId: 'Europe/Prague', locale: 'en-US' }); const NOON = Date.parse('2026-03-29T12:00:00+02:00'); /** Browsers and Node both put a narrow no-break space before PM; compare words, not spaces. */ const spaced = (s: string): string => s.replace(/\s+/g, ' '); test('first open asks; choosing Brno shows the sunset the engine computes; a reload keeps it', async ({ page }) => { await page.clock.setFixedTime(NOON); const { requests } = await open(page); await expect(page.getByRole('heading', { level: 2, name: en.sunset.question })).toBeVisible(); await page.getByRole('searchbox', { name: en.sunset.searchLabel }).fill('brno'); await page.getByRole('button', { name: /^Brno/ }).first().click(); const brno = searchTowns('brno')[0]!; const { at } = nextInvitation(NOON, brno); const time = new Intl.DateTimeFormat('en-US', { hour: 'numeric', minute: '2-digit', timeZone: brno.timeZone }).format(at); const line = page.locator('.sun p').first(); await expect(line).toHaveText(/in Brno\.$/); expect(spaced((await line.textContent()) ?? '')).toBe(spaced(en.sunset.tonight(time, 'Brno'))); await storedTown(page, 'Brno'); await page.reload(); await expect(line).toHaveText(/in Brno\.$/); await expect(page.getByRole('searchbox')).toHaveCount(0); // Searching and choosing loaded the town list from this origin and nothing else. expect(requests.filter((url) => !url.startsWith(server.origin + '/'))).toEqual([]); expect(requests.some((url) => /\/assets\/places-.*\.js$/.test(url))).toBe(true); }); test('"not now" guesses Prague from the device\'s zone and says it is a guess', async ({ page }) => { await page.clock.setFixedTime(NOON); await open(page); await page.getByRole('button', { name: en.sunset.skip('Prague') }).click(); await expect(page.locator('.sun p').first()).toHaveText(/^Tonight the sun sets at .* in Prague\.$/); await expect(page.getByText(en.sunset.guessed)).toBeVisible(); }); test('the question and the answer have no accessibility violations', async ({ page }) => { await page.clock.setFixedTime(NOON); await open(page); await page.getByRole('searchbox').fill('pra'); await expect(page.getByRole('status')).not.toHaveText(''); expect((await new AxeBuilder({ page }).analyze()).violations.map((v) => `${v.id}: ${v.help}`)).toEqual([]); await page.getByRole('button', { name: /^Prague/ }).first().click(); await expect(page.locator('.sun')).toBeVisible(); expect((await new AxeBuilder({ page }).analyze()).violations.map((v) => `${v.id}: ${v.help}`)).toEqual([]); }); });