import { describe, it, expect } from 'vitest'; import { readFileSync } from 'node:fs'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { parseConfig, stripJsonComments } from '../src/configParser'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); describe('configParser', () => { it('应移除 -- 行注释并保持 JSON 可解析', () => { const raw = '{\n -- 注释\n "name": "demo" -- 行尾注释\n}'; const stripped = stripJsonComments(raw); expect(stripped).not.toContain('-- 注释'); expect(() => JSON.parse(stripped)).not.toThrow(); }); it('应能解析 demo.json', () => { const demoPath = resolve(__dirname, '..', 'public', 'demo.json'); const text = readFileSync(demoPath, 'utf8'); const config = parseConfig(text); expect(config.name).toBe('demo'); expect(config.layers.length).toBeGreaterThan(0); expect(config.theme.colorPalettes['木']).toBe('#43A047'); expect(config.layers.some((layer) => layer.type === 'centerIcon')).toBe(true); expect(config.layers.some((layer) => layer.type === 'degreeRing')).toBe(true); }); it('缺少必填字段时应抛错', () => { const raw = '{ "background": "#000", "theme": { "colorPalettes": {} }, "layers": [] }'; expect(() => parseConfig(raw)).toThrow('name 为必填字符串'); }); });