test: 修复所有测试适配 PathEntry,全部通过

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-27 13:59:40 +08:00
parent d6e535aa98
commit 6d711d0f8e
2 changed files with 21 additions and 27 deletions
+1 -1
View File
@@ -234,7 +234,7 @@ describe('savePaths', () => {
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
resetStore(); resetStore();
useAppStore.setState({ sysPaths: ['A'], userPaths: ['B'] }); useAppStore.setState({ sysPaths: [pe('A')], userPaths: [pe('B')] });
}); });
it('保存成功', async () => { it('保存成功', async () => {
+20 -26
View File
@@ -22,10 +22,7 @@ const sampleData = {
describe('exportToJson', () => { describe('exportToJson', () => {
it('导出结构化 JSON', () => { it('导出结构化 JSON', () => {
const json = exportToJson({ const json = exportToJson(sampleData);
system: sampleData.system.map(e => e.path),
user: sampleData.user.map(e => e.path),
});
const parsed = JSON.parse(json); const parsed = JSON.parse(json);
expect(parsed.version).toBe('1.0'); expect(parsed.version).toBe('1.0');
expect(parsed.type).toBe('PathEditor'); expect(parsed.type).toBe('PathEditor');
@@ -42,23 +39,20 @@ describe('importFromJson', () => {
user: sampleData.user.map(e => e.path), user: sampleData.user.map(e => e.path),
}); });
const result = importFromJson(json); const result = importFromJson(json);
expect(result.system).toEqual(sampleData.system.map(e => e.path)); expect(result.system).toEqual(sampleData.system);
expect(result.user).toEqual(sampleData.user.map(e => e.path)); expect(result.user).toEqual(sampleData.user);
}); });
it('过滤空字符串', () => { it('过滤空字符串', () => {
const json = JSON.stringify({ system: ['C:\\', '', ' '], user: [] }); const json = JSON.stringify({ system: ['C:\\', '', ' '], user: [] });
const result = importFromJson(json); const result = importFromJson(json);
expect(result.system).toEqual(['C:\\']); expect(result.system).toEqual([pe('C:\\')]);
}); });
}); });
describe('exportToCsv', () => { describe('exportToCsv', () => {
it('导出 CSV 含 BOM', () => { it('导出 CSV 含 BOM', () => {
const csv = exportToCsv({ const csv = exportToCsv(sampleData);
system: sampleData.system.map(e => e.path),
user: sampleData.user.map(e => e.path),
});
expect(csv.startsWith('')).toBe(true); expect(csv.startsWith('')).toBe(true);
expect(csv).toContain('type,path'); expect(csv).toContain('type,path');
expect(csv).toContain('system,C:\\Windows'); expect(csv).toContain('system,C:\\Windows');
@@ -66,13 +60,13 @@ describe('exportToCsv', () => {
}); });
it('CSV 字段转义', () => { it('CSV 字段转义', () => {
const data = { system: ['C:\\Path,with,commas'], user: [] }; const data = { system: [pe('C:\\Path,with,commas')], user: [] };
const csv = exportToCsv(data); const csv = exportToCsv(data);
expect(csv).toContain('"C:\\Path,with,commas"'); expect(csv).toContain('"C:\\Path,with,commas"');
}); });
it('CSV 双引号转义', () => { it('CSV 双引号转义', () => {
const data = { system: ['Path with "quotes"'], user: [] }; const data = { system: [pe('Path with "quotes"')], user: [] };
const csv = exportToCsv(data); const csv = exportToCsv(data);
expect(csv).toContain('"Path with ""quotes"""'); expect(csv).toContain('"Path with ""quotes"""');
}); });
@@ -82,8 +76,8 @@ describe('importFromCsv', () => {
it('正确导入 CSV', () => { it('正确导入 CSV', () => {
const csv = 'type,path\nsystem,C:\\Windows\nuser,C:\\AppData\n'; const csv = 'type,path\nsystem,C:\\Windows\nuser,C:\\AppData\n';
const result = importFromCsv(csv); const result = importFromCsv(csv);
expect(result.system).toEqual(['C:\\Windows']); expect(result.system).toEqual([pe('C:\\Windows')]);
expect(result.user).toEqual(['C:\\AppData']); expect(result.user).toEqual([pe('C:\\AppData')]);
}); });
it('跳过未知类型', () => { it('跳过未知类型', () => {
@@ -96,7 +90,7 @@ describe('importFromCsv', () => {
it('处理带引号的 CSV 字段', () => { it('处理带引号的 CSV 字段', () => {
const csv = 'type,path\nsystem,"C:\\Path,With,Commas"'; const csv = 'type,path\nsystem,"C:\\Path,With,Commas"';
const result = importFromCsv(csv); const result = importFromCsv(csv);
expect(result.system).toEqual(['C:\\Path,With,Commas']); expect(result.system).toEqual([pe('C:\\Path,With,Commas')]);
}); });
}); });
@@ -104,13 +98,13 @@ describe('importFromTxt', () => {
it('逐行导入,跳过注释和空行', () => { it('逐行导入,跳过注释和空行', () => {
const txt = '# 这是注释\nC:\\Windows\n\nD:\\Projects\n# 另一个注释'; const txt = '# 这是注释\nC:\\Windows\n\nD:\\Projects\n# 另一个注释';
const paths = importFromTxt(txt); const paths = importFromTxt(txt);
expect(paths).toEqual(['C:\\Windows', 'D:\\Projects']); expect(paths).toEqual([pe('C:\\Windows'), pe('D:\\Projects')]);
}); });
it('跳过 BOM', () => { it('跳过 BOM', () => {
const txt = 'C:\\Windows'; const txt = 'C:\\Windows';
const paths = importFromTxt(txt); const paths = importFromTxt(txt);
expect(paths).toEqual(['C:\\Windows']); expect(paths).toEqual([pe('C:\\Windows')]);
}); });
}); });
@@ -120,9 +114,9 @@ describe('importFromContent', () => {
const jsonContent = JSON.stringify({ system: ['C:\\Test'], user: [] }); const jsonContent = JSON.stringify({ system: ['C:\\Test'], user: [] });
const txtContent = 'C:\\Test'; const txtContent = 'C:\\Test';
expect(importFromContent(csvContent, 'test.csv').system).toEqual(['C:\\Test']); expect(importFromContent(csvContent, 'test.csv').system).toEqual([pe('C:\\Test')]);
expect(importFromContent(jsonContent, 'test.json').system).toEqual(['C:\\Test']); expect(importFromContent(jsonContent, 'test.json').system).toEqual([pe('C:\\Test')]);
expect(importFromContent(txtContent, 'test.txt').system).toEqual(['C:\\Test']); expect(importFromContent(txtContent, 'test.txt').system).toEqual([pe('C:\\Test')]);
}); });
}); });
@@ -139,23 +133,23 @@ describe('detectExportFormat', () => {
}); });
describe('flattenImportResult', () => { describe('flattenImportResult', () => {
const data = { system: ['S1'], user: ['U1'] }; const data = { system: [pe('S1')], user: [pe('U1')] };
it('仅系统', () => { it('仅系统', () => {
const r = flattenImportResult(data, 'system'); const r = flattenImportResult(data, 'system');
expect(r.system).toEqual(['S1']); expect(r.system).toEqual([pe('S1')]);
expect(r.user).toEqual([]); expect(r.user).toEqual([]);
}); });
it('仅用户', () => { it('仅用户', () => {
const r = flattenImportResult(data, 'user'); const r = flattenImportResult(data, 'user');
expect(r.system).toEqual([]); expect(r.system).toEqual([]);
expect(r.user).toEqual(['U1']); expect(r.user).toEqual([pe('U1')]);
}); });
it('两者都导入', () => { it('两者都导入', () => {
const r = flattenImportResult(data, 'both'); const r = flattenImportResult(data, 'both');
expect(r.system).toEqual(['S1']); expect(r.system).toEqual([pe('S1')]);
expect(r.user).toEqual(['U1']); expect(r.user).toEqual([pe('U1')]);
}); });
}); });