From d33a8f6dad2add68b56bdc065fc73bc78bfa8107 Mon Sep 17 00:00:00 2001 From: bchanot Date: Mon, 26 Oct 2026 01:59:26 +0100 Subject: [PATCH] test(app): client journeys in Chromium on the isolated stack Create, search, edit, archive, archived filter, restore; pages of 25; keyboard only; two accounts; a concurrent edit; a retry after a lost answer sends the same request id; network failures; the leave guard; a change of language; FR/EN, desktop and 375 px, axe on every state. signIn shared. --- apps/app/e2e/shell/clients.spec.ts | 599 +++++++++++++++++++++++++++++ apps/app/e2e/shell/shell.spec.ts | 20 +- apps/app/e2e/support/sandbox.ts | 29 +- apps/app/e2e/support/shell.ts | 17 + 4 files changed, 638 insertions(+), 27 deletions(-) create mode 100644 apps/app/e2e/shell/clients.spec.ts create mode 100644 apps/app/e2e/support/shell.ts diff --git a/apps/app/e2e/shell/clients.spec.ts b/apps/app/e2e/shell/clients.spec.ts new file mode 100644 index 00000000..4096862d --- /dev/null +++ b/apps/app/e2e/shell/clients.spec.ts @@ -0,0 +1,599 @@ +import { randomUUID } from 'node:crypto' +import { en } from '@/i18n/en' +import { fr } from '@/i18n/fr' +import type { Dict } from '@/i18n/types' +import type { Page, TestInfo } from '@playwright/test' +import { sql } from '../../../../packages/supabase/tests/sandbox/stack' +import { + expect, + expectAccessible, + expectNoHorizontalOverflow, + test, +} from '../support/browser' +import { + createAccount, + routeToSandbox, + sandboxUrl, + workspaceOf, + type SandboxAccount, +} from '../support/sandbox' +import { signIn } from '../support/shell' + +// V1-1.5 clients on the isolated stack: the journeys of contract §11.2 that +// concern clients, keyboard only, two accounts, pages, a concurrent edit, a +// retry after a lost answer, network failures and the leave guard, in FR and +// EN, desktop and 375 px. Every fixture is fictitious and made per test. + +const LANGUAGES = [ + ['fr', fr], + ['en', en], +] as const + +const isPhone = (info: TestInfo) => info.project.name === 'chromium-375' + +/** A client row written as postgres in the account's workspace (setup, not the subject). */ +async function seedClient( + account: SandboxAccount, + displayName: string, + city: string | null = null, +) { + const ws = await workspaceOf(account) + return sql(`insert into public.clients (workspace_id, kind, display_name, city, create_request_id, create_request_digest) + values ('${ws}', 'person', '${displayName}', ${city ? `'${city}'` : 'null'}, gen_random_uuid(), '\\x00') returning id`) +} + +async function openClients(page: Page, dict: Dict, info: TestInfo) { + if (isPhone(info)) { + await page.getByRole('button', { name: dict['shell.open_menu'] }).click() + await page + .getByRole('dialog', { name: dict['shell.nav_label'] }) + .getByRole('navigation') + .getByRole('link', { name: dict['shell.nav.clients'] }) + .click() + } else { + await page + .getByRole('navigation', { name: dict['shell.nav_label'] }) + .getByRole('link', { name: dict['shell.nav.clients'] }) + .click() + } + await expect(page).toHaveURL(/\/app\/clients$/) + await expect( + page + .getByRole('main') + .getByRole('heading', { level: 1, name: dict['clients.list.title'] }), + ).toBeFocused() +} + +const listRows = (page: Page, dict: Dict) => + page + .getByRole('list', { name: dict['clients.list.results_label'] }) + .getByRole('listitem') + +for (const [locale, dict] of LANGUAGES) { + test.describe(`clients (${locale})`, () => { + test.use({ locale: locale === 'fr' ? 'fr-FR' : 'en-GB' }) + test.beforeEach(async ({ context }) => { + await routeToSandbox(context) + }) + + test('empty list, create, find, open, edit, archive, archived filter, restore', async ({ + page, + requests, + }, info) => { + const tag = randomUUID().slice(0, 8) + const name = `Écurie Fictive ${tag}` + const email = `contact-${tag}@exemple.test` + const account = await createAccount(locale, false) + requests.forbid( + account.email, + account.password, + account.displayName, + name, + email, + `Fictive ${tag}`, + '612345678', + ) + await signIn(page, account, dict) + await openClients(page, dict, info) + + await expect(page.getByText(dict['clients.list.empty'])).toBeVisible() + await expectAccessible(page, 'the empty client list') + await expectNoHorizontalOverflow(page, 'the empty client list') + + await page.getByRole('link', { name: dict['clients.list.new'] }).click() + await expect(page).toHaveURL(/\/app\/clients\/new$/) + await expect( + page.getByRole('main').getByRole('heading', { + level: 1, + name: dict['clients.form.create_title'], + }), + ).toBeFocused() + await expect( + page.getByRole('radio', { name: dict['clients.kind.person'] }), + ).toBeChecked() + await expect( + page.getByRole('combobox', { name: dict['clients.form.country'] }), + ).toHaveValue('') + await expectAccessible(page, 'the new client form') + await expectNoHorizontalOverflow(page, 'the new client form') + + await page + .getByRole('radio', { name: dict['clients.kind.organization'] }) + .check() + await page + .getByLabel(dict['clients.form.display_name.organization']) + .fill(name) + await page + .getByLabel(dict['clients.form.email']) + .fill(email.toUpperCase()) + const phone = page.getByLabel(dict['clients.form.phone']) + await phone.fill('06 12 34 56 78') + await phone.blur() + await expect(phone).toHaveValue('+33612345678') + await page.getByLabel(dict['clients.form.city']).fill('Lyon') + await page + .getByRole('combobox', { name: dict['clients.form.country'] }) + .selectOption('FR') + await page + .getByRole('button', { name: dict['clients.form.submit_create'] }) + .click() + + await expect(page).toHaveURL(/\/app\/clients\/[0-9a-f-]{36}$/) + await expect( + page.getByRole('main').getByRole('heading', { level: 1, name }), + ).toBeFocused() + await expect(page.getByText(dict['clients.detail.created'])).toBeVisible() + await expect( + page.getByRole('link', { name: '+33612345678' }), + ).toHaveAttribute('href', 'tel:+33612345678') + await expect(page.getByRole('link', { name: email })).toHaveAttribute( + 'href', + `mailto:${email}`, + ) + await expect(page.getByText(dict['clients.animals.none'])).toBeVisible() + await expectAccessible(page, 'the client page') + await expectNoHorizontalOverflow(page, 'the client page') + + await page + .getByRole('link', { name: dict['clients.detail.edit'] }) + .click() + await expect( + page.getByLabel(dict['clients.form.display_name.organization']), + ).toHaveValue(name) + await page.getByLabel(dict['clients.form.city']).fill('Annecy') + await page + .getByRole('button', { + name: dict['clients.form.submit_update'], + exact: true, + }) + .click() + await expect(page.getByText(dict['clients.detail.updated'])).toBeVisible() + await expect(page.getByText('Annecy')).toBeVisible() + + await page + .getByRole('link', { name: dict['clients.detail.back'] }) + .click() + const search = page.getByRole('searchbox', { + name: dict['clients.list.search_label'], + }) + for (const term of [ + `ecurie fictive ${tag}`, + '06 12 34', + email.toUpperCase(), + ]) { + await search.fill(term) + await expect(listRows(page, dict)).toHaveCount(1) + await expect(listRows(page, dict).first()).toContainText(name) + } + await search.fill(`zzz-${tag}`) + await expect( + page.getByText(dict['clients.list.no_results']), + ).toBeVisible() + await search.fill('') + await expect(listRows(page, dict).first()).toContainText( + dict['clients.list.animals_none'], + ) + await listRows(page, dict).first().getByRole('link').click() + + const archiveButton = page.getByRole('button', { + name: dict['clients.detail.archive'], + }) + await archiveButton.click() + const dialog = page.getByRole('dialog', { + name: dict['clients.archive.title'], + }) + await expect(dialog).toBeVisible() + await expect(dialog).toHaveAccessibleDescription( + dict['clients.archive.body'], + ) + await expect(dialog.getByRole('status')).toHaveCount(0) + await expectAccessible(page, 'the archive confirmation') + await page.keyboard.press('Escape') + await expect(dialog).toBeHidden() + await expect(archiveButton).toBeFocused() + await archiveButton.click() + await dialog + .getByRole('button', { + name: dict['clients.archive.confirm'], + exact: true, + }) + .click() + await expect(dialog).toBeHidden() + await expect( + page.getByText(dict['clients.detail.archived'], { exact: true }), + ).toBeVisible() + await expect( + page.getByText(dict['clients.detail.archived_notice']), + ).toBeVisible() + await expect( + page.getByRole('link', { name: dict['clients.detail.edit'] }), + ).toHaveCount(0) + expect( + await sql( + `select archived_at is not null from public.clients where display_name = '${name}'`, + ), + ).toBe('t') + + await page + .getByRole('link', { name: dict['clients.detail.back'] }) + .click() + await expect(page.getByText(dict['clients.list.empty'])).toBeVisible() + await page + .getByRole('switch', { name: dict['clients.list.archived_toggle'] }) + .check() + await expect(listRows(page, dict)).toHaveCount(1) + await expect(listRows(page, dict).first()).toContainText( + dict['clients.archived_badge'], + ) + await expectAccessible(page, 'the archived filter') + await listRows(page, dict).first().getByRole('link').click() + await page + .getByRole('button', { name: dict['clients.detail.restore'] }) + .click() + await expect( + page.getByText(dict['clients.detail.restored']), + ).toBeVisible() + await expect( + page.getByRole('link', { name: dict['clients.detail.edit'] }), + ).toBeVisible() + }) + + test('pages of 25 with « Afficher plus », in name order', async ({ + page, + requests, + }, info) => { + const account = await createAccount(locale, false) + requests.forbid(account.email, account.password, account.displayName) + for (let i = 1; i <= 30; i++) + await seedClient(account, `Client Paginé ${String(i).padStart(2, '0')}`) + await signIn(page, account, dict) + await openClients(page, dict, info) + await expect(listRows(page, dict)).toHaveCount(25) + await expect(listRows(page, dict).first()).toContainText( + 'Client Paginé 01', + ) + await expect(listRows(page, dict).nth(24)).toContainText( + 'Client Paginé 25', + ) + await page + .getByRole('button', { name: dict['clients.list.more'] }) + .click() + await expect(listRows(page, dict)).toHaveCount(30) + await expect(listRows(page, dict).nth(29)).toContainText( + 'Client Paginé 30', + ) + await expect( + page.getByRole('button', { name: dict['clients.list.more'] }), + ).toHaveCount(0) + await expectNoHorizontalOverflow(page, 'a long client list') + }) + + test('keyboard only: create a client, a refused submit focuses its field', async ({ + page, + requests, + }, info) => { + const name = `Clavier Fictif ${randomUUID().slice(0, 8)}` + const account = await createAccount(locale, false) + requests.forbid( + account.email, + account.password, + account.displayName, + name, + ) + await signIn(page, account, dict) + await openClients(page, dict, info) + await page.keyboard.press('Tab') + await expect( + page.getByRole('link', { name: dict['clients.list.new'] }), + ).toBeFocused() + await page.keyboard.press('Enter') + await expect( + page.getByRole('main').getByRole('heading', { + level: 1, + name: dict['clients.form.create_title'], + }), + ).toBeFocused() + await page.keyboard.press('Tab') + await expect( + page.getByRole('radio', { name: dict['clients.kind.person'] }), + ).toBeFocused() + await page.keyboard.press('Tab') + const field = page.getByLabel(dict['clients.form.display_name.person']) + await expect(field).toBeFocused() + await page.keyboard.press('Enter') + await expect(page.getByText(dict['clients.error.required'])).toBeVisible() + await expect(field).toBeFocused() + await expect(field).toHaveAttribute('aria-invalid', 'true') + await expectAccessible(page, 'the form with an error') + await page.keyboard.type(name) + await page.keyboard.press('Enter') + await expect( + page.getByRole('main').getByRole('heading', { level: 1, name }), + ).toBeFocused() + }) + + test('two accounts: nothing of A reaches B, a foreign id reads as unknown', async ({ + page, + requests, + }, info) => { + const secret = `Client Secret ${randomUUID().slice(0, 8)}` + const a = await createAccount(locale, false) + const b = await createAccount(locale, false) + requests.forbid( + a.email, + a.password, + b.email, + b.password, + b.displayName, + secret, + ) + const id = await seedClient(a, secret, 'Lyon') + await signIn(page, b, dict) + await openClients(page, dict, info) + await expect(page.getByText(dict['clients.list.empty'])).toBeVisible() + await page + .getByRole('searchbox', { name: dict['clients.list.search_label'] }) + .fill(secret) + await expect( + page.getByText(dict['clients.list.no_results']), + ).toBeVisible() + for (const path of [ + `/app/clients/${id}`, + `/app/clients/${id}/edit`, + `/app/clients/${randomUUID()}`, + '/app/clients/not-an-id', + ]) { + await page.goto(path) + await expect( + page.getByRole('main').getByRole('heading', { + level: 1, + name: dict['clients.not_found.title'], + }), + ).toBeVisible() + await expect(page.getByText(secret)).toHaveCount(0) + } + await expectAccessible(page, 'the client not found screen') + }) + + test('a concurrent edit: the saved values beside the typed ones, then a knowing save', async ({ + page, + requests, + }) => { + const name = `Révision Fictive ${randomUUID().slice(0, 8)}` + const account = await createAccount(locale, false) + requests.forbid( + account.email, + account.password, + account.displayName, + name, + ) + const id = await seedClient(account, name, 'Lyon') + await signIn(page, account, dict) + await page.goto(`/app/clients/${id}/edit`) + const city = page.getByLabel(dict['clients.form.city']) + await expect(city).toHaveValue('Lyon') + await city.fill('Annecy') + await sql( + `update public.clients set city = 'Vevey', revision = revision + 1 where id = '${id}'`, + ) + await page + .getByRole('button', { + name: dict['clients.form.submit_update'], + exact: true, + }) + .click() + await expect( + page.getByText(dict['clients.form.conflict_title']), + ).toBeVisible() + await expect(city).toHaveValue('Annecy') + await expect( + page.getByText( + dict['clients.form.stored_value'].replace('{value}', 'Vevey'), + ), + ).toBeVisible() + expect( + await sql( + `select city || '/' || revision from public.clients where id = '${id}'`, + ), + ).toBe('Vevey/2') + await expectAccessible(page, 'the conflict') + await page + .getByRole('button', { + name: dict['clients.form.submit_update'], + exact: true, + }) + .click() + await expect(page.getByText(dict['clients.detail.updated'])).toBeVisible() + expect( + await sql( + `select city || '/' || revision from public.clients where id = '${id}'`, + ), + ).toBe('Annecy/3') + }) + + test('a lost answer: the retry sends the same request id and one client exists', async ({ + page, + requests, + }) => { + const name = `Reprise Fictive ${randomUUID().slice(0, 8)}` + const account = await createAccount(locale, false) + requests.forbid( + account.email, + account.password, + account.displayName, + name, + ) + await signIn(page, account, dict) + const sent: string[] = [] + await page.route('**/rest/v1/rpc/create_client', async (route) => { + sent.push( + JSON.parse(route.request().postData() ?? '{}').create_request_id, + ) + if (sent.length > 1) return route.fallback() + // The server creates the client; its answer never reaches the page. + await route.fetch({ url: sandboxUrl(route.request().url()) ?? '' }) + await route.abort('failed') + }) + await page.goto('/app/clients/new') + await page.getByLabel(dict['clients.form.display_name.person']).fill(name) + const submit = page.getByRole('button', { + name: dict['clients.form.submit_create'], + }) + await submit.click() + await expect( + page.getByText(dict['clients.failure.network']), + ).toBeVisible() + expect( + await sql( + `select count(*) from public.clients where display_name = '${name}'`, + ), + ).toBe('1') + await submit.click() + await expect( + page.getByRole('main').getByRole('heading', { level: 1, name }), + ).toBeVisible() + expect(sent).toHaveLength(2) + expect(sent[0]).toBe(sent[1]) + expect( + await sql( + `select count(*) from public.clients where display_name = '${name}'`, + ), + ).toBe('1') + }) + + test('a failed list says so and retries; a failed save keeps the form', async ({ + page, + requests, + }, info) => { + const account = await createAccount(locale, false) + requests.forbid(account.email, account.password, account.displayName) + await seedClient(account, 'Client Réseau') + await signIn(page, account, dict) + let failing = true + await page.route('**/rest/v1/rpc/search_clients', (route) => + failing ? route.abort('failed') : route.fallback(), + ) + await openClients(page, dict, info) + await expect( + page.getByText(dict['clients.failure.network']), + ).toBeVisible() + failing = false + await page.getByRole('button', { name: dict['common.retry'] }).click() + await expect(listRows(page, dict)).toHaveCount(1) + + await page.route('**/rest/v1/rpc/create_client', (route) => + route.fulfill({ + status: 400, + contentType: 'application/json', + body: '{"code":"P0001","message":"rate_limited"}', + }), + ) + await page.goto('/app/clients/new') + await page + .getByLabel(dict['clients.form.display_name.person']) + .fill('Client Limité') + await page + .getByRole('button', { name: dict['clients.form.submit_create'] }) + .click() + await expect( + page.getByText(dict['clients.failure.rate_limited']), + ).toBeVisible() + await expect( + page.getByLabel(dict['clients.form.display_name.person']), + ).toHaveValue('Client Limité') + }) + + test('unsaved changes: a link asks first, staying keeps them', async ({ + page, + requests, + }, info) => { + const account = await createAccount(locale, false) + requests.forbid(account.email, account.password, account.displayName) + await signIn(page, account, dict) + await page.goto('/app/clients/new') + const field = page.getByLabel(dict['clients.form.display_name.person']) + await field.fill('Brouillon Fictif') + const cancel = page.getByRole('link', { name: dict['common.cancel'] }) + await cancel.click() + const dialog = page.getByRole('dialog', { name: dict['leave.title'] }) + await expect(dialog).toBeVisible() + await expectAccessible(page, 'the leave confirmation') + await dialog.getByRole('button', { name: dict['leave.stay'] }).click() + await expect(dialog).toBeHidden() + await expect(page).toHaveURL(/\/app\/clients\/new$/) + await expect(field).toHaveValue('Brouillon Fictif') + await expect(cancel).toBeFocused() + + if (isPhone(info)) { + await page + .getByRole('button', { name: dict['shell.open_menu'] }) + .click() + await page + .getByRole('dialog', { name: dict['shell.nav_label'] }) + .getByRole('navigation') + .getByRole('link', { name: dict['shell.nav.dashboard'] }) + .click() + } else { + await page + .getByRole('navigation', { name: dict['shell.nav_label'] }) + .getByRole('link', { name: dict['shell.nav.dashboard'] }) + .click() + } + await expect(dialog).toBeVisible() + await dialog.getByRole('button', { name: dict['leave.confirm'] }).click() + await expect(page).toHaveURL(/\/app\/?$/) + await expect(page.getByRole('dialog')).toHaveCount(0) + }) + + test('the list follows a change of language', async ({ + page, + requests, + }, info) => { + const account = await createAccount(locale, false) + requests.forbid(account.email, account.password, account.displayName) + await seedClient(account, 'Client Bilingue') + await signIn(page, account, dict) + await openClients(page, dict, info) + const other = locale === 'fr' ? en : fr + await page + .getByRole('button', { + name: dict[ + locale === 'fr' ? 'lang.switch_to_en' : 'lang.switch_to_fr' + ], + }) + .click() + await expect( + page.getByRole('link', { name: other['clients.list.new'] }), + ).toBeVisible() + await expect( + page.getByRole('searchbox', { + name: other['clients.list.search_label'], + }), + ).toBeVisible() + await expect(listRows(page, other).first()).toContainText( + other['clients.kind.person'], + ) + await expectAccessible(page, 'the list after a change of language') + }) + }) +} diff --git a/apps/app/e2e/shell/shell.spec.ts b/apps/app/e2e/shell/shell.spec.ts index 7bb018f2..5cf63dbf 100644 --- a/apps/app/e2e/shell/shell.spec.ts +++ b/apps/app/e2e/shell/shell.spec.ts @@ -1,6 +1,5 @@ import { en } from '@/i18n/en' import { fr } from '@/i18n/fr' -import type { Dict } from '@/i18n/types' import type { Page } from '@playwright/test' import { expect, @@ -10,11 +9,8 @@ import { focusedName, test, } from '../support/browser' -import { - createAccount, - routeToSandbox, - type SandboxAccount, -} from '../support/sandbox' +import { createAccount, routeToSandbox } from '../support/sandbox' +import { signIn } from '../support/shell' // The authenticated shell on the isolated stack: sign-in, dashboard, the // account photo confirmation and the mobile drawer, through the shared modal. @@ -24,18 +20,6 @@ const LANGUAGES = [ ['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[] = [] diff --git a/apps/app/e2e/support/sandbox.ts b/apps/app/e2e/support/sandbox.ts index dec7ebdb..c333264e 100644 --- a/apps/app/e2e/support/sandbox.ts +++ b/apps/app/e2e/support/sandbox.ts @@ -21,17 +21,20 @@ const ROUTES: [prefix: string, target: string][] = [ ['/storage/v1/', `${storageUrl}/`], ] +/** The sandbox URL a request to the fake Supabase origin maps to, or null when unmapped. */ +export function sandboxUrl(url: string): string | null { + const { pathname, search } = new URL(url) + const path = pathname + search + const mapped = ROUTES.find(([prefix]) => path.startsWith(prefix)) + return mapped ? mapped[1] + path.slice(mapped[0].length) : null +} + /** 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), - }) + const target = sandboxUrl(route.request().url()) + if (!target) return route.abort('blockedbyclient') + const response = await route.fetch({ url: target }) await route.fulfill({ response }) }) } @@ -43,6 +46,7 @@ const AVATAR_WEBP = Buffer.from( ) export interface SandboxAccount { + id: string email: string password: string displayName: string @@ -105,5 +109,12 @@ export async function createAccount( }) const { id } = (await response.json()) as { id: string } if (withAvatar) await seedAvatar(id) - return account + return { ...account, id } +} + +/** The personal workspace of a sandbox account (created by the server at sign-up). */ +export function workspaceOf(account: SandboxAccount): Promise { + return sql( + `select id from public.workspaces where personal_user_id = '${account.id}'`, + ) } diff --git a/apps/app/e2e/support/shell.ts b/apps/app/e2e/support/shell.ts new file mode 100644 index 00000000..95c9d9c7 --- /dev/null +++ b/apps/app/e2e/support/shell.ts @@ -0,0 +1,17 @@ +import type { Dict } from '@/i18n/types' +import type { Page } from '@playwright/test' +import { expect } from './browser' +import type { SandboxAccount } from './sandbox' + +/** Signs in through the login form; resolves on the dashboard once the profile answered. */ +export 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) +}