fix(ui): preserve modal focus rings and discard policy drafts

(cherry picked from commit a2eff9938d1d2c22c16e73f790a7c32f9c150a23)
This commit is contained in:
sam committed 2026-11-27 15:51:34 +11:00
1 parent 74deeb74d5
commit 5dc5dbbafc
4 files changed
+133 -3

No files matched your search

+49
View File
@@ -0,0 +1,49 @@
const assert = require('node:assert/strict');
module.exports = async ({ page, go, shot }) => {
// An isolated catalogue entry keeps the check independent of the live source.
await page.route('**/api/extensions/catalogue', route => route.fulfill({ json: [{
name: 'preview-extension', display_name: 'Preview extension', description: 'An extension to preview.',
contract: 'workflow', group: 'platform', latest: '1.0.0', default_version: '1.0.0',
versions: [{ version: '1.0.0', digest: 'preview-digest', registered: true, settings: [] }],
}] }));
await page.setViewportSize({ width: 390, height: 600 });
await go('/settings/extensions?tab=marketplace');
const opener = page.getByRole('button', { name: 'Details for Preview extension' });
const preview = page.getByRole('dialog', { name: 'Preview extension' });
const version = preview.getByRole('combobox', { name: 'Version' });
const close = preview.getByRole('button', { name: 'Close' });
await opener.click();
await page.keyboard.press('Tab');
await page.keyboard.press('Shift+Tab');
assert.ok(await version.evaluate(el => el === document.activeElement));
await shot('version-focus');
const { clearance, ring } = await version.evaluate(el => {
let scroll = el.parentElement;
while (getComputedStyle(scroll).overflowY !== 'auto') scroll = scroll.parentElement;
const bounds = scroll.getBoundingClientRect();
const field = el.getBoundingClientRect();
const css = getComputedStyle(el);
return {
clearance: Math.min(field.left - bounds.left, bounds.left + scroll.clientWidth - field.right),
ring: Math.max(3, parseFloat(css.outlineWidth) + parseFloat(css.outlineOffset)),
};
});
assert.ok(clearance + 0.1 >= ring, `Version focus ring: ${clearance}px clearance for ${ring}px ring`);
await page.keyboard.press('Space');
const options = page.getByRole('listbox');
await options.waitFor();
await page.keyboard.press('Escape');
await options.waitFor({ state: 'hidden' });
await preview.waitFor();
await page.keyboard.press('Escape');
await preview.waitFor({ state: 'detached' });
assert.ok(await opener.evaluate(el => el === document.activeElement));
await opener.click();
await close.click();
await preview.waitFor({ state: 'detached' });
assert.ok(await opener.evaluate(el => el === document.activeElement));
};
+80
View File
@@ -0,0 +1,80 @@
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');
});
};
+3 -2
View File
@@ -209,7 +209,7 @@ onMounted(load);
<DialogTitle>{{ draft.id || "New policy" }}</DialogTitle>
<DialogDescription>Set the policy scope and Cedar expression.</DialogDescription>
</DialogHeader>
<form id="policy-editor" class="min-h-0 flex-1 space-y-3 overflow-y-auto pr-1" @submit.prevent="save">
<form id="policy-editor" class="min-h-0 flex-1 space-y-3 overflow-y-auto p-1" @submit.prevent="save">
<div class="flex flex-wrap gap-2">
<Input v-model="draft.id" :readonly="editingExisting" placeholder="policy id" aria-label="Policy id" class="w-56" />
<Select v-model="draft.level">
@@ -256,7 +256,8 @@ onMounted(load);
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Keep editing</AlertDialogCancel>
<AlertDialogAction @click="confirmDiscard">Discard changes</AlertDialogAction>
<!-- The controlled discard action must consume the callback before closing the alert. -->
<Button type="button" @click="confirmDiscard">Discard changes</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
+1 -1
View File
@@ -63,7 +63,7 @@ const authority = computed(() => grants(selected.value?.grants));
<DialogDescription>{{ entry?.description || "Manifest metadata and authority." }}</DialogDescription>
</DialogHeader>
<div class="min-h-0 flex-1 space-y-5 overflow-y-auto pr-1">
<div class="min-h-0 flex-1 space-y-5 overflow-y-auto p-1">
<MetadataList :rows="entryDetails" />
<SettingRow v-if="entry?.versions.length" label="Version" for="catalogue-preview-version">