import { describe, expect, it } from 'vitest';
import {
decodeSubtitleBuffer,
getSubtitleExtension,
MAX_SUBTITLE_CUES,
normalizeSubtitleFile,
normalizeSubtitleLanguage,
parseSubtitleCues,
sanitizeSubtitleLabel,
SAFE_SUBTITLE_PROXY_PATH,
serializeWebVtt,
subtitleProxyPathToObjectKey,
} from '@/lib/subtitle-validation';
const UUID = '11111111-2222-3333-4444-555555555555';
function encode(text: string): Uint8Array {
return new TextEncoder().encode(text);
}
describe('getSubtitleExtension', () => {
it('accepts the two subtitle formats and nothing else', () => {
expect(getSubtitleExtension('cut.srt')).toBe('srt');
expect(getSubtitleExtension('cut.VTT')).toBe('vtt');
expect(getSubtitleExtension('cut.ass')).toBeNull();
expect(getSubtitleExtension('cut.srt.exe')).toBeNull();
});
});
describe('normalizeSubtitleLanguage', () => {
it('lowercases so a re-upload replaces the track it means to', () => {
expect(normalizeSubtitleLanguage('TR')).toBe('tr');
expect(normalizeSubtitleLanguage(' en-US ')).toBe('en-us');
expect(normalizeSubtitleLanguage('zh-Hant-TW')).toBe('zh-hant-tw');
});
it('rejects anything that is not a language tag', () => {
expect(normalizeSubtitleLanguage('')).toBeNull();
expect(normalizeSubtitleLanguage('t')).toBeNull();
expect(normalizeSubtitleLanguage('tr; drop table')).toBeNull();
expect(normalizeSubtitleLanguage('{\\an8}', ''].join(
'\n'
)
);
expect(cues[0].text).toBe('tiltalert(1)');
});
it('escapes the leftovers of a rejected tag so it cannot be reassembled', () => {
// Deleting `` out of the middle would close the two halves into a `