From 58b7253c76277c4ab6f6663c23dda6aa38a09b5b Mon Sep 17 00:00:00 2001 From: "helpcode.ai" Date: Sun, 6 Dec 2026 09:34:20 +0100 Subject: [PATCH] Keep tool editor edits when the connector page re-renders --- .../src/components/tool-editor/index.tsx | 13 ++- packages/frontend/src/lib/sentry-scrub.ts | 4 + .../tests/e2e/tool-editor-keeps-edits.spec.ts | 86 +++++++++++++++++++ 3 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 packages/frontend/tests/e2e/tool-editor-keeps-edits.spec.ts diff --git a/packages/frontend/src/components/tool-editor/index.tsx b/packages/frontend/src/components/tool-editor/index.tsx index d59f753c5..40efb230d 100644 --- a/packages/frontend/src/components/tool-editor/index.tsx +++ b/packages/frontend/src/components/tool-editor/index.tsx @@ -193,10 +193,15 @@ export function ToolEditor({ // What the loaded tool has beyond what the form renders; written back on save. const [preserved, setPreserved] = useState(undefined); - // Initialize from existing tool + // Initialize from existing tool. The connector page passes `existingTool` + // as a new object on every render, so the reset is keyed on its content: + // keyed on the object, any re-render of the page (a status message, the + // "Add Tool" panel opening) wiped what the user had typed. + const existingToolJson = existingTool ? JSON.stringify(existingTool) : ''; useEffect(() => { - if (existingTool) { - const form = formStateFromParsed(parseExistingTool(existingTool, type)); + if (existingToolJson) { + const loaded = JSON.parse(existingToolJson) as ExistingTool; + const form = formStateFromParsed(parseExistingTool(loaded, type)); setName(form.name); setDescription(form.description); setMethod(form.method); @@ -219,7 +224,7 @@ export function ToolEditor({ setResponseMappingExtra(form.responseMappingExtra); setPreserved(form.preserved); } - }, [existingTool, type]); + }, [existingToolJson, type]); // Auto-detect path params from {param} patterns const detectPathParams = useCallback((pathStr: string) => { diff --git a/packages/frontend/src/lib/sentry-scrub.ts b/packages/frontend/src/lib/sentry-scrub.ts index c66c5fd54..908adf449 100644 --- a/packages/frontend/src/lib/sentry-scrub.ts +++ b/packages/frontend/src/lib/sentry-scrub.ts @@ -175,6 +175,10 @@ export const BROWSER_IGNORE_ERRORS: Array = [ // A WebExtension (runtime.sendMessage is extension-only API; the site // never calls it) messaging a tab that has closed (ANYTHINGMCP-WEBSITE-J on the site). /Invalid call to runtime\.sendMessage\(\)\. Tab not found/, + // Chrome's answer when an extension's content script messages an extension + // page that is not listening; the app never calls chrome.runtime + // (ANYTHINGMCP-CLOUD-FRONTEND-13, on /login). + /^Could not establish connection\. Receiving end does not exist\.$/, // Google Tag Manager's own beacons blocked by an ad blocker or dropped by // the network; the SDK names the host in the message (ANYTHINGMCP-CLOUD-FRONTEND-D). // GA4 sends its beacons to analytics.google.com (ANYTHINGMCP-CLOUD-FRONTEND-N, -P), diff --git a/packages/frontend/tests/e2e/tool-editor-keeps-edits.spec.ts b/packages/frontend/tests/e2e/tool-editor-keeps-edits.spec.ts new file mode 100644 index 000000000..57ee07ef7 --- /dev/null +++ b/packages/frontend/tests/e2e/tool-editor-keeps-edits.spec.ts @@ -0,0 +1,86 @@ +import { expect, test } from '@playwright/test'; + +/** + * The connector page hands the tool editor a new `existingTool` object on + * every render. The editor reset its form whenever that object changed, so + * any re-render of the page while a tool was open (here: opening the + * "Add Tool" panel) threw away what the user had typed. + */ + +const USER = { + id: 'u1', + email: 'test@example.com', + name: 'Test User', + role: 'ADMIN', + organizationId: 'o1', + emailVerified: true, +}; + +const CONNECTOR = { + id: 'c1', + name: 'Tool Edit Connector', + type: 'REST', + baseUrl: 'https://api.example.com', + authType: 'NONE', + authConfig: null, + isActive: true, + headers: {}, + envVars: { API_REGION: 'eu' }, + maskedEnvVars: [], + config: {}, + tools: [ + { + id: 't1', + name: 'get_users', + description: 'List users.', + isEnabled: true, + parameters: { type: 'object', properties: {} }, + endpointMapping: { method: 'GET', path: '/users' }, + responseMapping: null, + }, + ], + createdAt: '2026-09-01T00:00:00Z', + userId: 'u1', + organizationId: 'o1', +}; + +test('editing a tool keeps typed text when the page re-renders', async ({ page, baseURL }) => { + await page.context().addCookies([{ name: 'amcp_token', value: 'test-token', url: baseURL! }]); + await page.addInitScript((user) => { + localStorage.setItem('amcp_token', 'test-token'); + localStorage.setItem('amcp_user', JSON.stringify(user)); + }, USER); + + await page.route(/\/api\//, async (route) => { + const url = route.request().url(); + const json = (body: unknown) => + route.fulfill({ status: 200, contentType: 'application/json', body: JSON.stringify(body) }); + if (url.includes('/api/connectors/proxy-availability')) return json({ available: false }); + if (url.includes('/api/connectors/c1/catalog-diff')) return json({ catalogManaged: false }); + if (url.includes('/api/connectors/c1')) return json(CONNECTOR); + if (url.includes('/api/users/me/onboarding-state')) return json({ onboardingCompletedAt: '2026-01-01T00:00:00Z' }); + if (url.includes('/api/users/me')) return json(USER); + if (url.includes('/api/organizations/current')) return json({ id: 'o1', name: 'Acme', createdAt: '2026-01-01' }); + if (url.includes('/api/organizations/mine')) return json([{ id: 'o1', name: 'Acme', role: 'ADMIN', joinedAt: '2026-01-01' }]); + if (url.includes('/api/license/status')) return json({ plan: 'community', status: 'active' }); + if (url.includes('/api/connectors')) return json([CONNECTOR]); + return json({}); + }); + + await page.goto('/connectors/c1'); + + const card = page.locator('div', { has: page.getByText('get_users', { exact: true }) }).filter({ + has: page.getByRole('button', { name: 'Edit', exact: true }), + }); + await card.last().getByRole('button', { name: 'Edit', exact: true }).click(); + + const nameField = page.getByPlaceholder('get_users'); + await expect(nameField).toHaveValue('get_users'); + await nameField.fill('list_active_users'); + + // Re-renders the page while the editor stays open. + await page.getByRole('button', { name: 'Add Tool' }).click(); + + await expect(page.getByPlaceholder('get_users')).toHaveCount(2); + await expect(page.getByPlaceholder('get_users').last()).toHaveValue('list_active_users'); +});