BlueGit

timfa filemanager

Files

filemanager / tests / image-subtitles.test.js

image-subtitles.test.js

Clamp subtitle tail ranges
timfa Tim Falken · pushed by timfa · 5 months ago
be2f2fac History
image-subtitles.test.js 5.7 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('accepts zero-length subtitle range windows as empty requests', () =>
83 {
84 const parsed = parseRenderRangeOptions({
85 filePath: '/tmp/example.mkv',
86 streamIndex: 0,
87 startTimeSeconds: 10,
88 endTimeSeconds: 10,
89 canvasWidth: 1920,
90 canvasHeight: 1080,
91 });
92
93 expect(parsed.startTimeSeconds).toBe(10);
94 expect(parsed.endTimeSeconds).toBe(10);
95 expect(parsed.isValid).toBe(true);
96 });
97
98 test('prune calculates drop threshold as end plus 5-second grace', () =>
99 {
100 // When a subtitle event ends at 30s, it should be dropped once the slowest
101 // viewer passes 35s (event end + grace).
102 const startTime = 20;
103 const endTime = 30;
104 const dropThreshold = endTime + 5;
105
106 expect(startTime).toBeLessThan(endTime);
107 expect(dropThreshold).toBe(35);
108
109 // Before threshold: subtitle should be kept
110 const beforeThreshold = 34.999;
111 expect(beforeThreshold < dropThreshold).toBe(true);
112
113 // At or after threshold: subtitle should be dropped
114 const atThreshold = 35;
115 expect(atThreshold >= dropThreshold).toBe(true);
116
117 // Later: definitely should be dropped
118 const afterThreshold = 45;
119 expect(afterThreshold >= dropThreshold).toBe(true);
120 });
121});
122
123describe('subtitle cache directory management', () =>
124{
125 test('debug snapshot reports unbounded item caps and timing metrics', () =>
126 {
127 const snapshot = getImageSubtitleDebugSnapshot();
128
129 expect(snapshot.eventCache.maxItems).toBeNull();
130 expect(snapshot.frameCache.maxItems).toBeNull();
131 expect(snapshot.metrics).toEqual(expect.objectContaining({
132 eventCacheHits: expect.any(Number),
133 eventCacheMisses: expect.any(Number),
134 eventCacheBuilds: expect.any(Number),
135 frameCacheHits: expect.any(Number),
136 frameCacheMisses: expect.any(Number),
137 frameRenders: expect.any(Number),
138 }));
139 expect(snapshot.metrics.eventBuildDurationMs).toEqual(expect.objectContaining({
140 last: expect.any(Number),
141 max: expect.any(Number),
142 average: expect.any(Number),
143 }));
144 });
145
146 test('purgeSubtitleCacheDir removes the subtitle cache directory and its contents', () =>
147 {
148 const testDir = path.join(os.tmpdir(), 'filemanager-video-subtitles');
149 fs.mkdirSync(testDir, { recursive: true });
150 fs.writeFileSync(path.join(testDir, 'test.sup'), 'placeholder');
151
152 purgeSubtitleCacheDir();
153
154 expect(fs.existsSync(testDir)).toBe(false);
155 });
156
157 test('purgeSubtitleCacheDir does not throw when the directory does not exist', () =>
158 {
159 expect(() => purgeSubtitleCacheDir()).not.toThrow();
160 });
161
162 test('renderImageSubtitleFrame returns quickly while event cache build is pending', async () =>
163 {
164 jest.resetModules();
165
166 jest.doMock('child_process', () => ({
167 spawn: jest.fn(() =>
168 {
169 const { EventEmitter } = require('events');
170 const child = new EventEmitter();
171 child.stderr = new EventEmitter();
172 child.stdout = new EventEmitter();
173 child.kill = jest.fn();
174 return child;
175 }),
176 }));
177
178 let isolated;
179 jest.isolateModules(() =>
180 {
181 isolated = require('../server/image-subtitles');
182 });
183
184 const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'filemanager-image-subtitle-pending-'));
185 const filePath = path.join(tmpRoot, 'video.mkv');
186 fs.writeFileSync(filePath, 'placeholder');
187
188 const fastResult = await Promise.race([
189 isolated.renderImageSubtitleFrame({
190 filePath,
191 streamIndex: 0,
192 timeSeconds: 1,
193 canvasWidth: 1280,
194 canvasHeight: 720,
195 }),
196 new Promise((resolve) => setTimeout(() => resolve('__timeout__'), 120)),
197 ]);
198
199 expect(fastResult).toBeNull();
200
201 jest.dontMock('child_process');
202 });
203});