| 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 | |
| 12 | beforeEach(() => |
| 13 | { |
| 14 | jest.resetModules(); |
| 15 | fs.rmSync(logDir, { recursive: true, force: true }); |
| 16 | }); |
| 17 | |
| 18 | test('writes timestamped per-video subtitle log files', () => |
| 19 | { |
| 20 | const { appendSubtitleDebugEntries } = require('../server/subtitle-debug-log'); |
| 21 | const timestampMs = new Date('2026-05-25T10:00:00Z').getTime(); |
| 22 | |
| 23 | const targetPath = appendSubtitleDebugEntries({ |
| 24 | videoName: 'My Film.mkv', |
| 25 | context: { source: 'app' }, |
| 26 | entries: [ |
| 27 | { loggedAtMs: timestampMs, eventType: 'subtitle_png_requested' }, |
| 28 | ], |
| 29 | }); |
| 30 | |
| 31 | expect(targetPath).toContain(`${timestampMs}-My-Film.mkv-subtitlelog.txt`); |
| 32 | const text = fs.readFileSync(targetPath, 'utf8'); |
| 33 | expect(text).toContain('subtitle_png_requested'); |
| 34 | expect(text).toContain('"source":"app"'); |
| 35 | }); |
| 36 | |
| 37 | test('reuses one timestamped file per playback session', () => |
| 38 | { |
| 39 | const { appendSubtitleDebugEntries } = require('../server/subtitle-debug-log'); |
| 40 | |
| 41 | const firstPath = appendSubtitleDebugEntries({ |
| 42 | videoName: 'My Film.mkv', |
| 43 | context: { source: 'app', sessionId: 'sess-1' }, |
| 44 | entries: [ |
| 45 | { loggedAtMs: 1000, eventType: 'subtitle_png_requested' }, |
| 46 | ], |
| 47 | }); |
| 48 | |
| 49 | const secondPath = appendSubtitleDebugEntries({ |
| 50 | videoName: 'My Film.mkv', |
| 51 | context: { source: 'app', sessionId: 'sess-1' }, |
| 52 | entries: [ |
| 53 | { loggedAtMs: 2000, eventType: 'subtitle_png_received' }, |
| 54 | ], |
| 55 | }); |
| 56 | |
| 57 | expect(secondPath).toBe(firstPath); |
| 58 | |
| 59 | const text = fs.readFileSync(firstPath, 'utf8'); |
| 60 | expect(text).toContain('subtitle_png_requested'); |
| 61 | expect(text).toContain('subtitle_png_received'); |
| 62 | }); |
| 63 | |
| 64 | test('prunes oldest subtitle log files when cache exceeds 10 MB', () => |
| 65 | { |
| 66 | const logModule = require('../server/subtitle-debug-log'); |
| 67 | |
| 68 | const oldPath = logModule.appendSubtitleDebugEntries({ |
| 69 | videoName: 'old-video.mkv', |
| 70 | entries: [ |
| 71 | { loggedAtMs: new Date('2026-05-24T10:00:00Z').getTime(), eventType: 'old-log', payload: 'x'.repeat(6 * 1024 * 1024) }, |
| 72 | ], |
| 73 | }); |
| 74 | const newPath = logModule.appendSubtitleDebugEntries({ |
| 75 | videoName: 'new-video.mkv', |
| 76 | entries: [ |
| 77 | { loggedAtMs: new Date('2026-05-25T10:00:00Z').getTime(), eventType: 'new-log', payload: 'y'.repeat(6 * 1024 * 1024) }, |
| 78 | ], |
| 79 | }); |
| 80 | |
| 81 | expect(fs.existsSync(newPath)).toBe(true); |
| 82 | expect(fs.existsSync(oldPath)).toBe(false); |
| 83 | |
| 84 | const entries = fs.readdirSync(logDir); |
| 85 | expect(entries).toHaveLength(1); |
| 86 | expect(entries[0]).toContain('new-video.mkv-subtitlelog.txt'); |
| 87 | }); |
| 88 | }); |