feat: v0.7.4 时序语义修正 · 防线实效补漏 · 全量测试翻倍 — 2406 用例 + jsdom 组件测试全量回归
P1 修复面收口: - 超时三态区分(aborted→USER_INTERRUPT / ETIMEDOUT→TIMEOUT / 其余→ERROR), 根治"真实网络超时被误报为用户中断" - 流空闲超时统一(SSE/Ollama/Anthropic 读循环 60s 无数据抛 504 进重试通道) - 同会话并发 sendMessage 防重入(isRunning 守卫)+ 会话存在性预检 + 前置调用移入 try(ERROR+DONE 双事件保证,根治 isStreaming 假死) - 清空审计后 resetChainCache(根治 verifyChain 误报 TAMPERED) - DONE 不再提前清理 TRACE(TERMINATED 统一收尾,补全最终迭代录制) - IME 合成回车不发送(普通 Enter + Cmd/Ctrl+Enter 双分支)+ handleSend 闭包修复 P2 安全纵深: - preload 移除原始 electronAPI 暴露(渲染层零使用,关掉 XSS invoke 任意通道单点风险) - CORS 同源回显根治(仅当前浏览页面 Origin,did-navigate 同步) - MEMORY.md 命令保护正则扩展(括号/$/反引号/< 重定向边界 + 前导路径) - write_file append TOCTOU 统一(open 后 realpath 校验,新文件分支补漏) - 敏感键归一化(authKey 驼峰/连字符命中)+ MCP headers 鉴权值加密落库 - ReDoS 检测共享化(search_files/file_editor 统一拦截) - run_tests/lint_code 升风险 + 需确认 + npx --no-install(执行边界对齐 run_command) - MCP/SearXNG/llm.baseURL/updateFeedUrl 配置类 URL 高危目标校验(IPv6 去括号 + 十六进制映射解析 + 尾点剥离) P3 架构还债: - temperature/maxTokens 热生效(引擎/编排器/SubAgent 三处接线)+ setBatch 单事务落盘 - SessionRecorder flush 竞态根治(flushPromise 等待 + 超限内联落盘 + stopRecording async) - 内存收口(lastConsolidationBySession LRU / subTraces 清理 / 会话删除 disposeEngine) - i18n 全量收口(28 组件 + 353 key 双字典,状态标签改渲染时函数) - 死代码清理(updateTraceStep/HEADER_HEIGHT/void preA/失实注释) - 斜杠菜单 MUI 化 + 删除逻辑收敛 resetSessionState + Blob URL 统一释放 + 用户消息"仅保存"落库(saveMessage 透传前端 id 修复 id 错位) P4 能力演进: - 死循环检测拆分(驻留前置 + 乒乓后置带进度信号,合法交替不误报) - run-lock 30s 超时强制 abort(旧 run 卡死不无限排队) - RETRY 双通道 stream_reset(前端按 run 归属精确清空,根治重试文本重复) - FTS5 trigram 中文子串搜索(迁移 9 版本化 SCHEMA_VERSION=2,≤2 字符 LIKE 回退) - getContextWindow 兜底 1M→128K(未知模型防 413) 测试: - 855 → 2406 用例(+1551,2.8 倍):服务层 +325(含 MemoryManager 51 新用例)、 工具实体 +483、IPC/适配器 +390(含 OpenAI/Anthropic/Ollama 独立套件)、 纯函数表格化 +330;引入 jsdom + @testing-library(14 组件测试文件 249 用例) - 修复 R1(saveMessage id 透传)/ R2(stream_reset 精确归属)两个回归缺陷 - 遗留低危项清零:git-tools 顺序耦合 / web-fetch 真实时间退避 / slo 内存断言 / mcp-security 多余 skipIf / deepseek-balance 命名误导 / 组件 mock 注入脆弱性 版本: 0.7.4; README 同步(工具风险表/版本徽章); 依赖: 移除 @electron-toolkit/preload, 新增 jsdom/@testing-library(devDependencies 不打包) 回归: typecheck 双端 0 错误; ESLint 0/0; Electron ABI 全量 2406/2406 零跳过; 系统 Node 2110 通过 296 跳过(better-sqlite3 ABI)
This commit is contained in:
@@ -0,0 +1,369 @@
|
||||
/**
|
||||
* web_browser 工具测试(v0.7.5 新建覆盖)
|
||||
*
|
||||
* 通过 mock browser-window-manager(BrowserWindowManager 假实现)与
|
||||
* ssrf-guard(validateSSRF)锁定 9 种 action 的路由契约:
|
||||
* open/screenshot/evaluate/extract/click/type/scroll/wait/close
|
||||
* 及参数校验(缺 selector / 缺 text / 缺 script)与错误传播。
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
|
||||
vi.mock('electron-log', () => ({
|
||||
default: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
|
||||
}));
|
||||
|
||||
const ssrfMock = vi.hoisted(() => ({
|
||||
validateSSRF: vi.fn(async () => undefined),
|
||||
}));
|
||||
vi.mock('../ssrf-guard', () => ({
|
||||
validateSSRF: ssrfMock.validateSSRF,
|
||||
}));
|
||||
|
||||
const managerMock = vi.hoisted(() => {
|
||||
const methods: Record<string, ReturnType<typeof vi.fn>> = {};
|
||||
for (const m of [
|
||||
'open',
|
||||
'screenshot',
|
||||
'evaluate',
|
||||
'extract',
|
||||
'click',
|
||||
'type',
|
||||
'scroll',
|
||||
'wait',
|
||||
'close',
|
||||
]) {
|
||||
methods[m] = vi.fn();
|
||||
}
|
||||
return { methods };
|
||||
});
|
||||
vi.mock('../browser-window-manager', () => ({
|
||||
BrowserWindowManager: class {
|
||||
open = managerMock.methods.open;
|
||||
screenshot = managerMock.methods.screenshot;
|
||||
evaluate = managerMock.methods.evaluate;
|
||||
extract = managerMock.methods.extract;
|
||||
click = managerMock.methods.click;
|
||||
type = managerMock.methods.type;
|
||||
scroll = managerMock.methods.scroll;
|
||||
wait = managerMock.methods.wait;
|
||||
close = managerMock.methods.close;
|
||||
static cleanup = vi.fn(async () => undefined);
|
||||
},
|
||||
}));
|
||||
|
||||
import { WebBrowserTool } from '../browser';
|
||||
import type { ToolExecutionContext } from '../../../types/metona-tool';
|
||||
|
||||
const context: ToolExecutionContext = {
|
||||
sessionId: 't',
|
||||
workspacePath: process.cwd(),
|
||||
iteration: 1,
|
||||
requestId: 'r',
|
||||
};
|
||||
|
||||
describe('web_browser — 入口与 open', () => {
|
||||
let tool: WebBrowserTool;
|
||||
beforeEach(() => {
|
||||
tool = new WebBrowserTool();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
afterEach(() => vi.clearAllMocks());
|
||||
|
||||
it('缺 action → 报错', async () => {
|
||||
const r = (await tool.execute({}, context)) as { success: boolean; error?: string };
|
||||
expect(r.success).toBe(false);
|
||||
expect(String(r.error)).toContain('action');
|
||||
});
|
||||
|
||||
it('未知 action → 报错', async () => {
|
||||
const r = (await tool.execute({ action: 'frobnicate' }, context)) as {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
};
|
||||
expect(r.success).toBe(false);
|
||||
expect(String(r.error)).toContain('Unknown action');
|
||||
});
|
||||
|
||||
it('open 缺 url / 非法协议 → 拒绝', async () => {
|
||||
const noUrl = (await tool.execute({ action: 'open' }, context)) as { success: boolean };
|
||||
expect(noUrl.success).toBe(false);
|
||||
const fileUrl = (await tool.execute({ action: 'open', url: 'file:///x' }, context)) as {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
};
|
||||
expect(fileUrl.success).toBe(false);
|
||||
expect(String(fileUrl.error)).toContain('URL must start with');
|
||||
});
|
||||
|
||||
it('open SSRF 拦截(内网 IP)→ 拒绝且不创建窗口', async () => {
|
||||
ssrfMock.validateSSRF.mockRejectedValueOnce(
|
||||
new Error('Blocked SSRF: private/loopback address'),
|
||||
);
|
||||
const r = (await tool.execute({ action: 'open', url: 'http://127.0.0.1:1/' }, context)) as {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
};
|
||||
expect(r.success).toBe(false);
|
||||
expect(String(r.error)).toContain('Blocked SSRF');
|
||||
expect(managerMock.methods.open).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('open 成功返回 title/url(manager.open 结果展开)', async () => {
|
||||
managerMock.methods.open.mockResolvedValue({ title: 'Page', url: 'https://ok.test/' });
|
||||
const r = (await tool.execute({ action: 'open', url: 'https://ok.test/' }, context)) as {
|
||||
success: boolean;
|
||||
action: string;
|
||||
title?: string;
|
||||
url?: string;
|
||||
};
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.action).toBe('open');
|
||||
expect(r.title).toBe('Page');
|
||||
expect(r.url).toBe('https://ok.test/');
|
||||
expect(managerMock.methods.open).toHaveBeenCalledWith({
|
||||
url: 'https://ok.test/',
|
||||
waitSelector: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('open 透传 wait_selector', async () => {
|
||||
managerMock.methods.open.mockResolvedValue({ title: 'P', url: 'https://ok.test/' });
|
||||
await tool.execute({ action: 'open', url: 'https://ok.test/', wait_selector: '#app' }, context);
|
||||
expect(managerMock.methods.open).toHaveBeenCalledWith({
|
||||
url: 'https://ok.test/',
|
||||
waitSelector: '#app',
|
||||
});
|
||||
});
|
||||
|
||||
it('open 管理器抛错 → success:false', async () => {
|
||||
managerMock.methods.open.mockRejectedValue(new Error('navigation failed'));
|
||||
const r = (await tool.execute({ action: 'open', url: 'https://ok.test/' }, context)) as {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
};
|
||||
expect(r.success).toBe(false);
|
||||
expect(String(r.error)).toContain('navigation failed');
|
||||
});
|
||||
});
|
||||
|
||||
describe('web_browser — screenshot / evaluate / extract', () => {
|
||||
let tool: WebBrowserTool;
|
||||
beforeEach(() => {
|
||||
tool = new WebBrowserTool();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
afterEach(() => vi.clearAllMocks());
|
||||
|
||||
it('screenshot 成功返回 image/width/height/mime_type', async () => {
|
||||
managerMock.methods.screenshot.mockResolvedValue({
|
||||
data: 'iVBORw0KGgoAAAA',
|
||||
width: 800,
|
||||
height: 600,
|
||||
});
|
||||
const r = (await tool.execute({ action: 'screenshot' }, context)) as {
|
||||
success: boolean;
|
||||
image?: string;
|
||||
width?: number;
|
||||
height?: number;
|
||||
mime_type?: string;
|
||||
};
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.image).toBe('iVBORw0KGgoAAAA');
|
||||
expect(r.width).toBe(800);
|
||||
expect(r.height).toBe(600);
|
||||
expect(r.mime_type).toBe('image/png');
|
||||
});
|
||||
|
||||
it('screenshot 支持 full_page 与 selector 透传', async () => {
|
||||
managerMock.methods.screenshot.mockResolvedValue({ data: 'x', width: 1, height: 1 });
|
||||
await tool.execute({ action: 'screenshot', full_page: true, selector: '#main' }, context);
|
||||
expect(managerMock.methods.screenshot).toHaveBeenCalledWith({
|
||||
fullPage: true,
|
||||
selector: '#main',
|
||||
});
|
||||
});
|
||||
|
||||
it('screenshot 管理器抛错 → success:false', async () => {
|
||||
managerMock.methods.screenshot.mockRejectedValue(new Error('capture failed'));
|
||||
const r = (await tool.execute({ action: 'screenshot' }, context)) as {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
};
|
||||
expect(r.success).toBe(false);
|
||||
});
|
||||
|
||||
it('evaluate 成功返回脚本结果', async () => {
|
||||
managerMock.methods.evaluate.mockResolvedValue(42);
|
||||
const r = (await tool.execute({ action: 'evaluate', script: '1+1' }, context)) as {
|
||||
success: boolean;
|
||||
result?: unknown;
|
||||
};
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.result).toBe(42);
|
||||
});
|
||||
|
||||
it('evaluate 缺 script → 拒绝', async () => {
|
||||
const r = (await tool.execute({ action: 'evaluate' }, context)) as { success: boolean };
|
||||
expect(r.success).toBe(false);
|
||||
expect(managerMock.methods.evaluate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('extract 成功返回 text/links/link_count', async () => {
|
||||
managerMock.methods.extract.mockResolvedValue({
|
||||
text: '页面文本',
|
||||
links: ['https://a.test/', 'https://b.test/'],
|
||||
});
|
||||
const r = (await tool.execute({ action: 'extract' }, context)) as {
|
||||
success: boolean;
|
||||
text?: string;
|
||||
links?: string[];
|
||||
link_count?: number;
|
||||
};
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.text).toBe('页面文本');
|
||||
expect(r.link_count).toBe(2);
|
||||
});
|
||||
|
||||
it('extract 透传 selector', async () => {
|
||||
managerMock.methods.extract.mockResolvedValue({ text: 't', links: [] });
|
||||
await tool.execute({ action: 'extract', selector: 'article' }, context);
|
||||
expect(managerMock.methods.extract).toHaveBeenCalledWith('article');
|
||||
});
|
||||
});
|
||||
|
||||
describe('web_browser — click / type / scroll / wait / close', () => {
|
||||
let tool: WebBrowserTool;
|
||||
beforeEach(() => {
|
||||
tool = new WebBrowserTool();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
afterEach(() => vi.clearAllMocks());
|
||||
|
||||
it('click 成功返回 clicked:true', async () => {
|
||||
managerMock.methods.click.mockResolvedValue(undefined);
|
||||
const r = (await tool.execute({ action: 'click', selector: '#btn' }, context)) as {
|
||||
success: boolean;
|
||||
clicked?: boolean;
|
||||
};
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.clicked).toBe(true);
|
||||
expect(managerMock.methods.click).toHaveBeenCalledWith('#btn', false);
|
||||
});
|
||||
|
||||
it('click 缺 selector → 拒绝', async () => {
|
||||
const r = (await tool.execute({ action: 'click' }, context)) as { success: boolean };
|
||||
expect(r.success).toBe(false);
|
||||
});
|
||||
|
||||
it('click wait 参数透传', async () => {
|
||||
managerMock.methods.click.mockResolvedValue(undefined);
|
||||
await tool.execute({ action: 'click', selector: '#x', wait: true }, context);
|
||||
expect(managerMock.methods.click).toHaveBeenCalledWith('#x', true);
|
||||
});
|
||||
|
||||
it('type 成功返回 typed 长度', async () => {
|
||||
managerMock.methods.type.mockResolvedValue(undefined);
|
||||
const r = (await tool.execute(
|
||||
{ action: 'type', selector: '#input', text: 'hello world' },
|
||||
context,
|
||||
)) as { success: boolean; typed?: number };
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.typed).toBe(11);
|
||||
expect(managerMock.methods.type).toHaveBeenCalledWith('#input', 'hello world', {
|
||||
clear: true,
|
||||
submit: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('type 缺 selector 或 text → 拒绝', async () => {
|
||||
const noSel = (await tool.execute({ action: 'type', text: 'x' }, context)) as {
|
||||
success: boolean;
|
||||
};
|
||||
expect(noSel.success).toBe(false);
|
||||
const noText = (await tool.execute({ action: 'type', selector: '#i' }, context)) as {
|
||||
success: boolean;
|
||||
};
|
||||
expect(noText.success).toBe(false);
|
||||
expect(managerMock.methods.type).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('type 支持 clear/submit 透传', async () => {
|
||||
managerMock.methods.type.mockResolvedValue(undefined);
|
||||
await tool.execute(
|
||||
{ action: 'type', selector: '#f', text: 'v', clear: false, submit: true },
|
||||
context,
|
||||
);
|
||||
expect(managerMock.methods.type).toHaveBeenCalledWith('#f', 'v', {
|
||||
clear: false,
|
||||
submit: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('scroll 成功返回 direction/selector', async () => {
|
||||
managerMock.methods.scroll.mockResolvedValue(undefined);
|
||||
const r = (await tool.execute({ action: 'scroll', direction: 'bottom' }, context)) as {
|
||||
success: boolean;
|
||||
direction?: string;
|
||||
};
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.direction).toBe('bottom');
|
||||
expect(managerMock.methods.scroll).toHaveBeenCalledWith({
|
||||
direction: 'bottom',
|
||||
selector: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('scroll 缺省 direction=down', async () => {
|
||||
managerMock.methods.scroll.mockResolvedValue(undefined);
|
||||
const r = (await tool.execute({ action: 'scroll' }, context)) as { success: boolean };
|
||||
expect(r.success).toBe(true);
|
||||
expect(managerMock.methods.scroll).toHaveBeenCalledWith({
|
||||
direction: 'down',
|
||||
selector: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('wait 成功返回 waited_for(selector 形态)', async () => {
|
||||
managerMock.methods.wait.mockResolvedValue(undefined);
|
||||
const r = (await tool.execute({ action: 'wait', selector: '.ready' }, context)) as {
|
||||
success: boolean;
|
||||
waited_for?: string;
|
||||
};
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.waited_for).toBe('.ready');
|
||||
expect(managerMock.methods.wait).toHaveBeenCalledWith({ selector: '.ready', timeMs: 1000 });
|
||||
});
|
||||
|
||||
it('wait 缺 selector → waited_for 为时间形态,time_ms 透传', async () => {
|
||||
managerMock.methods.wait.mockResolvedValue(undefined);
|
||||
const r = (await tool.execute({ action: 'wait', time_ms: 500 }, context)) as {
|
||||
success: boolean;
|
||||
waited_for?: string;
|
||||
};
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.waited_for).toBe('500ms');
|
||||
expect(managerMock.methods.wait).toHaveBeenCalledWith({ selector: undefined, timeMs: 500 });
|
||||
});
|
||||
|
||||
it('close 成功返回 closed:true', async () => {
|
||||
managerMock.methods.close.mockResolvedValue(undefined);
|
||||
const r = (await tool.execute({ action: 'close' }, context)) as {
|
||||
success: boolean;
|
||||
closed?: boolean;
|
||||
};
|
||||
expect(r.success).toBe(true);
|
||||
expect(r.closed).toBe(true);
|
||||
expect(managerMock.methods.close).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('wait 管理器抛错 → success:false', async () => {
|
||||
managerMock.methods.wait.mockRejectedValue(new Error('timeout waiting'));
|
||||
const r = (await tool.execute({ action: 'wait', selector: '.x' }, context)) as {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
};
|
||||
expect(r.success).toBe(false);
|
||||
expect(String(r.error)).toContain('timeout waiting');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user