'use strict'; const fs = require('fs'); const os = require('os'); const path = require('path'); const { parseRenderOptions, getImageSubtitleDebugSnapshot, purgeSubtitleCacheDir } = require('../server/image-subtitles'); describe('image subtitle option parsing', () => { test('accepts streamIndex 0 as a valid value', () => { const parsed = parseRenderOptions({ filePath: '/tmp/example.mkv', streamIndex: 0, timeSeconds: 24.5, canvasWidth: 1920, canvasHeight: 1080, }); expect(parsed.streamIndex).toBe(0); expect(parsed.timeSeconds).toBeCloseTo(24.5, 3); expect(parsed.isValid).toBe(true); }); test('rejects non-numeric streamIndex values', () => { const parsed = parseRenderOptions({ filePath: '/tmp/example.mkv', streamIndex: '', timeSeconds: 24.5, }); expect(parsed.isValid).toBe(false); }); test('caps subtitle render canvas and applies render scale', () => { const parsed = parseRenderOptions({ filePath: '/tmp/example.mkv', streamIndex: 0, timeSeconds: 5, canvasWidth: 4096, canvasHeight: 2160, }); expect(parsed.canvasWidth).toBe(960); expect(parsed.canvasHeight).toBe(506); expect(parsed.canvasWidth).toBeLessThanOrEqual(1280); expect(parsed.canvasHeight).toBeLessThanOrEqual(720); }); test('preserves requested aspect ratio when capped and scaled', () => { const parsed = parseRenderOptions({ filePath: '/tmp/example.mkv', streamIndex: 0, timeSeconds: 5, canvasWidth: 3840, canvasHeight: 1600, }); expect(parsed.canvasWidth).toBe(960); expect(parsed.canvasHeight).toBe(400); }); test('prune calculates drop threshold as end plus duration plus 5 seconds', () => { // When a subtitle event is 20-30s (10s duration), it should be dropped after 45s // This tests the logic: dropThreshold = endTime + (endTime - startTime) + 5 const startTime = 20; const endTime = 30; const duration = endTime - startTime; const dropThreshold = endTime + duration + 5; expect(dropThreshold).toBe(45); // Before threshold: subtitle should be kept const beforeThreshold = 44.999; expect(beforeThreshold < dropThreshold).toBe(true); // At or after threshold: subtitle should be dropped const atThreshold = 45; expect(atThreshold >= dropThreshold).toBe(true); // Later: definitely should be dropped const afterThreshold = 55; expect(afterThreshold >= dropThreshold).toBe(true); }); }); describe('subtitle cache directory management', () => { test('debug snapshot reports unbounded item caps and timing metrics', () => { const snapshot = getImageSubtitleDebugSnapshot(); expect(snapshot.eventCache.maxItems).toBeNull(); expect(snapshot.frameCache.maxItems).toBeNull(); expect(snapshot.metrics).toEqual(expect.objectContaining({ eventCacheHits: expect.any(Number), eventCacheMisses: expect.any(Number), eventCacheBuilds: expect.any(Number), frameCacheHits: expect.any(Number), frameCacheMisses: expect.any(Number), frameRenders: expect.any(Number), })); expect(snapshot.metrics.eventBuildDurationMs).toEqual(expect.objectContaining({ last: expect.any(Number), max: expect.any(Number), average: expect.any(Number), })); }); test('purgeSubtitleCacheDir removes the subtitle cache directory and its contents', () => { const testDir = path.join(os.tmpdir(), 'filemanager-video-subtitles'); fs.mkdirSync(testDir, { recursive: true }); fs.writeFileSync(path.join(testDir, 'test.sup'), 'placeholder'); purgeSubtitleCacheDir(); expect(fs.existsSync(testDir)).toBe(false); }); test('purgeSubtitleCacheDir does not throw when the directory does not exist', () => { expect(() => purgeSubtitleCacheDir()).not.toThrow(); }); });