Files
metona-ollama-desktop/tests/crypto.test.ts
T
thzxx b66945c8a7
CI / verify (push) Successful in 1m2s
v0.17.1: 退出释放显存修正 + 备份完整性 + 核心逻辑测试补课 + 上下文逻辑收敛 + 记忆日志可读性
修复:
- 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
2026-08-26 15:02:47 +08:00

46 lines
1.8 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { encryptData, decryptData } from '../src/renderer/services/crypto.js';
describe('crypto — AES-256-GCM 备份编码', () => {
it('加密数据生成带 MAGIC 标志的 Blob', async () => {
const blob = await encryptData({ hello: 'world' });
// 读 MAGIC 前 8 字节 = METONA1\0
const magic = new Uint8Array(await blob.slice(0, 8).arrayBuffer());
const expected = new TextEncoder().encode('METONA1\0');
expect(Array.from(magic)).toEqual(Array.from(expected));
});
it('加密解密往返保持一致(对象)', async () => {
const original = { a: 1, b: 'text', c: [true, false, null] };
const blob = await encryptData(original);
const buf = await blob.arrayBuffer();
const out = await decryptData(buf);
expect(out).toEqual(original);
});
it('加密解密往返保持一致(数组)', async () => {
const original = ['one', 'two', { three: 3 }];
const blob = await encryptData(original);
const out = await decryptData(await blob.arrayBuffer());
expect(out).toEqual(original);
});
it('每次加密生成不同输出(随机 salt/iv)', async () => {
const blob1 = await encryptData({ k: 'v' });
const blob2 = await encryptData({ k: 'v' });
const b1 = new Uint8Array(await blob1.arrayBuffer());
const b2 = new Uint8Array(await blob2.arrayBuffer());
expect(b1).not.toEqual(b2);
});
it('解密非 .metona 文件抛出错误', async () => {
const garbage = new TextEncoder().encode('NOTAMETONAFILE').buffer;
await expect(decryptData(garbage)).rejects.toThrow('不是有效的');
});
it('空对象往返', async () => {
const blob = await encryptData({});
expect(await decryptData(await blob.arrayBuffer())).toEqual({});
});
});