Files
lingxi/packages/shared/src/naming.spec.ts
T
Stev_Wang 23067edb8f feat(api): 搭建 API 与 Worker 骨架及契约基线
NestJS 11 + Fastify 统一错误体与 trace_id,/health 真实探活 DB/Redis,OpenAPI 3.1 契约含 20 个错误码;shared 包提供错误码、权限点、ULID、分页与通用 schema;worker 完成队列进程骨架。
2026-11-02 17:20:46 +08:00

48 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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);
});
});