mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-06-29 01:45:54 +08:00
bfd114d80f
架构重构: - StringList 类替换为不可变 string[](消除 dataVersion hack,Zustand 自然检测变化) - UndoRedoManager.undo/redo 返回新数组而非原地修改 - 删除 dataVersion 字段和 _bumpVersion() - 启用 TypeScript strict 模式 死代码清理: - 删除 string-list.ts, string-list.test.ts, use-path-validation.ts - Rust AppError 保留供未来使用 功能修复: - importFromJson 添加 try/catch - handleClean 使用真实格式验证替代 () => true - savePaths 保存前调用 backup_registry,处理部分保存失败 - importFromJson 校验非 object 类型输入 i18n 完善: - MergePreview/StatusBar 硬编码中文 → t() 调用 - 新增 merge.* 和 status.* 翻译键 Rust 改进: - registry.rs 抽取 load_paths/save_paths 通用函数,消除重复 - registry 新增 6 个单元测试(split/join/roundtrip) - backup.rs 时间戳加毫秒防覆盖,回退路径改为 home_dir 元数据: - package.json 名称→patheditor, 版本→4.0.0 - 新增 CHANGELOG.md - 移除 UndoRedoButtons 废弃注释 - tsconfig 添加 strict:true Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
139 lines
4.3 KiB
TypeScript
139 lines
4.3 KiB
TypeScript
import { describe, it, expect, beforeEach } from 'vitest';
|
|
import { UndoRedoManager, OperationType, TargetType, type OpRecord } from '../../src/core/undo-redo';
|
|
|
|
function makeRecord(type: OperationType, target: TargetType, index: number, count: number, oldPaths: string[], newPaths: string[]): OpRecord {
|
|
return { type, target, index, count, oldPaths, newPaths };
|
|
}
|
|
|
|
describe('UndoRedoManager', () => {
|
|
let mgr: UndoRedoManager;
|
|
let sys: string[];
|
|
let user: string[];
|
|
|
|
beforeEach(() => {
|
|
mgr = new UndoRedoManager(50);
|
|
sys = ['C:\\Windows', 'C:\\Program Files'];
|
|
user = ['C:\\Users\\me\\AppData'];
|
|
});
|
|
|
|
it('初始不可撤销不可重做', () => {
|
|
expect(mgr.canUndo()).toBe(false);
|
|
expect(mgr.canRedo()).toBe(false);
|
|
});
|
|
|
|
it('ADD 撤销/重做', () => {
|
|
sys.push('C:\\NewPath');
|
|
mgr.push(makeRecord(OperationType.ADD, TargetType.SYSTEM, 2, 1, [], ['C:\\NewPath']));
|
|
|
|
const u = mgr.undo(sys, user)!;
|
|
expect(u[0]).toEqual(['C:\\Windows', 'C:\\Program Files']);
|
|
|
|
const r = mgr.redo(...u)!;
|
|
expect(r[0]).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]).toBe(removed);
|
|
|
|
const r = mgr.redo(...u)!;
|
|
expect(r[0]).toEqual(['C:\\Program Files']);
|
|
});
|
|
|
|
it('EDIT 撤销/重做', () => {
|
|
mgr.push(makeRecord(OperationType.EDIT, TargetType.SYSTEM, 0, 1, ['C:\\Windows'], ['C:\\Edited']));
|
|
sys[0] = 'C:\\Edited';
|
|
|
|
const u = mgr.undo(sys, user)!;
|
|
expect(u[0][0]).toBe('C:\\Windows');
|
|
|
|
const r = mgr.redo(...u)!;
|
|
expect(r[0][0]).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]).toEqual(['C:\\Windows', 'C:\\Program Files']);
|
|
|
|
const r = mgr.redo(...u)!;
|
|
expect(r[0]).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]).toEqual(['C:\\Windows', 'C:\\Program Files']);
|
|
});
|
|
|
|
it('CLEAN 撤销/重做', () => {
|
|
const old = [...sys];
|
|
const cleaned = ['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 = ['C:\\New1', '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, [], ['first']));
|
|
mgr.undo(sys, user);
|
|
expect(mgr.canRedo()).toBe(true);
|
|
mgr.push(makeRecord(OperationType.ADD, TargetType.SYSTEM, 0, 1, [], ['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, [], [`path_${i}`]));
|
|
}
|
|
expect(small.historyLength).toBe(3);
|
|
});
|
|
|
|
it('操作 USER 路径', () => {
|
|
user.push('C:\\NewUserPath');
|
|
mgr.push(makeRecord(OperationType.ADD, TargetType.USER, 1, 1, [], ['C:\\NewUserPath']));
|
|
const u = mgr.undo(sys, user)!;
|
|
expect(u[1]).toEqual(['C:\\Users\\me\\AppData']);
|
|
expect(u[0]).toEqual(['C:\\Windows', 'C:\\Program Files']);
|
|
});
|
|
});
|