Files
brave-search-mcp-server/src/config.test.ts
T
Sampson fd28e3e7b3 feat(config): support reading Brave API key from a file (#313)
* feat: support reading Brave API key from a file
* chore: add secret file template and gitignore local keys
* docs: document optional Docker Compose secrets workflow
* fix: trim whitespace API keys and exclude secrets from Docker build
* docs: clarify container paths for BRAVE_API_KEY_FILE
* test: cover API key file config and expand Docker build context ignores
* test: isolate getOptions tests from local env vars
* docs: clarify optional use of BRAVE_API_KEY_FILE
* docs: document BRAVE_API_KEY_FILE in AWS Marketplace catalog template
2026-06-24 13:53:51 -05:00

82 lines
2.1 KiB
TypeScript

import assert from 'node:assert/strict';
import { mkdtempSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, it } from 'node:test';
import { getOptions } from './config.js';
const CONFIG_ENV_VARS = [
'BRAVE_API_KEY',
'BRAVE_API_KEY_FILE',
'BRAVE_MCP_LOG_LEVEL',
'BRAVE_MCP_TRANSPORT',
'BRAVE_MCP_ENABLED_TOOLS',
'BRAVE_MCP_DISABLED_TOOLS',
'BRAVE_MCP_PORT',
'BRAVE_MCP_HOST',
'BRAVE_MCP_STATELESS',
] as const;
describe('getOptions', () => {
const originalArgv = process.argv;
const originalEnv = Object.fromEntries(CONFIG_ENV_VARS.map((key) => [key, process.env[key]]));
beforeEach(() => {
for (const key of CONFIG_ENV_VARS) {
delete process.env[key];
}
});
afterEach(() => {
process.argv = originalArgv;
for (const key of CONFIG_ENV_VARS) {
if (originalEnv[key] === undefined) {
delete process.env[key];
} else {
process.env[key] = originalEnv[key];
}
}
});
it('rejects whitespace-only API keys', () => {
process.argv = ['node', 'index.js', '--brave-api-key', ' '];
assert.equal(getOptions(), false);
});
it('reads API key from --brave-api-key-file', () => {
const dir = mkdtempSync(join(tmpdir(), 'brave-api-key-'));
const filePath = join(dir, 'key.txt');
writeFileSync(filePath, 'from-file\n');
process.argv = ['node', 'index.js', '--brave-api-key-file', filePath];
const options = getOptions();
assert.notEqual(options, false);
if (options) {
assert.equal(options.braveApiKey, 'from-file');
}
});
it('prefers file key over --brave-api-key', () => {
const dir = mkdtempSync(join(tmpdir(), 'brave-api-key-'));
const filePath = join(dir, 'key.txt');
writeFileSync(filePath, 'from-file\n');
process.argv = [
'node',
'index.js',
'--brave-api-key',
'from-env',
'--brave-api-key-file',
filePath,
];
const options = getOptions();
assert.notEqual(options, false);
if (options) {
assert.equal(options.braveApiKey, 'from-file');
}
});
});