工程化(从零到一): - 新增 Gitea Actions CI(debian-latest):类型检查 + Lint + 单元测试 + 产物编译验证 - 新增 husky + lint-staged 预提交钩子(lint-staged + typecheck 门禁) - 移除坏脚本 test:e2e(无 Playwright 配置必失败);prebuild 改用内置 fs.rmSync - 依赖清理:移除死依赖 sql.js(2MB)/@playwright/test,@types/shell-quote 移至 devDependencies 安全加固: - PolicyEngine 频率限制按会话隔离(多会话并发不再互抢配额) - ConfirmationHook 拒绝记忆加 10 分钟 TTL + 恢复询问入口(新增 2 个 IPC 通道) - Windows run_command 白名单工具(git/node/npm/npx/pnpm/yarn/tsc)改走 cmd.exe /c + 参数数组执行,收窄 shell 注入面 - web_search 四引擎 HTML 解析迁移 node-html-parser(结构化主层 + 正则降级) 缺陷修复(测试驱动发现): - mapError 大小写缺陷:网络错误码永远落入 UNKNOWN 无法触发重试 - 搜狗解析器自我过滤:相对链接补全后又被 sogou.com 过滤导致结果全丢 - 百度复合类名重复收录:class="result c-container" 被双重匹配 测试补齐(113 → 194 用例): - 新增 5 个测试文件:sse-stream / base-adapter / confirmation-hook / ipc-agent 编排链路 / web-search 解析器 - 覆盖 sendMessage 全分支、SSE 流解析、错误映射、确认钩子竞态/超时/批量审批 体验升级: - OutputValidator 验证结果可见化(VALIDATION 流事件 → 聊天流提示卡) - SettingsModal 巨型组件拆分(1503 行 → 10 个文件,可独立维护) - MessageList 接入 react-virtuoso 真虚拟滚动(千条消息恒定开销) - MCP 新增 streamable HTTP 传输支持(SDK 内置传输 + DB 迁移 6 + UI 双模式)
175 lines
5.8 KiB
TypeScript
175 lines
5.8 KiB
TypeScript
/**
|
|
* web_search 搜索引擎 HTML 解析器单元测试(v0.4.1 测试补齐)
|
|
* 覆盖:node-html-parser 结构化解析(主层)、自域名链接过滤、
|
|
* 相对链接补全、空/异常 HTML 容错
|
|
*/
|
|
|
|
import { describe, it, expect } from 'vitest';
|
|
import { parseBing, parseBaidu, parseSogou, parse360 } from '../web-search';
|
|
|
|
describe('parseBing — 结构化解析', () => {
|
|
const BING_HTML = `
|
|
<html><body>
|
|
<ol id="b_results">
|
|
<li class="b_algo">
|
|
<h2><a href="https://example.com/article-1">第一篇 TypeScript 文章</a></h2>
|
|
<p>这是第一条结果的摘要内容,讲述 TypeScript 高级用法。</p>
|
|
</li>
|
|
<li class="b_algo">
|
|
<h2><a href="https://example.com/article-2">第二篇 Node.js 文章</a></h2>
|
|
<div class="b_caption"><p>第二条结果的摘要。</p></div>
|
|
</li>
|
|
<li class="b_algo">
|
|
<!-- 自身域名链接应被过滤 -->
|
|
<h2><a href="https://www.bing.com/video?q=x">Bing 内部视频链接</a></h2>
|
|
<p>不应出现在结果中。</p>
|
|
</li>
|
|
</ol>
|
|
</body></html>
|
|
`;
|
|
|
|
it('解析结果块并提取 title/url/snippet', () => {
|
|
const results = parseBing(BING_HTML);
|
|
expect(results).toHaveLength(2);
|
|
expect(results[0]).toMatchObject({
|
|
title: '第一篇 TypeScript 文章',
|
|
url: 'https://example.com/article-1',
|
|
snippet: '这是第一条结果的摘要内容,讲述 TypeScript 高级用法。',
|
|
engine: 'bing',
|
|
weight: 90,
|
|
});
|
|
expect(results[1].url).toBe('https://example.com/article-2');
|
|
});
|
|
|
|
it('过滤指向 bing.com 自身域名的链接', () => {
|
|
const results = parseBing(BING_HTML);
|
|
expect(results.some((r) => r.url.includes('bing.com'))).toBe(false);
|
|
});
|
|
|
|
it('空 HTML 返回空数组', () => {
|
|
expect(parseBing('')).toHaveLength(0);
|
|
expect(parseBing('<html><body></body></html>')).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe('parseBaidu — 结构化解析', () => {
|
|
const BAIDU_HTML = `
|
|
<html><body>
|
|
<div id="content_left">
|
|
<div class="result c-container new-pmd" srcid="1">
|
|
<h3 class="t"><a data-url="https://example.com/real-url-1" href="https://www.baidu.com/link?url=xyz">百度结果一</a></h3>
|
|
<span class="content-right_8Zs40">第一条摘要内容。</span>
|
|
</div>
|
|
<div class="result c-container" srcid="2">
|
|
<h3><a href="https://www.baidu.com/link?url=abc">跳转链接结果</a></h3>
|
|
<span class="content-right_8Zs40">无 data-url 的结果(跳转链接被过滤)。</span>
|
|
</div>
|
|
</div>
|
|
</body></html>
|
|
`;
|
|
|
|
it('优先使用 data-url 真实链接', () => {
|
|
const results = parseBaidu(BAIDU_HTML);
|
|
expect(results).toHaveLength(1);
|
|
expect(results[0]).toMatchObject({
|
|
title: '百度结果一',
|
|
url: 'https://example.com/real-url-1',
|
|
engine: '百度',
|
|
weight: 80,
|
|
});
|
|
});
|
|
|
|
it('baidu.com/link 跳转链接被过滤', () => {
|
|
const results = parseBaidu(BAIDU_HTML);
|
|
expect(results.some((r) => r.url.includes('baidu.com/link'))).toBe(false);
|
|
});
|
|
|
|
it('空 HTML 返回空数组', () => {
|
|
expect(parseBaidu('')).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe('parseSogou — 结构化解析', () => {
|
|
const SOGOU_HTML = `
|
|
<html><body>
|
|
<div class="results">
|
|
<div class="vrwrap">
|
|
<h3><a href="/link?url=sogou-internal-1">搜狗结果一</a></h3>
|
|
<div class="str_info">搜狗结果一的摘要文本。</div>
|
|
</div>
|
|
<div class="rb">
|
|
<h3><a href="https://example.com/direct">搜狗直链结果</a></h3>
|
|
<p class="space-txt">直链结果的摘要。</p>
|
|
</div>
|
|
</div>
|
|
</body></html>
|
|
`;
|
|
|
|
it('相对链接补全 sogou.com 前缀', () => {
|
|
const results = parseSogou(SOGOU_HTML);
|
|
expect(results).toHaveLength(2);
|
|
expect(results[0]).toMatchObject({
|
|
title: '搜狗结果一',
|
|
url: 'https://www.sogou.com/link?url=sogou-internal-1',
|
|
engine: '搜狗',
|
|
weight: 75,
|
|
});
|
|
});
|
|
|
|
it('http 开头的直链不补全前缀', () => {
|
|
const results = parseSogou(SOGOU_HTML);
|
|
expect(results[1].url).toBe('https://example.com/direct');
|
|
expect(results[1].snippet).toBe('直链结果的摘要。');
|
|
});
|
|
|
|
it('空 HTML 返回空数组', () => {
|
|
expect(parseSogou('')).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe('parse360 — 结构化解析', () => {
|
|
const SO360_HTML = `
|
|
<html><body>
|
|
<div class="res-list">
|
|
<ul>
|
|
<li class="res-list">
|
|
<h3 class="res-title"><a href="https://example.com/360-result">360 结果一</a></h3>
|
|
<p class="res-desc">360 搜索结果的摘要描述。</p>
|
|
</li>
|
|
<li class="res-list">
|
|
<h3><a href="https://www.so.com/internal">360 内部链接</a></h3>
|
|
<p class="res-desc">不应出现。</p>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</body></html>
|
|
`;
|
|
|
|
it('解析结果并过滤 so.com 自身链接', () => {
|
|
const results = parse360(SO360_HTML);
|
|
expect(results).toHaveLength(1);
|
|
expect(results[0]).toMatchObject({
|
|
title: '360 结果一',
|
|
url: 'https://example.com/360-result',
|
|
snippet: '360 搜索结果的摘要描述。',
|
|
engine: '360搜索',
|
|
weight: 75,
|
|
});
|
|
});
|
|
|
|
it('空 HTML 返回空数组', () => {
|
|
expect(parse360('')).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe('解析器降级路径', () => {
|
|
it('结构化解析无结果且正则也无结果时返回空数组(不抛错)', () => {
|
|
// 非搜索结果页 HTML(如错误页/验证码页)
|
|
const notSearchPage = '<html><body><div class="captcha">请输入验证码</div></body></html>';
|
|
expect(parseBing(notSearchPage)).toHaveLength(0);
|
|
expect(parseBaidu(notSearchPage)).toHaveLength(0);
|
|
expect(parseSogou(notSearchPage)).toHaveLength(0);
|
|
expect(parse360(notSearchPage)).toHaveLength(0);
|
|
});
|
|
});
|