const assert = require('node:assert/strict'); module.exports = async ({ page, go, fail, shot }) => { const opener = page.getByRole('button', { name: 'New policy', exact: true }); const editor = page.getByRole('dialog', { name: /New policy|draft-policy/ }); const confirm = page.getByRole('alertdialog', { name: 'Discard unsaved changes?' }); const id = editor.getByRole('textbox', { name: 'Policy id' }); const cedar = editor.getByRole('textbox', { name: 'Cedar policy' }); const dismissals = { Close: () => editor.getByRole('button', { name: 'Close', exact: true }).click({ timeout: 2000 }), Cancel: () => editor.getByRole('button', { name: 'Cancel', exact: true }).click({ timeout: 2000 }), Escape: () => page.keyboard.press('Escape'), Backdrop: () => page.mouse.click(8, 8), }; const focused = (field) => field.evaluate(el => el === document.activeElement); const closed = async () => { await page.locator('[role="alertdialog"]').waitFor({ state: 'detached', timeout: 2000 }); await page.locator('[role="dialog"]').waitFor({ state: 'detached', timeout: 2000 }); assert.ok(await focused(opener), 'focus did not return to New policy'); }; const cases = async (scenario, rows, run) => { for (const row of rows) { // A failed modal must not block the next independently tested path. await go('/org/policies'); try { await run(row); } catch (error) { fail(`${scenario} ${row}: ${error.message}`); } } }; await cases('Mobile', ['focus clearance'], async () => { await page.setViewportSize({ width: 390, height: 600 }); await opener.click(); for (const field of [id, cedar]) { // Opening focuses the ID; Tab past the level selector to reach Cedar. if (field === cedar) { await page.keyboard.press('Tab'); await page.keyboard.press('Tab'); } assert.ok(await focused(field), 'keyboard focus did not reach the policy field'); const { name, clearance, ring } = await field.evaluate(el => { const form = el.closest('form'); const bounds = form.getBoundingClientRect(); const field = el.getBoundingClientRect(); const css = getComputedStyle(el); return { name: el.getAttribute('aria-label'), clearance: Math.min(field.left - bounds.left, field.top - bounds.top, bounds.left + form.clientWidth - field.right, bounds.top + form.clientHeight - field.bottom), ring: Math.max(3, parseFloat(css.outlineWidth) + parseFloat(css.outlineOffset)), }; }); if (clearance + 0.1 < ring) fail(`${name} focus ring: ${clearance}px clearance for ${ring}px ring`); await shot(name); } }); await page.setViewportSize({ width: 1440, height: 900 }); await cases('Clean', Object.keys(dismissals), async (action) => { await opener.click(); await dismissals[action](); await closed(); }); await cases('Dirty', Object.keys(dismissals), async (action) => { await opener.click(); await id.pressSequentially('draft-policy'); await dismissals[action](); const keep = confirm.getByRole('button', { name: 'Keep editing' }); await keep.waitFor({ timeout: 2000 }); if (action === 'Escape') await page.keyboard.press('Escape'); else await keep.click({ timeout: 2000 }); await page.locator('[role="alertdialog"]').waitFor({ state: 'detached', timeout: 2000 }); assert.equal(await id.inputValue(), 'draft-policy'); assert.ok(await editor.evaluate(el => el.contains(document.activeElement)), 'focus did not return to the editor'); await dismissals[action](); await confirm.getByRole('button', { name: 'Discard changes' }).click({ timeout: 2000 }); await closed(); await opener.click(); assert.equal(await id.inputValue(), '', 'discarded policy reappeared on reopen'); }); };