Skip to content

Commit 74d71fa

Browse files
committed
🧪 增加parseUserConfig的测试
1 parent 1874a35 commit 74d71fa

2 files changed

Lines changed: 70 additions & 26 deletions

File tree

src/pkg/utils/script.test.ts

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -301,41 +301,58 @@ describe("parseUserConfig", () => {
301301
test("解析单个YAML配置", () => {
302302
const code = `
303303
/* ==UserConfig==
304-
name: 测试配置
305-
value: 123
306-
enabled: true
304+
group1:
305+
config1:
306+
title: 测试配置
307+
default: 123
308+
enabled: true
307309
==/UserConfig== */
308310
309311
console.log('Hello World');
310312
`;
311313

312314
const result = parseUserConfig(code);
313315
expect(result).toEqual({
314-
name: "测试配置",
315-
value: 123,
316-
enabled: true,
316+
group1: {
317+
config1: {
318+
title: "测试配置",
319+
default: 123,
320+
enabled: true,
321+
index: 0,
322+
},
323+
},
317324
});
318325
});
319326

320327
test("解析多个YAML配置(用---分隔)", () => {
321328
const code = `
322329
/* ==UserConfig==
323-
name: 配置1
324-
value: 123
330+
group1:
331+
config1:
332+
title: 配置1
333+
default: 123
325334
---
326-
name: 配置2
327-
value: 456
328-
enabled: true
335+
group1:
336+
config2:
337+
title: 配置2
338+
default: 456
339+
enabled: true
329340
==/UserConfig== */
330341
331342
console.log('Hello World');
332343
`;
333344

334345
const result = parseUserConfig(code);
335346
expect(result).toEqual({
336-
name: "配置2", // 后面的配置会覆盖前面的同名配置
337-
value: 456,
338-
enabled: true,
347+
group1: {
348+
config2: {
349+
// 后面的配置会覆盖前面的同名分组
350+
title: "配置2",
351+
default: 456,
352+
enabled: true,
353+
index: 0,
354+
},
355+
},
339356
});
340357
});
341358

@@ -357,18 +374,30 @@ console.log('Hello World');
357374
expect(result).toEqual({});
358375
});
359376

360-
test("解析格式错误的YAML应该处理异常", () => {
377+
test("解析格式错误的YAML应该抛出错误", () => {
361378
const code = `
362379
/* ==UserConfig==
363380
name: 配置
364381
invalid yaml: [
365-
==UserConfig== */
382+
==/UserConfig== */
383+
384+
console.log('Hello World');
385+
`;
386+
387+
expect(() => parseUserConfig(code)).toThrow();
388+
});
389+
390+
test("不符合分组规范的YAML配置应该抛出错误", () => {
391+
const code = `
392+
/* ==UserConfig==
393+
name: 测试配置
394+
value: 123
395+
enabled: true
396+
==/UserConfig== */
366397
367398
console.log('Hello World');
368399
`;
369400

370-
// 这个测试检查函数是否能够处理YAML解析错误
371-
// 具体行为取决于YAML.parse的实现
372-
expect(() => parseUserConfig(code)).not.toThrow();
401+
expect(() => parseUserConfig(code)).toThrow('UserConfig group "name" is not a valid object.');
373402
});
374403
});

src/pkg/utils/yaml.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,31 @@ export function parseUserConfig(code: string): UserConfig | undefined {
77
if (!config) {
88
return undefined;
99
}
10+
1011
const configs = config[1].trim().split(/[-]{3,}/);
1112
const ret: UserConfig = {};
12-
configs.forEach((val) => {
13+
14+
for (const val of configs) {
1315
const obj: UserConfig = parse(val);
14-
Object.keys(obj || {}).forEach((key) => {
15-
ret[key] = obj[key];
16-
Object.keys(ret[key] || {}).forEach((subKey, subIndex) => {
17-
ret[key][subKey].index = ret[key][subKey].index || subIndex; // 确保index存在
16+
if (!obj || typeof obj !== "object") {
17+
continue;
18+
}
19+
20+
// 验证是否符合分组规范:group -> config -> properties
21+
for (const [groupKey, groupValue] of Object.entries(obj)) {
22+
if (!groupValue || typeof groupValue !== "object") {
23+
// 如果分组值不是对象,说明不符合规范
24+
throw new Error(`UserConfig group "${groupKey}" is not a valid object.`);
25+
}
26+
27+
ret[groupKey] = groupValue;
28+
Object.keys(ret[groupKey] || {}).forEach((subKey, subIndex) => {
29+
if (ret[groupKey][subKey] && typeof ret[groupKey][subKey] === "object") {
30+
ret[groupKey][subKey].index = ret[groupKey][subKey].index || subIndex; // 确保index存在
31+
}
1832
});
19-
});
20-
});
33+
}
34+
}
35+
2136
return ret;
2237
}

0 commit comments

Comments
 (0)