feat: Add comprehensive tests for contentPattern and glob pattern matching

- Implemented final verification tests for contentPattern to validate behavior with empty strings, dangerous patterns, and normal patterns.
- Created glob pattern matching tests to verify regex conversion and matching functionality.
- Developed infinite loop risk tests using Worker threads to isolate potential blocking operations.
- Introduced optimized contentPattern tests to validate improvements in the findMatches function.
- Added verification tests to assess the effectiveness of contentPattern optimizations.
- Conducted safety tests for contentPattern to identify edge cases and potential vulnerabilities.
- Implemented unrestricted loop tests to analyze infinite loop risks without match limits.
- Developed tests for zero-width pattern detection logic to ensure proper handling of dangerous regex patterns.
This commit is contained in:
catlog22
2026-02-09 11:13:01 +08:00
parent dfe153778c
commit 964292ebdb
62 changed files with 7588 additions and 374 deletions

View File

@@ -0,0 +1,98 @@
/**
* 测试危险模式拦截功能
* 通过捕获 console.error 来验证危险模式是否被检测
*/
import { executeTool } from './ccw/dist/tools/index.js';
// 捕获 console.error
const originalError = console.error;
const errorLogs = [];
console.error = (...args) => {
errorLogs.push(args.join(' '));
originalError(...args);
};
async function testDangerousPatterns() {
console.log('=== 危险模式拦截测试 ===\n');
const tests = [
{
name: '空字符串模式',
pattern: '',
shouldReject: true
},
{
name: '零宽匹配 *',
pattern: 'x*',
shouldReject: true
},
{
name: '或空匹配 a|',
pattern: 'a|',
shouldReject: true
},
{
name: '点星 .*',
pattern: '.*',
shouldReject: true
},
{
name: '正常模式',
pattern: 'CCW',
shouldReject: false
},
{
name: '正常模式 TODO',
pattern: 'TODO',
shouldReject: false
}
];
for (const test of tests) {
errorLogs.length = 0; // 清空错误日志
console.log(`\n测试: ${test.name}`);
console.log(`模式: "${test.pattern}"`);
console.log(`预期: ${test.shouldReject ? '应该拒绝' : '应该接受'}`);
try {
const result = await executeTool('read_file', {
paths: 'README.md',
contentPattern: test.pattern,
includeContent: false
});
// 检查是否有错误日志
const hasError = errorLogs.some(log =>
log.includes('contentPattern error') || log.includes('contentPattern warning')
);
if (test.shouldReject) {
if (hasError) {
console.log(`✅ 正确拒绝 - ${errorLogs[0]}`);
} else {
console.log(`❌ 未拒绝 - 应该拒绝但接受了`);
}
} else {
if (hasError) {
console.log(`❌ 错误拒绝 - ${errorLogs[0]}`);
} else {
console.log(`✅ 正常接受 - 找到 ${result.result.files.length} 个文件`);
}
}
} catch (error) {
console.log(`❌ 异常: ${error.message}`);
}
}
console.log(`\n${'='.repeat(60)}`);
// 恢复原始 console.error
console.error = originalError;
console.log('\n测试完成');
}
testDangerousPatterns().catch(console.error);