feat: v0.7.2 安全收口 · 断链接线 · 观测补洞 — 230 用例扩充与全量回归
P1 修复面收口: /clear 全链路根治(前端清空联动 DB messages+摘要游标+TRACE 快照, IPC 语义改"操作完成"; 流式中拒绝); web_browser open 补 SSRF 校验(Chromium 旁路关闭, 与 web_fetch/http_request 同源 validateSSRF); MCP 工具结果纳入注入扫描(mcp_* 前缀 按网络来源同级 full 模式, 收敛 resolveScanMode 单点); Trace 落库/入 store 双重瘦身 (tool_result base64/超长字段剥离, metadata 防 MB 级膨胀); 文本附件 512KB 闸门 (file.slice 首段读取+truncated 标志随消息持久化+主进程附件提示感知截断); 单实例锁(requestSingleInstanceLock + second-instance 聚焦已有窗口) P2 安全纵深: ConfirmationHook 多窗口化(确认请求/超时提示改全窗口广播, getAllWindows 空时回退 mainWindow, fail-closed 判定升级双通道); mcp_servers.headers 全链路接线(safeParseHeaders 容错解析+SSE/StreamableHTTP requestInit 注入+IPC 逐项 校验+设置页 JSON 输入, 远程 MCP 鉴权头可用) P3 断链接线: llm:listModels IPC(六家 adapter 动态模型发现首次接线, 配置完整性 前置校验); Ollama pullModel IPC+设置页下载卡片(进度/取消/能力徽标, v0.7.0 死代码 激活); 后台会话运行指示(sessionRunStates 图+Sidebar 状态点, 多会话并发可见); IR 卫生(移除 THINKING_START/END 死枚举, constraints 标注预留) P4 质量与文档: i18n 第二阶段(确认弹框/侧栏/状态栏/AgentMonitor/终止原因出层, 外观设置 zh-CN/en-US 切换, ui.locale 持久化, 渲染时求值规避异步注册); README/D1 文档对齐(http_request 风险等级/用例数/实现状态注记); 版本号 0.7.2 测试: 507 → 737 用例(+230, 11 个新文件)。覆盖补齐: context-builder/consolidator/ orchestrator/workspace.service/session-recorder/config-layering/secure-config/ network-proxy + IPC mcp/tasks/memory/app/data 域 + 渲染层 store 与流事件管线纯函数。 测试驱动修复: workspace.appendMemory 中文分区 \b 词边界失效(JS \b 不含 CJK), 固化条目恒追加文件末尾产生重复分区头 → (?=\n|$) 前瞻断言根治 回归: typecheck 双端 0 错误; ESLint 0/0; 系统 Node 687 通过 50 跳过; Electron ABI 全量 737/737 零跳过
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
/**
|
||||
* WorkspaceService 测试(v0.7.2 覆盖补齐 —— 此前零测试)
|
||||
*
|
||||
* 锁定工作空间生命周期契约:
|
||||
* 1. initialize 创建目录结构(logs/.metona)与必需文件(SOUL.md/MEMORY.md)
|
||||
* 2. MEMORY.md 模板元数据头 + 5 分区结构
|
||||
* 3. appendMemory 分区追加语义(含未知 section 兜底)
|
||||
* 4. validateMemoryFormat 自动修正(缺元数据头/缺分区)
|
||||
* 5. updateMemoryTimestamp / reload(外部编辑同步)
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, rmSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { tmpdir } from 'os';
|
||||
|
||||
import { WorkspaceService } from '../workspace.service';
|
||||
|
||||
let wsRoot: string;
|
||||
|
||||
beforeEach(() => {
|
||||
wsRoot = mkdtempSync(join(tmpdir(), 'metona-ws-'));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
try {
|
||||
rmSync(wsRoot, { recursive: true, force: true });
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
});
|
||||
|
||||
describe('WorkspaceService — initialize', () => {
|
||||
it('创建工作空间目录结构 + 两个必需文件 + 自动目录', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
const info = svc.initialize();
|
||||
|
||||
expect(info.isValid).toBe(true);
|
||||
expect(info.path).toBe(wsPath);
|
||||
expect(existsSync(join(wsPath, 'SOUL.md'))).toBe(true);
|
||||
expect(existsSync(join(wsPath, 'MEMORY.md'))).toBe(true);
|
||||
expect(existsSync(join(wsPath, 'logs'))).toBe(true);
|
||||
expect(existsSync(join(wsPath, '.metona'))).toBe(true);
|
||||
// 本次启动自动创建的文件计入 missingFiles
|
||||
expect(info.missingFiles).toEqual(['SOUL.md', 'MEMORY.md']);
|
||||
});
|
||||
|
||||
it('已有完整文件的工作空间:missingFiles 为空且内容不被覆盖', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
mkdirSync(wsPath, { recursive: true });
|
||||
writeFileSync(join(wsPath, 'SOUL.md'), '# 自定义人格', 'utf-8');
|
||||
writeFileSync(
|
||||
join(wsPath, 'MEMORY.md'),
|
||||
'# MEMORY.md\n> 创建时间: x\n> 最后更新: y\n> 工作空间: z\n',
|
||||
'utf-8',
|
||||
);
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
const info = svc.initialize();
|
||||
expect(info.missingFiles).toEqual([]);
|
||||
expect(svc.getFiles().soul).toBe('# 自定义人格');
|
||||
});
|
||||
|
||||
it('MEMORY.md 模板包含元数据头与核心分区', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
expect(content).toContain('# MEMORY.md');
|
||||
expect(content).toContain('> 创建时间:');
|
||||
expect(content).toContain('> 最后更新:');
|
||||
expect(content).toContain('> 工作空间:');
|
||||
for (const section of [
|
||||
'## 用户偏好',
|
||||
'## 项目上下文',
|
||||
'## 重要决策',
|
||||
'## 待办事项',
|
||||
'## 已知问题',
|
||||
]) {
|
||||
expect(content).toContain(section);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('WorkspaceService — appendMemory', () => {
|
||||
it('追加到指定 section 末尾(不侵入下一分区)', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
|
||||
svc.appendMemory('用户偏好', '偏好深色主题');
|
||||
svc.appendMemory('用户偏好', '偏好简洁回复');
|
||||
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
const sectionStart = content.indexOf('## 用户偏好');
|
||||
const sectionEnd = content.indexOf('## 项目上下文');
|
||||
const section = content.slice(sectionStart, sectionEnd);
|
||||
expect(section).toContain('- 偏好深色主题');
|
||||
expect(section).toContain('- 偏好简洁回复');
|
||||
});
|
||||
|
||||
it('未知 section 追加到文件末尾并自动创建分区头', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
|
||||
svc.appendMemory('自定义分区', '条目内容');
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
expect(content).toContain('## 自定义分区');
|
||||
expect(content).toContain('- 条目内容');
|
||||
});
|
||||
});
|
||||
|
||||
describe('WorkspaceService — validateMemoryFormat 自动修正', () => {
|
||||
it('缺失元数据头与核心分区被自动补齐,内容不丢失', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
mkdirSync(wsPath, { recursive: true });
|
||||
writeFileSync(join(wsPath, 'MEMORY.md'), '# MEMORY.md\n- 用户留下的重要内容', 'utf-8');
|
||||
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
expect(svc.validateMemoryFormat()).toBe(false); // false = 有修正
|
||||
|
||||
const content = readFileSync(join(wsPath, 'MEMORY.md'), 'utf-8');
|
||||
expect(content).toContain('> 创建时间:');
|
||||
expect(content).toContain('> 工作空间:');
|
||||
for (const section of ['## 用户偏好', '## 项目上下文', '## 重要决策']) {
|
||||
expect(content).toContain(section);
|
||||
}
|
||||
// 原内容保留
|
||||
expect(content).toContain('- 用户留下的重要内容');
|
||||
});
|
||||
|
||||
it('格式完整时返回 true 且不触发写盘', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
expect(svc.validateMemoryFormat()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('WorkspaceService — 时间戳与 reload', () => {
|
||||
it('updateMemoryTimestamp 更新"最后更新"行', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
|
||||
svc.updateMemoryTimestamp();
|
||||
const content = svc.getFiles().memory;
|
||||
expect(content).toMatch(/> 最后更新: \d{4}-\d{2}-\d{2}T/);
|
||||
});
|
||||
|
||||
it('reload 同步外部编辑(用户手动改文件后)', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
|
||||
writeFileSync(join(wsPath, 'SOUL.md'), '# 外部修改后的人格', 'utf-8');
|
||||
const files = svc.reload();
|
||||
expect(files.soul).toBe('# 外部修改后的人格');
|
||||
});
|
||||
|
||||
it('getFiles 返回副本(外部修改引用不影响内部状态)', () => {
|
||||
const wsPath = join(wsRoot, 'ws');
|
||||
const svc = new WorkspaceService(wsPath);
|
||||
svc.initialize();
|
||||
const files = svc.getFiles();
|
||||
files.soul = 'tampered';
|
||||
expect(svc.getFiles().soul).not.toBe('tampered');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user