| 1 | 'use strict'; |
| 2 | |
| 3 | const fs = require('fs'); |
| 4 | const os = require('os'); |
| 5 | const path = require('path'); |
| 6 | const { parseRenderOptions, getImageSubtitleDebugSnapshot, purgeSubtitleCacheDir } = require('../server/image-subtitles'); |
| 7 | |
| 8 | describe('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('prune calculates drop threshold as end plus 5-second grace', () => |
| 67 | { |
| 68 | // When a subtitle event ends at 30s, it should be dropped once the slowest |
| 69 | // viewer passes 35s (event end + grace). |
| 70 | const startTime = 20; |
| 71 | const endTime = 30; |
| 72 | const dropThreshold = endTime + 5; |
| 73 | |
| 74 | expect(startTime).toBeLessThan(endTime); |
| 75 | expect(dropThreshold).toBe(35); |
| 76 | |
| 77 | // Before threshold: subtitle should be kept |
| 78 | const beforeThreshold = 34.999; |
| 79 | expect(beforeThreshold < dropThreshold).toBe(true); |
| 80 | |
| 81 | // At or after threshold: subtitle should be dropped |
| 82 | const atThreshold = 35; |
| 83 | expect(atThreshold >= dropThreshold).toBe(true); |
| 84 | |
| 85 | // Later: definitely should be dropped |
| 86 | const afterThreshold = 45; |
| 87 | expect(afterThreshold >= dropThreshold).toBe(true); |
| 88 | }); |
| 89 | }); |
| 90 | |
| 91 | describe('subtitle cache directory management', () => |
| 92 | { |
| 93 | test('debug snapshot reports unbounded item caps and timing metrics', () => |
| 94 | { |
| 95 | const snapshot = getImageSubtitleDebugSnapshot(); |
| 96 | |
| 97 | expect(snapshot.eventCache.maxItems).toBeNull(); |
| 98 | expect(snapshot.frameCache.maxItems).toBeNull(); |
| 99 | expect(snapshot.metrics).toEqual(expect.objectContaining({ |
| 100 | eventCacheHits: expect.any(Number), |
| 101 | eventCacheMisses: expect.any(Number), |
| 102 | eventCacheBuilds: expect.any(Number), |
| 103 | frameCacheHits: expect.any(Number), |
| 104 | frameCacheMisses: expect.any(Number), |
| 105 | frameRenders: expect.any(Number), |
| 106 | })); |
| 107 | expect(snapshot.metrics.eventBuildDurationMs).toEqual(expect.objectContaining({ |
| 108 | last: expect.any(Number), |
| 109 | max: expect.any(Number), |
| 110 | average: expect.any(Number), |
| 111 | })); |
| 112 | }); |
| 113 | |
| 114 | test('purgeSubtitleCacheDir removes the subtitle cache directory and its contents', () => |
| 115 | { |
| 116 | const testDir = path.join(os.tmpdir(), 'filemanager-video-subtitles'); |
| 117 | fs.mkdirSync(testDir, { recursive: true }); |
| 118 | fs.writeFileSync(path.join(testDir, 'test.sup'), 'placeholder'); |
| 119 | |
| 120 | purgeSubtitleCacheDir(); |
| 121 | |
| 122 | expect(fs.existsSync(testDir)).toBe(false); |
| 123 | }); |
| 124 | |
| 125 | test('purgeSubtitleCacheDir does not throw when the directory does not exist', () => |
| 126 | { |
| 127 | expect(() => purgeSubtitleCacheDir()).not.toThrow(); |
| 128 | }); |
| 129 | |
| 130 | test('renderImageSubtitleFrame returns quickly while event cache build is pending', async () => |
| 131 | { |
| 132 | jest.resetModules(); |
| 133 | |
| 134 | jest.doMock('child_process', () => ({ |
| 135 | spawn: jest.fn(() => |
| 136 | { |
| 137 | const { EventEmitter } = require('events'); |
| 138 | const child = new EventEmitter(); |
| 139 | child.stderr = new EventEmitter(); |
| 140 | child.stdout = new EventEmitter(); |
| 141 | child.kill = jest.fn(); |
| 142 | return child; |
| 143 | }), |
| 144 | })); |
| 145 | |
| 146 | let isolated; |
| 147 | jest.isolateModules(() => |
| 148 | { |
| 149 | isolated = require('../server/image-subtitles'); |
| 150 | }); |
| 151 | |
| 152 | const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'filemanager-image-subtitle-pending-')); |
| 153 | const filePath = path.join(tmpRoot, 'video.mkv'); |
| 154 | fs.writeFileSync(filePath, 'placeholder'); |
| 155 | |
| 156 | const fastResult = await Promise.race([ |
| 157 | isolated.renderImageSubtitleFrame({ |
| 158 | filePath, |
| 159 | streamIndex: 0, |
| 160 | timeSeconds: 1, |
| 161 | canvasWidth: 1280, |
| 162 | canvasHeight: 720, |
| 163 | }), |
| 164 | new Promise((resolve) => setTimeout(() => resolve('__timeout__'), 120)), |
| 165 | ]); |
| 166 | |
| 167 | expect(fastResult).toBeNull(); |
| 168 | |
| 169 | jest.dontMock('child_process'); |
| 170 | }); |
| 171 | }); |