bookworm-smart-assistant/tests/browserbase-wrapper-env.test.js

68 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* browserbase-mcp-wrapper.js validateEnv() 单元测试
* 测试环境变量缺失时的快速失败行为
*/
const { execFileSync } = require('child_process');
const path = require('path');
const WRAPPER = path.join(__dirname, '..', 'scripts', 'browserbase-mcp-wrapper.js');
function runWrapper(envOverrides = {}) {
// 构造干净环境:只保留 PATH + 显式覆盖
const env = {
PATH: process.env.PATH || process.env.Path || '',
SystemRoot: process.env.SystemRoot || '',
...envOverrides
};
try {
const out = execFileSync('node', [WRAPPER], {
env,
timeout: 5000,
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe']
});
return { code: 0, stdout: out, stderr: '' };
} catch (err) {
return {
code: err.status,
stdout: err.stdout || '',
stderr: err.stderr || ''
};
}
}
describe('browserbase-mcp-wrapper validateEnv', () => {
test('缺少两个密钥时应 exit(1) 并输出明确错误', () => {
const result = runWrapper({});
expect(result.code).toBe(1);
expect(result.stderr).toContain('BROWSERBASE_API_KEY');
expect(result.stderr).toContain('BROWSERBASE_PROJECT_ID');
expect(result.stderr).toContain('FATAL');
});
test('只缺少 API_KEY 时应报错并列出缺失变量', () => {
const result = runWrapper({ BROWSERBASE_PROJECT_ID: 'FAKE_PROJECT_FOR_UNIT_TEST' });
expect(result.code).toBe(1);
expect(result.stderr).toContain('BROWSERBASE_API_KEY');
expect(result.stderr).not.toContain('BROWSERBASE_PROJECT_ID');
});
test('只缺少 PROJECT_ID 时应报错并列出缺失变量', () => {
const result = runWrapper({ BROWSERBASE_API_KEY: 'FAKE_KEY_FOR_UNIT_TEST' });
expect(result.code).toBe(1);
expect(result.stderr).toContain('BROWSERBASE_PROJECT_ID');
expect(result.stderr).not.toContain('BROWSERBASE_API_KEY');
});
test('两个密钥都存在时不应因 validateEnv 退出', () => {
// 注意: 会因为后续逻辑(代理检查/找不到 MCP bin退出
// 但 stderr 不应包含 validateEnv 的 FATAL 信息
const result = runWrapper({
BROWSERBASE_API_KEY: 'FAKE_KEY_FOR_UNIT_TEST',
BROWSERBASE_PROJECT_ID: 'FAKE_PROJECT_FOR_UNIT_TEST'
});
expect(result.stderr).not.toContain('FATAL');
expect(result.stderr).not.toContain('缺少必需环境变量');
});
});