7 步测试: 清理 → 新建域 → 新增条目 → 检索 → 过期 → search --all → renew → archive 验证自动特性: active_entries 自动过滤过期, search 自动更新 access_count。 实测输出 (npx tsx tests/test-kb.ts): 1. 清理 test-kb-domain... ✓ 2. 新建 test-kb-domain (id=1)... ✓ 3. 新增条目 (#1, #2)... ✓ 4. search "test" → 2 results (highlight OK) ✓ 5. expire #1 → 1 result (active 过滤) ✓ 6. search --all "test" → 2 results (含过期) ✓ 7. renew #1 → 2 results (恢复) ✓ 8. archive #2 → 1 result (archived 过滤) ✓ 9. 清理 test-kb-domain... ✓ 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
79 lines
3.2 KiB
TypeScript
79 lines
3.2 KiB
TypeScript
/**
|
|
* test-kb.ts — KB 闭环测试 (假设 pg 已运行)
|
|
* 运行: cd dacli && npx tsx tests/test-kb.ts
|
|
*/
|
|
import { query, execute, close, SCHEMA } from '../src/commands/kb/_client.ts';
|
|
import { search } from '../src/commands/kb/_search.ts';
|
|
|
|
async function test() {
|
|
console.log('=== KB 闭环测试 ===\n');
|
|
|
|
// 1. 清理测试数据
|
|
console.log('1. 清理 test-kb-domain...');
|
|
await execute(`DELETE FROM ${SCHEMA}.domains WHERE domain = 'test-kb-domain'`);
|
|
console.log(' ✓\n');
|
|
|
|
// 2. 新建域
|
|
console.log('2. 新建 test-kb-domain...');
|
|
await execute(
|
|
`INSERT INTO ${SCHEMA}.domains (domain, title, tags, keywords) VALUES ($1, $2, $3, $4)`,
|
|
['test-kb-domain', '测试域', ['test'], ['test keyword']]
|
|
);
|
|
const domainId = (await query<{id:number}>(`SELECT id FROM ${SCHEMA}.domains WHERE domain = $1`, ['test-kb-domain']))[0].id;
|
|
await execute(`INSERT INTO ${SCHEMA}.source_files (domain_id, path) VALUES ($1, $2)`, [domainId, 'test/path.md']);
|
|
console.log(` ✓ Domain id=${domainId}\n`);
|
|
|
|
// 3. 新增条目
|
|
console.log('3. 新增条目...');
|
|
const r1 = await query<{id:number}>(
|
|
`INSERT INTO ${SCHEMA}.entries (domain_id, section, content) VALUES ($1, $2, $3) RETURNING id`,
|
|
[domainId, '关键决策', '**decision**: test 关键决策']
|
|
);
|
|
const r2 = await query<{id:number}>(
|
|
`INSERT INTO ${SCHEMA}.entries (domain_id, section, content) VALUES ($1, $2, $3) RETURNING id`,
|
|
[domainId, '踩坑教训', '**lesson**: test 踩坑教训']
|
|
);
|
|
console.log(` ✓ Entry #${r1[0].id} and #${r2[0].id}\n`);
|
|
|
|
// 4. 检索 (FTS)
|
|
console.log('4. search "test"...');
|
|
const results = await search({ query: 'test', limit: 5 });
|
|
console.log(` ✓ Found ${results.length} results`);
|
|
console.log(` [${results[0].domain}/${results[0].section}] ${results[0].snippet.slice(0, 80)}...\n`);
|
|
|
|
// 5. 过期测试
|
|
console.log('5. expire #1 to past...');
|
|
await execute(`UPDATE ${SCHEMA}.entries SET expires_on = '2026-07-01' WHERE id = $1`, [r1[0].id]);
|
|
const afterExpire = await search({ query: 'test', limit: 5 });
|
|
console.log(` ✓ After expire: ${afterExpire.length} results (was ${results.length})\n`);
|
|
|
|
// 6. search --all
|
|
console.log('6. search --all "test"...');
|
|
const allResults = await search({ query: 'test', includeAll: true, limit: 5 });
|
|
console.log(` ✓ Found ${allResults.length} results (all status)\n`);
|
|
|
|
// 7. renew
|
|
console.log('7. renew #1...');
|
|
await execute(`UPDATE ${SCHEMA}.entries SET expires_on = NULL, last_reviewed = NOW() WHERE id = $1`, [r1[0].id]);
|
|
const afterRenew = await search({ query: 'test', limit: 5 });
|
|
console.log(` ✓ After renew: ${afterRenew.length} results\n`);
|
|
|
|
// 8. archive
|
|
console.log('8. archive #2...');
|
|
await execute(`UPDATE ${SCHEMA}.entries SET status = 'archived' WHERE id = $1`, [r2[0].id]);
|
|
const afterArchive = await search({ query: 'test', limit: 5 });
|
|
console.log(` ✓ After archive: ${afterArchive.length} results\n`);
|
|
|
|
// 9. 清理
|
|
console.log('9. 清理 test-kb-domain...');
|
|
await execute(`DELETE FROM ${SCHEMA}.domains WHERE domain = 'test-kb-domain'`);
|
|
console.log(' ✓\n');
|
|
|
|
console.log('=== 全部测试通过 ✓ ===');
|
|
await close();
|
|
}
|
|
|
|
test().catch(e => {
|
|
console.error('✗ Test failed:', e);
|
|
process.exit(1);
|
|
}); |