chore: fix merge config (#311)

This commit is contained in:
Pavel Feldman 2025-04-30 08:41:19 -07:00 committed by GitHub
parent fd22def4c5
commit 6d6b1a384b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 73 additions and 14 deletions

View File

@ -89,7 +89,7 @@ export async function configFromCLIOptions(cliOptions: CLIOptions): Promise<Conf
const launchOptions: LaunchOptions = {
channel,
executablePath: cliOptions.executablePath,
headless: cliOptions.headless ?? false,
headless: cliOptions.headless,
};
if (browserName === 'chromium')
@ -158,24 +158,33 @@ export async function outputFile(config: Config, name: string): Promise<string>
return path.join(result, fileName);
}
function pickDefined<T extends object>(obj: T | undefined): Partial<T> {
return Object.fromEntries(
Object.entries(obj ?? {}).filter(([_, v]) => v !== undefined)
) as Partial<T>;
}
function mergeConfig(base: Config, overrides: Config): Config {
const browser: Config['browser'] = {
...base.browser,
...overrides.browser,
...pickDefined(base.browser),
...pickDefined(overrides.browser),
launchOptions: {
...base.browser?.launchOptions,
...overrides.browser?.launchOptions,
...pickDefined(base.browser?.launchOptions),
...pickDefined(overrides.browser?.launchOptions),
...{ assistantMode: true },
},
contextOptions: {
...base.browser?.contextOptions,
...overrides.browser?.contextOptions,
...pickDefined(base.browser?.contextOptions),
...pickDefined(overrides.browser?.contextOptions),
},
};
if (browser.browserName !== 'chromium')
delete browser.launchOptions.channel;
return {
...base,
...overrides,
...pickDefined(base),
...pickDefined(overrides),
browser,
};
}

View File

@ -41,6 +41,7 @@ program
.option('--config <path>', 'Path to the configuration file.')
.action(async options => {
const config = await resolveConfig(options);
console.error(config);
const serverList = new ServerList(() => createServer(config));
setupExitWatchdog(serverList);

View File

@ -34,11 +34,11 @@ type TestFixtures = {
cdpEndpoint: string;
server: TestServer;
httpsServer: TestServer;
mcpHeadless: boolean;
mcpBrowser: string | undefined;
};
type WorkerFixtures = {
mcpHeadless: boolean;
mcpBrowser: string | undefined;
_workerServers: { server: TestServer, httpsServer: TestServer };
};
@ -112,11 +112,11 @@ export const test = baseTest.extend<TestFixtures, WorkerFixtures>({
browserProcess.kill();
},
mcpHeadless: [async ({ headless }, use) => {
mcpHeadless: async ({ headless }, use) => {
await use(headless);
}, { scope: 'worker' }],
},
mcpBrowser: ['chrome', { option: true, scope: 'worker' }],
mcpBrowser: ['chrome', { option: true }],
_workerServers: [async ({}, use, workerInfo) => {
const port = 8907 + workerInfo.workerIndex * 4;

49
tests/headed.spec.ts Normal file
View File

@ -0,0 +1,49 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test, expect } from './fixtures';
for (const mcpHeadless of [false, true]) {
test.describe(`mcpHeadless: ${mcpHeadless}`, () => {
test.use({ mcpHeadless });
test.skip(process.platform === 'linux', 'Auto-detection wont let this test run on linux');
test('browser', async ({ client, server, mcpBrowser }) => {
test.skip(!['chrome', 'msedge', 'chromium'].includes(mcpBrowser ?? ''), 'Only chrome is supported for this test');
server.route('/', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`
<body></body>
<script>
document.body.textContent = navigator.userAgent;
</script>
`);
});
const response = await client.callTool({
name: 'browser_navigate',
arguments: {
url: server.PREFIX,
},
});
expect(response).toContainTextContent(`Mozilla/5.0`);
if (mcpHeadless)
expect(response).toContainTextContent(`HeadlessChrome`);
else
expect(response).not.toContainTextContent(`HeadlessChrome`);
});
});
}