| 1 | 'use strict'; |
| 2 | |
| 3 | const fs = require('fs'); |
| 4 | const os = require('os'); |
| 5 | const path = require('path'); |
| 6 | const { parseRenderOptions, pruneOldImageSubtitleCaches, 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 duration plus 5 seconds', () => |
| 67 | { |
| 68 | // When a subtitle event is 20-30s (10s duration), it should be dropped after 45s |
| 69 | // This tests the logic: dropThreshold = endTime + (endTime - startTime) + 5 |
| 70 | const startTime = 20; |
| 71 | const endTime = 30; |
| 72 | const duration = endTime - startTime; |
| 73 | const dropThreshold = endTime + duration + 5; |
| 74 | |
| 75 | expect(dropThreshold).toBe(45); |
| 76 | |
| 77 | // Before threshold: subtitle should be kept |
| 78 | const beforeThreshold = 44.999; |
| 79 | expect(beforeThreshold < dropThreshold).toBe(true); |
| 80 | |
| 81 | // At or after threshold: subtitle should be dropped |
| 82 | const atThreshold = 45; |
| 83 | expect(atThreshold >= dropThreshold).toBe(true); |
| 84 | |
| 85 | // Later: definitely should be dropped |
| 86 | const afterThreshold = 55; |
| 87 | expect(afterThreshold >= dropThreshold).toBe(true); |
| 88 | }); |
| 89 | }); |
| 90 | |
| 91 | describe('subtitle cache directory management', () => |
| 92 | { |
| 93 | test('purgeSubtitleCacheDir removes the subtitle cache directory and its contents', () => |
| 94 | { |
| 95 | const testDir = path.join(os.tmpdir(), 'filemanager-video-subtitles'); |
| 96 | fs.mkdirSync(testDir, { recursive: true }); |
| 97 | fs.writeFileSync(path.join(testDir, 'test.sup'), 'placeholder'); |
| 98 | |
| 99 | purgeSubtitleCacheDir(); |
| 100 | |
| 101 | expect(fs.existsSync(testDir)).toBe(false); |
| 102 | }); |
| 103 | |
| 104 | test('purgeSubtitleCacheDir does not throw when the directory does not exist', () => |
| 105 | { |
| 106 | expect(() => purgeSubtitleCacheDir()).not.toThrow(); |
| 107 | }); |
| 108 | }); |