Files
Netcatty/scripts/fixtures/ai-tool-access/browser-qa.cjs
T
陈大猫 30a74f7c1d refactor(settings): simplify AI tool access configuration (#3957)
Move infrequent Tool Access / MCP / privacy / HTTP setup into focused dialogs; keep status and clear actions on the main page. Rebased onto current main after locale/settings conflicts.

Codex CLEAN (gpt-6.1-terra). CI lint-and-test green. Focused privacy/MCP/guidance tests passed on rebase tip. No release.
2026-10-27 20:59:11 -07:00

174 lines
13 KiB
JavaScript

// Run against Vite in a fresh browser context. Requires Playwright in the runner.
// Screenshots from this script are browser evidence, not native Electron acceptance.
const assert = require('node:assert/strict');
const { chromium } = require('playwright');
const { installAiToolAccessFixture } = require('./bridge.cjs');
const base = process.env.NETCATTY_QA_URL || 'http://127.0.0.1:5173';
const output = process.env.NETCATTY_QA_OUTPUT || '/tmp/netcatty-ai-tool-qa';
const { mkdirSync } = require('node:fs');
mkdirSync(output, { recursive: true });
(async () => {
const browser = await chromium.launch({ executablePath: process.env.CHROMIUM_PATH || '/usr/bin/chromium', headless: true, args: ['--no-sandbox'] });
try {
const context = await browser.newContext({ viewport: { width: 1200, height: 900 }, permissions: ['clipboard-read', 'clipboard-write'] });
await context.addInitScript(installAiToolAccessFixture, { locale: 'en', theme: 'dark', holdClients: true });
await context.route('**/*', (route) => new URL(route.request().url()).origin === new URL(base).origin ? route.continue() : route.abort());
const page = await context.newPage();
const errors = [];
page.on('pageerror', (error) => errors.push(error.message));
await page.goto(`${base}/#/settings`);
await page.getByRole('tab', { name: 'AI', exact: true }).click();
await page.getByRole('tab', { name: 'Tool Access', exact: true }).click();
await page.getByRole('button', { name: 'Configure privacy', exact: true }).waitFor();
const mcpHeader = page.locator('[data-settings-anchor="ai-external-mcp"]').locator(':scope > div').first();
const mcpTitleBox = await mcpHeader.getByRole('heading', { name: 'External MCP', exact: true }).boundingBox();
const mcpPill = mcpHeader.getByRole('status');
const mcpPillBox = await mcpPill.boundingBox();
assert.ok(Math.abs(mcpTitleBox.y + mcpTitleBox.height / 2 - mcpPillBox.y - mcpPillBox.height / 2) <= 1, 'MCP title and status pill share a vertical center');
assert.equal(await mcpPill.getAttribute('tabindex'), null);
assert.equal(await mcpPill.evaluate((el) => getComputedStyle(el).borderTopWidth), '0px');
const httpHeader = page.locator('[data-settings-anchor="ai-external-mcp-http"]').locator(':scope > div').first();
assert.match(await httpHeader.getByRole('status').innerText(), /Not listening/);
const dialog = () => page.getByRole('dialog');
const close = async () => { await dialog().getByRole('button', { name: 'Close', exact: true }).last().click(); await dialog().waitFor({ state: 'hidden' }); };
const mutations = () => page.evaluate(() => window.__aiToolQa.calls.filter((call) => ['config', 'enable', 'CodexAdd', 'ClaudeAdd', 'GrokAdd'].includes(call.name)));
const initial = (await mutations()).length;
await page.screenshot({ path: `${output}/main-en-dark.png` });
await page.getByRole('button', { name: 'View setup prompt' }).click();
assert.match(await dialog().innerText(), /fixture\/netcatty\/launcher/);
await dialog().getByRole('button', { name: 'Copy', exact: true }).click();
assert.match(await page.evaluate(() => navigator.clipboard.readText()), /get_environment/);
await page.keyboard.press('Escape');
assert.equal(await page.getByRole('button', { name: 'View setup prompt' }).evaluate((el) => el === document.activeElement), true);
await page.getByRole('button', { name: 'Configure behavior' }).click();
assert.match(await dialog().innerText(), /immediately/);
assert.equal(await dialog().getByRole('spinbutton', { name: 'Idle timeout', exact: true }).count(), 0);
await dialog().getByRole('combobox').nth(1).click();
await page.getByRole('option', { name: 'Temporary', exact: true }).click();
await dialog().getByRole('spinbutton', { name: 'Idle timeout', exact: true }).waitFor();
await close();
await page.getByRole('button', { name: 'Configure clients' }).click();
assert.equal(await dialog().getByRole('button', { name: 'Add to Codex', exact: true }).isEnabled(), false);
await page.waitForTimeout(3200); // At least one quiet runtime poll must overlap client discovery.
await page.evaluate(() => { window.__aiToolQa.holdClients = false; window.__aiToolQa.pendingClients.splice(0).forEach((resolve) => resolve()); });
await dialog().getByRole('button', { name: 'Add to Codex', exact: true }).and(page.locator(':enabled')).waitFor();
const beforeAdd = (await mutations()).length;
await dialog().getByRole('tab', { name: 'Cursor', exact: true }).click();
await dialog().getByRole('tab', { name: 'Other', exact: true }).click();
assert.equal((await mutations()).length, beforeAdd, 'opening and switching clients must not install anything');
await dialog().getByText('Manual setup', { exact: true }).click();
await dialog().getByLabel('Format', { exact: true }).selectOption('prompt');
assert.match(await dialog().innerText(), /Synthetic setup prompt/);
await dialog().getByRole('tab', { name: 'Codex', exact: true }).click();
await dialog().getByRole('button', { name: 'Add to Codex', exact: true }).click();
await dialog().getByText('Configured', { exact: true }).waitFor();
assert.equal((await page.evaluate(() => window.__aiToolQa.calls.filter((call) => call.name === 'CodexAdd'))).length, 1);
await close();
await page.getByRole('button', { name: 'Configure HTTP' }).click();
assert.match(await dialog().innerText(), /immediately/);
assert.equal(await dialog().getByRole('button', { name: 'Regenerate token', exact: true }).isVisible(), false);
const port = dialog().getByRole('spinbutton', { name: 'Port', exact: true });
const beforeInvalid = (await mutations()).length;
await port.fill('99');
await port.blur();
await dialog().getByRole('alert').waitFor();
assert.equal(await port.inputValue(), '99');
assert.equal((await mutations()).length, beforeInvalid);
await port.fill('47400');
await port.blur();
assert.equal((await mutations()).length, beforeInvalid + 1);
await page.evaluate(() => { window.__aiToolQa.rejectHttp = true; window.__aiToolQa.holdHttp = true; });
await port.fill('47401');
await port.blur();
await dialog().getByText('Applying HTTP configuration...', { exact: true }).waitFor();
await page.waitForTimeout(3200); // A quiet poll completes while the write is still pending.
await page.evaluate(() => { window.__aiToolQa.holdHttp = false; window.__aiToolQa.pendingHttp.splice(0).forEach((resolve) => resolve()); });
await dialog().getByRole('alert').filter({ hasText: 'Fixture: port unavailable.' }).waitFor();
assert.equal(await port.inputValue(), '47401', 'rejected HTTP settings retain input');
await page.screenshot({ path: `${output}/http-error-returned-en-dark.png` });
await page.waitForTimeout(3200); // Runtime polling must not erase operation failures.
assert.match(await dialog().innerText(), /Fixture: port unavailable/);
await page.evaluate(() => { window.__aiToolQa.rejectHttp = false; window.__aiToolQa.throwHttp = true; });
await dialog().getByRole('button', { name: 'Retry', exact: true }).click();
await dialog().getByRole('alert').filter({ hasText: 'Fixture: HTTP configuration threw.' }).waitFor();
assert.equal(await port.inputValue(), '47401', 'thrown HTTP configuration retains input');
await page.screenshot({ path: `${output}/http-error-thrown-en-dark.png` });
await page.evaluate(() => { window.__aiToolQa.throwHttp = false; });
await dialog().getByRole('button', { name: 'Retry', exact: true }).click();
await dialog().getByRole('alert').waitFor({ state: 'hidden' });
await dialog().getByRole('switch', { name: 'Enable HTTP', exact: true }).click();
await dialog().getByRole('switch', { name: 'Listen on another address', exact: true }).last().click();
await dialog().getByRole('textbox', { name: 'Listen address', exact: true }).waitFor();
await dialog().getByRole('textbox', { name: 'Client address', exact: true }).fill('192.0.2.10');
await dialog().getByRole('textbox', { name: 'Client address', exact: true }).blur();
await dialog().getByText('Connection details', { exact: true }).click();
assert.equal(await dialog().locator('pre').filter({ hasText: 'SYNTHETIC_FIXTURE_TOKEN_NEVER_REAL' }).count(), 0, 'all token-bearing previews start masked');
await page.screenshot({ path: `${output}/http-masked-en-dark.png` });
await dialog().getByRole('button', { name: 'Show secret', exact: true }).first().click();
assert.equal(await dialog().locator('pre').filter({ hasText: 'SYNTHETIC_FIXTURE_TOKEN_NEVER_REAL' }).count(), 1);
await close();
await page.getByRole('button', { name: 'Configure HTTP' }).click();
await dialog().getByText('Connection details', { exact: true }).click();
assert.equal(await dialog().locator('pre').filter({ hasText: 'SYNTHETIC_FIXTURE_TOKEN_NEVER_REAL' }).count(), 0);
assert.equal(await port.inputValue(), '47401');
await close();
await page.getByRole('button', { name: 'Configure HTTP' }).click();
await dialog().getByText('Connection details', { exact: true }).click();
await page.evaluate(() => { window.__aiToolQa.rejectRotate = true; });
await dialog().getByRole('button', { name: 'Regenerate', exact: true }).click();
await dialog().getByRole('alert').filter({ hasText: 'Fixture: token rotation unavailable.' }).waitFor();
await page.evaluate(() => { window.__aiToolQa.rejectRotate = false; window.__aiToolQa.throwRotate = true; });
await dialog().getByRole('button', { name: 'Retry', exact: true }).click();
await dialog().getByRole('alert').filter({ hasText: 'Fixture: token rotation threw.' }).waitFor();
await page.evaluate(() => { window.__aiToolQa.throwRotate = false; });
await dialog().getByRole('button', { name: 'Retry', exact: true }).click();
await dialog().getByRole('alert').waitFor({ state: 'hidden' });
await page.evaluate(() => {
window.__qaWriteText = navigator.clipboard.writeText.bind(navigator.clipboard);
navigator.clipboard.writeText = async () => { throw new Error('Fixture: clipboard unavailable'); };
});
await dialog().getByRole('button', { name: 'Copy', exact: true }).first().click();
await dialog().getByRole('alert').waitFor();
assert.match(await dialog().innerText(), /copy/i);
await page.evaluate(() => { navigator.clipboard.writeText = window.__qaWriteText; });
await dialog().getByRole('button', { name: 'Copy', exact: true }).first().click();
await dialog().getByRole('alert').waitFor({ state: 'hidden' });
await close();
await page.getByRole('button', { name: 'Configure privacy', exact: true }).click();
const beforeDraft = (await mutations()).length;
await dialog().getByRole('textbox', { name: 'Additional IP ranges (CIDR)', exact: true }).fill('192.0.2.0/24');
await dialog().getByRole('button', { name: 'Cancel', exact: true }).click();
assert.equal((await mutations()).length, beforeDraft);
await page.getByRole('button', { name: 'Configure privacy', exact: true }).click();
assert.equal(await dialog().getByRole('textbox', { name: 'Additional IP ranges (CIDR)', exact: true }).inputValue(), '');
await dialog().getByRole('textbox', { name: 'Custom regular expressions', exact: true }).fill('[');
await page.evaluate(() => { window.__aiToolQa.rejectPrivacy = true; });
await dialog().getByRole('button', { name: 'Apply', exact: true }).click();
await dialog().getByRole('alert').waitFor();
assert.equal(await dialog().getByRole('textbox', { name: 'Custom regular expressions', exact: true }).inputValue(), '[');
await page.screenshot({ path: `${output}/privacy-error-en-dark.png` });
await page.keyboard.press('Escape');
await page.getByRole('button', { name: 'Search settings', exact: true }).click();
await page.getByRole('combobox', { name: 'Search settings…', exact: true }).fill('Streamable HTTP');
await page.getByRole('option', { name: /Streamable HTTP/ }).click();
await dialog().getByRole('heading', { name: 'Streamable HTTP', exact: true }).waitFor();
await close();
await page.getByRole('button', { name: 'Search settings', exact: true }).click();
await page.getByRole('combobox', { name: 'Search settings…', exact: true }).fill('Approval alerts');
await page.getByRole('option', { name: /Approval alerts/ }).click();
await dialog().getByRole('heading', { name: 'MCP behavior', exact: true }).waitFor();
await close();
await page.setViewportSize({ width: 640, height: 580 });
await page.getByRole('button', { name: 'Configure behavior' }).click();
const bounds = await dialog().boundingBox();
assert.ok(bounds.x >= 0 && bounds.y >= 0 && bounds.width <= 640 && bounds.height <= 580);
await close();
assert.deepEqual(errors, []);
assert.ok((await mutations()).length > initial);
console.log('PASS: actual settings renderer; guidance/copy/focus, behavior conditionals, explicit client install, HTTP validation/immediate commit/masking/returned-and-thrown-config-and-rotation-errors/retry/copy-failure, privacy failure/cancel/reopen, narrow viewport.');
await context.close();
} finally { await browser.close(); }
})().catch((error) => { console.error(error); process.exitCode = 1; });