File size: 2,279 Bytes
c92aa92 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | import { z } from 'zod';
import { normalizeEngineName } from '../tools/setupTools.js';
const SUPPORTED_ENGINES = [
'baidu',
'bing',
'linuxdo',
'csdn',
'duckduckgo',
'exa',
'brave',
'juejin'
] as const;
const engineSchema = z.array(
z.string()
.min(1)
.transform(normalizeEngineName)
.pipe(z.enum(SUPPORTED_ENGINES))
).min(1);
type SuccessCase = {
input: string[];
expected: string[];
};
type FailureCase = {
input: string[];
reason: string;
};
const successCases: SuccessCase[] = [
{ input: ['bing'], expected: ['bing'] },
{ input: ['Bing'], expected: ['bing'] },
{ input: ['DuckDuckGo'], expected: ['duckduckgo'] },
{ input: ['duck-duck-go'], expected: ['duckduckgo'] },
{ input: ['linux.do'], expected: ['linuxdo'] },
{ input: [' CSDN ', 'JueJin'], expected: ['csdn', 'juejin'] }
];
const failureCases: FailureCase[] = [
{ input: ['Google'], reason: 'unsupported engine should fail' },
{ input: [''], reason: 'empty engine should fail' }
];
function assertEqualArray(actual: string[], expected: string[], label: string): void {
const ok = actual.length === expected.length && actual.every((value, index) => value === expected[index]);
if (!ok) {
throw new Error(`${label}: expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
}
}
function runSuccessCases(): void {
for (const testCase of successCases) {
const parsed = engineSchema.parse(testCase.input);
assertEqualArray(parsed, testCase.expected, `success case ${JSON.stringify(testCase.input)}`);
console.log(`✅ success: ${JSON.stringify(testCase.input)} -> ${JSON.stringify(parsed)}`);
}
}
function runFailureCases(): void {
for (const testCase of failureCases) {
const result = engineSchema.safeParse(testCase.input);
if (result.success) {
throw new Error(`failure case should fail: ${JSON.stringify(testCase.input)} (${testCase.reason})`);
}
console.log(`✅ expected failure: ${JSON.stringify(testCase.input)} (${testCase.reason})`);
}
}
function main(): void {
runSuccessCases();
runFailureCases();
console.log('\nEngine normalization tests passed.');
}
main();
|