import { describe, expect, it } from 'vitest'; import { dedupeName, sanitizeFilename } from './naming.js'; describe('sanitizeFilename', () => { it('替换路径分隔符与非法字符,保留中文', () => { expect(sanitizeFilename('素材/标签:01*?.png')).toBe('素材_标签_01__.png'); }); it('去除首尾空格与点,空名回退', () => { expect(sanitizeFilename(' ..脚本草稿 .. ')).toBe('脚本草稿'); expect(sanitizeFilename(' ')).toBe('未命名'); expect(sanitizeFilename('***', { fallback: 'file' })).toBe('file'); }); it('Windows 保留名追加下划线', () => { expect(sanitizeFilename('CON')).toBe('CON_'); expect(sanitizeFilename('lpt1')).toBe('lpt1_'); }); it('超长时保留扩展名', () => { const name = sanitizeFilename(`${'a'.repeat(200)}.png`, { maxLength: 20 }); expect(name).toHaveLength(20); expect(name.endsWith('.png')).toBe(true); }); }); describe('dedupeName(R-01 同名自动加序号)', () => { it('不冲突时原样返回', () => { expect(dedupeName('脚本-01', new Set())).toBe('脚本-01'); }); it('冲突时追加两位序号', () => { const existing = new Set(['素材.png', '素材-02.png']); expect(dedupeName('素材.png', existing)).toBe('素材-03.png'); }); it('带序号的名字基于原始基名递增', () => { const existing = new Set(['素材-02.png', '素材-03.png']); expect(dedupeName('素材-03.png', existing)).toBe('素材-04.png'); }); it('序号耗尽时抛错', () => { const existing = new Set(['a', ...Array.from({ length: 3 }, (_, i) => `a-0${String(i + 2)}`)]); expect(() => dedupeName('a', existing, 3)).toThrow(RangeError); }); });