mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-06-28 17:25:54 +08:00
8c0e80d862
新增配置文件: - .editorconfig — 跨编辑器代码风格统一 - .gitattributes — 行尾符 CRLF 规范化 - .prettierrc + .prettierignore — 前端代码格式化 - .markdownlint.json — Markdown 格式规范 - commitlint.config.js — Conventional Commits 强制校验 新增 GitHub 社区文件: - .github/dependabot.yml — 依赖自动更新 (npm + Cargo + Actions) - .github/CODEOWNERS — 自动 PR 审查分配 - .github/FUNDING.yml — 开源赞助入口 新增文档: - ROADMAP.md — v5.1/v5.2/v6.0 路线图 - SUPPORT.md — 帮助与支持指南 - docs/screenshots/ — 截图目录就位 新增 Git Hooks: - .husky/pre-commit — lint-staged 自动格式化+修复 - .husky/commit-msg — commitlint 校验提交消息 CI 强化 (.github/workflows/ci.yml): - 新增 Prettier 格式检查步骤 - 新增 cargo fmt --check 步骤 - 新增 Vitest 覆盖率生成 + Codecov 上报 修复: - index.html 标题 v4.0 → v5.0 - PathEditDialog set-state-in-effect 改用 useRef prevOpen 守卫 - use-app-actions.test.tsx 缺失 @vitest-environment jsdom - 所有 TS/TSX 文件 Prettier 格式化统一 配置更新: - vitest.config.ts — v8 覆盖率 + 阈值门禁 (60%/70%) - package.json — format/format:check/test:coverage/prepare 脚本 + lint-staged - .gitignore — 新增 coverage/sync-conflict/playwright-report - README.md — 新增 coverage + platform 徽章 + 截图区域
296 lines
10 KiB
TypeScript
296 lines
10 KiB
TypeScript
/** @vitest-environment jsdom */
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
// ── Mock 外部依赖 ──
|
|
|
|
vi.mock('@tauri-apps/api/core', () => ({
|
|
invoke: vi.fn().mockResolvedValue(undefined),
|
|
}));
|
|
|
|
const mockOpen = vi.fn().mockResolvedValue(null);
|
|
const mockAsk = vi.fn().mockResolvedValue(true);
|
|
vi.mock('@tauri-apps/plugin-dialog', () => ({
|
|
open: (...args: unknown[]) => mockOpen(...args),
|
|
ask: (...args: unknown[]) => mockAsk(...args),
|
|
}));
|
|
|
|
vi.mock('@/i18n', () => ({
|
|
default: {
|
|
t: vi.fn((key: string, opts?: Record<string, unknown>) => {
|
|
if (key === 'status.deleted') return `已删除 ${opts?.count} 条`;
|
|
if (key === 'status.saveWarningLongPaths') return 'PATH 长度超限';
|
|
return key;
|
|
}),
|
|
},
|
|
}));
|
|
|
|
vi.mock('@/hooks/use-keyboard', () => ({
|
|
useKeyboard: vi.fn(),
|
|
}));
|
|
|
|
import { renderHook, act } from '@testing-library/react';
|
|
import { useAppStore } from '@/store/app-store';
|
|
import { UndoRedoManager, TargetType } from '@/core/undo-redo';
|
|
import { invoke } from '@tauri-apps/api/core';
|
|
import type { PathEntry } from '@/core/path-entry';
|
|
|
|
const mockedInvoke = vi.mocked(invoke);
|
|
|
|
function pe(s: string, enabled = true): PathEntry {
|
|
return { path: s, enabled };
|
|
}
|
|
|
|
function resetStore(sys: PathEntry[] = [], user: PathEntry[] = []) {
|
|
useAppStore.setState({
|
|
sysPaths: sys,
|
|
userPaths: user,
|
|
undoRedo: new UndoRedoManager(50),
|
|
_savedSys: sys,
|
|
_savedUser: user,
|
|
selectedIndices: [],
|
|
isModified: false,
|
|
isLoading: false,
|
|
isSaving: false,
|
|
isAdmin: true,
|
|
statusMessage: '',
|
|
});
|
|
}
|
|
|
|
// useAppActions 需要 DialogState,创建一个 mock
|
|
function mockDialogs() {
|
|
return {
|
|
editDialog: { open: false, index: -1, value: '', target: TargetType.SYSTEM },
|
|
newDialog: false,
|
|
helpOpen: false,
|
|
importDialog: { open: false, system: [] as PathEntry[], user: [] as PathEntry[] },
|
|
setEditDialog: vi.fn(),
|
|
setNewDialog: vi.fn(),
|
|
setHelpOpen: vi.fn(),
|
|
setImportDialog: vi.fn(),
|
|
setAnalyzeOpen: vi.fn(),
|
|
setProfilesOpen: vi.fn(),
|
|
};
|
|
}
|
|
|
|
describe('useAppActions', () => {
|
|
let dialogs: ReturnType<typeof mockDialogs>;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
resetStore([pe('C:\\Windows'), pe('C:\\Program Files')], [pe('D:\\User')]);
|
|
dialogs = mockDialogs();
|
|
});
|
|
|
|
// ── handleNew ──
|
|
|
|
it('handleNew 打开新建弹窗', async () => {
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
act(() => {
|
|
result.current.handleNew();
|
|
});
|
|
expect(dialogs.setNewDialog).toHaveBeenCalledWith(true);
|
|
});
|
|
|
|
// ── handleEdit ──
|
|
|
|
it('handleEdit 打开编辑弹窗(有选中项)', async () => {
|
|
useAppStore.setState({ selectedIndices: [0] });
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
act(() => {
|
|
result.current.handleEdit();
|
|
});
|
|
expect(dialogs.setEditDialog).toHaveBeenCalledWith({
|
|
open: true,
|
|
index: 0,
|
|
value: 'C:\\Windows',
|
|
target: TargetType.SYSTEM,
|
|
});
|
|
});
|
|
|
|
it('handleEdit 无选中项不操作', async () => {
|
|
useAppStore.setState({ selectedIndices: [] });
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
act(() => {
|
|
result.current.handleEdit();
|
|
});
|
|
expect(dialogs.setEditDialog).not.toHaveBeenCalled();
|
|
});
|
|
|
|
// ── handleDelete ──
|
|
|
|
it('handleDelete 删除选中项', async () => {
|
|
useAppStore.setState({ selectedIndices: [0] });
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
act(() => {
|
|
result.current.handleDelete();
|
|
});
|
|
expect(useAppStore.getState().sysPaths.map((e) => e.path)).toEqual(['C:\\Program Files']);
|
|
});
|
|
|
|
it('handleDelete 无选中项不操作', async () => {
|
|
useAppStore.setState({ selectedIndices: [] });
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
act(() => {
|
|
result.current.handleDelete();
|
|
});
|
|
expect(useAppStore.getState().sysPaths.length).toBe(2);
|
|
});
|
|
|
|
// ── handleMoveUp / handleMoveDown ──
|
|
|
|
it('handleMoveUp 上移选中项', async () => {
|
|
useAppStore.setState({ selectedIndices: [1] });
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
act(() => {
|
|
result.current.handleMoveUp();
|
|
});
|
|
expect(useAppStore.getState().sysPaths.map((e) => e.path)).toEqual([
|
|
'C:\\Program Files',
|
|
'C:\\Windows',
|
|
]);
|
|
});
|
|
|
|
it('handleMoveDown 下移选中项', async () => {
|
|
useAppStore.setState({ selectedIndices: [0] });
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
act(() => {
|
|
result.current.handleMoveDown();
|
|
});
|
|
expect(useAppStore.getState().sysPaths.map((e) => e.path)).toEqual([
|
|
'C:\\Program Files',
|
|
'C:\\Windows',
|
|
]);
|
|
});
|
|
|
|
// ── handleClean ──
|
|
|
|
it('handleClean 清理无效路径', async () => {
|
|
resetStore([pe('C:\\Windows'), pe('invalid_path!@#')]);
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
act(() => {
|
|
result.current.handleClean();
|
|
});
|
|
expect(useAppStore.getState().sysPaths.map((e) => e.path)).toEqual(['C:\\Windows']);
|
|
expect(useAppStore.getState().statusMessage).toContain('已删除 1 条');
|
|
});
|
|
|
|
// ── handleNewConfirm ──
|
|
|
|
it('handleNewConfirm 添加新路径', async () => {
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
act(() => {
|
|
result.current.handleNewConfirm('C:\\New');
|
|
});
|
|
expect(useAppStore.getState().sysPaths.map((e) => e.path)).toContain('C:\\New');
|
|
expect(dialogs.setNewDialog).toHaveBeenCalledWith(false);
|
|
});
|
|
|
|
it('handleNewConfirm 空白不添加', async () => {
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
act(() => {
|
|
result.current.handleNewConfirm(' ');
|
|
});
|
|
expect(useAppStore.getState().sysPaths.length).toBe(2);
|
|
});
|
|
|
|
// ── handleEditConfirm ──
|
|
|
|
it('handleEditConfirm 修改路径', async () => {
|
|
dialogs.editDialog = { open: true, index: 0, value: 'C:\\Windows', target: TargetType.SYSTEM };
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
act(() => {
|
|
result.current.handleEditConfirm('C:\\Edited');
|
|
});
|
|
expect(useAppStore.getState().sysPaths[0].path).toBe('C:\\Edited');
|
|
});
|
|
|
|
// ── handleImportSelect ──
|
|
|
|
it('handleImportSelect both 模式替换双 hive', async () => {
|
|
const sysImport = [pe('C:\\ImportSys')];
|
|
const usrImport = [pe('D:\\ImportUsr')];
|
|
dialogs.importDialog = { open: true, system: sysImport, user: usrImport };
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
act(() => {
|
|
result.current.handleImportSelect('both');
|
|
});
|
|
expect(useAppStore.getState().sysPaths.map((e) => e.path)).toEqual(['C:\\ImportSys']);
|
|
expect(useAppStore.getState().userPaths.map((e) => e.path)).toEqual(['D:\\ImportUsr']);
|
|
expect(dialogs.setImportDialog).toHaveBeenCalledWith({ open: false, system: [], user: [] });
|
|
});
|
|
|
|
it('handleImportSelect system 模式只替换 system', async () => {
|
|
dialogs.importDialog = {
|
|
open: true,
|
|
system: [pe('C:\\ImportSys')],
|
|
user: [pe('D:\\ImportUsr')],
|
|
};
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
act(() => {
|
|
result.current.handleImportSelect('system');
|
|
});
|
|
expect(useAppStore.getState().sysPaths.map((e) => e.path)).toEqual(['C:\\ImportSys']);
|
|
expect(useAppStore.getState().userPaths.map((e) => e.path)).toEqual(['D:\\User']); // 未变
|
|
});
|
|
|
|
// ── handleSave ──
|
|
|
|
it('handleSave 正常保存', async () => {
|
|
mockedInvoke.mockResolvedValue(undefined);
|
|
vi.spyOn(useAppStore.getState(), 'savePaths').mockResolvedValue({ kind: 'success' });
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
await act(async () => {
|
|
await result.current.handleSave();
|
|
});
|
|
// savePaths is called
|
|
expect(useAppStore.getState().savePaths).toHaveBeenCalled();
|
|
});
|
|
|
|
it('handleSave 超长确认后强制保存', async () => {
|
|
// 第一次 savePaths 返回 warning(超长)
|
|
// 第二次(force=true)返回 success
|
|
let callCount = 0;
|
|
vi.spyOn(useAppStore.getState(), 'savePaths').mockImplementation(async (force?: boolean) => {
|
|
callCount++;
|
|
if (!force) return { kind: 'warning', reason: 'lengthExceeded' }; // 第一次:超长警告
|
|
return { kind: 'success' }; // 第二次:强制保存成功
|
|
});
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
await act(async () => {
|
|
await result.current.handleSave();
|
|
});
|
|
expect(callCount).toBe(2);
|
|
expect(mockAsk).toHaveBeenCalled();
|
|
});
|
|
|
|
it('handleSave 普通失败不弹确认框', async () => {
|
|
let callCount = 0;
|
|
vi.spyOn(useAppStore.getState(), 'savePaths').mockImplementation(async () => {
|
|
callCount++;
|
|
return { kind: 'failure', message: '权限不足' };
|
|
});
|
|
const { useAppActions } = await import('@/hooks/use-app-actions');
|
|
const { result } = renderHook(() => useAppActions('system', dialogs));
|
|
await act(async () => {
|
|
await result.current.handleSave();
|
|
});
|
|
expect(callCount).toBe(1); // 仅调用一次,不重试
|
|
expect(mockAsk).not.toHaveBeenCalled();
|
|
});
|
|
});
|