CI / verify (push) Successful in 1m2s
修复: - main.ts 退出释放模型显存改用 getSetting(serverUrl),不再硬编码 127.0.0.1:11434(避免非默认地址时释放请求打到错误端口) - 备份导出/导入并入 localStorage 持久化状态(会话摘要、度量历史、轨迹降级缓存、主题),版本升级到 v2,实现完整备份 - 工具数量改为 getEnabledToolDefinitions().length 动态计算,删除写死"32 个"的硬编码 - 记忆日志区分操作来源:memory:write 透传 reason,标注"新增记忆/替换/删除/清空/TTL 衰减清理/访问统计写回(无新条目)",避免"写了但看不到新记忆"的困惑 可维护性: - 上下文压力逻辑收敛到统一 calculateContextStats,删除 getContextPressureLevel / getTrendAwareCompressThreshold 的重复实现 - 消除 validateToolArgs 同名碰撞(agent-engine 本地版改名 validateToolArgsQuick) - 子代理工具集改用 getEnabledToolDefinitions() 基线,跟随全局启用开关与 Plan 模式 - 抽取 html-utils.ts 纯函数模块(实体解码/HTML→文本/HTML→Markdown/拦截页检测/相关性评分),tool-handlers-system 净减约 190 行重复代码 - 统一静态导入(savePlanTracker/setPlanModeActive/collectDiagnostics/addWrittenFile) - console.* 使用处补充豁免说明(启动/退出/刷盘阶段无渲染进程可推送日志) - run_command 工具描述改为反映可配置执行模式 测试: - 新增 7 个测试文件 + 扩展 2 个,共 273 个测试(原 34 → 273) - 覆盖 agent-engine / agent-safety / context-manager / tool-registry / result-formatter / tool-parsing / memory-service / crypto / build-context / html-utils / utils / tool-handlers-fs - 全部通过 npm run typecheck && npm test && npm run build
97 lines
3.6 KiB
TypeScript
97 lines
3.6 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import {
|
|
formatToolResultForModel,
|
|
summarizeAuditResult,
|
|
} from '../src/renderer/services/result-formatter.js';
|
|
import type { ToolResult } from '../src/renderer/types.js';
|
|
|
|
describe('formatToolResultForModel', () => {
|
|
it('失败结果返回统一错误 JSON', () => {
|
|
const out = formatToolResultForModel('read_file', { success: false, error: 'boom' });
|
|
expect(out).toContain('"success":false');
|
|
expect(out).toContain('boom');
|
|
});
|
|
|
|
it('web_search 格式化结果列表与抓取内容', () => {
|
|
const r: ToolResult = {
|
|
success: true,
|
|
query: 'rust',
|
|
total: 1,
|
|
results: [{ title: 'T', url: 'http://x', snippet: 'snippet' }],
|
|
_fetched: [{ url: 'http://x', title: 'T', content: 'full content here' }],
|
|
};
|
|
const out = formatToolResultForModel('web_search', r);
|
|
expect(out).toContain('T');
|
|
expect(out).toContain('http://x');
|
|
expect(out).toContain('已抓取');
|
|
});
|
|
|
|
it('web_fetch 返回内容', () => {
|
|
const out = formatToolResultForModel('web_fetch', { success: true, url: 'http://x', content: 'body' });
|
|
expect(out).toContain('body');
|
|
});
|
|
|
|
it('read_file 返回路径与内容', () => {
|
|
const out = formatToolResultForModel('read_file', { success: true, path: '/a.txt', content: 'abc', lines: 1, truncated: false });
|
|
expect(out).toContain('/a.txt');
|
|
expect(out).toContain('abc');
|
|
});
|
|
|
|
it('run_command 返回 stdout/stderr', () => {
|
|
const out = formatToolResultForModel('run_command', { success: true, stdout: 'out', stderr: '', exitCode: 0, duration: 10 });
|
|
expect(out).toContain('out');
|
|
expect(out).toContain('exitCode');
|
|
});
|
|
|
|
it('memory add 去重信号转为软提醒', () => {
|
|
const out = formatToolResultForModel('memory', { success: true, action: 'add', duplicate: true, message: '相同内容已存在' });
|
|
expect(out).toContain('相同内容已存在');
|
|
});
|
|
|
|
it('memory read_all 格式化分组', () => {
|
|
const r: ToolResult = {
|
|
success: true,
|
|
action: 'read_all',
|
|
entries: [
|
|
{ id: 'mem_1', type: 'rule', content: '规则一', importance: 9, tags: ['r1'] },
|
|
{ id: 'mem_2', type: 'fact', content: '事实一', importance: 5, tags: ['f1'] },
|
|
],
|
|
total: 2,
|
|
};
|
|
const out = formatToolResultForModel('memory', r);
|
|
expect(out).toContain('规则(必须遵守)');
|
|
expect(out).toContain('事实一');
|
|
});
|
|
|
|
it('delete_file 单个返回删除信息', () => {
|
|
const out = formatToolResultForModel('delete_file', { success: true, path: '/x', deleted: true, type: 'file', deletedSize: 100 });
|
|
expect(out).toContain('已删除');
|
|
});
|
|
|
|
it('diff 相同返回 no-position', () => {
|
|
const out = formatToolResultForModel('diff', { success: true, identical: true, message: '文件内容完全相同,无差异' });
|
|
expect(out).toContain('完全相同');
|
|
});
|
|
|
|
it('未知工具走默认 JSON 序列化', () => {
|
|
const out = formatToolResultForModel('unknown_tool', { success: true, someField: 'val' });
|
|
expect(out).toContain('someField');
|
|
});
|
|
});
|
|
|
|
describe('summarizeAuditResult', () => {
|
|
it('write_file 摘要含路径与字节数', () => {
|
|
const s = summarizeAuditResult('write_file', { success: true, path: '/a.txt', bytesWritten: 100, created: true });
|
|
expect(s).toContain('/a.txt');
|
|
});
|
|
|
|
it('run_command 摘要在失败时含 exit code', () => {
|
|
const s = summarizeAuditResult('run_command', { success: false, exitCode: 1 });
|
|
expect(s).toContain('失败');
|
|
});
|
|
|
|
it('默认工具名返回完成', () => {
|
|
expect(summarizeAuditResult('calculator', { success: true })).toContain('完成');
|
|
});
|
|
});
|