| 1 | 'use strict'; |
| 2 | |
| 3 | const fs = require('fs'); |
| 4 | const os = require('os'); |
| 5 | const path = require('path'); |
| 6 | |
| 7 | describe('subtitle debug log writer', () => |
| 8 | { |
| 9 | const modulePath = require.resolve('../server/subtitle-debug-log'); |
| 10 | const logDir = path.join(os.tmpdir(), 'filemanager-subtitle-debug-logs'); |
| 11 | const originalContextLimit = process.env.SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX; |
| 12 | |
| 13 | beforeEach(() => |
| 14 | { |
| 15 | jest.resetModules(); |
| 16 | fs.rmSync(logDir, { recursive: true, force: true }); |
| 17 | }); |
| 18 | |
| 19 | afterEach(() => |
| 20 | { |
| 21 | if (originalContextLimit === undefined) delete process.env.SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX; |
| 22 | else process.env.SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX = originalContextLimit; |
| 23 | }); |
| 24 | |
| 25 | test('writes timestamped per-video subtitle log files', () => |
| 26 | { |
| 27 | const { appendSubtitleDebugEntries } = require('../server/subtitle-debug-log'); |
| 28 | const timestampMs = new Date('2026-05-25T10:00:00Z').getTime(); |
| 29 | |
| 30 | const targetPath = appendSubtitleDebugEntries({ |
| 31 | videoName: 'My Film.mkv', |
| 32 | context: { source: 'app' }, |
| 33 | entries: [ |
| 34 | { loggedAtMs: timestampMs, eventType: 'subtitle_png_requested' }, |
| 35 | ], |
| 36 | }); |
| 37 | |
| 38 | expect(targetPath).toContain(`${timestampMs}-My-Film.mkv-subtitlelog.txt`); |
| 39 | const text = fs.readFileSync(targetPath, 'utf8'); |
| 40 | expect(text).toContain('subtitle_png_requested'); |
| 41 | expect(text).toContain('"source":"app"'); |
| 42 | }); |
| 43 | |
| 44 | test('reuses one timestamped file per playback session', () => |
| 45 | { |
| 46 | const { appendSubtitleDebugEntries } = require('../server/subtitle-debug-log'); |
| 47 | |
| 48 | const firstPath = appendSubtitleDebugEntries({ |
| 49 | videoName: 'My Film.mkv', |
| 50 | context: { source: 'app', sessionId: 'sess-1' }, |
| 51 | entries: [ |
| 52 | { loggedAtMs: 1000, eventType: 'subtitle_png_requested' }, |
| 53 | ], |
| 54 | }); |
| 55 | |
| 56 | const secondPath = appendSubtitleDebugEntries({ |
| 57 | videoName: 'My Film.mkv', |
| 58 | context: { source: 'app', sessionId: 'sess-1' }, |
| 59 | entries: [ |
| 60 | { loggedAtMs: 2000, eventType: 'subtitle_png_received' }, |
| 61 | ], |
| 62 | }); |
| 63 | |
| 64 | expect(secondPath).toBe(firstPath); |
| 65 | |
| 66 | const text = fs.readFileSync(firstPath, 'utf8'); |
| 67 | expect(text).toContain('subtitle_png_requested'); |
| 68 | expect(text).toContain('subtitle_png_received'); |
| 69 | }); |
| 70 | |
| 71 | test('prunes oldest subtitle log files when cache exceeds 10 MB', () => |
| 72 | { |
| 73 | const logModule = require('../server/subtitle-debug-log'); |
| 74 | |
| 75 | const oldPath = logModule.appendSubtitleDebugEntries({ |
| 76 | videoName: 'old-video.mkv', |
| 77 | entries: [ |
| 78 | { loggedAtMs: new Date('2026-05-24T10:00:00Z').getTime(), eventType: 'old-log', payload: 'x'.repeat(6 * 1024 * 1024) }, |
| 79 | ], |
| 80 | }); |
| 81 | const newPath = logModule.appendSubtitleDebugEntries({ |
| 82 | videoName: 'new-video.mkv', |
| 83 | entries: [ |
| 84 | { loggedAtMs: new Date('2026-05-25T10:00:00Z').getTime(), eventType: 'new-log', payload: 'y'.repeat(6 * 1024 * 1024) }, |
| 85 | ], |
| 86 | }); |
| 87 | |
| 88 | expect(fs.existsSync(newPath)).toBe(true); |
| 89 | expect(fs.existsSync(oldPath)).toBe(false); |
| 90 | |
| 91 | const entries = fs.readdirSync(logDir); |
| 92 | expect(entries).toHaveLength(1); |
| 93 | expect(entries[0]).toContain('new-video.mkv-subtitlelog.txt'); |
| 94 | }); |
| 95 | |
| 96 | test('evicts old context keys when subtitle log context map reaches cap', () => |
| 97 | { |
| 98 | process.env.SUBTITLE_DEBUG_LOG_CONTEXT_KEYS_MAX = '2'; |
| 99 | jest.resetModules(); |
| 100 | const logModule = require('../server/subtitle-debug-log'); |
| 101 | |
| 102 | const firstPath = logModule.appendSubtitleDebugEntries({ |
| 103 | videoName: 'ctx-video.mkv', |
| 104 | context: { source: 'app', sessionId: 'sess-1' }, |
| 105 | entries: [{ loggedAtMs: 1000, eventType: 'first' }], |
| 106 | }); |
| 107 | |
| 108 | for (let i = 0; i < 6; i++) |
| 109 | { |
| 110 | logModule.appendSubtitleDebugEntries({ |
| 111 | videoName: `ctx-video-${i}.mkv`, |
| 112 | context: { source: 'app', sessionId: `sess-${i + 2}` }, |
| 113 | entries: [{ loggedAtMs: 2000 + i, eventType: 'filler' }], |
| 114 | }); |
| 115 | } |
| 116 | |
| 117 | const secondPath = logModule.appendSubtitleDebugEntries({ |
| 118 | videoName: 'ctx-video.mkv', |
| 119 | context: { source: 'app', sessionId: 'sess-1' }, |
| 120 | entries: [{ loggedAtMs: 999999, eventType: 'second' }], |
| 121 | }); |
| 122 | |
| 123 | expect(typeof firstPath).toBe('string'); |
| 124 | expect(typeof secondPath).toBe('string'); |
| 125 | expect(logModule.getSubtitleDebugLogContextCount()).toBeLessThanOrEqual(2); |
| 126 | }); |
| 127 | }); |