'use strict'; const { parseRenderOptions, pruneOldImageSubtitleCaches } = 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(640); expect(parsed.canvasHeight).toBe(338); 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(640); expect(parsed.canvasHeight).toBe(267); }); 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); }); });