BlueGit

timfa filemanager

Files

filemanager / tests / image-subtitles.test.js

image-subtitles.test.js

Add subtitle range prefetching
timfa Tim Falken · pushed by timfa · 5 months ago
e3527da6 History
image-subtitles.test.js 5.3 KiB Download Raw
1'use strict';
2
3const fs = require('fs');
4const os = require('os');
5const path = require('path');
6const { parseRenderOptions, parseRenderRangeOptions, getImageSubtitleDebugSnapshot, purgeSubtitleCacheDir } = require('../server/image-subtitles');
7
8describe('image subtitle option parsing', () =>
9{
10 test('accepts streamIndex 0 as a valid value', () =>
11 {
12 const parsed = parseRenderOptions({
13 filePath: '/tmp/example.mkv',
14 streamIndex: 0,
15 timeSeconds: 24.5,
16 canvasWidth: 1920,
17 canvasHeight: 1080,
18 });
19
20 expect(parsed.streamIndex).toBe(0);
21 expect(parsed.timeSeconds).toBeCloseTo(24.5, 3);
22 expect(parsed.isValid).toBe(true);
23 });
24
25 test('rejects non-numeric streamIndex values', () =>
26 {
27 const parsed = parseRenderOptions({
28 filePath: '/tmp/example.mkv',
29 streamIndex: '',
30 timeSeconds: 24.5,
31 });
32
33 expect(parsed.isValid).toBe(false);
34 });
35
36 test('caps subtitle render canvas and applies render scale', () =>
37 {
38 const parsed = parseRenderOptions({
39 filePath: '/tmp/example.mkv',
40 streamIndex: 0,
41 timeSeconds: 5,
42 canvasWidth: 4096,
43 canvasHeight: 2160,
44 });
45
46 expect(parsed.canvasWidth).toBe(960);
47 expect(parsed.canvasHeight).toBe(506);
48 expect(parsed.canvasWidth).toBeLessThanOrEqual(1280);
49 expect(parsed.canvasHeight).toBeLessThanOrEqual(720);
50 });
51
52 test('preserves requested aspect ratio when capped and scaled', () =>
53 {
54 const parsed = parseRenderOptions({
55 filePath: '/tmp/example.mkv',
56 streamIndex: 0,
57 timeSeconds: 5,
58 canvasWidth: 3840,
59 canvasHeight: 1600,
60 });
61
62 expect(parsed.canvasWidth).toBe(960);
63 expect(parsed.canvasHeight).toBe(400);
64 });
65
66 test('accepts valid subtitle range windows', () =>
67 {
68 const parsed = parseRenderRangeOptions({
69 filePath: '/tmp/example.mkv',
70 streamIndex: 0,
71 startTimeSeconds: 5,
72 endTimeSeconds: 10,
73 canvasWidth: 1920,
74 canvasHeight: 1080,
75 });
76
77 expect(parsed.startTimeSeconds).toBe(5);
78 expect(parsed.endTimeSeconds).toBe(10);
79 expect(parsed.isValid).toBe(true);
80 });
81
82 test('prune calculates drop threshold as end plus 5-second grace', () =>
83 {
84 // When a subtitle event ends at 30s, it should be dropped once the slowest
85 // viewer passes 35s (event end + grace).
86 const startTime = 20;
87 const endTime = 30;
88 const dropThreshold = endTime + 5;
89
90 expect(startTime).toBeLessThan(endTime);
91 expect(dropThreshold).toBe(35);
92
93 // Before threshold: subtitle should be kept
94 const beforeThreshold = 34.999;
95 expect(beforeThreshold < dropThreshold).toBe(true);
96
97 // At or after threshold: subtitle should be dropped
98 const atThreshold = 35;
99 expect(atThreshold >= dropThreshold).toBe(true);
100
101 // Later: definitely should be dropped
102 const afterThreshold = 45;
103 expect(afterThreshold >= dropThreshold).toBe(true);
104 });
105});
106
107describe('subtitle cache directory management', () =>
108{
109 test('debug snapshot reports unbounded item caps and timing metrics', () =>
110 {
111 const snapshot = getImageSubtitleDebugSnapshot();
112
113 expect(snapshot.eventCache.maxItems).toBeNull();
114 expect(snapshot.frameCache.maxItems).toBeNull();
115 expect(snapshot.metrics).toEqual(expect.objectContaining({
116 eventCacheHits: expect.any(Number),
117 eventCacheMisses: expect.any(Number),
118 eventCacheBuilds: expect.any(Number),
119 frameCacheHits: expect.any(Number),
120 frameCacheMisses: expect.any(Number),
121 frameRenders: expect.any(Number),
122 }));
123 expect(snapshot.metrics.eventBuildDurationMs).toEqual(expect.objectContaining({
124 last: expect.any(Number),
125 max: expect.any(Number),
126 average: expect.any(Number),
127 }));
128 });
129
130 test('purgeSubtitleCacheDir removes the subtitle cache directory and its contents', () =>
131 {
132 const testDir = path.join(os.tmpdir(), 'filemanager-video-subtitles');
133 fs.mkdirSync(testDir, { recursive: true });
134 fs.writeFileSync(path.join(testDir, 'test.sup'), 'placeholder');
135
136 purgeSubtitleCacheDir();
137
138 expect(fs.existsSync(testDir)).toBe(false);
139 });
140
141 test('purgeSubtitleCacheDir does not throw when the directory does not exist', () =>
142 {
143 expect(() => purgeSubtitleCacheDir()).not.toThrow();
144 });
145
146 test('renderImageSubtitleFrame returns quickly while event cache build is pending', async () =>
147 {
148 jest.resetModules();
149
150 jest.doMock('child_process', () => ({
151 spawn: jest.fn(() =>
152 {
153 const { EventEmitter } = require('events');
154 const child = new EventEmitter();
155 child.stderr = new EventEmitter();
156 child.stdout = new EventEmitter();
157 child.kill = jest.fn();
158 return child;
159 }),
160 }));
161
162 let isolated;
163 jest.isolateModules(() =>
164 {
165 isolated = require('../server/image-subtitles');
166 });
167
168 const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'filemanager-image-subtitle-pending-'));
169 const filePath = path.join(tmpRoot, 'video.mkv');
170 fs.writeFileSync(filePath, 'placeholder');
171
172 const fastResult = await Promise.race([
173 isolated.renderImageSubtitleFrame({
174 filePath,
175 streamIndex: 0,
176 timeSeconds: 1,
177 canvasWidth: 1280,
178 canvasHeight: 720,
179 }),
180 new Promise((resolve) => setTimeout(() => resolve('__timeout__'), 120)),
181 ]);
182
183 expect(fastResult).toBeNull();
184
185 jest.dontMock('child_process');
186 });
187});