| 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 dated per-video subtitle log files', () => |
| 19 | { |
| 20 | const { appendSubtitleDebugEntries } = require('../server/subtitle-debug-log'); |
| 21 | |
| 22 | const targetPath = appendSubtitleDebugEntries({ |
| 23 | videoName: 'My Film.mkv', |
| 24 | context: { source: 'app' }, |
| 25 | entries: [ |
| 26 | { loggedAtMs: new Date('2026-05-25T10:00:00Z').getTime(), eventType: 'subtitle_png_requested' }, |
| 27 | ], |
| 28 | }); |
| 29 | |
| 30 | expect(targetPath).toContain('2026-05-25-My-Film.mkv-subtitlelog.txt'); |
| 31 | const text = fs.readFileSync(targetPath, 'utf8'); |
| 32 | expect(text).toContain('subtitle_png_requested'); |
| 33 | expect(text).toContain('"source":"app"'); |
| 34 | }); |
| 35 | |
| 36 | test('prunes oldest subtitle log files when cache exceeds 10 MB', () => |
| 37 | { |
| 38 | const logModule = require('../server/subtitle-debug-log'); |
| 39 | |
| 40 | const oldPath = logModule.appendSubtitleDebugEntries({ |
| 41 | videoName: 'old-video.mkv', |
| 42 | entries: [ |
| 43 | { loggedAtMs: new Date('2026-05-24T10:00:00Z').getTime(), eventType: 'old-log', payload: 'x'.repeat(6 * 1024 * 1024) }, |
| 44 | ], |
| 45 | }); |
| 46 | const newPath = logModule.appendSubtitleDebugEntries({ |
| 47 | videoName: 'new-video.mkv', |
| 48 | entries: [ |
| 49 | { loggedAtMs: new Date('2026-05-25T10:00:00Z').getTime(), eventType: 'new-log', payload: 'y'.repeat(6 * 1024 * 1024) }, |
| 50 | ], |
| 51 | }); |
| 52 | |
| 53 | expect(fs.existsSync(newPath)).toBe(true); |
| 54 | expect(fs.existsSync(oldPath)).toBe(false); |
| 55 | |
| 56 | const entries = fs.readdirSync(logDir); |
| 57 | expect(entries).toHaveLength(1); |
| 58 | expect(entries[0]).toContain('new-video.mkv-subtitlelog.txt'); |
| 59 | }); |
| 60 | }); |