fix(browser): preserve chat focus during agent browsing
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import type { ElectronApplication, Page } from 'playwright-core';
|
||||
import { appendFile } from 'node:fs/promises';
|
||||
import { createServer } from 'node:http';
|
||||
import { createRequire } from 'node:module';
|
||||
import { resolve } from 'node:path';
|
||||
import type { NativeAgentBrowserFixture } from './fixtures/native-agent-browser';
|
||||
import { expect, getStableWindow, test } from './fixtures/electron';
|
||||
|
||||
type CapturedRequest = {
|
||||
@@ -102,6 +106,7 @@ async function installCodingFirstChatHost(
|
||||
};
|
||||
const mainGlobal = globalThis as typeof globalThis & {
|
||||
__makelorePiFirstChatE2E?: MainState;
|
||||
__makeloreNativeBrowserE2E?: NativeAgentBrowserFixture;
|
||||
};
|
||||
const state: MainState = {
|
||||
captured: [],
|
||||
@@ -593,6 +598,9 @@ async function installCodingFirstChatHost(
|
||||
: []).map((item) => ({ ...item, ...metadata.get(item.id) })),
|
||||
});
|
||||
}
|
||||
if (path.startsWith('/api/agent-browser/') && mainGlobal.__makeloreNativeBrowserE2E) {
|
||||
return respond(await mainGlobal.__makeloreNativeBrowserE2E.route(path, body));
|
||||
}
|
||||
if (path.startsWith('/api/agent-browser/state?')) {
|
||||
return respond({ success: true, browser: browserSnapshot() });
|
||||
}
|
||||
@@ -2045,6 +2053,93 @@ test('work tab prepares the page automatically and keeps the student draft throu
|
||||
expect(requests.filter((request) => request.path.endsWith('/prompt'))).toHaveLength(0);
|
||||
});
|
||||
|
||||
test('agent browser continues in the chat tab without repeatedly selecting Work', async ({ launchElectronApp }) => {
|
||||
const require = createRequire(resolve('package.json'));
|
||||
const { build } = createRequire(require.resolve('vite/package.json'))('esbuild') as typeof import('esbuild');
|
||||
const bundlePath = test.info().outputPath('native-agent-browser.cjs');
|
||||
await build({
|
||||
entryPoints: [resolve('tests/e2e/fixtures/native-agent-browser.ts')],
|
||||
outfile: bundlePath, bundle: true, platform: 'node', format: 'cjs', external: ['electron'],
|
||||
});
|
||||
let pageLoads = 0;
|
||||
const server = createServer((_request, response) => {
|
||||
pageLoads += 1;
|
||||
response.setHeader('Content-Type', 'text/html; charset=utf-8');
|
||||
response.end('<html><body><h1>Browser background test</h1><script>console.log("page-ready")</script></body></html>');
|
||||
});
|
||||
await new Promise<void>((done) => server.listen(0, '127.0.0.1', done));
|
||||
const address = server.address();
|
||||
if (!address || typeof address === 'string') throw new Error('Missing loopback port');
|
||||
const url = 'http://127.0.0.1:' + address.port + '/';
|
||||
const electronApp = await launchElectronApp({ skipSetup: true });
|
||||
try {
|
||||
let page = await getStableWindow(electronApp);
|
||||
const connection = await page.evaluate(async () => ({ token: await window.electron.ipcRenderer.invoke('hostapi:token') as string, baseUrl: await window.electron.ipcRenderer.invoke('hostapi:base-url') as string }));
|
||||
await installCodingFirstChatHost(electronApp, connection, true);
|
||||
await settleSnapshot(electronApp);
|
||||
await disableCodingEventSource(page);
|
||||
await page.reload(); page = await getStableWindow(electronApp);
|
||||
await page.getByTestId('ai-module-option-programming').click();
|
||||
await page.evaluate(() => { window.location.hash = '/chat'; });
|
||||
const composer = page.getByTestId('coding-message-composer').getByRole('textbox');
|
||||
await composer.fill('继续对话,不要抢走标签');
|
||||
await electronApp.evaluate(({ app, BrowserWindow }, input) => {
|
||||
const { NativeAgentBrowserFixture } = process.mainModule!.require(input.bundlePath) as typeof import('./fixtures/native-agent-browser');
|
||||
(globalThis as typeof globalThis & { __makeloreNativeBrowserE2E?: NativeAgentBrowserFixture }).__makeloreNativeBrowserE2E =
|
||||
new NativeAgentBrowserFixture(BrowserWindow.getAllWindows()[0], app.getPath('userData'), input.url);
|
||||
}, { bundlePath, url });
|
||||
const execute = async (input: Record<string, unknown>) => await electronApp.evaluate(async (_electron, command) => {
|
||||
return await (globalThis as typeof globalThis & { __makeloreNativeBrowserE2E: NativeAgentBrowserFixture }).__makeloreNativeBrowserE2E.execute(command);
|
||||
}, input);
|
||||
const opened = await execute({ action: 'open', url });
|
||||
expect(opened.value.visible).toBe(true);
|
||||
expect(opened.showRequests).toBe(1);
|
||||
await expect(page.getByRole('tab', { name: '作品', exact: true })).toHaveAttribute('aria-selected', 'true');
|
||||
await page.getByRole('tab', { name: '操作对话', exact: true }).click();
|
||||
await expect.poll(async () => (await execute({ action: 'status' })).value.visible).toBe(false);
|
||||
await composer.focus();
|
||||
const generation = opened.value.generation;
|
||||
for (let attempt = 0; attempt < 3; attempt += 1) {
|
||||
// Include a new URL: open may navigate, but must not reveal the native view.
|
||||
const reused = await execute({ action: 'open', url: url + '?attempt=' + attempt });
|
||||
expect(reused.value).toMatchObject({ browserId: opened.value.browserId, generation, visible: false });
|
||||
expect(reused.showRequests).toBe(1);
|
||||
const evaluated = await execute({
|
||||
action: 'send_cdp', method: 'Runtime.evaluate',
|
||||
params: { expression: 'document.querySelector("h1").textContent', returnByValue: true },
|
||||
});
|
||||
expect(evaluated.value).toMatchObject({ kind: 'inline', value: { result: { value: 'Browser background test' } } });
|
||||
await expect(page.getByRole('tab', { name: '操作对话', exact: true })).toHaveAttribute('aria-selected', 'true');
|
||||
await expect(composer).toHaveValue('继续对话,不要抢走标签');
|
||||
}
|
||||
const loadsBeforeReload = pageLoads;
|
||||
await execute({ action: 'navigate', navigation: 'reload' });
|
||||
await expect.poll(() => pageLoads).toBeGreaterThan(loadsBeforeReload);
|
||||
const events = await execute({ action: 'read_events', after: 0 });
|
||||
expect(events.value.events.some((event: { method: string }) => event.method === 'Runtime.consoleAPICalled')).toBe(true);
|
||||
const screenshot = await execute({ action: 'send_cdp', method: 'Page.captureScreenshot' });
|
||||
expect(screenshot.value).toMatchObject({ attachmentId: expect.any(String), mime: 'image/png' });
|
||||
expect(screenshot.showRequests).toBe(1);
|
||||
expect((await execute({ action: 'status' })).value.visible).toBe(false);
|
||||
await expect(composer).toHaveValue('继续对话,不要抢走标签');
|
||||
await expect(composer).toBeFocused();
|
||||
await page.getByRole('tab', { name: '作品', exact: true }).click();
|
||||
await expect.poll(async () => (await execute({ action: 'status' })).value.visible).toBe(true);
|
||||
expect((await execute({ action: 'status' })).value).toMatchObject({ browserId: opened.value.browserId, generation });
|
||||
await page.getByRole('tab', { name: '操作对话', exact: true }).click();
|
||||
await expect.poll(async () => (await execute({ action: 'status' })).value.visible).toBe(false);
|
||||
await execute({ action: 'close' });
|
||||
expect((await execute({ action: 'status' })).value.state).toBe('closed');
|
||||
} finally {
|
||||
await electronApp.evaluate(async () => {
|
||||
const mainGlobal = globalThis as typeof globalThis & { __makeloreNativeBrowserE2E?: NativeAgentBrowserFixture };
|
||||
await mainGlobal.__makeloreNativeBrowserE2E?.dispose();
|
||||
delete mainGlobal.__makeloreNativeBrowserE2E;
|
||||
});
|
||||
await new Promise<void>((done, reject) => server.close((error) => error ? reject(error) : done()));
|
||||
}
|
||||
});
|
||||
|
||||
test('work failure offers a useful recovery and retains the student draft after opening', async ({ launchElectronApp }) => {
|
||||
const electronApp = await launchElectronApp({ skipSetup: true });
|
||||
let page = await getStableWindow(electronApp);
|
||||
|
||||
Reference in New Issue
Block a user