mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-06-29 01:45:54 +08:00
refactor: 全面代码质量提升 — StringList→string[], strict 模式, 死代码清理
架构重构: - 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>
This commit is contained in:
@@ -1,100 +1,31 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import {
|
||||
pathRemoveAt,
|
||||
pathMoveUp,
|
||||
pathMoveDown,
|
||||
pathClean,
|
||||
batchRemoveAt,
|
||||
} from '../../src/core/path-manager';
|
||||
import { StringList } from '../../src/core/string-list';
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { pathClean } from '../../src/core/path-manager';
|
||||
|
||||
// 模拟验证函数:所有路径都"有效"
|
||||
const alwaysValid = () => true;
|
||||
|
||||
// 模拟验证函数:C:\\Invalid 无效
|
||||
const validateFn = (path: string) => !path.includes('Invalid');
|
||||
|
||||
describe('pathRemoveAt', () => {
|
||||
it('删除指定索引', () => {
|
||||
const list = StringList.fromArray(['a', 'b', 'c']);
|
||||
pathRemoveAt(list, 1);
|
||||
expect(list.toArray()).toEqual(['a', 'c']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pathMoveUp', () => {
|
||||
it('上移元素', () => {
|
||||
const list = StringList.fromArray(['a', 'b', 'c']);
|
||||
pathMoveUp(list, 1);
|
||||
expect(list.toArray()).toEqual(['b', 'a', 'c']);
|
||||
});
|
||||
|
||||
it('第一个元素不能上移', () => {
|
||||
const list = StringList.fromArray(['a', 'b']);
|
||||
expect(pathMoveUp(list, 0)).toBe(false);
|
||||
expect(list.toArray()).toEqual(['a', 'b']);
|
||||
});
|
||||
|
||||
it('无效索引不能上移', () => {
|
||||
const list = StringList.fromArray(['a']);
|
||||
expect(pathMoveUp(list, -1)).toBe(false);
|
||||
expect(pathMoveUp(list, 5)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pathMoveDown', () => {
|
||||
it('下移元素', () => {
|
||||
const list = StringList.fromArray(['a', 'b', 'c']);
|
||||
pathMoveDown(list, 0);
|
||||
expect(list.toArray()).toEqual(['b', 'a', 'c']);
|
||||
});
|
||||
|
||||
it('最后一个元素不能下移', () => {
|
||||
const list = StringList.fromArray(['a', 'b']);
|
||||
expect(pathMoveDown(list, 1)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('batchRemoveAt', () => {
|
||||
it('批量删除(按从大到小排序)', () => {
|
||||
const list = StringList.fromArray(['a', 'b', 'c', 'd', 'e']);
|
||||
batchRemoveAt(list, [0, 2, 4]);
|
||||
expect(list.toArray()).toEqual(['b', 'd']);
|
||||
});
|
||||
|
||||
it('删除乱序索引', () => {
|
||||
const list = StringList.fromArray(['a', 'b', 'c', 'd']);
|
||||
batchRemoveAt(list, [3, 0]);
|
||||
expect(list.toArray()).toEqual(['b', 'c']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pathClean', () => {
|
||||
it('移除无效路径', () => {
|
||||
const list = StringList.fromArray(['C:\\Valid', 'C:\\Invalid', 'D:\\Valid']);
|
||||
const removed = pathClean(list, validateFn);
|
||||
expect(list.toArray()).toEqual(['C:\\Valid', 'D:\\Valid']);
|
||||
const [kept, removed] = pathClean(['C:\\Valid', 'C:\\Invalid', 'D:\\Valid'], validateFn);
|
||||
expect(kept).toEqual(['C:\\Valid', 'D:\\Valid']);
|
||||
expect(removed).toEqual(['C:\\Invalid']);
|
||||
});
|
||||
|
||||
it('移除重复路径(保留一个)', () => {
|
||||
const list = StringList.fromArray(['C:\\Valid', 'C:\\Valid', 'D:\\Valid']);
|
||||
const removed = pathClean(list, alwaysValid);
|
||||
expect(list.length).toBe(2);
|
||||
expect(removed.length).toBeGreaterThanOrEqual(1);
|
||||
it('移除重复路径保留第一个', () => {
|
||||
const [kept, removed] = pathClean(['C:\\Valid', 'C:\\Valid', 'D:\\Valid'], alwaysValid);
|
||||
expect(kept.length).toBe(2);
|
||||
expect(removed.length).toBe(1);
|
||||
});
|
||||
|
||||
it('全部有效无变化', () => {
|
||||
const list = StringList.fromArray(['C:\\a', 'D:\\b']);
|
||||
const removed = pathClean(list, alwaysValid);
|
||||
expect(list.toArray()).toEqual(['C:\\a', 'D:\\b']);
|
||||
const [kept, removed] = pathClean(['C:\\a', 'D:\\b'], alwaysValid);
|
||||
expect(kept).toEqual(['C:\\a', 'D:\\b']);
|
||||
expect(removed.length).toBe(0);
|
||||
});
|
||||
|
||||
it('全部无效全部移除', () => {
|
||||
const list = StringList.fromArray(['C:\\Invalid1', 'C:\\Invalid2']);
|
||||
const removed = pathClean(list, validateFn);
|
||||
expect(list.length).toBe(0);
|
||||
const [kept, removed] = pathClean(['C:\\Invalid1', 'C:\\Invalid2'], validateFn);
|
||||
expect(kept.length).toBe(0);
|
||||
expect(removed.length).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user