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, dropLateReplays, 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.afterEach(async ({ context }) => { await dropLateReplays(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() // The animal section is read again for the restored client. await expect(page.getByText(dict['clients.animals.none'])).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'], }), ).toBeFocused() 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') }) }) }