import AxeBuilder from '@axe-core/playwright' import { expect, test } from '@playwright/test' async function config(page, available = true) { await page.route('**/api/contact/config', route => route.fulfill({ json: { email: 'business@example.com', available } })) } async function draft(page) { await page.getByLabel('Name', { exact: true }).fill('Example Person') await page.getByLabel('Email', { exact: true }).fill('person@example.com') await page.getByLabel('Project description', { exact: true }).fill('A useful software project enquiry.') } for (const width of [320, 390, 768, 1024, 1536]) { test(`Contact reference and form at ${width}`, async ({ page }) => { await config(page) await page.setViewportSize({ width, height: 1024 }) const errors = [] page.on('pageerror', e => errors.push(e.message)) await page.goto('/contact') await page.evaluate(() => globalThis.document.fonts.ready) await expect(page.getByRole('heading', { level: 1, name: 'Have a project in mind?' })).toBeVisible() await expect(page.getByRole('textbox')).toHaveCount(3) await expect(page.getByRole('combobox')).toHaveCount(1) await expect(page.getByLabel('Company', { exact: true })).toHaveCount(0) expect(await page.locator('html').evaluate(el => el.scrollWidth <= el.clientWidth)).toBe(true) if (width === 1536) { const box = await page.getByRole('form').boundingBox() for (const [actual, ref] of [[box.x, 60], [box.y, 397], [box.width, 914], [box.height, 521]]) expect(Math.abs(actual - ref)).toBeLessThan(6) } await page.getByRole('button', { name: 'Send Message' }).click() await expect(page.getByLabel('Name', { exact: true })).toBeFocused() await expect(page.getByText('Enter your name.', { exact: true })).toBeVisible() expect(errors).toEqual([]) }) } for (const theme of ['dark', 'light']) { test(`Contact keyboard, errors and contrast in ${theme}`, async ({ page, isMobile }) => { await config(page) await page.addInitScript(theme => { if (!localStorage.getItem('bortstack-theme')) localStorage.setItem('bortstack-theme', theme) }, theme) await page.goto('/contact') await page.keyboard.press('Tab') await expect(page.getByRole('link', { name: 'Skip to main content' })).toBeFocused() await page.keyboard.press('Enter') await expect(page.getByRole('main')).toBeFocused() expect((await new AxeBuilder({ page }).analyze()).violations).toEqual([]) await page.getByRole('button', { name: 'Send Message' }).click() expect((await new AxeBuilder({ page }).analyze()).violations).toEqual([]) if (isMobile) await page.getByRole('button', { name: 'Toggle navigation' }).click() await expect(page.getByRole('navigation', { name: 'Main navigation' }).getByRole('link', { name: 'Contact', exact: true })).toHaveAttribute('aria-current', 'page') await page.getByRole('button', { name: `Switch to ${theme === 'dark' ? 'light' : 'dark'} mode` }).click() await page.reload() await expect(page.locator('html')).toHaveAttribute('data-bs-theme', theme === 'dark' ? 'light' : 'dark') }) } test('Contact retains failed draft and key, then succeeds without duplicate submission', async ({ page }) => { await config(page) const keys = [], bodies = [] let finish await page.route('**/api/contact', async route => { keys.push(route.request().headers()['idempotency-key']) bodies.push(route.request().postDataJSON()) if (keys.length === 1) await route.fulfill({ status: 503, json: { message: 'Sending could not be confirmed. Please retry.' } }) else { await new Promise(resolve => { finish = resolve }); await route.fulfill({ json: { success: true } }) } }) await page.goto('/contact') await draft(page) await page.getByRole('button', { name: 'Send Message' }).click() await expect(page.getByRole('alert')).toContainText('could not be confirmed') await expect(page.getByRole('alert')).toBeFocused() await expect(page.getByLabel('Name', { exact: true })).toHaveValue('Example Person') await page.getByRole('button', { name: 'Send Message' }).click() await expect(page.getByRole('button', { name: 'Sending…' })).toBeDisabled() await expect(page.getByLabel('Name', { exact: true })).toBeDisabled() await expect.poll(() => keys.length).toBe(2) finish() await expect(page.getByRole('status')).toContainText('accepted for delivery') expect(keys[0]).toBe(keys[1]) expect(bodies[0]).toEqual(bodies[1]) expect(Object.keys(bodies[0]).sort()).toEqual(['description', 'email', 'name', 'service', 'website']) await expect(page.getByLabel('Name', { exact: true })).toHaveValue('') }) test('Contact shows server errors and unavailable delivery honestly', async ({ page }) => { await config(page, false) await page.route('**/api/contact', r => r.fulfill({ status: 400, json: { message: 'Please check the highlighted fields.', errors: { email: 'Enter a valid email address.' } } })) await page.goto('/contact') await expect(page.getByText(/Enquiries are temporarily unavailable/)).toBeVisible() await draft(page) await page.getByRole('button', { name: 'Send Message' }).click() await expect(page.getByRole('alert')).toContainText('check the highlighted fields') await expect(page.getByLabel('Email', { exact: true })).toHaveAttribute('aria-invalid', 'true') await page.unroute('**/api/contact') await page.route('**/api/contact', r => r.abort()) await page.getByRole('button', { name: 'Send Message' }).click() await expect(page.getByRole('alert')).toContainText('draft is kept') await expect(page.getByText('Your enquiry has been accepted for delivery. Thank you.')).toHaveCount(0) await page.unroute('**/api/contact') await page.route('**/api/contact', r => r.fulfill({ status: 502, contentType: 'text/html', body: '