mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-06-29 01:45:54 +08:00
cbf99f12fd
安全修复 (CRITICAL): - 启用 CSP (default-src 'self') - read_text_file 限制文件扩展名白名单 (.json/.csv/.txt) - capabilities 显式声明窗口权限 - profile 名校验增强 (null 字节/控制字符/长度限制) 功能修复 (HIGH): - AnalyzeDialog 重新打开时正确刷新数据 - UndoRedoButtons 订阅路径长度变化确保响应性 - 禁用状态持久化错误处理 (.catch → console.warn) - 硬编码中文全部迁移到 i18n (6 处) - PATH 长度检查改用 UTF-16 字符计数 - PATH 写入前 null 字节校验 - CLI export 拒绝写入系统目录 - savePaths 职责分离: window.confirm → Tauri ask() 对话框 代码质量 (MEDIUM): - 导入路径统一过滤 (sanitize_paths: null 字节/分号/空白) - 原子写入 (atomic_write: disabled.json + profiles) - 验证缓存自动清理 (PathTable useEffect) - Scanner 线程错误处理改进 (.unwrap → .map_err) - Ctrl+F 去重 (移除 use-keyboard 重复处理) - Profile 路径列表 key 修复 (index → path) - 生产构建启用日志插件 (Warn 级别) - export_paths JSON 序列化改 expect 测试: - Rust: 35 → 48 测试 (+13) - Frontend: 80 → 85 测试 (+5) - Vitest 全局 jsdom + 覆盖率阈值 (80%) - 安装 @vitest/coverage-v8 + test:coverage 脚本 - 移除未使用的 @testing-library/jest-dom 工程化: - CI 添加 Cargo 缓存 (Swatinem/rust-cache@v2) - CI 添加 cargo fmt --check - tsconfig.test.json 覆盖测试文件类型检查 - cargo fmt 全量格式化 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
203 lines
6.3 KiB
TypeScript
203 lines
6.3 KiB
TypeScript
import { describe, it, expect, beforeEach } from 'vitest';
|
||
import { UndoRedoManager, OperationType, TargetType, type OpRecord } from '../../src/core/undo-redo';
|
||
import type { PathEntry } from '../../src/core/path-entry';
|
||
|
||
function pe(s: string, enabled: boolean = true): PathEntry {
|
||
return { path: s, enabled };
|
||
}
|
||
|
||
function makeRecord(type: OperationType, target: TargetType, index: number, count: number, oldPaths: PathEntry[], newPaths: PathEntry[]): OpRecord {
|
||
return { type, target, index, count, oldPaths, newPaths };
|
||
}
|
||
|
||
describe('UndoRedoManager', () => {
|
||
let mgr: UndoRedoManager;
|
||
let sys: PathEntry[];
|
||
let user: PathEntry[];
|
||
|
||
beforeEach(() => {
|
||
mgr = new UndoRedoManager(50);
|
||
sys = [pe('C:\\Windows'), pe('C:\\Program Files')];
|
||
user = [pe('C:\\Users\\me\\AppData')];
|
||
});
|
||
|
||
it('初始不可撤销不可重做', () => {
|
||
expect(mgr.canUndo()).toBe(false);
|
||
expect(mgr.canRedo()).toBe(false);
|
||
});
|
||
|
||
it('ADD 撤销/重做', () => {
|
||
sys.push(pe('C:\\NewPath'));
|
||
mgr.push(makeRecord(OperationType.ADD, TargetType.SYSTEM, 2, 1, [], [pe('C:\\NewPath')]));
|
||
|
||
const u = mgr.undo(sys, user)!;
|
||
expect(u[0].map(e => e.path)).toEqual(['C:\\Windows', 'C:\\Program Files']);
|
||
|
||
const r = mgr.redo(...u)!;
|
||
expect(r[0].map(e => e.path)).toEqual(['C:\\Windows', 'C:\\Program Files', 'C:\\NewPath']);
|
||
});
|
||
|
||
it('DELETE 撤销/重做', () => {
|
||
const removed = sys[0];
|
||
mgr.push(makeRecord(OperationType.DELETE, TargetType.SYSTEM, 0, 1, [removed], []));
|
||
sys.splice(0, 1);
|
||
|
||
const u = mgr.undo(sys, user)!;
|
||
expect(u[0][0].path).toBe(removed.path);
|
||
|
||
const r = mgr.redo(...u)!;
|
||
expect(r[0].map(e => e.path)).toEqual(['C:\\Program Files']);
|
||
});
|
||
|
||
it('EDIT 撤销/重做', () => {
|
||
mgr.push(makeRecord(OperationType.EDIT, TargetType.SYSTEM, 0, 1, [pe('C:\\Windows')], [pe('C:\\Edited')]));
|
||
sys[0] = pe('C:\\Edited');
|
||
|
||
const u = mgr.undo(sys, user)!;
|
||
expect(u[0][0].path).toBe('C:\\Windows');
|
||
|
||
const r = mgr.redo(...u)!;
|
||
expect(r[0][0].path).toBe('C:\\Edited');
|
||
});
|
||
|
||
it('MOVE_UP 撤销/重做', () => {
|
||
mgr.push(makeRecord(OperationType.MOVE_UP, TargetType.SYSTEM, 1, 1, [], []));
|
||
[sys[0], sys[1]] = [sys[1], sys[0]];
|
||
|
||
const u = mgr.undo(sys, user)!;
|
||
expect(u[0].map(e => e.path)).toEqual(['C:\\Windows', 'C:\\Program Files']);
|
||
|
||
const r = mgr.redo(...u)!;
|
||
expect(r[0].map(e => e.path)).toEqual(['C:\\Program Files', 'C:\\Windows']);
|
||
});
|
||
|
||
it('MOVE_DOWN 撤销/重做', () => {
|
||
mgr.push(makeRecord(OperationType.MOVE_DOWN, TargetType.SYSTEM, 0, 1, [], []));
|
||
[sys[0], sys[1]] = [sys[1], sys[0]];
|
||
|
||
const u = mgr.undo(sys, user)!;
|
||
expect(u[0].map(e => e.path)).toEqual(['C:\\Windows', 'C:\\Program Files']);
|
||
});
|
||
|
||
it('CLEAN 撤销/重做', () => {
|
||
const old = [...sys];
|
||
const cleaned = [pe('C:\\Windows')];
|
||
mgr.push(makeRecord(OperationType.CLEAN, TargetType.SYSTEM, 0, 2, old, cleaned));
|
||
sys = cleaned;
|
||
|
||
const u = mgr.undo(sys, user)!;
|
||
expect(u[0]).toEqual(old);
|
||
|
||
const r = mgr.redo(...u)!;
|
||
expect(r[0]).toEqual(cleaned);
|
||
});
|
||
|
||
it('CLEAR 撤销/重做', () => {
|
||
const old = [...sys];
|
||
mgr.push(makeRecord(OperationType.CLEAR, TargetType.SYSTEM, 0, 2, old, []));
|
||
sys = [];
|
||
|
||
const u = mgr.undo(sys, user)!;
|
||
expect(u[0]).toEqual(old);
|
||
|
||
const r = mgr.redo(...u)!;
|
||
expect(r[0]).toEqual([]);
|
||
});
|
||
|
||
it('IMPORT 撤销/重做', () => {
|
||
const old = [...sys];
|
||
const imported = [pe('C:\\New1'), pe('C:\\New2')];
|
||
mgr.push(makeRecord(OperationType.IMPORT, TargetType.SYSTEM, 0, 2, old, imported));
|
||
sys = imported;
|
||
|
||
const u = mgr.undo(sys, user)!;
|
||
expect(u[0]).toEqual(old);
|
||
|
||
const r = mgr.redo(...u)!;
|
||
expect(r[0]).toEqual(imported);
|
||
});
|
||
|
||
it('新操作后截断重做分支', () => {
|
||
mgr.push(makeRecord(OperationType.ADD, TargetType.SYSTEM, 0, 1, [], [pe('first')]));
|
||
mgr.undo(sys, user);
|
||
expect(mgr.canRedo()).toBe(true);
|
||
mgr.push(makeRecord(OperationType.ADD, TargetType.SYSTEM, 0, 1, [], [pe('second')]));
|
||
expect(mgr.canRedo()).toBe(false);
|
||
});
|
||
|
||
it('超出最大历史容量时移除最旧记录', () => {
|
||
const small = new UndoRedoManager(3);
|
||
for (let i = 0; i < 5; i++) {
|
||
small.push(makeRecord(OperationType.ADD, TargetType.SYSTEM, 0, 1, [], [pe(`path_${i}`)]));
|
||
}
|
||
expect(small.historyLength).toBe(3);
|
||
});
|
||
|
||
it('非连续多选 DELETE 撤销恢复到原始位置', () => {
|
||
// 扩展初始数组
|
||
sys.push(pe('C:\\Extra1'), pe('C:\\Extra2'));
|
||
const old = [...sys];
|
||
// 删除 indices [1, 3](C:\Program Files 和 C:\Extra2)
|
||
const removed = [sys[1], sys[3]];
|
||
mgr.push({
|
||
type: OperationType.DELETE, target: TargetType.SYSTEM,
|
||
index: 1, count: 2,
|
||
oldPaths: removed, newPaths: [],
|
||
indices: [1, 3],
|
||
});
|
||
sys.splice(3, 1);
|
||
sys.splice(1, 1);
|
||
|
||
const u = mgr.undo(sys, user)!;
|
||
expect(u[0]).toEqual(old);
|
||
|
||
const r = mgr.redo(...u)!;
|
||
expect(r[0].map(e => e.path)).toEqual(['C:\\Windows', 'C:\\Extra1']);
|
||
});
|
||
|
||
it('操作 USER 路径', () => {
|
||
user.push(pe('C:\\NewUserPath'));
|
||
mgr.push(makeRecord(OperationType.ADD, TargetType.USER, 1, 1, [], [pe('C:\\NewUserPath')]));
|
||
const u = mgr.undo(sys, user)!;
|
||
expect(u[1].map(e => e.path)).toEqual(['C:\\Users\\me\\AppData']);
|
||
expect(u[0].map(e => e.path)).toEqual(['C:\\Windows', 'C:\\Program Files']);
|
||
});
|
||
|
||
it('TOGGLE 撤销/重做', () => {
|
||
sys[0] = pe('C:\\Windows', false);
|
||
mgr.push(makeRecord(OperationType.TOGGLE, TargetType.SYSTEM, 0, 1,
|
||
[pe('C:\\Windows', true)], [pe('C:\\Windows', false)]));
|
||
|
||
const u = mgr.undo(sys, user)!;
|
||
expect(u[0][0].enabled).toBe(true);
|
||
|
||
const r = mgr.redo(...u)!;
|
||
expect(r[0][0].enabled).toBe(false);
|
||
});
|
||
|
||
it('IMPORT_BOTH 撤销/重做(同时修改系统和用户路径)', () => {
|
||
const oldSys = [...sys];
|
||
const oldUser = [...user];
|
||
const newSys = [pe('C:\\ImportedSys')];
|
||
const newUser = [pe('C:\\ImportedUser')];
|
||
|
||
mgr.push({
|
||
type: OperationType.IMPORT_BOTH,
|
||
target: TargetType.SYSTEM,
|
||
index: 0, count: 0,
|
||
oldPaths: oldSys, newPaths: newSys,
|
||
oldPathsOther: oldUser, newPathsOther: newUser,
|
||
});
|
||
sys = newSys;
|
||
user = newUser;
|
||
|
||
const u = mgr.undo(sys, user)!;
|
||
expect(u[0]).toEqual(oldSys);
|
||
expect(u[1]).toEqual(oldUser);
|
||
|
||
const r = mgr.redo(...u)!;
|
||
expect(r[0]).toEqual(newSys);
|
||
expect(r[1]).toEqual(newUser);
|
||
});
|
||
});
|