diff --git a/apps/app/e2e/playwright.shell.config.ts b/apps/app/e2e/playwright.shell.config.ts new file mode 100644 index 00000000..9ceaf607 --- /dev/null +++ b/apps/app/e2e/playwright.shell.config.ts @@ -0,0 +1,48 @@ +/** + * `pnpm e2e:shell` (scripts/db-sandbox.sh e2e-shell): the authenticated /app + * shell in Chromium against the isolated stack of `pnpm db:sandbox`, never DEV. + * The dev server is built with a Supabase URL that resolves nowhere and runs + * on a loopback port of its own; e2e/support/sandbox.ts replays its calls on + * the sandbox. No trace is kept: it would hold the session tokens. + */ +import { defineConfig } from '@playwright/test' +import type { GuardOptions } from './support/browser' +import { PROJECTS } from './support/browser' + +const ORIGIN = 'http://127.0.0.1:5192' +const FAKE_SUPABASE_ORIGIN = 'http://sandbox.faunosteo.invalid' +const anonKey = process.env.SANDBOX_ANON_KEY +if (!anonKey) + throw new Error( + 'SANDBOX_ANON_KEY missing: run pnpm e2e:shell (the sandbox must be up)', + ) + +export default defineConfig({ + testDir: './shell', + outputDir: './test-results/shell', + fullyParallel: true, + forbidOnly: true, + retries: 0, + reporter: [ + ['list'], + ['html', { outputFolder: './playwright-report/shell', open: 'never' }], + ], + use: { + baseURL: ORIGIN, + allowedOrigins: [ORIGIN, FAKE_SUPABASE_ORIGIN], + trace: 'off', + screenshot: 'only-on-failure', + }, + projects: PROJECTS, + webServer: { + command: 'node_modules/.bin/vite --host 127.0.0.1 --port 5192 --strictPort', + cwd: '..', + url: `${ORIGIN}/`, + reuseExistingServer: false, + timeout: 120_000, + env: { + VITE_SUPABASE_URL: FAKE_SUPABASE_ORIGIN, + VITE_SUPABASE_ANON_KEY: anonKey, + }, + }, +}) diff --git a/apps/app/e2e/shell/shell.spec.ts b/apps/app/e2e/shell/shell.spec.ts new file mode 100644 index 00000000..7bb018f2 --- /dev/null +++ b/apps/app/e2e/shell/shell.spec.ts @@ -0,0 +1,181 @@ +import { en } from '@/i18n/en' +import { fr } from '@/i18n/fr' +import type { Dict } from '@/i18n/types' +import type { Page } from '@playwright/test' +import { + expect, + expectAccessible, + expectNoHorizontalOverflow, + focusOwner, + focusedName, + test, +} from '../support/browser' +import { + createAccount, + routeToSandbox, + type SandboxAccount, +} from '../support/sandbox' + +// The authenticated shell on the isolated stack: sign-in, dashboard, the +// account photo confirmation and the mobile drawer, through the shared modal. + +const LANGUAGES = [ + ['fr', fr], + ['en', en], +] as const + +async function signIn(page: Page, account: SandboxAccount, dict: Dict) { + await page.goto('/login') + await page.getByLabel(dict['auth.email']).fill(account.email) + await page + .getByLabel(dict['auth.password'], { exact: true }) + .fill(account.password) + await page.getByRole('button', { name: dict['auth.login.submit'] }).click() + await expect(page).toHaveURL(/\/app\/?$/) + // The greeting needs the profile: the session and the first REST call went through. + await expect(page.locator('main h1')).toContainText(account.displayName) +} + +/** Focus steps of `count` Tab presses, the browser's interface written « browser ». */ +async function tabWalk(page: Page, count: number): Promise { + const steps: string[] = [] + for (let i = 0; i < count; i++) { + await page.keyboard.press('Tab') + const owner = await focusOwner(page) + steps.push(owner === 'dialog' ? await focusedName(page) : owner) + } + return steps +} + +for (const [locale, dict] of LANGUAGES) { + test.describe(`shell (${locale})`, () => { + test.use({ locale: locale === 'fr' ? 'fr-FR' : 'en-GB' }) + test.beforeEach(async ({ context }) => { + await routeToSandbox(context) + }) + + test('sign-in, dashboard, then the photo confirmation of the account', async ({ + page, + requests, + }) => { + const account = await createAccount(locale, true) + requests.forbid(account.email, account.password, account.displayName) + await signIn(page, account, dict) + await expectAccessible(page, 'dashboard') + await expectNoHorizontalOverflow(page, 'dashboard') + + await page.getByTestId('topbar-account').click() + await expect(page).toHaveURL(/\/app\/account$/) + const remove = page.getByRole('button', { + name: dict['account.avatar.remove'], + }) + await expect(remove).toBeVisible() + await expect( + page + .getByTestId('avatar-section') + .getByRole('img', { name: dict['account.avatar.alt'] }), + ).toBeVisible() + await expectAccessible(page, 'account') + await expectNoHorizontalOverflow(page, 'account') + + await remove.focus() + await page.keyboard.press('Enter') + const dialog = page.getByRole('dialog', { + name: dict['account.avatar.remove_title'], + }) + await expect(dialog).toBeVisible() + await expect(dialog).toHaveAccessibleDescription( + dict['account.avatar.remove_body'], + ) + expect(await focusedName(page)).toBe(dict['common.close']) + expect(await tabWalk(page, 4)).toEqual([ + dict['common.cancel'], + dict['account.avatar.remove_confirm'], + 'browser', + dict['common.close'], + ]) + await expectAccessible(page, 'the photo confirmation') + await expectNoHorizontalOverflow(page, 'the photo confirmation') + await page.keyboard.press('Escape') + await expect(dialog).toBeHidden() + await expect(remove).toBeFocused() + + // A slow removal: the confirmation must stay open until it settles, or the + // focus would return to a button about to vanish. + let settled = false + await page.route('**/rest/v1/rpc/remove_avatar', async (route) => { + await new Promise((resolve) => setTimeout(resolve, 800)) + settled = true + await route.fallback() + }) + await page.keyboard.press('Enter') + await expect(dialog).toBeVisible() + await dialog + .getByRole('button', { + name: dict['account.avatar.remove_confirm'], + exact: true, + }) + .focus() + await page.keyboard.press('Enter') + await page.waitForTimeout(400) + expect(settled).toBe(false) + await expect(dialog).toBeVisible() + await page.keyboard.press('Escape') + await expect(dialog).toBeVisible() + await expect(dialog).toBeHidden() + expect(settled).toBe(true) + await expect(page.getByText(dict['account.avatar.removed'])).toBeVisible() + await expect(remove).toHaveCount(0) + // The visible button, not the file input it opens (out of the tab order). + const change = page + .getByTestId('avatar-section') + .locator('button', { hasText: dict['account.avatar.change'] }) + await expect(change).toBeFocused() + }) + + test('mobile drawer: contained focus, Escape, return to the menu button @phone', async ({ + page, + requests, + }) => { + const account = await createAccount(locale, false) + requests.forbid(account.email, account.password, account.displayName) + await signIn(page, account, dict) + + const menu = page.getByRole('button', { name: dict['shell.open_menu'] }) + await menu.focus() + await page.keyboard.press('Enter') + const drawer = page.getByRole('dialog', { name: dict['shell.nav_label'] }) + await expect(drawer).toBeVisible() + expect(await focusOwner(page)).toBe('dialog') + const steps = await tabWalk(page, 12) + expect( + steps.filter((step) => step.startsWith('outside') || step === 'body'), + ).toEqual([]) + expect(steps).toContain(dict['shell.sign_out']) + expect(steps).toContain('browser') + await expectAccessible(page, 'the open drawer') + await expectNoHorizontalOverflow(page, 'the open drawer') + await page.keyboard.press('Escape') + await expect(drawer).toBeHidden() + await expect(menu).toBeFocused() + + await page.keyboard.press('Enter') + await drawer + .getByRole('link', { name: dict['shell.nav.account'] }) + .click() + await expect(drawer).toBeHidden() + await expect(page).toHaveURL(/\/app\/account$/) + + // Widening past the sidebar breakpoint closes the drawer and frees the page. + await menu.click() + await expect(drawer).toBeVisible() + await page.setViewportSize({ width: 1280, height: 800 }) + await expect(drawer).toBeHidden() + await page + .getByRole('link', { name: dict['shell.nav.dashboard'] }) + .first() + .click() + await expect(page).toHaveURL(/\/app\/?$/) + }) + }) +} diff --git a/apps/app/e2e/support/browser.ts b/apps/app/e2e/support/browser.ts index bfec691f..b4e5b545 100644 --- a/apps/app/e2e/support/browser.ts +++ b/apps/app/e2e/support/browser.ts @@ -12,10 +12,14 @@ import { type Project, } from '@playwright/test' -/** Desktop and the 375 × 667 phone of the V1-1 contract (§12.5); Chromium only until V1-5. */ +/** + * Desktop and the 375 × 667 phone of the V1-1 contract (§12.5); Chromium only + * until V1-5. A test tagged `@phone` covers what exists on small screens only. + */ export const PROJECTS: Project[] = [ { name: 'chromium-desktop', + grepInvert: /@phone/, use: { ...devices['Desktop Chrome'], viewport: { width: 1280, height: 800 }, @@ -40,16 +44,23 @@ export interface GuardOptions { forbiddenInUrls: string[] } +export interface RequestGuard { + /** Adds values made during the test (a fictitious account) to `forbiddenInUrls`. */ + forbid: (...values: string[]) => void +} + /** * Every test runs under a request guard: a request to an origin outside the * allowlist (the DEV stack, a CDN, anything) is aborted, and the test fails - * listing it, as it fails on a URL holding a fixture value. + * listing it, as it fails on a URL holding a fixture value (masked in the + * report, which never shows the value itself). */ -export const test = base.extend({ +export const test = base.extend({ allowedOrigins: [[], { option: true }], forbiddenInUrls: [[], { option: true }], - requestGuard: [ + requests: [ async ({ context, allowedOrigins, forbiddenInUrls }, use) => { + const forbidden = forbiddenInUrls.filter((value) => value !== '') const refused: string[] = [] const leaking: string[] = [] await context.route( @@ -61,10 +72,19 @@ export const test = base.extend({ ) context.on('request', (request) => { const url = decodeURIComponent(request.url()) - if (forbiddenInUrls.some((value) => value && url.includes(value))) - leaking.push(url) + const found = forbidden.filter((value) => url.includes(value)) + if (found.length > 0) + leaking.push( + found.reduce( + (masked, value) => masked.replaceAll(value, ''), + url, + ), + ) + }) + await use({ + forbid: (...values) => + forbidden.push(...values.filter((v) => v !== '')), }) - await use() expect(refused, 'requests outside the allowed origins').toEqual([]) expect(leaking, 'request URLs carrying a fixture value').toEqual([]) }, @@ -77,13 +97,23 @@ export { expect } /** * Serious violations found before V1-1.3, outside its code, listed in TODO RR-7 * until their own fix: the scan lets these exact nodes through and fails on any - * other. The language switcher's buttons show « FR » / « EN » but are named - * « Passer en français » / « Switch to English » (WCAG 2.5.3 label in name). + * other (WCAG 2.5.3 label in name, 1.3.1 info and relationships). */ -const KNOWN_VIOLATIONS: { id: string; target: RegExp }[] = [ +const KNOWN_VIOLATIONS: { id: string; target: RegExp; note: string }[] = [ { id: 'label-content-name-mismatch', target: /^button\[aria-label="(Passer en français|Switch to English)"\]$/, + note: 'LanguageSwitcher: shows « FR » / « EN », named « Passer en français » / « Switch to English »', + }, + { + id: 'label-content-name-mismatch', + target: /^\.pr-1$/, + note: 'AppLayout top bar account link: shows the display name, named « Mon compte » / « My account »', + }, + { + id: 'definition-list', + target: /^dl$/, + note: 'DashboardRoute account card: the verified-status row is a div without dt/dd inside the dl', }, ] diff --git a/apps/app/e2e/support/sandbox.ts b/apps/app/e2e/support/sandbox.ts new file mode 100644 index 00000000..dec7ebdb --- /dev/null +++ b/apps/app/e2e/support/sandbox.ts @@ -0,0 +1,109 @@ +/** + * The shell suite's backend: the throwaway stack of `pnpm db:sandbox`, never + * DEV. The app is built against a fake origin that resolves nowhere; every + * request to it is replayed on the sandbox's loopback services, the way Kong + * maps them on the CLI stack. Accounts are fictitious and made per test. + */ +import { createHash, randomBytes, randomUUID } from 'node:crypto' +import type { BrowserContext } from '@playwright/test' +import { sql, stack } from '../../../../packages/supabase/tests/sandbox/stack' + +/** The Supabase URL the e2e:shell dev server is built with (RFC 6761 `.invalid`). */ +export const FAKE_SUPABASE_ORIGIN = 'http://sandbox.faunosteo.invalid' + +const storageUrl = process.env.SANDBOX_STORAGE_URL +if (!storageUrl) + throw new Error('SANDBOX_STORAGE_URL missing: run pnpm e2e:shell') + +const ROUTES: [prefix: string, target: string][] = [ + ['/auth/v1/', `${stack.authUrl}/`], + ['/rest/v1/', `${stack.restUrl}/`], + ['/storage/v1/', `${storageUrl}/`], +] + +/** Replays the page's Supabase calls on the sandbox; an unmapped path is refused. */ +export async function routeToSandbox(context: BrowserContext): Promise { + await context.route(`${FAKE_SUPABASE_ORIGIN}/**`, async (route) => { + const path = + new URL(route.request().url()).pathname + + new URL(route.request().url()).search + const mapped = ROUTES.find(([prefix]) => path.startsWith(prefix)) + if (!mapped) return route.abort('blockedbyclient') + const response = await route.fetch({ + url: mapped[1] + path.slice(mapped[0].length), + }) + await route.fulfill({ response }) + }) +} + +/** 1 × 1 lossless WebP: a real image for the clean avatar the account starts with. */ +const AVATAR_WEBP = Buffer.from( + 'UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA==', + 'base64', +) + +export interface SandboxAccount { + email: string + password: string + displayName: string +} + +async function admin(path: string, init: RequestInit): Promise { + const response = await fetch(`${stack.authUrl}${path}`, { + ...init, + headers: { + Authorization: `Bearer ${stack.serviceKey}`, + apikey: stack.serviceKey, + 'Content-Type': 'application/json', + }, + }) + if (!response.ok) + throw new Error(`sandbox admin ${path}: HTTP ${response.status}`) + return response +} + +/** The clean avatar the worker would have produced: row as postgres, object with the service key. */ +async function seedAvatar(ownerId: string): Promise { + const assetId = randomUUID() + const key = `profiles/${ownerId}/${assetId}/avatar.webp` + const sha256 = createHash('sha256').update(AVATAR_WEBP).digest('hex') + const upload = await fetch(`${storageUrl}/object/avatars/${key}`, { + method: 'POST', + headers: { + Authorization: `Bearer ${stack.serviceKey}`, + 'Content-Type': 'image/webp', + }, + body: AVATAR_WEBP, + }) + if (!upload.ok) + throw new Error(`sandbox avatar upload: HTTP ${upload.status}`) + await sql(`insert into public.assets + (id, owner_id, kind, bucket_id, object_key, status, declared_mime, declared_size, byte_size, sha256) + values ('${assetId}', '${ownerId}', 'profile_avatar', 'avatars', '${key}', 'clean', + 'image/webp', ${AVATAR_WEBP.length}, ${AVATAR_WEBP.length}, '${sha256}')`) +} + +/** A confirmed fictitious account, its profile in `locale`, with or without a photo. */ +export async function createAccount( + locale: 'fr' | 'en', + withAvatar: boolean, +): Promise { + const tag = randomUUID().slice(0, 8) + const account = { + email: `e2e-shell-${tag}@sandbox.local`, + password: randomBytes(18).toString('base64url'), + displayName: `Praticienne Fictive ${tag}`, + } + const response = await admin('/admin/users', { + method: 'POST', + body: JSON.stringify({ + email: account.email, + password: account.password, + email_confirm: true, + user_metadata: { display_name: account.displayName, locale }, + }), + }) + const { id } = (await response.json()) as { id: string } + if (withAvatar) await seedAvatar(id) + return account +} diff --git a/apps/app/package.json b/apps/app/package.json index 63ed3519..efe9a954 100644 --- a/apps/app/package.json +++ b/apps/app/package.json @@ -12,7 +12,8 @@ "test": "vitest", "test:run": "vitest run", "lint": "eslint . --max-warnings 0", - "e2e:ui": "playwright test -c e2e/playwright.ui.config.ts" + "e2e:ui": "playwright test -c e2e/playwright.ui.config.ts", + "e2e:shell": "playwright test -c e2e/playwright.shell.config.ts" }, "dependencies": { "@faunosteo/core-wasm": "workspace:*", diff --git a/package.json b/package.json index 90a61252..51fc100b 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,8 @@ "worker:test": "docker build -f crates/worker/Dockerfile --target test -t faunosteo_worker:test .", "worker:image": "docker build -f crates/worker/Dockerfile -t faunosteo_worker:dev .", "e2e:avatar": "bash scripts/avatar-e2e.sh", - "e2e:ui": "pnpm --filter @faunosteo/app e2e:ui" + "e2e:ui": "pnpm --filter @faunosteo/app e2e:ui", + "e2e:shell": "bash scripts/db-sandbox.sh e2e-shell" }, "devDependencies": { "pnpm": "10.13.1", diff --git a/scripts/db-sandbox.sh b/scripts/db-sandbox.sh index 58d35e40..b9bb0002 100755 --- a/scripts/db-sandbox.sh +++ b/scripts/db-sandbox.sh @@ -11,6 +11,7 @@ # pnpm db:sandbox test-db the pgTAP files (`supabase test db`) # pnpm db:sandbox types [--check] generated types (write, or diff with the committed file) # pnpm db:sandbox test packages/supabase/tests/sandbox (HTTP, concurrency) +# pnpm db:sandbox e2e-shell = pnpm e2e:shell: the /app shell in Chromium (apps/app/e2e) # pnpm db:sandbox sql psql as postgres on stdin # pnpm db:sandbox down # @@ -213,6 +214,15 @@ cmd_down() { echo "== sandbox removed" } +# A test runner gets the endpoints and keys through its environment, never argv. +with_sandbox_env() ( + export SANDBOX_DB_CONTAINER="$DB" SANDBOX_PG_PASSWORD="$SB_PW" + export SANDBOX_AUTH_URL="http://127.0.0.1:$PORT_AUTH" SANDBOX_REST_URL="http://127.0.0.1:$PORT_REST" + export SANDBOX_STORAGE_URL="http://127.0.0.1:$PORT_STORAGE" SANDBOX_MAIL_URL="http://127.0.0.1:$PORT_MAIL" + export SANDBOX_ANON_KEY="$SB_ANON_KEY" SANDBOX_SERVICE_KEY="$SB_SERVICE_KEY" + "$@" +) + case "${1:-}" in up) shift; cmd_up "$@" ;; down) cmd_down ;; @@ -225,12 +235,8 @@ case "${1:-}" in else printf '%s\n' "$out" > packages/supabase/src/database.types.ts; echo "== types written" fi ;; - test) - load_secrets - SANDBOX_DB_CONTAINER="$DB" SANDBOX_PG_PASSWORD="$SB_PW" SANDBOX_AUTH_URL="http://127.0.0.1:$PORT_AUTH" \ - SANDBOX_REST_URL="http://127.0.0.1:$PORT_REST" SANDBOX_MAIL_URL="http://127.0.0.1:$PORT_MAIL" \ - SANDBOX_ANON_KEY="$SB_ANON_KEY" SANDBOX_SERVICE_KEY="$SB_SERVICE_KEY" \ - node_modules/.bin/pnpm --filter @faunosteo/supabase test:sandbox ;; + test) load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/supabase test:sandbox ;; + e2e-shell) load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/app e2e:shell ;; sql) load_secrets; sb_psql ;; - *) sed -n '2,23p' "$0"; exit 1 ;; + *) sed -n '2,24p' "$0"; exit 1 ;; esac