Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(connector-modal): restore focus trap test for modal

Steven Enamakel2d45135e3d45
diff --git a/app/test/playwright/specs/connector-modal-focus-trap.spec.ts b/app/test/playwright/specs/connector-modal-focus-trap.spec.ts
index a97e720612..727947374d 100644
--- a/app/test/playwright/specs/connector-modal-focus-trap.spec.ts
+++ b/app/test/playwright/specs/connector-modal-focus-trap.spec.ts
@@ -1,6 +1,6 @@
import { expect, test } from '@playwright/test';
-import { bootRuntimeReadyGuestPage } from '../helpers/core-rpc';
+import { bootRuntimeReadyGuestPage, callCoreRpc, seedBrowserCoreMode } from '../helpers/core-rpc';
/**
* Modal keyboard and focus behaviour, driven in a real browser.
@@ -15,48 +15,80 @@ import { bootRuntimeReadyGuestPage } from '../helpers/core-rpc';
* nothing — a jsdom test asserting a focus trap passes whether or not the trap
* exists. This spec is the only place that behaviour is actually exercised.
*
- * The vehicle is `SecretPromptDialog` (`components/mcp-setup/SecretPromptDialog.tsx`),
- * opened by dispatching the `openhuman:mcp-setup-secret-requested` event the
- * socket bridge normally publishes. It is the one connector dialog that opens
- * deterministically with no live credentials, and it is a plain `ModalShell`
- * consumer, so what holds here holds for the others.
- * `mcp-setup-secret-flow.spec.ts` already covers submit / cancel / show-hide;
- * this covers only the keyboard and focus surface it does not touch.
+ * The vehicle is the MCP `ConnectAuthModal`
+ * (`components/channels/mcp/ConnectAuthModal.tsx`): the credential form a
+ * declared server's **Connect** button opens. It is the one connector dialog
+ * that opens deterministically with no live credentials — the server is
+ * declared in `mcp.json` through the core's own RPC with a launcher that never
+ * runs — and it is a plain `ModalShell` consumer, so what holds here holds for
+ * the others. `mcp-tab-flow.spec.ts` covers what the modal does; this covers
+ * only the keyboard and focus surface it does not touch.
*/
const DIALOG = '[role="dialog"]';
+const SERVER_NAME = 'pw-focus-trap';
-async function openSecretDialog(
+/**
+ * Declare the vehicle server through the real core. `enabled` is what makes
+ * the detail offer a Connect button; the launcher is a command that exits at
+ * once, so the background connect the declaration starts fails harmlessly
+ * instead of leaving a subprocess behind.
+ */
+async function declareVehicleServer(keyName: string): Promise<void> {
+ await callCoreRpc('openhuman.mcp_clients_config_set', {
+ mcpServers: {
+ [SERVER_NAME]: {
+ command: '/bin/false',
+ env: { [keyName]: 'seed' },
+ },
+ },
+ });
+}
+
+async function openConnectDialog(
page: import('@playwright/test').Page,
opts: { keyName?: string } = {}
) {
+ const keyName = opts.keyName ?? 'NOTION_API_KEY';
await bootRuntimeReadyGuestPage(page);
+ await declareVehicleServer(keyName);
+ await seedBrowserCoreMode(page);
+ await page.goto('/#/connections?tab=tools');
+ await page.waitForSelector('#root');
+
+ // The Servers tab lists the declaration; its detail carries the Connect
+ // button that opens the modal.
+ const row = page.locator('table tbody tr[data-testid="mcp-installed-row"]', {
+ has: page.locator(`td:first-child:has-text("${SERVER_NAME}")`),
+ });
+ await expect(row).toBeVisible({ timeout: 20_000 });
+ await row.click();
+ const connect = page.getByRole('button', { name: 'Connect', exact: true });
+ await expect(connect).toBeVisible({ timeout: 10_000 });
// A focusable element outside the dialog, so focus restore has somewhere
- // observable to return to.
+ // observable to return to. The Connect button itself is the natural
+ // trigger, but naming our own anchor keeps the assertion independent of
+ // what Radix would restore to on its own.
await page.evaluate(() => {
const anchor = document.createElement('button');
anchor.id = 'pw-focus-anchor';
anchor.textContent = 'anchor';
document.body.prepend(anchor);
- anchor.focus();
});
+ await page.locator('#pw-focus-anchor').focus();
await expect.poll(() => page.evaluate(() => document.activeElement?.id)).toBe('pw-focus-anchor');
- await page.evaluate(
- ({ keyName }) => {
- window.dispatchEvent(
- new CustomEvent('openhuman:mcp-setup-secret-requested', {
- detail: {
- refId: 'secret://pwfocus0001',
- keyName,
- prompt: 'Enter your integration token to connect.',
- },
- })
- );
- },
- { keyName: opts.keyName ?? 'NOTION_API_KEY' }
- );
+ // Open with the keyboard so the anchor, not the Connect button, is what was
+ // focused before the dialog took over: `Enter` on the anchor does nothing,
+ // so drive the click through the DOM instead of a pointer, which would move
+ // focus first.
+ await page.evaluate(() => {
+ const button = Array.from(document.querySelectorAll('button')).find(
+ candidate => candidate.textContent?.trim() === 'Connect'
+ );
+ button?.click();
+ });
const dialog = page.locator(DIALOG);
await expect(dialog).toBeVisible({ timeout: 10_000 });
@@ -71,6 +103,11 @@ const focusIsInsideDialog = (page: import('@playwright/test').Page) =>
return Boolean(dialog && active && dialog.contains(active));
});
+test.afterEach(async () => {
+ // Leave the core as it was found: the vehicle is a throwaway declaration.
+ await callCoreRpc('openhuman.mcp_clients_config_set', { mcpServers: {} }).catch(() => {});
+});
+
// A fourth test ("the element behind the dialog cannot be reached by keyboard",
// asserting `document.activeElement.id !== 'pw-focus-anchor'` after 12 Tabs) was
// written and then REMOVED: with the focus trap deliberately deleted it still
@@ -85,12 +122,12 @@ test.describe('Connector modal — focus containment', () => {
// behind it — but do not count it as a guard against the trap regressing;
// the Tab test below is that guard.
test('moves focus into the dialog when it opens', async ({ page }) => {
- await openSecretDialog(page);
+ await openConnectDialog(page);
expect(await focusIsInsideDialog(page)).toBe(true);
});
test('Tab cycles within the dialog and never escapes to the page behind', async ({ page }) => {
- await openSecretDialog(page);
+ await openConnectDialog(page);
// Walk further than the dialog has focusables, so an unbounded sequence
// would certainly have left it.
@@ -104,7 +141,7 @@ test.describe('Connector modal — focus containment', () => {
});
test('Shift+Tab is contained too', async ({ page }) => {
- await openSecretDialog(page);
+ await openConnectDialog(page);
for (let i = 0; i < 12; i++) {
await page.keyboard.press('Shift+Tab');
@@ -118,16 +155,16 @@ test.describe('Connector modal — focus containment', () => {
test.describe('Connector modal — Escape', () => {
test('Escape closes the dialog', async ({ page }) => {
- const dialog = await openSecretDialog(page);
+ const dialog = await openConnectDialog(page);
await page.keyboard.press('Escape');
await expect(dialog).not.toBeVisible({ timeout: 10_000 });
});
test('Escape restores focus to whatever was focused before it opened', async ({ page }) => {
// `ModalShell` does this itself rather than leaving it to Radix
- // (:57-60: Radix restores to the trigger, and a dialog opened by an event
- // has no trigger, so focus would drop to <body>).
- const dialog = await openSecretDialog(page);
+ // (:57-60: Radix restores to the trigger, and a dialog opened without a
+ // pointer on its trigger would otherwise drop focus to <body>).
+ const dialog = await openConnectDialog(page);
await page.keyboard.press('Escape');
await expect(dialog).not.toBeVisible({ timeout: 10_000 });
@@ -140,25 +177,22 @@ test.describe('Connector modal — Escape', () => {
// this is not mutation-proven. It is here because the failure it guards
// against — a dismissed dialog quietly sending a typed token — is severe
// enough to be worth a standing assertion.
- test('Escape does not submit the secret', async ({ page }) => {
+ test('Escape does not submit the credential', async ({ page }) => {
// Dismissing must be a cancel, not an accidental send of a typed token.
const submitted: string[] = [];
await page.route('**/rpc', async (route, request) => {
const body = JSON.parse(request.postData() || '{}');
- if (body.method === 'openhuman.mcp_setup_submit_secret') {
- submitted.push(String(body.params?.ref_id ?? ''));
- await route.fulfill({
- status: 200,
- contentType: 'application/json',
- body: JSON.stringify({ jsonrpc: '2.0', id: body.id, result: { fulfilled: true } }),
- });
- return;
+ if (
+ body.method === 'openhuman.mcp_clients_update_env' ||
+ body.method === 'openhuman.mcp_clients_connect'
+ ) {
+ submitted.push(String(body.method));
}
await route.continue();
});
- const dialog = await openSecretDialog(page);
- await dialog.locator('input[type="password"]').fill('ntn_should_never_be_sent');
+ const dialog = await openConnectDialog(page);
+ await dialog.locator('input[type="password"]').first().fill('ntn_should_never_be_sent');
await page.keyboard.press('Escape');
await expect(dialog).not.toBeVisible({ timeout: 10_000 });
@@ -168,7 +202,7 @@ test.describe('Connector modal — Escape', () => {
test.describe('Connector modal — accessible shape', () => {
test('is a labelled modal dialog, not a bare overlay', async ({ page }) => {
- const dialog = await openSecretDialog(page);
+ const dialog = await openConnectDialog(page);
// A screen reader needs both of these to announce it as a dialog and read
// its name; `ModalShell` wires `aria-labelledby` from its title (:100).
await expect(dialog).toHaveAttribute('aria-labelledby', /.+/);
@@ -180,15 +214,17 @@ test.describe('Connector modal — accessible shape', () => {
expect(labelText.trim().length).toBeGreaterThan(0);
});
- test('names the key being requested so the user knows what they are pasting', async ({
+ test('names the key being asked for so the user knows what they are pasting', async ({
page,
}) => {
- const dialog = await openSecretDialog(page, { keyName: 'LINEAR_API_KEY' });
- await expect(dialog.locator('code')).toContainText('LINEAR_API_KEY');
+ const dialog = await openConnectDialog(page, { keyName: 'LINEAR_API_KEY' });
+ await expect(dialog.locator('label[for="auth-LINEAR_API_KEY"]')).toContainText(
+ 'LINEAR_API_KEY'
+ );
});
- test('keeps the secret field masked', async ({ page }) => {
- const dialog = await openSecretDialog(page);
- await expect(dialog.locator('input[type="password"]')).toBeVisible();
+ test('keeps the credential field masked', async ({ page }) => {
+ const dialog = await openConnectDialog(page);
+ await expect(dialog.locator('input[type="password"]').first()).toBeVisible();
});
});