Files
Shade/packages/shade-media/tests/subtitles.test.ts
Sterister 422a37e830 feat(media): subtitles that travel with the film — CLI, browser <track>, ExoPlayer
`shade media put Film.mkv` seals Film.srt / Film.no.srt / Film.en.vtt
found beside it and carries their grants in the film's sealed meta
(extra["shade.subtitles"]), so they follow the film alone or in an album
and the relay sees unrelated objects. /watch/ adds them as <track>s (SRT
to VTT, labels in the viewer's language, the viewer's language on, one at
a time); `media get` writes them back; Android opens them with
ShadeMedia.openFilm and gives them to ExoPlayer as side-loaded tracks
(ShadeMediaDataSource now resolves readers by URI).

Verified: 76 TS tests; Chromium nb-NO shows the Norwegian track only, en-US
the English one; film + subtitles round-trip bit-identical; Robolectric
ExoPlayer sees both text tracks with the Norwegian one default; a shared
vector checks the meta field in Kotlin (82/0).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-18 22:01:33 +02:00

60 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, test, expect } from 'bun:test';
import { MediaFormatError, validateMeta } from '../src/index.js';
import type { MediaGrant } from '../src/index.js';
import { SUBTITLES_KEY, srtToVtt, subtitleFor, subtitlesOf, withSubtitles } from '../src/subtitles.js';
const grant = (fill: number): MediaGrant => ({
kind: 'shade.media-grant/v1',
mediaId: new Uint8Array(16).fill(fill),
mediaKey: new Uint8Array(32).fill(fill),
rootDigest: new Uint8Array(32).fill(fill),
url: `https://relay.example/v1/media/${fill}`,
meta: { name: `sub-${fill}.srt` },
});
describe('subtitles', () => {
test.each([
['Film', 'Film.srt', ''],
['Film', 'Film.no.srt', 'no'],
['Film', 'Film.en-GB.vtt', 'en-GB'],
['Film', 'Film.NO.SRT', 'NO'],
['Film', 'Film 2.srt', null],
['Film', 'Film.mkv', null],
['Film', 'Film.forced.english.srt', null],
['Film', 'Other.no.srt', null],
['S01E02', 'S01E02.nb-NO.srt', 'nb-NO'],
] as const)('subtitleFor(%p, %p) = %p', (base, file, want) => {
expect(subtitleFor(base, file)).toBe(want);
});
test('travel in the meta, and survive meta validation', () => {
const meta = withSubtitles({ name: 'Film.mkv', mime: 'video/x-matroska', extra: { series: 'X' } }, [
{ grant: grant(1), lang: 'no' },
{ grant: grant(2), lang: 'en', label: 'English (SDH)' },
{ grant: grant(3) },
]);
expect(meta.extra?.series).toBe('X');
const back = subtitlesOf(validateMeta(JSON.parse(JSON.stringify(meta))));
expect(back.map((t) => [t.lang, t.label])).toEqual([['no', undefined], ['en', 'English (SDH)'], [undefined, undefined]]);
expect(back[0]!.grant).toEqual(grant(1));
expect(subtitlesOf({ name: 'no subs' })).toEqual([]);
expect(subtitlesOf(undefined)).toEqual([]);
});
test('strict: junk, live grants and bad language tags are refused', () => {
const bad = (v: string) => () => subtitlesOf({ extra: { [SUBTITLES_KEY]: v } });
expect(bad('{')).toThrow(MediaFormatError);
expect(bad('{}')).toThrow(MediaFormatError);
expect(bad('[{"grant":{"kind":"shade.media-grant/v1"}}]')).toThrow(MediaFormatError);
expect(bad(JSON.stringify([{ lang: 'no; drop', grant: JSON.parse(JSON.stringify({ kind: 'x' })) }]))).toThrow(MediaFormatError);
expect(() => withSubtitles({}, [{ grant: grant(1), lang: '../../x' }])).toThrow(/language/);
});
test('SubRip becomes WebVTT; WebVTT passes through', () => {
const srt = '1\r\n00:00:01,000 --> 00:00:02,500\r\nHei, verden!\r\n\r\n2\r\n00:01:00,250 --> 00:01:02,000\r\nÆ, ø og å\r\n';
expect(srtToVtt(srt)).toBe('WEBVTT\n\n1\n00:00:01.000 --> 00:00:02.500\nHei, verden!\n\n2\n00:01:00.250 --> 00:01:02.000\nÆ, ø og å\n');
const vtt = 'WEBVTT\n\n00:00.000 --> 00:01.000\nx\n';
expect(srtToVtt(vtt)).toBe(vtt);
});
});