| 1 | 'use strict'; |
| 2 | |
| 3 | const fs = require('fs'); |
| 4 | const path = require('path'); |
| 5 | const { JSDOM } = require('jsdom'); |
| 6 | |
| 7 | function flushAsyncWork (window, rounds = 6) |
| 8 | { |
| 9 | let chain = Promise.resolve(); |
| 10 | for (let i = 0; i < rounds; i++) |
| 11 | { |
| 12 | chain = chain.then(() => new Promise((resolve) => window.setTimeout(resolve, 0))); |
| 13 | } |
| 14 | return chain; |
| 15 | } |
| 16 | |
| 17 | function buildPlayerHarness (window) |
| 18 | { |
| 19 | const registeredComponents = new Map(); |
| 20 | |
| 21 | class MockButton |
| 22 | { |
| 23 | constructor(player) |
| 24 | { |
| 25 | this._player = player; |
| 26 | this._el = null; |
| 27 | } |
| 28 | |
| 29 | player () |
| 30 | { |
| 31 | return this._player; |
| 32 | } |
| 33 | |
| 34 | buildCSSClass () |
| 35 | { |
| 36 | return 'vjs-control'; |
| 37 | } |
| 38 | |
| 39 | createEl () |
| 40 | { |
| 41 | const el = window.document.createElement('button'); |
| 42 | el.className = this.buildCSSClass(); |
| 43 | return el; |
| 44 | } |
| 45 | |
| 46 | el () |
| 47 | { |
| 48 | if (!this._el) this._el = this.createEl(); |
| 49 | return this._el; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | registeredComponents.set('Button', MockButton); |
| 54 | |
| 55 | const videojs = { |
| 56 | getComponent: (name) => registeredComponents.get(name), |
| 57 | registerComponent: (name, component) => |
| 58 | { |
| 59 | registeredComponents.set(name, component); |
| 60 | }, |
| 61 | }; |
| 62 | |
| 63 | const events = new Map(); |
| 64 | const playerEl = window.document.createElement('div'); |
| 65 | const progressControlEl = window.document.createElement('div'); |
| 66 | progressControlEl.className = 'vjs-progress-control'; |
| 67 | playerEl.appendChild(progressControlEl); |
| 68 | let currentTime = 0; |
| 69 | let durationSeconds = 10; |
| 70 | let bufferedEndSeconds = null; |
| 71 | |
| 72 | const player = { |
| 73 | ready (cb) |
| 74 | { |
| 75 | if (typeof cb === 'function') cb(); |
| 76 | }, |
| 77 | on (type, fn) |
| 78 | { |
| 79 | if (!events.has(type)) events.set(type, new Set()); |
| 80 | events.get(type).add(fn); |
| 81 | }, |
| 82 | trigger (type) |
| 83 | { |
| 84 | const listeners = events.get(type); |
| 85 | if (!listeners) return; |
| 86 | for (const fn of Array.from(listeners)) fn(); |
| 87 | }, |
| 88 | currentTime () |
| 89 | { |
| 90 | return currentTime; |
| 91 | }, |
| 92 | setCurrentTime (next) |
| 93 | { |
| 94 | currentTime = Number(next); |
| 95 | }, |
| 96 | duration (nextValue) |
| 97 | { |
| 98 | if (typeof nextValue === 'number') |
| 99 | { |
| 100 | durationSeconds = nextValue; |
| 101 | } |
| 102 | return durationSeconds; |
| 103 | }, |
| 104 | bufferedEnd () |
| 105 | { |
| 106 | return bufferedEndSeconds; |
| 107 | }, |
| 108 | setBufferedEnd (next) |
| 109 | { |
| 110 | bufferedEndSeconds = Number.isFinite(next) ? Number(next) : null; |
| 111 | }, |
| 112 | el () |
| 113 | { |
| 114 | return playerEl; |
| 115 | }, |
| 116 | getChild (name) |
| 117 | { |
| 118 | if (name !== 'controlBar') return null; |
| 119 | return { |
| 120 | addChild (componentName) |
| 121 | { |
| 122 | const Component = registeredComponents.get(componentName); |
| 123 | if (!Component) return null; |
| 124 | const instance = new Component(player); |
| 125 | playerEl.appendChild(instance.el()); |
| 126 | return instance; |
| 127 | }, |
| 128 | }; |
| 129 | }, |
| 130 | }; |
| 131 | |
| 132 | return { player, videojs }; |
| 133 | } |
| 134 | |
| 135 | function loadSharedVideoPlayer (window) |
| 136 | { |
| 137 | const script = fs.readFileSync(path.join(__dirname, '../public/shared-video-player.js'), 'utf8'); |
| 138 | window.eval(script); |
| 139 | return window.FilemanagerSharedVideoPlayer; |
| 140 | } |
| 141 | |
| 142 | describe('shared image subtitle prefetching', () => |
| 143 | { |
| 144 | function createWindow () |
| 145 | { |
| 146 | const dom = new JSDOM('<!doctype html><html><body></body></html>', { |
| 147 | url: 'http://localhost/', |
| 148 | runScripts: 'dangerously', |
| 149 | pretendToBeVisual: true, |
| 150 | }); |
| 151 | |
| 152 | dom.window.AbortController = AbortController; |
| 153 | dom.window.Response = Response; |
| 154 | dom.window.Headers = Headers; |
| 155 | dom.window.Request = Request; |
| 156 | dom.window.URL.createObjectURL = () => 'blob:subtitle'; |
| 157 | dom.window.URL.revokeObjectURL = () => { }; |
| 158 | |
| 159 | return dom.window; |
| 160 | } |
| 161 | |
| 162 | test('prefetches lookahead frames even when no subtitle is currently active', async () => |
| 163 | { |
| 164 | const window = createWindow(); |
| 165 | const helper = loadSharedVideoPlayer(window); |
| 166 | const { player, videojs } = buildPlayerHarness(window); |
| 167 | const requestedTimes = []; |
| 168 | |
| 169 | window.fetch = jest.fn(async (url) => |
| 170 | { |
| 171 | const parsed = new URL(url, 'http://localhost/'); |
| 172 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 173 | return new Response(null, { |
| 174 | status: 204, |
| 175 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 176 | }); |
| 177 | }); |
| 178 | |
| 179 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 180 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 181 | }, { |
| 182 | lookaheadSeconds: 1, |
| 183 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 184 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 185 | }), |
| 186 | }); |
| 187 | |
| 188 | await flushAsyncWork(window); |
| 189 | |
| 190 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1])); |
| 191 | |
| 192 | window.close(); |
| 193 | }); |
| 194 | |
| 195 | test('prefetches subtitle frames through known video duration even with short lookahead', async () => |
| 196 | { |
| 197 | const window = createWindow(); |
| 198 | const helper = loadSharedVideoPlayer(window); |
| 199 | const { player, videojs } = buildPlayerHarness(window); |
| 200 | const requestedTimes = []; |
| 201 | |
| 202 | player.duration(3); |
| 203 | |
| 204 | window.fetch = jest.fn(async (url) => |
| 205 | { |
| 206 | const parsed = new URL(url, 'http://localhost/'); |
| 207 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 208 | return new Response(null, { |
| 209 | status: 204, |
| 210 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 211 | }); |
| 212 | }); |
| 213 | |
| 214 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 215 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 216 | }, { |
| 217 | lookaheadSeconds: 0.5, |
| 218 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 219 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 220 | }), |
| 221 | }); |
| 222 | |
| 223 | await flushAsyncWork(window, 18); |
| 224 | |
| 225 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3])); |
| 226 | |
| 227 | window.close(); |
| 228 | }); |
| 229 | |
| 230 | test('prefetch expands to video end when duration metadata appears later', async () => |
| 231 | { |
| 232 | const window = createWindow(); |
| 233 | const helper = loadSharedVideoPlayer(window); |
| 234 | const { player, videojs } = buildPlayerHarness(window); |
| 235 | const requestedTimes = []; |
| 236 | |
| 237 | player.duration(Number.NaN); |
| 238 | |
| 239 | window.fetch = jest.fn(async (url) => |
| 240 | { |
| 241 | const parsed = new URL(url, 'http://localhost/'); |
| 242 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 243 | return new Response(null, { |
| 244 | status: 204, |
| 245 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 246 | }); |
| 247 | }); |
| 248 | |
| 249 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 250 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 251 | }, { |
| 252 | lookaheadSeconds: 0.5, |
| 253 | initialPrefetchSeconds: 0, |
| 254 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 255 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 256 | }), |
| 257 | }); |
| 258 | |
| 259 | await flushAsyncWork(window, 6); |
| 260 | expect(requestedTimes).toContain(0); |
| 261 | |
| 262 | requestedTimes.length = 0; |
| 263 | player.duration(4); |
| 264 | player.trigger('durationchange'); |
| 265 | await flushAsyncWork(window, 14); |
| 266 | |
| 267 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4])); |
| 268 | |
| 269 | window.close(); |
| 270 | }); |
| 271 | |
| 272 | test('prefetches at least through the buffered video end', async () => |
| 273 | { |
| 274 | const window = createWindow(); |
| 275 | const helper = loadSharedVideoPlayer(window); |
| 276 | const { player, videojs } = buildPlayerHarness(window); |
| 277 | const requestedTimes = []; |
| 278 | |
| 279 | player.setBufferedEnd(3); |
| 280 | |
| 281 | window.fetch = jest.fn(async (url) => |
| 282 | { |
| 283 | const parsed = new URL(url, 'http://localhost/'); |
| 284 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 285 | return new Response(null, { |
| 286 | status: 204, |
| 287 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 288 | }); |
| 289 | }); |
| 290 | |
| 291 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 292 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 293 | }, { |
| 294 | lookaheadSeconds: 0.5, |
| 295 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 296 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 297 | }), |
| 298 | }); |
| 299 | |
| 300 | await flushAsyncWork(window, 12); |
| 301 | |
| 302 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3])); |
| 303 | |
| 304 | window.close(); |
| 305 | }); |
| 306 | |
| 307 | test('range prefetch clamps the tail window to the known video duration', async () => |
| 308 | { |
| 309 | const window = createWindow(); |
| 310 | const helper = loadSharedVideoPlayer(window); |
| 311 | const { player, videojs } = buildPlayerHarness(window); |
| 312 | const requestedRanges = []; |
| 313 | |
| 314 | player.duration(10); |
| 315 | player.setCurrentTime(8.5); |
| 316 | |
| 317 | window.fetch = jest.fn(async (url) => |
| 318 | { |
| 319 | const parsed = new URL(url, 'http://localhost/'); |
| 320 | requestedRanges.push({ |
| 321 | start: Number(parsed.searchParams.get('startTimeSeconds')), |
| 322 | end: Number(parsed.searchParams.get('endTimeSeconds')), |
| 323 | }); |
| 324 | return new Response(JSON.stringify({ status: 'ok', frames: [] }), { |
| 325 | status: 200, |
| 326 | headers: { 'Content-Type': 'application/json' }, |
| 327 | }); |
| 328 | }); |
| 329 | |
| 330 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 331 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 332 | }, { |
| 333 | lookaheadSeconds: 0, |
| 334 | initialPrefetchSeconds: 0, |
| 335 | currentFetchEnabled: false, |
| 336 | cacheMissFallbackEnabled: false, |
| 337 | prefetchAdaptiveStepEnabled: true, |
| 338 | prefetchStepSecondsMin: 3, |
| 339 | prefetchStepSecondsMax: 3, |
| 340 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 341 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 342 | }), |
| 343 | buildPrefetchRequest: ({ currentTime, endTime, streamIndex }) => ({ |
| 344 | url: `/subtitle-range?streamIndex=${streamIndex}&startTimeSeconds=${currentTime}&endTimeSeconds=${endTime}`, |
| 345 | }), |
| 346 | }); |
| 347 | |
| 348 | player.trigger('timeupdate'); |
| 349 | await flushAsyncWork(window, 10); |
| 350 | |
| 351 | expect(requestedRanges.length).toBeGreaterThan(0); |
| 352 | expect(requestedRanges[0]).toEqual({ start: 8.5, end: 10 }); |
| 353 | expect(requestedRanges.every((range) => range.end <= 10)).toBe(true); |
| 354 | |
| 355 | window.close(); |
| 356 | }); |
| 357 | |
| 358 | test('prefetch respects configured larger prefetch step size', async () => |
| 359 | { |
| 360 | const window = createWindow(); |
| 361 | const helper = loadSharedVideoPlayer(window); |
| 362 | const { player, videojs } = buildPlayerHarness(window); |
| 363 | const requestedTimes = []; |
| 364 | |
| 365 | player.duration(6); |
| 366 | |
| 367 | window.fetch = jest.fn(async (url) => |
| 368 | { |
| 369 | const parsed = new URL(url, 'http://localhost/'); |
| 370 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 371 | return new Response(null, { |
| 372 | status: 204, |
| 373 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 374 | }); |
| 375 | }); |
| 376 | |
| 377 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 378 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 379 | }, { |
| 380 | lookaheadSeconds: 0, |
| 381 | initialPrefetchSeconds: 0, |
| 382 | prefetchStepSeconds: 2, |
| 383 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 384 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 385 | }), |
| 386 | }); |
| 387 | |
| 388 | await flushAsyncWork(window, 14); |
| 389 | |
| 390 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 2, 4, 6])); |
| 391 | |
| 392 | window.close(); |
| 393 | }); |
| 394 | |
| 395 | test('adaptive prefetch step increases stride when cache lead grows', async () => |
| 396 | { |
| 397 | const window = createWindow(); |
| 398 | const helper = loadSharedVideoPlayer(window); |
| 399 | const { player, videojs } = buildPlayerHarness(window); |
| 400 | const requestedTimes = []; |
| 401 | |
| 402 | player.duration(40); |
| 403 | |
| 404 | window.fetch = jest.fn(async (url) => |
| 405 | { |
| 406 | const parsed = new URL(url, 'http://localhost/'); |
| 407 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 408 | return new Response(null, { |
| 409 | status: 204, |
| 410 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 411 | }); |
| 412 | }); |
| 413 | |
| 414 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 415 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 416 | }, { |
| 417 | lookaheadSeconds: 20, |
| 418 | initialPrefetchSeconds: 0, |
| 419 | prefetchAdaptiveStepEnabled: true, |
| 420 | prefetchStepSecondsMin: 1, |
| 421 | prefetchStepSecondsMax: 5, |
| 422 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 423 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 424 | }), |
| 425 | }); |
| 426 | |
| 427 | await flushAsyncWork(window, 20); |
| 428 | |
| 429 | const strideSamples = []; |
| 430 | for (let i = 1; i < requestedTimes.length; i++) |
| 431 | { |
| 432 | const stride = requestedTimes[i] - requestedTimes[i - 1]; |
| 433 | if (Number.isFinite(stride) && stride > 0) strideSamples.push(Number(stride.toFixed(3))); |
| 434 | } |
| 435 | |
| 436 | expect(strideSamples.some((stride) => stride > 1.25)).toBe(true); |
| 437 | |
| 438 | window.close(); |
| 439 | }); |
| 440 | |
| 441 | test('prefetch can cache subtitle frames from a range request and render immediately', async () => |
| 442 | { |
| 443 | const window = createWindow(); |
| 444 | const helper = loadSharedVideoPlayer(window); |
| 445 | const { player, videojs } = buildPlayerHarness(window); |
| 446 | const requestedUrls = []; |
| 447 | |
| 448 | player.duration(12); |
| 449 | player.setCurrentTime(2); |
| 450 | |
| 451 | window.fetch = jest.fn(async (url) => |
| 452 | { |
| 453 | const parsed = new URL(url, 'http://localhost/'); |
| 454 | requestedUrls.push(parsed.pathname + parsed.search); |
| 455 | |
| 456 | if (parsed.pathname === '/subtitle-range') |
| 457 | { |
| 458 | return new Response(JSON.stringify({ |
| 459 | status: 'ok', |
| 460 | startTimeSeconds: Number(parsed.searchParams.get('startTimeSeconds')), |
| 461 | endTimeSeconds: Number(parsed.searchParams.get('endTimeSeconds')), |
| 462 | frames: [ |
| 463 | { |
| 464 | left: 0, |
| 465 | top: 0, |
| 466 | width: 320, |
| 467 | height: 180, |
| 468 | canvasWidth: 640, |
| 469 | canvasHeight: 360, |
| 470 | startTimeSeconds: 2, |
| 471 | endTimeSeconds: 4, |
| 472 | contentType: 'image/png', |
| 473 | frameBase64: Buffer.from('png-data').toString('base64'), |
| 474 | }, |
| 475 | ], |
| 476 | }), { |
| 477 | status: 200, |
| 478 | headers: { 'Content-Type': 'application/json' }, |
| 479 | }); |
| 480 | } |
| 481 | |
| 482 | return new Response(null, { |
| 483 | status: 204, |
| 484 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 485 | }); |
| 486 | }); |
| 487 | |
| 488 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 489 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 490 | }, { |
| 491 | currentFetchEnabled: false, |
| 492 | cacheMissFallbackEnabled: false, |
| 493 | lookaheadSeconds: 0, |
| 494 | initialPrefetchSeconds: 0, |
| 495 | buildPrefetchRequest: ({ currentTime, endTime, streamIndex }) => ({ |
| 496 | url: `/subtitle-range?streamIndex=${streamIndex}&startTimeSeconds=${currentTime}&endTimeSeconds=${endTime}`, |
| 497 | }), |
| 498 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 499 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 500 | }), |
| 501 | }); |
| 502 | |
| 503 | await flushAsyncWork(window, 10); |
| 504 | |
| 505 | expect(requestedUrls.some((url) => url.startsWith('/subtitle-range'))).toBe(true); |
| 506 | expect(requestedUrls.some((url) => url.startsWith('/subtitle-frame'))).toBe(false); |
| 507 | |
| 508 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 509 | expect(overlay).toBeTruthy(); |
| 510 | expect(overlay.style.display).toBe('block'); |
| 511 | |
| 512 | window.close(); |
| 513 | }); |
| 514 | |
| 515 | test('prefetch uses furthest buffered end when player.bufferedEnd under-reports', async () => |
| 516 | { |
| 517 | const window = createWindow(); |
| 518 | const helper = loadSharedVideoPlayer(window); |
| 519 | const { player, videojs } = buildPlayerHarness(window); |
| 520 | const requestedTimes = []; |
| 521 | |
| 522 | player.setBufferedEnd(0); |
| 523 | player.tech = () => ({ |
| 524 | el: () => ({ |
| 525 | buffered: { |
| 526 | length: 1, |
| 527 | end: () => 6, |
| 528 | }, |
| 529 | }), |
| 530 | }); |
| 531 | |
| 532 | window.fetch = jest.fn(async (url) => |
| 533 | { |
| 534 | const parsed = new URL(url, 'http://localhost/'); |
| 535 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 536 | return new Response(null, { |
| 537 | status: 204, |
| 538 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 539 | }); |
| 540 | }); |
| 541 | |
| 542 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 543 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 544 | }, { |
| 545 | lookaheadSeconds: 0, |
| 546 | initialPrefetchSeconds: 0, |
| 547 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 548 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 549 | }), |
| 550 | }); |
| 551 | |
| 552 | await flushAsyncWork(window, 16); |
| 553 | |
| 554 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6])); |
| 555 | |
| 556 | window.close(); |
| 557 | }); |
| 558 | |
| 559 | test('prefetch frontier jumps to current playback time instead of replaying stale lag', async () => |
| 560 | { |
| 561 | const window = createWindow(); |
| 562 | const helper = loadSharedVideoPlayer(window); |
| 563 | const { player, videojs } = buildPlayerHarness(window); |
| 564 | const requestedTimes = []; |
| 565 | |
| 566 | player.duration(60); |
| 567 | player.setBufferedEnd(2); |
| 568 | |
| 569 | window.fetch = jest.fn(async (url) => |
| 570 | { |
| 571 | const parsed = new URL(url, 'http://localhost/'); |
| 572 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 573 | requestedTimes.push(timeSeconds); |
| 574 | return new Response(null, { |
| 575 | status: 204, |
| 576 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 577 | }); |
| 578 | }); |
| 579 | |
| 580 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 581 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 582 | }, { |
| 583 | lookaheadSeconds: 0, |
| 584 | initialPrefetchSeconds: 0, |
| 585 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 586 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 587 | }), |
| 588 | }); |
| 589 | |
| 590 | await flushAsyncWork(window, 8); |
| 591 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5])); |
| 592 | |
| 593 | requestedTimes.length = 0; |
| 594 | player.setCurrentTime(20); |
| 595 | player.setBufferedEnd(40); |
| 596 | player.trigger('timeupdate'); |
| 597 | await flushAsyncWork(window, 8); |
| 598 | |
| 599 | expect(requestedTimes).toContain(20); |
| 600 | expect(requestedTimes.some((value) => value < 20)).toBe(false); |
| 601 | const prefetchAfterJump = requestedTimes.filter((value) => value > 20); |
| 602 | if (prefetchAfterJump.length) |
| 603 | { |
| 604 | expect(Math.min(...prefetchAfterJump)).toBeGreaterThanOrEqual(20.5); |
| 605 | } |
| 606 | |
| 607 | window.close(); |
| 608 | }); |
| 609 | |
| 610 | test('does not restart current subtitle request while one is in flight', async () => |
| 611 | { |
| 612 | const window = createWindow(); |
| 613 | const helper = loadSharedVideoPlayer(window); |
| 614 | const { player, videojs } = buildPlayerHarness(window); |
| 615 | const pendingResolvers = []; |
| 616 | const requestTimes = []; |
| 617 | |
| 618 | window.fetch = jest.fn((url, init = {}) => |
| 619 | { |
| 620 | const parsed = new URL(url, 'http://localhost/'); |
| 621 | requestTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 622 | |
| 623 | if (pendingResolvers.length === 0) |
| 624 | { |
| 625 | return new Promise((resolve) => |
| 626 | { |
| 627 | pendingResolvers.push(() => resolve(new Response(null, { |
| 628 | status: 204, |
| 629 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 630 | }))); |
| 631 | }); |
| 632 | } |
| 633 | |
| 634 | expect(init.signal && init.signal.aborted).toBe(false); |
| 635 | return Promise.resolve(new Response(null, { |
| 636 | status: 204, |
| 637 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 638 | })); |
| 639 | }); |
| 640 | |
| 641 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 642 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 643 | }, { |
| 644 | lookaheadSeconds: 0, |
| 645 | initialPrefetchSeconds: 0, |
| 646 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 647 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 648 | }), |
| 649 | }); |
| 650 | |
| 651 | await flushAsyncWork(window, 3); |
| 652 | expect(window.fetch).toHaveBeenCalledTimes(1); |
| 653 | |
| 654 | player.setCurrentTime(1.5); |
| 655 | player.trigger('timeupdate'); |
| 656 | player.setCurrentTime(2.25); |
| 657 | player.trigger('timeupdate'); |
| 658 | await flushAsyncWork(window, 2); |
| 659 | |
| 660 | expect(window.fetch).toHaveBeenCalledTimes(1); |
| 661 | |
| 662 | pendingResolvers[0](); |
| 663 | await flushAsyncWork(window, 5); |
| 664 | |
| 665 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 666 | expect(requestTimes).toEqual(expect.arrayContaining([0, 2.25])); |
| 667 | |
| 668 | window.close(); |
| 669 | }); |
| 670 | |
| 671 | test('renders a prefetched frame for the current paused time without waiting for another timeupdate', async () => |
| 672 | { |
| 673 | const window = createWindow(); |
| 674 | const helper = loadSharedVideoPlayer(window); |
| 675 | const { player, videojs } = buildPlayerHarness(window); |
| 676 | |
| 677 | player.duration(120); |
| 678 | player.setCurrentTime(118.95); |
| 679 | |
| 680 | window.fetch = jest.fn(async (url) => |
| 681 | { |
| 682 | const parsed = new URL(url, 'http://localhost/'); |
| 683 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 684 | const headers = { |
| 685 | 'Content-Type': 'image/png', |
| 686 | }; |
| 687 | |
| 688 | if (Math.abs(timeSeconds - 118.95) < 0.001) |
| 689 | { |
| 690 | headers['X-Subtitle-Frame-Start-Time'] = '118.90'; |
| 691 | headers['X-Subtitle-Frame-End-Time'] = '119.00'; |
| 692 | headers['X-Subtitle-Frame-Width'] = '640'; |
| 693 | headers['X-Subtitle-Frame-Height'] = '80'; |
| 694 | headers['X-Subtitle-Frame-Canvas-Width'] = '1920'; |
| 695 | headers['X-Subtitle-Frame-Canvas-Height'] = '1080'; |
| 696 | return new Response(new Uint8Array([137, 80, 78, 71]), { status: 200, headers }); |
| 697 | } |
| 698 | |
| 699 | return new Response(null, { |
| 700 | status: 204, |
| 701 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 702 | }); |
| 703 | }); |
| 704 | |
| 705 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 706 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 707 | }, { |
| 708 | currentFetchEnabled: false, |
| 709 | cacheMissFallbackEnabled: false, |
| 710 | lookaheadSeconds: 0, |
| 711 | initialPrefetchSeconds: 0, |
| 712 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 713 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 714 | }), |
| 715 | }); |
| 716 | |
| 717 | await flushAsyncWork(window, 10); |
| 718 | |
| 719 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 720 | const overlayImg = overlay && overlay.querySelector('img'); |
| 721 | expect(overlay).toBeTruthy(); |
| 722 | expect(overlayImg).toBeTruthy(); |
| 723 | expect(overlay.style.display).toBe('block'); |
| 724 | expect(overlayImg.getAttribute('src')).toBe('blob:subtitle'); |
| 725 | |
| 726 | window.close(); |
| 727 | }); |
| 728 | |
| 729 | test('syncs caption visibility from cache between sparse timeupdate events', async () => |
| 730 | { |
| 731 | const window = createWindow(); |
| 732 | const helper = loadSharedVideoPlayer(window); |
| 733 | const { player, videojs } = buildPlayerHarness(window); |
| 734 | |
| 735 | // JSDOM raf cadence can be inconsistent; force timeout fallback for stable checks. |
| 736 | window.requestAnimationFrame = undefined; |
| 737 | window.cancelAnimationFrame = undefined; |
| 738 | |
| 739 | player.duration(20); |
| 740 | |
| 741 | window.fetch = jest.fn(async () => |
| 742 | { |
| 743 | return new Response(new Uint8Array([137, 80, 78, 71]), { |
| 744 | status: 200, |
| 745 | headers: { |
| 746 | 'Content-Type': 'image/png', |
| 747 | 'X-Subtitle-Frame-Start-Time': '1', |
| 748 | 'X-Subtitle-Frame-End-Time': '2', |
| 749 | 'X-Subtitle-Frame-Width': '640', |
| 750 | 'X-Subtitle-Frame-Height': '80', |
| 751 | 'X-Subtitle-Frame-Canvas-Width': '1920', |
| 752 | 'X-Subtitle-Frame-Canvas-Height': '1080', |
| 753 | }, |
| 754 | }); |
| 755 | }); |
| 756 | |
| 757 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 758 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 759 | }, { |
| 760 | lookaheadSeconds: 0, |
| 761 | initialPrefetchSeconds: 0, |
| 762 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 763 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 764 | }), |
| 765 | }); |
| 766 | |
| 767 | await flushAsyncWork(window, 8); |
| 768 | |
| 769 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 770 | expect(overlay).toBeTruthy(); |
| 771 | |
| 772 | player.setCurrentTime(1.2); |
| 773 | player.trigger('play'); |
| 774 | await new Promise((resolve) => window.setTimeout(resolve, 50)); |
| 775 | await flushAsyncWork(window, 2); |
| 776 | expect(overlay.style.display).toBe('block'); |
| 777 | |
| 778 | player.setCurrentTime(2.2); |
| 779 | await new Promise((resolve) => window.setTimeout(resolve, 50)); |
| 780 | await flushAsyncWork(window, 2); |
| 781 | expect(overlay.style.display).toBe('none'); |
| 782 | |
| 783 | controller.detach(); |
| 784 | await flushAsyncWork(window, 2); |
| 785 | window.close(); |
| 786 | }); |
| 787 | |
| 788 | test('uses video-frame media clock for precise cached caption timing', async () => |
| 789 | { |
| 790 | const window = createWindow(); |
| 791 | const helper = loadSharedVideoPlayer(window); |
| 792 | const { player, videojs } = buildPlayerHarness(window); |
| 793 | |
| 794 | let frameCallback = null; |
| 795 | let frameCallbackId = 0; |
| 796 | const videoEl = window.document.createElement('video'); |
| 797 | videoEl.requestVideoFrameCallback = jest.fn((cb) => |
| 798 | { |
| 799 | frameCallback = cb; |
| 800 | frameCallbackId += 1; |
| 801 | return frameCallbackId; |
| 802 | }); |
| 803 | videoEl.cancelVideoFrameCallback = jest.fn(); |
| 804 | player.el().appendChild(videoEl); |
| 805 | |
| 806 | window.fetch = jest.fn(async () => |
| 807 | { |
| 808 | return new Response(new Uint8Array([137, 80, 78, 71]), { |
| 809 | status: 200, |
| 810 | headers: { |
| 811 | 'Content-Type': 'image/png', |
| 812 | 'X-Subtitle-Frame-Start-Time': '1', |
| 813 | 'X-Subtitle-Frame-End-Time': '2', |
| 814 | 'X-Subtitle-Frame-Width': '640', |
| 815 | 'X-Subtitle-Frame-Height': '80', |
| 816 | 'X-Subtitle-Frame-Canvas-Width': '1920', |
| 817 | 'X-Subtitle-Frame-Canvas-Height': '1080', |
| 818 | }, |
| 819 | }); |
| 820 | }); |
| 821 | |
| 822 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 823 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 824 | }, { |
| 825 | lookaheadSeconds: 0, |
| 826 | initialPrefetchSeconds: 0, |
| 827 | currentFetchEnabled: false, |
| 828 | cacheMissFallbackEnabled: false, |
| 829 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 830 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 831 | }), |
| 832 | }); |
| 833 | |
| 834 | await flushAsyncWork(window, 8); |
| 835 | |
| 836 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 837 | expect(overlay).toBeTruthy(); |
| 838 | expect(videoEl.requestVideoFrameCallback).toHaveBeenCalled(); |
| 839 | expect(typeof frameCallback).toBe('function'); |
| 840 | |
| 841 | player.setCurrentTime(9); |
| 842 | player.trigger('play'); |
| 843 | |
| 844 | frameCallback(1000, { mediaTime: 1.5 }); |
| 845 | await flushAsyncWork(window, 3); |
| 846 | expect(overlay.style.display).toBe('block'); |
| 847 | |
| 848 | const nextFrameCallback = frameCallback; |
| 849 | nextFrameCallback(1016, { mediaTime: 2.2 }); |
| 850 | await flushAsyncWork(window, 3); |
| 851 | expect(overlay.style.display).toBe('none'); |
| 852 | |
| 853 | controller.detach(); |
| 854 | await flushAsyncWork(window, 2); |
| 855 | window.close(); |
| 856 | }); |
| 857 | |
| 858 | test('aborts in-flight prefetch requests when the overlay is detached', async () => |
| 859 | { |
| 860 | const window = createWindow(); |
| 861 | const helper = loadSharedVideoPlayer(window); |
| 862 | const { player, videojs } = buildPlayerHarness(window); |
| 863 | const activeSignals = []; |
| 864 | |
| 865 | window.fetch = jest.fn((url, init = {}) => |
| 866 | { |
| 867 | const parsed = new URL(url, 'http://localhost/'); |
| 868 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 869 | |
| 870 | if (timeSeconds === 0) |
| 871 | { |
| 872 | return Promise.resolve(new Response(null, { |
| 873 | status: 204, |
| 874 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 875 | })); |
| 876 | } |
| 877 | |
| 878 | activeSignals.push(init.signal || null); |
| 879 | |
| 880 | return new Promise((resolve, reject) => |
| 881 | { |
| 882 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 883 | { |
| 884 | init.signal.addEventListener('abort', () => |
| 885 | { |
| 886 | reject(new DOMException('Aborted', 'AbortError')); |
| 887 | }, { once: true }); |
| 888 | } |
| 889 | }); |
| 890 | }); |
| 891 | |
| 892 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 893 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 894 | }, { |
| 895 | lookaheadSeconds: 1, |
| 896 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 897 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 898 | }), |
| 899 | }); |
| 900 | |
| 901 | await flushAsyncWork(window, 3); |
| 902 | expect(activeSignals.length).toBeGreaterThan(0); |
| 903 | |
| 904 | controller.detach(); |
| 905 | await flushAsyncWork(window, 2); |
| 906 | |
| 907 | expect(activeSignals.every(signal => signal && signal.aborted)).toBe(true); |
| 908 | |
| 909 | window.close(); |
| 910 | }); |
| 911 | |
| 912 | test('logs current subtitle request lifecycle events and flushes them on detach', async () => |
| 913 | { |
| 914 | const window = createWindow(); |
| 915 | const helper = loadSharedVideoPlayer(window); |
| 916 | const { player, videojs } = buildPlayerHarness(window); |
| 917 | const sentBatches = []; |
| 918 | |
| 919 | window.fetch = jest.fn(async () => |
| 920 | { |
| 921 | return new Response('png-data', { |
| 922 | status: 200, |
| 923 | headers: { |
| 924 | 'Content-Type': 'image/png', |
| 925 | 'X-Subtitle-Frame-Status': 'ok', |
| 926 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 927 | 'X-Subtitle-Response-Sent-At-Ms': '1012', |
| 928 | 'X-Subtitle-Frame-Start-Time': '1.5', |
| 929 | 'X-Subtitle-Frame-End-Time': '3.2', |
| 930 | 'X-Subtitle-Frame-Width': '320', |
| 931 | 'X-Subtitle-Frame-Height': '180', |
| 932 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 933 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 934 | }, |
| 935 | }); |
| 936 | }); |
| 937 | |
| 938 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 939 | sessionId: 'sess-1', |
| 940 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 941 | }, { |
| 942 | lookaheadSeconds: 0, |
| 943 | sendSubtitleLogBatch: (entries) => |
| 944 | { |
| 945 | sentBatches.push(entries); |
| 946 | return Promise.resolve(); |
| 947 | }, |
| 948 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 949 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 950 | }), |
| 951 | }); |
| 952 | |
| 953 | await flushAsyncWork(window, 8); |
| 954 | controller.detach(); |
| 955 | await flushAsyncWork(window, 4); |
| 956 | |
| 957 | const flattened = sentBatches.flat(); |
| 958 | expect(flattened.map((entry) => entry.eventType)).toEqual(expect.arrayContaining([ |
| 959 | 'subtitle_png_requested', |
| 960 | 'subtitle_png_sent', |
| 961 | 'subtitle_png_received', |
| 962 | ])); |
| 963 | |
| 964 | window.close(); |
| 965 | }); |
| 966 | |
| 967 | test('logs prefetch subtitle lifecycle events', async () => |
| 968 | { |
| 969 | const window = createWindow(); |
| 970 | const helper = loadSharedVideoPlayer(window); |
| 971 | const { player, videojs } = buildPlayerHarness(window); |
| 972 | const sentBatches = []; |
| 973 | |
| 974 | window.fetch = jest.fn(async () => |
| 975 | { |
| 976 | return new Response(null, { |
| 977 | status: 204, |
| 978 | headers: { |
| 979 | 'X-Subtitle-Frame-Status': 'none', |
| 980 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 981 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 982 | 'X-Subtitle-Frame-Start-Time': '2', |
| 983 | 'X-Subtitle-Frame-End-Time': '3', |
| 984 | }, |
| 985 | }); |
| 986 | }); |
| 987 | |
| 988 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 989 | sessionId: 'sess-1', |
| 990 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 991 | }, { |
| 992 | lookaheadSeconds: 1, |
| 993 | sendSubtitleLogBatch: (entries) => |
| 994 | { |
| 995 | sentBatches.push(entries); |
| 996 | return Promise.resolve(); |
| 997 | }, |
| 998 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 999 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1000 | }), |
| 1001 | }); |
| 1002 | |
| 1003 | await flushAsyncWork(window, 8); |
| 1004 | controller.detach(); |
| 1005 | await flushAsyncWork(window, 4); |
| 1006 | |
| 1007 | const flattened = sentBatches.flat(); |
| 1008 | const prefetchEvents = flattened.filter((entry) => entry.requestKind === 'prefetch').map((entry) => entry.eventType); |
| 1009 | expect(prefetchEvents).toEqual(expect.arrayContaining([ |
| 1010 | 'subtitle_png_requested', |
| 1011 | 'subtitle_png_sent', |
| 1012 | 'subtitle_png_not_available', |
| 1013 | ])); |
| 1014 | |
| 1015 | window.close(); |
| 1016 | }); |
| 1017 | |
| 1018 | test('cache-only current mode avoids direct current subtitle fetch requests', async () => |
| 1019 | { |
| 1020 | const window = createWindow(); |
| 1021 | const helper = loadSharedVideoPlayer(window); |
| 1022 | const { player, videojs } = buildPlayerHarness(window); |
| 1023 | const sentBatches = []; |
| 1024 | |
| 1025 | player.setCurrentTime(8); |
| 1026 | window.fetch = jest.fn(async () => |
| 1027 | { |
| 1028 | return new Response(null, { |
| 1029 | status: 204, |
| 1030 | headers: { |
| 1031 | 'X-Subtitle-Frame-Status': 'none', |
| 1032 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 1033 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 1034 | }, |
| 1035 | }); |
| 1036 | }); |
| 1037 | |
| 1038 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1039 | sessionId: 'sess-1', |
| 1040 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1041 | }, { |
| 1042 | currentFetchEnabled: false, |
| 1043 | cacheMissFallbackEnabled: false, |
| 1044 | lookaheadSeconds: 0, |
| 1045 | sendSubtitleLogBatch: (entries) => |
| 1046 | { |
| 1047 | sentBatches.push(entries); |
| 1048 | return Promise.resolve(); |
| 1049 | }, |
| 1050 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1051 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1052 | }), |
| 1053 | }); |
| 1054 | |
| 1055 | await flushAsyncWork(window, 8); |
| 1056 | controller.detach(); |
| 1057 | await flushAsyncWork(window, 4); |
| 1058 | |
| 1059 | const flattened = sentBatches.flat(); |
| 1060 | expect(flattened.length).toBeGreaterThan(0); |
| 1061 | const requestKinds = new Set(flattened.map((entry) => entry.requestKind).filter(Boolean)); |
| 1062 | expect(requestKinds.has('prefetch')).toBe(true); |
| 1063 | expect(requestKinds.has('current')).toBe(false); |
| 1064 | |
| 1065 | window.close(); |
| 1066 | }); |
| 1067 | |
| 1068 | test('cache-only mode falls back to throttled current fetch on cache miss', async () => |
| 1069 | { |
| 1070 | const window = createWindow(); |
| 1071 | const helper = loadSharedVideoPlayer(window); |
| 1072 | const { player, videojs } = buildPlayerHarness(window); |
| 1073 | |
| 1074 | let requestCount = 0; |
| 1075 | window.fetch = jest.fn(async () => |
| 1076 | { |
| 1077 | requestCount += 1; |
| 1078 | if (requestCount === 1) |
| 1079 | { |
| 1080 | return new Response(new Uint8Array([137, 80, 78, 71]), { |
| 1081 | status: 200, |
| 1082 | headers: { |
| 1083 | 'Content-Type': 'image/png', |
| 1084 | 'X-Subtitle-Frame-Start-Time': '2.00', |
| 1085 | 'X-Subtitle-Frame-End-Time': '2.50', |
| 1086 | 'X-Subtitle-Frame-Width': '600', |
| 1087 | 'X-Subtitle-Frame-Height': '72', |
| 1088 | 'X-Subtitle-Frame-Canvas-Width': '1920', |
| 1089 | 'X-Subtitle-Frame-Canvas-Height': '1080', |
| 1090 | }, |
| 1091 | }); |
| 1092 | } |
| 1093 | |
| 1094 | return new Response(null, { |
| 1095 | status: 204, |
| 1096 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 1097 | }); |
| 1098 | }); |
| 1099 | |
| 1100 | player.setCurrentTime(2.2); |
| 1101 | |
| 1102 | helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1103 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1104 | }, { |
| 1105 | currentFetchEnabled: false, |
| 1106 | cacheMissFallbackIntervalMs: 2000, |
| 1107 | lookaheadSeconds: 0, |
| 1108 | initialPrefetchSeconds: 0, |
| 1109 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1110 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1111 | }), |
| 1112 | }); |
| 1113 | |
| 1114 | await flushAsyncWork(window, 10); |
| 1115 | |
| 1116 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 1117 | const overlayImg = overlay && overlay.querySelector('img'); |
| 1118 | expect(window.fetch).toHaveBeenCalled(); |
| 1119 | expect(overlay.style.display).toBe('block'); |
| 1120 | expect(overlayImg.getAttribute('src')).toBe('blob:subtitle'); |
| 1121 | |
| 1122 | window.close(); |
| 1123 | }); |
| 1124 | |
| 1125 | test('cache-miss fallback does not reset prefetch progress on repeated triggers', async () => |
| 1126 | { |
| 1127 | const window = createWindow(); |
| 1128 | const helper = loadSharedVideoPlayer(window); |
| 1129 | const { player, videojs } = buildPlayerHarness(window); |
| 1130 | |
| 1131 | let nowMs = 1000; |
| 1132 | const realNow = window.Date.now; |
| 1133 | window.Date.now = () => nowMs; |
| 1134 | |
| 1135 | let abortedCount = 0; |
| 1136 | window.fetch = jest.fn((url, init = {}) => |
| 1137 | { |
| 1138 | return new Promise((resolve, reject) => |
| 1139 | { |
| 1140 | const signal = init && init.signal; |
| 1141 | const onAbort = () => |
| 1142 | { |
| 1143 | abortedCount += 1; |
| 1144 | const err = new Error('aborted'); |
| 1145 | err.name = 'AbortError'; |
| 1146 | reject(err); |
| 1147 | }; |
| 1148 | |
| 1149 | if (signal && typeof signal.addEventListener === 'function') |
| 1150 | { |
| 1151 | if (signal.aborted) return onAbort(); |
| 1152 | signal.addEventListener('abort', onAbort, { once: true }); |
| 1153 | } |
| 1154 | |
| 1155 | window.setTimeout(() => |
| 1156 | { |
| 1157 | if (signal && signal.aborted) return; |
| 1158 | resolve(new Response(null, { |
| 1159 | status: 204, |
| 1160 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 1161 | })); |
| 1162 | }, 80); |
| 1163 | }); |
| 1164 | }); |
| 1165 | |
| 1166 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1167 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1168 | }, { |
| 1169 | currentFetchEnabled: false, |
| 1170 | cacheMissFallbackIntervalMs: 20, |
| 1171 | lookaheadSeconds: 2, |
| 1172 | initialPrefetchSeconds: 0, |
| 1173 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1174 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1175 | }), |
| 1176 | }); |
| 1177 | |
| 1178 | await flushAsyncWork(window, 4); |
| 1179 | |
| 1180 | player.setCurrentTime(5); |
| 1181 | player.trigger('timeupdate'); |
| 1182 | nowMs += 40; |
| 1183 | player.setCurrentTime(5.5); |
| 1184 | player.trigger('timeupdate'); |
| 1185 | nowMs += 40; |
| 1186 | player.setCurrentTime(6); |
| 1187 | player.trigger('timeupdate'); |
| 1188 | |
| 1189 | await flushAsyncWork(window, 12); |
| 1190 | expect(abortedCount).toBe(0); |
| 1191 | |
| 1192 | window.Date.now = realNow; |
| 1193 | controller.detach(); |
| 1194 | window.close(); |
| 1195 | }); |
| 1196 | |
| 1197 | test('cache-only mode performs initial warmup prefetch window on load', async () => |
| 1198 | { |
| 1199 | const window = createWindow(); |
| 1200 | const helper = loadSharedVideoPlayer(window); |
| 1201 | const { player, videojs } = buildPlayerHarness(window); |
| 1202 | const requestedTimes = []; |
| 1203 | |
| 1204 | window.fetch = jest.fn(async (url) => |
| 1205 | { |
| 1206 | const parsed = new URL(url, 'http://localhost/'); |
| 1207 | requestedTimes.push(Number(parsed.searchParams.get('timeSeconds'))); |
| 1208 | return new Response(null, { |
| 1209 | status: 204, |
| 1210 | headers: { |
| 1211 | 'X-Subtitle-Frame-Status': 'none', |
| 1212 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 1213 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 1214 | }, |
| 1215 | }); |
| 1216 | }); |
| 1217 | |
| 1218 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1219 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1220 | }, { |
| 1221 | currentFetchEnabled: false, |
| 1222 | lookaheadSeconds: 0, |
| 1223 | initialPrefetchSeconds: 2, |
| 1224 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1225 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1226 | }), |
| 1227 | }); |
| 1228 | |
| 1229 | await flushAsyncWork(window, 12); |
| 1230 | |
| 1231 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 0.5, 1, 1.5, 2])); |
| 1232 | |
| 1233 | controller.detach(); |
| 1234 | await flushAsyncWork(window, 2); |
| 1235 | window.close(); |
| 1236 | }); |
| 1237 | |
| 1238 | test('renders requested and received subtitle markers on the seek bar', async () => |
| 1239 | { |
| 1240 | const window = createWindow(); |
| 1241 | const helper = loadSharedVideoPlayer(window); |
| 1242 | const { player, videojs } = buildPlayerHarness(window); |
| 1243 | |
| 1244 | window.fetch = jest.fn(async () => |
| 1245 | { |
| 1246 | return new Response('png-data', { |
| 1247 | status: 200, |
| 1248 | headers: { |
| 1249 | 'Content-Type': 'image/png', |
| 1250 | 'X-Subtitle-Frame-Status': 'ok', |
| 1251 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 1252 | 'X-Subtitle-Response-Sent-At-Ms': '1012', |
| 1253 | 'X-Subtitle-Frame-Start-Time': '2.5', |
| 1254 | 'X-Subtitle-Frame-End-Time': '3.5', |
| 1255 | 'X-Subtitle-Frame-Width': '320', |
| 1256 | 'X-Subtitle-Frame-Height': '180', |
| 1257 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1258 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1259 | }, |
| 1260 | }); |
| 1261 | }); |
| 1262 | |
| 1263 | player.duration(10); |
| 1264 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1265 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1266 | }, { |
| 1267 | lookaheadSeconds: 0, |
| 1268 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1269 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1270 | }), |
| 1271 | }); |
| 1272 | |
| 1273 | await flushAsyncWork(window, 8); |
| 1274 | |
| 1275 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 1276 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 1277 | expect(requestedMarker).toBeTruthy(); |
| 1278 | expect(receivedMarker).toBeTruthy(); |
| 1279 | expect(requestedMarker.style.display).toBe('block'); |
| 1280 | expect(receivedMarker.style.display).toBe('block'); |
| 1281 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 1282 | expect(receivedMarker.style.left).toBe('25.0000%'); |
| 1283 | expect(requestedMarker.textContent).toBe(']'); |
| 1284 | expect(receivedMarker.textContent).toBe('['); |
| 1285 | |
| 1286 | controller.detach(); |
| 1287 | await new Promise((resolve) => window.setTimeout(resolve, 300)); |
| 1288 | await flushAsyncWork(window, 2); |
| 1289 | |
| 1290 | window.close(); |
| 1291 | }); |
| 1292 | |
| 1293 | test('advances both seek markers when subtitle request returns 204 without frame timing headers', async () => |
| 1294 | { |
| 1295 | const window = createWindow(); |
| 1296 | const helper = loadSharedVideoPlayer(window); |
| 1297 | const { player, videojs } = buildPlayerHarness(window); |
| 1298 | |
| 1299 | player.duration(10); |
| 1300 | player.setCurrentTime(4); |
| 1301 | window.fetch = jest.fn(async () => |
| 1302 | { |
| 1303 | return new Response(null, { |
| 1304 | status: 204, |
| 1305 | headers: { |
| 1306 | 'X-Subtitle-Frame-Status': 'none', |
| 1307 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 1308 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 1309 | }, |
| 1310 | }); |
| 1311 | }); |
| 1312 | |
| 1313 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1314 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1315 | }, { |
| 1316 | lookaheadSeconds: 0, |
| 1317 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1318 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1319 | }), |
| 1320 | }); |
| 1321 | |
| 1322 | await flushAsyncWork(window, 8); |
| 1323 | |
| 1324 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 1325 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 1326 | expect(requestedMarker).toBeTruthy(); |
| 1327 | expect(receivedMarker).toBeTruthy(); |
| 1328 | expect(requestedMarker.style.display).toBe('block'); |
| 1329 | expect(receivedMarker.style.display).toBe('block'); |
| 1330 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 1331 | expect(receivedMarker.style.left).toBe('100.0000%'); |
| 1332 | expect(requestedMarker.textContent).toBe(']'); |
| 1333 | expect(receivedMarker.textContent).toBe('['); |
| 1334 | |
| 1335 | controller.detach(); |
| 1336 | await flushAsyncWork(window, 2); |
| 1337 | window.close(); |
| 1338 | }); |
| 1339 | |
| 1340 | test('advances seek markers to the end of an empty prefetch tail window', async () => |
| 1341 | { |
| 1342 | const window = createWindow(); |
| 1343 | const helper = loadSharedVideoPlayer(window); |
| 1344 | const { player, videojs } = buildPlayerHarness(window); |
| 1345 | |
| 1346 | player.duration(120); |
| 1347 | player.setCurrentTime(110); |
| 1348 | |
| 1349 | window.fetch = jest.fn(async () => |
| 1350 | { |
| 1351 | return new Response(null, { |
| 1352 | status: 204, |
| 1353 | headers: { |
| 1354 | 'X-Subtitle-Frame-Status': 'none', |
| 1355 | 'X-Subtitle-Request-Received-At-Ms': '1000', |
| 1356 | 'X-Subtitle-Response-Sent-At-Ms': '1010', |
| 1357 | }, |
| 1358 | }); |
| 1359 | }); |
| 1360 | |
| 1361 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1362 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1363 | }, { |
| 1364 | lookaheadSeconds: 0, |
| 1365 | initialPrefetchSeconds: 0, |
| 1366 | currentFetchEnabled: false, |
| 1367 | cacheMissFallbackEnabled: false, |
| 1368 | prefetchAdaptiveStepEnabled: true, |
| 1369 | prefetchStepSecondsMin: 10, |
| 1370 | prefetchStepSecondsMax: 10, |
| 1371 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1372 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1373 | }), |
| 1374 | buildPrefetchRequest: ({ currentTime, endTime, streamIndex }) => ({ |
| 1375 | url: `/subtitle-range?streamIndex=${streamIndex}&startTimeSeconds=${currentTime}&endTimeSeconds=${endTime}`, |
| 1376 | }), |
| 1377 | }); |
| 1378 | |
| 1379 | player.trigger('timeupdate'); |
| 1380 | await flushAsyncWork(window, 8); |
| 1381 | |
| 1382 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 1383 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 1384 | expect(requestedMarker).toBeTruthy(); |
| 1385 | expect(receivedMarker).toBeTruthy(); |
| 1386 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 1387 | expect(receivedMarker.style.left).toBe('100.0000%'); |
| 1388 | |
| 1389 | controller.detach(); |
| 1390 | await flushAsyncWork(window, 2); |
| 1391 | window.close(); |
| 1392 | }); |
| 1393 | |
| 1394 | test('keeps existing marker positions while the next request is still in flight', async () => |
| 1395 | { |
| 1396 | const window = createWindow(); |
| 1397 | const helper = loadSharedVideoPlayer(window); |
| 1398 | const { player, videojs } = buildPlayerHarness(window); |
| 1399 | let pendingResolve = null; |
| 1400 | |
| 1401 | player.duration(10); |
| 1402 | window.fetch = jest.fn((url) => |
| 1403 | { |
| 1404 | const parsed = new URL(url, 'http://localhost/'); |
| 1405 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1406 | |
| 1407 | if (timeSeconds === 0) |
| 1408 | { |
| 1409 | return Promise.resolve(new Response('png-data', { |
| 1410 | status: 200, |
| 1411 | headers: { |
| 1412 | 'Content-Type': 'image/png', |
| 1413 | 'X-Subtitle-Frame-Status': 'ok', |
| 1414 | 'X-Subtitle-Frame-Start-Time': '2', |
| 1415 | 'X-Subtitle-Frame-End-Time': '3', |
| 1416 | 'X-Subtitle-Frame-Width': '320', |
| 1417 | 'X-Subtitle-Frame-Height': '180', |
| 1418 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1419 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1420 | }, |
| 1421 | })); |
| 1422 | } |
| 1423 | |
| 1424 | return new Promise((resolve) => |
| 1425 | { |
| 1426 | pendingResolve = resolve; |
| 1427 | }); |
| 1428 | }); |
| 1429 | |
| 1430 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1431 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1432 | }, { |
| 1433 | lookaheadSeconds: 0, |
| 1434 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1435 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1436 | }), |
| 1437 | }); |
| 1438 | |
| 1439 | await flushAsyncWork(window, 8); |
| 1440 | |
| 1441 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 1442 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 1443 | expect(requestedMarker).toBeTruthy(); |
| 1444 | expect(receivedMarker).toBeTruthy(); |
| 1445 | expect(requestedMarker.style.left).toBe('0.0000%'); |
| 1446 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 1447 | |
| 1448 | player.setCurrentTime(1.5); |
| 1449 | player.trigger('timeupdate'); |
| 1450 | await flushAsyncWork(window, 2); |
| 1451 | |
| 1452 | expect(requestedMarker.style.left).toBe('0.0000%'); |
| 1453 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 1454 | |
| 1455 | pendingResolve(new Response(null, { |
| 1456 | status: 204, |
| 1457 | headers: { |
| 1458 | 'X-Subtitle-Frame-Status': 'none', |
| 1459 | }, |
| 1460 | })); |
| 1461 | |
| 1462 | await flushAsyncWork(window, 8); |
| 1463 | expect(requestedMarker.style.display).toBe('block'); |
| 1464 | expect(receivedMarker.style.display).toBe('block'); |
| 1465 | expect(requestedMarker.style.left).toBe('15.0000%'); |
| 1466 | expect(receivedMarker.style.left).toBe('20.0000%'); |
| 1467 | |
| 1468 | controller.detach(); |
| 1469 | await flushAsyncWork(window, 2); |
| 1470 | window.close(); |
| 1471 | }); |
| 1472 | |
| 1473 | test('keeps marker positions forward-only when later responses resolve with earlier times', async () => |
| 1474 | { |
| 1475 | const window = createWindow(); |
| 1476 | const helper = loadSharedVideoPlayer(window); |
| 1477 | const { player, videojs } = buildPlayerHarness(window); |
| 1478 | const requestedTimes = []; |
| 1479 | |
| 1480 | player.duration(12); |
| 1481 | window.fetch = jest.fn(async (url) => |
| 1482 | { |
| 1483 | const parsed = new URL(url, 'http://localhost/'); |
| 1484 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1485 | requestedTimes.push(timeSeconds); |
| 1486 | |
| 1487 | if (requestedTimes.length === 1) |
| 1488 | { |
| 1489 | return new Response('png-data', { |
| 1490 | status: 200, |
| 1491 | headers: { |
| 1492 | 'Content-Type': 'image/png', |
| 1493 | 'X-Subtitle-Frame-Status': 'ok', |
| 1494 | 'X-Subtitle-Frame-Start-Time': '4', |
| 1495 | 'X-Subtitle-Frame-End-Time': '5', |
| 1496 | 'X-Subtitle-Frame-Width': '320', |
| 1497 | 'X-Subtitle-Frame-Height': '180', |
| 1498 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1499 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1500 | }, |
| 1501 | }); |
| 1502 | } |
| 1503 | |
| 1504 | return new Response('png-data', { |
| 1505 | status: 200, |
| 1506 | headers: { |
| 1507 | 'Content-Type': 'image/png', |
| 1508 | 'X-Subtitle-Frame-Status': 'ok', |
| 1509 | 'X-Subtitle-Frame-Start-Time': '2', |
| 1510 | 'X-Subtitle-Frame-End-Time': '3', |
| 1511 | 'X-Subtitle-Frame-Width': '320', |
| 1512 | 'X-Subtitle-Frame-Height': '180', |
| 1513 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1514 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1515 | }, |
| 1516 | }); |
| 1517 | }); |
| 1518 | |
| 1519 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1520 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1521 | }, { |
| 1522 | lookaheadSeconds: 0, |
| 1523 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1524 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1525 | }), |
| 1526 | }); |
| 1527 | |
| 1528 | await flushAsyncWork(window, 8); |
| 1529 | |
| 1530 | const requestedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-right'); |
| 1531 | const receivedMarker = player.el().querySelector('.vjs-image-subtitle-seek-marker-left'); |
| 1532 | expect(requestedMarker).toBeTruthy(); |
| 1533 | expect(receivedMarker).toBeTruthy(); |
| 1534 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 1535 | expect(receivedMarker.style.left).toBe('33.3333%'); |
| 1536 | |
| 1537 | player.setCurrentTime(1.5); |
| 1538 | player.trigger('timeupdate'); |
| 1539 | await flushAsyncWork(window, 8); |
| 1540 | |
| 1541 | // Second response reports an older start time, but markers stay monotonic. |
| 1542 | expect(requestedMarker.style.left).toBe('100.0000%'); |
| 1543 | expect(receivedMarker.style.left).toBe('33.3333%'); |
| 1544 | |
| 1545 | controller.detach(); |
| 1546 | await flushAsyncWork(window, 2); |
| 1547 | window.close(); |
| 1548 | }); |
| 1549 | |
| 1550 | test('continues current subtitle refreshes after a force-seek aborts the in-flight request', async () => |
| 1551 | { |
| 1552 | const window = createWindow(); |
| 1553 | const helper = loadSharedVideoPlayer(window); |
| 1554 | const { player, videojs } = buildPlayerHarness(window); |
| 1555 | const requestedTimes = []; |
| 1556 | |
| 1557 | window.fetch = jest.fn((url, init = {}) => |
| 1558 | { |
| 1559 | const parsed = new URL(url, 'http://localhost/'); |
| 1560 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1561 | requestedTimes.push(timeSeconds); |
| 1562 | |
| 1563 | if (requestedTimes.length === 1) |
| 1564 | { |
| 1565 | return new Promise((resolve, reject) => |
| 1566 | { |
| 1567 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 1568 | { |
| 1569 | init.signal.addEventListener('abort', () => |
| 1570 | { |
| 1571 | reject(new window.DOMException('Aborted', 'AbortError')); |
| 1572 | }, { once: true }); |
| 1573 | } |
| 1574 | }); |
| 1575 | } |
| 1576 | |
| 1577 | return Promise.resolve(new Response(null, { |
| 1578 | status: 204, |
| 1579 | headers: { |
| 1580 | 'X-Subtitle-Frame-Status': 'none', |
| 1581 | }, |
| 1582 | })); |
| 1583 | }); |
| 1584 | |
| 1585 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1586 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1587 | }, { |
| 1588 | lookaheadSeconds: 0, |
| 1589 | currentRequestTimeoutMs: 300, |
| 1590 | logToConsole: false, |
| 1591 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1592 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1593 | }), |
| 1594 | }); |
| 1595 | |
| 1596 | await flushAsyncWork(window, 4); |
| 1597 | const afterInitialLoadCalls = window.fetch.mock.calls.length; |
| 1598 | expect(afterInitialLoadCalls).toBeGreaterThan(0); |
| 1599 | |
| 1600 | player.setCurrentTime(5); |
| 1601 | player.trigger('seeking'); |
| 1602 | await flushAsyncWork(window, 20); |
| 1603 | |
| 1604 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 1605 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 5])); |
| 1606 | |
| 1607 | controller.detach(); |
| 1608 | await flushAsyncWork(window, 2); |
| 1609 | window.close(); |
| 1610 | }); |
| 1611 | |
| 1612 | test('recovers when a current subtitle request hangs and times out', async () => |
| 1613 | { |
| 1614 | const window = createWindow(); |
| 1615 | const helper = loadSharedVideoPlayer(window); |
| 1616 | const { player, videojs } = buildPlayerHarness(window); |
| 1617 | const requestedTimes = []; |
| 1618 | |
| 1619 | window.fetch = jest.fn((url, init = {}) => |
| 1620 | { |
| 1621 | const parsed = new URL(url, 'http://localhost/'); |
| 1622 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1623 | requestedTimes.push(timeSeconds); |
| 1624 | |
| 1625 | if (requestedTimes.length === 1) |
| 1626 | { |
| 1627 | return new Promise((resolve, reject) => |
| 1628 | { |
| 1629 | if (init.signal && typeof init.signal.addEventListener === 'function') |
| 1630 | { |
| 1631 | init.signal.addEventListener('abort', () => |
| 1632 | { |
| 1633 | reject(new window.DOMException('Aborted', 'AbortError')); |
| 1634 | }, { once: true }); |
| 1635 | } |
| 1636 | }); |
| 1637 | } |
| 1638 | |
| 1639 | return Promise.resolve(new Response(null, { |
| 1640 | status: 204, |
| 1641 | headers: { 'X-Subtitle-Frame-Status': 'none' }, |
| 1642 | })); |
| 1643 | }); |
| 1644 | |
| 1645 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1646 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1647 | }, { |
| 1648 | lookaheadSeconds: 0, |
| 1649 | currentRequestTimeoutMs: 250, |
| 1650 | logToConsole: false, |
| 1651 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1652 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1653 | }), |
| 1654 | }); |
| 1655 | |
| 1656 | await flushAsyncWork(window, 3); |
| 1657 | |
| 1658 | player.setCurrentTime(1.5); |
| 1659 | player.trigger('timeupdate'); |
| 1660 | |
| 1661 | await new Promise((resolve) => window.setTimeout(resolve, 320)); |
| 1662 | player.trigger('timeupdate'); |
| 1663 | await new Promise((resolve) => window.setTimeout(resolve, 220)); |
| 1664 | await flushAsyncWork(window, 20); |
| 1665 | |
| 1666 | expect(window.fetch.mock.calls.length).toBeGreaterThanOrEqual(2); |
| 1667 | expect(requestedTimes).toEqual(expect.arrayContaining([0, 1.5])); |
| 1668 | |
| 1669 | controller.detach(); |
| 1670 | await new Promise((resolve) => window.setTimeout(resolve, 200)); |
| 1671 | await flushAsyncWork(window, 20); |
| 1672 | window.close(); |
| 1673 | }); |
| 1674 | |
| 1675 | test('reuses cached subtitle frames after rewinding when server no longer has older frames', async () => |
| 1676 | { |
| 1677 | const window = createWindow(); |
| 1678 | const helper = loadSharedVideoPlayer(window); |
| 1679 | const { player, videojs } = buildPlayerHarness(window); |
| 1680 | |
| 1681 | player.duration(10); |
| 1682 | player.setCurrentTime(2); |
| 1683 | window.fetch = jest.fn(async (url) => |
| 1684 | { |
| 1685 | const parsed = new URL(url, 'http://localhost/'); |
| 1686 | const timeSeconds = Number(parsed.searchParams.get('timeSeconds')); |
| 1687 | |
| 1688 | if (Math.abs(timeSeconds - 2) < 1e-6) |
| 1689 | { |
| 1690 | return new Response('png-data', { |
| 1691 | status: 200, |
| 1692 | headers: { |
| 1693 | 'Content-Type': 'image/png', |
| 1694 | 'X-Subtitle-Frame-Status': 'ok', |
| 1695 | 'X-Subtitle-Frame-Start-Time': '2', |
| 1696 | 'X-Subtitle-Frame-End-Time': '3', |
| 1697 | 'X-Subtitle-Frame-Width': '320', |
| 1698 | 'X-Subtitle-Frame-Height': '180', |
| 1699 | 'X-Subtitle-Frame-Canvas-Width': '640', |
| 1700 | 'X-Subtitle-Frame-Canvas-Height': '360', |
| 1701 | }, |
| 1702 | }); |
| 1703 | } |
| 1704 | |
| 1705 | return new Response(null, { |
| 1706 | status: 204, |
| 1707 | headers: { |
| 1708 | 'X-Subtitle-Frame-Status': 'none', |
| 1709 | }, |
| 1710 | }); |
| 1711 | }); |
| 1712 | |
| 1713 | const controller = helper.bindImageSubtitleOverlay(player, videojs, {}, { |
| 1714 | imageSubtitleTracks: [{ label: 'PGS English', streamIndex: 0, default: true }], |
| 1715 | }, { |
| 1716 | lookaheadSeconds: 0, |
| 1717 | buildFrameRequest: ({ currentTime, streamIndex }) => ({ |
| 1718 | url: `/subtitle-frame?streamIndex=${streamIndex}&timeSeconds=${currentTime}`, |
| 1719 | }), |
| 1720 | }); |
| 1721 | |
| 1722 | await flushAsyncWork(window, 8); |
| 1723 | const afterInitialLoadCalls = window.fetch.mock.calls.length; |
| 1724 | expect(afterInitialLoadCalls).toBeGreaterThan(0); |
| 1725 | |
| 1726 | player.setCurrentTime(4); |
| 1727 | player.trigger('timeupdate'); |
| 1728 | await flushAsyncWork(window, 8); |
| 1729 | const afterForwardSeekCalls = window.fetch.mock.calls.length; |
| 1730 | expect(afterForwardSeekCalls).toBeGreaterThanOrEqual(afterInitialLoadCalls); |
| 1731 | |
| 1732 | const overlay = player.el().querySelector('.video-subtitle-overlay'); |
| 1733 | expect(overlay).toBeTruthy(); |
| 1734 | expect(overlay.style.display).toBe('none'); |
| 1735 | |
| 1736 | player.setCurrentTime(2.5); |
| 1737 | player.trigger('timeupdate'); |
| 1738 | await flushAsyncWork(window, 8); |
| 1739 | |
| 1740 | expect(window.fetch.mock.calls.length).toBe(afterForwardSeekCalls); |
| 1741 | expect(overlay.style.display).toBe('block'); |
| 1742 | |
| 1743 | controller.detach(); |
| 1744 | await flushAsyncWork(window, 2); |
| 1745 | window.close(); |
| 1746 | }); |
| 1747 | |
| 1748 | test('registers stream cache service worker once when supported', async () => |
| 1749 | { |
| 1750 | const window = createWindow(); |
| 1751 | const helper = loadSharedVideoPlayer(window); |
| 1752 | const register = jest.fn(async () => ({})); |
| 1753 | window.navigator.serviceWorker = { register }; |
| 1754 | |
| 1755 | const first = await helper.enableVideoStreamClientCache(); |
| 1756 | const second = await helper.enableVideoStreamClientCache(); |
| 1757 | |
| 1758 | expect(first).toBe(true); |
| 1759 | expect(second).toBe(true); |
| 1760 | expect(register).toHaveBeenCalledTimes(1); |
| 1761 | expect(register).toHaveBeenCalledWith('/stream-cache-sw.js', { |
| 1762 | scope: '/', |
| 1763 | updateViaCache: 'none', |
| 1764 | }); |
| 1765 | |
| 1766 | window.close(); |
| 1767 | }); |
| 1768 | |
| 1769 | test('stream-ahead prefetch follows playlists and fetches segment assets', async () => |
| 1770 | { |
| 1771 | const window = createWindow(); |
| 1772 | const helper = loadSharedVideoPlayer(window); |
| 1773 | const requestedUrls = []; |
| 1774 | |
| 1775 | window.fetch = jest.fn(async (input) => |
| 1776 | { |
| 1777 | const url = String(input && input.url ? input.url : input); |
| 1778 | requestedUrls.push(url); |
| 1779 | |
| 1780 | if (url === 'http://localhost/master.m3u8') |
| 1781 | { |
| 1782 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1783 | status: 200, |
| 1784 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1785 | }); |
| 1786 | } |
| 1787 | |
| 1788 | if (url === 'http://localhost/stream_0.m3u8') |
| 1789 | { |
| 1790 | return new Response('#EXTM3U\n#EXTINF:4,\nseg0.ts\n#EXTINF:4,\nseg1.ts\n#EXT-X-ENDLIST\n', { |
| 1791 | status: 200, |
| 1792 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1793 | }); |
| 1794 | } |
| 1795 | |
| 1796 | if (url === 'http://localhost/seg0.ts' || url === 'http://localhost/seg1.ts') |
| 1797 | { |
| 1798 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1799 | status: 200, |
| 1800 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1801 | }); |
| 1802 | } |
| 1803 | |
| 1804 | return new Response(null, { status: 404 }); |
| 1805 | }); |
| 1806 | |
| 1807 | const controller = helper.bindStreamAheadPrefetch({ |
| 1808 | playlistUrl: 'http://localhost/master.m3u8', |
| 1809 | pollIntervalMs: 0, |
| 1810 | credentials: 'same-origin', |
| 1811 | }); |
| 1812 | |
| 1813 | expect(controller).toBeTruthy(); |
| 1814 | await controller.done(); |
| 1815 | |
| 1816 | expect(requestedUrls).toEqual(expect.arrayContaining([ |
| 1817 | 'http://localhost/master.m3u8', |
| 1818 | 'http://localhost/stream_0.m3u8', |
| 1819 | 'http://localhost/seg0.ts', |
| 1820 | 'http://localhost/seg1.ts', |
| 1821 | ])); |
| 1822 | |
| 1823 | controller.stop(); |
| 1824 | window.close(); |
| 1825 | }); |
| 1826 | |
| 1827 | test('stream-ahead prefetch reports cached-through progress for fetched segments', async () => |
| 1828 | { |
| 1829 | const window = createWindow(); |
| 1830 | const helper = loadSharedVideoPlayer(window); |
| 1831 | const progressSamples = []; |
| 1832 | |
| 1833 | window.fetch = jest.fn(async (input) => |
| 1834 | { |
| 1835 | const url = String(input && input.url ? input.url : input); |
| 1836 | |
| 1837 | if (url === 'http://localhost/master.m3u8') |
| 1838 | { |
| 1839 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1840 | status: 200, |
| 1841 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1842 | }); |
| 1843 | } |
| 1844 | |
| 1845 | if (url === 'http://localhost/stream_0.m3u8') |
| 1846 | { |
| 1847 | return new Response('#EXTM3U\n#EXTINF:4,\nseg0.ts\n#EXTINF:4,\nseg1.ts\n#EXT-X-ENDLIST\n', { |
| 1848 | status: 200, |
| 1849 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1850 | }); |
| 1851 | } |
| 1852 | |
| 1853 | if (url === 'http://localhost/seg0.ts' || url === 'http://localhost/seg1.ts') |
| 1854 | { |
| 1855 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1856 | status: 200, |
| 1857 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1858 | }); |
| 1859 | } |
| 1860 | |
| 1861 | return new Response(null, { status: 404 }); |
| 1862 | }); |
| 1863 | |
| 1864 | const controller = helper.bindStreamAheadPrefetch({ |
| 1865 | playlistUrl: 'http://localhost/master.m3u8', |
| 1866 | pollIntervalMs: 0, |
| 1867 | credentials: 'same-origin', |
| 1868 | }); |
| 1869 | |
| 1870 | expect(controller).toBeTruthy(); |
| 1871 | const unsubscribe = controller.subscribe((status) => |
| 1872 | { |
| 1873 | progressSamples.push(status && status.cachedThroughSeconds); |
| 1874 | }); |
| 1875 | |
| 1876 | await controller.done(); |
| 1877 | unsubscribe(); |
| 1878 | |
| 1879 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 1880 | |
| 1881 | controller.stop(); |
| 1882 | window.close(); |
| 1883 | }); |
| 1884 | |
| 1885 | test('stream-ahead prefetch infers cached-through progress from segment filenames', async () => |
| 1886 | { |
| 1887 | const window = createWindow(); |
| 1888 | const helper = loadSharedVideoPlayer(window); |
| 1889 | const progressSamples = []; |
| 1890 | |
| 1891 | window.fetch = jest.fn(async (input) => |
| 1892 | { |
| 1893 | const url = String(input && input.url ? input.url : input); |
| 1894 | |
| 1895 | if (url === 'http://localhost/master.m3u8') |
| 1896 | { |
| 1897 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1898 | status: 200, |
| 1899 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1900 | }); |
| 1901 | } |
| 1902 | |
| 1903 | if (url === 'http://localhost/stream_0.m3u8') |
| 1904 | { |
| 1905 | return new Response('#EXTM3U\nstream_0_segment_000000.ts\nstream_0_segment_000001.ts\n#EXT-X-ENDLIST\n', { |
| 1906 | status: 200, |
| 1907 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1908 | }); |
| 1909 | } |
| 1910 | |
| 1911 | if (url === 'http://localhost/stream_0_segment_000000.ts' || url === 'http://localhost/stream_0_segment_000001.ts') |
| 1912 | { |
| 1913 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1914 | status: 200, |
| 1915 | headers: { 'Content-Type': 'video/mp2t' }, |
| 1916 | }); |
| 1917 | } |
| 1918 | |
| 1919 | return new Response(null, { status: 404 }); |
| 1920 | }); |
| 1921 | |
| 1922 | const controller = helper.bindStreamAheadPrefetch({ |
| 1923 | playlistUrl: 'http://localhost/master.m3u8', |
| 1924 | pollIntervalMs: 0, |
| 1925 | credentials: 'same-origin', |
| 1926 | }); |
| 1927 | |
| 1928 | expect(controller).toBeTruthy(); |
| 1929 | const unsubscribe = controller.subscribe((status) => |
| 1930 | { |
| 1931 | progressSamples.push(status && status.cachedThroughSeconds); |
| 1932 | }); |
| 1933 | |
| 1934 | await controller.done(); |
| 1935 | unsubscribe(); |
| 1936 | |
| 1937 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 1938 | |
| 1939 | controller.stop(); |
| 1940 | window.close(); |
| 1941 | }); |
| 1942 | |
| 1943 | test('stream-ahead prefetch infers cached-through progress for m4s segment filenames', async () => |
| 1944 | { |
| 1945 | const window = createWindow(); |
| 1946 | const helper = loadSharedVideoPlayer(window); |
| 1947 | const progressSamples = []; |
| 1948 | |
| 1949 | window.fetch = jest.fn(async (input) => |
| 1950 | { |
| 1951 | const url = String(input && input.url ? input.url : input); |
| 1952 | |
| 1953 | if (url === 'http://localhost/master.m3u8') |
| 1954 | { |
| 1955 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 1956 | status: 200, |
| 1957 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1958 | }); |
| 1959 | } |
| 1960 | |
| 1961 | if (url === 'http://localhost/stream_0.m3u8') |
| 1962 | { |
| 1963 | return new Response('#EXTM3U\nstream_0_segment_000000.m4s\nstream_0_segment_000001.m4s\n#EXT-X-ENDLIST\n', { |
| 1964 | status: 200, |
| 1965 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 1966 | }); |
| 1967 | } |
| 1968 | |
| 1969 | if (url === 'http://localhost/stream_0_segment_000000.m4s' || url === 'http://localhost/stream_0_segment_000001.m4s') |
| 1970 | { |
| 1971 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 1972 | status: 200, |
| 1973 | headers: { 'Content-Type': 'video/mp4' }, |
| 1974 | }); |
| 1975 | } |
| 1976 | |
| 1977 | return new Response(null, { status: 404 }); |
| 1978 | }); |
| 1979 | |
| 1980 | const controller = helper.bindStreamAheadPrefetch({ |
| 1981 | playlistUrl: 'http://localhost/master.m3u8', |
| 1982 | pollIntervalMs: 0, |
| 1983 | credentials: 'same-origin', |
| 1984 | }); |
| 1985 | |
| 1986 | expect(controller).toBeTruthy(); |
| 1987 | const unsubscribe = controller.subscribe((status) => |
| 1988 | { |
| 1989 | progressSamples.push(status && status.cachedThroughSeconds); |
| 1990 | }); |
| 1991 | |
| 1992 | await controller.done(); |
| 1993 | unsubscribe(); |
| 1994 | |
| 1995 | expect(progressSamples.some((value) => Number.isFinite(value) && value >= 8)).toBe(true); |
| 1996 | |
| 1997 | controller.stop(); |
| 1998 | window.close(); |
| 1999 | }); |
| 2000 | |
| 2001 | test('stream-ahead prefetch reports ratio progress from fetched sections when timing is unknown', async () => |
| 2002 | { |
| 2003 | const window = createWindow(); |
| 2004 | const helper = loadSharedVideoPlayer(window); |
| 2005 | const statusSamples = []; |
| 2006 | |
| 2007 | window.fetch = jest.fn(async (input) => |
| 2008 | { |
| 2009 | const url = String(input && input.url ? input.url : input); |
| 2010 | |
| 2011 | if (url === 'http://localhost/master.m3u8') |
| 2012 | { |
| 2013 | return new Response('#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=100000\nstream_0.m3u8\n#EXT-X-ENDLIST\n', { |
| 2014 | status: 200, |
| 2015 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 2016 | }); |
| 2017 | } |
| 2018 | |
| 2019 | if (url === 'http://localhost/stream_0.m3u8') |
| 2020 | { |
| 2021 | return new Response('#EXTM3U\nchunk-alpha.bin\nchunk-beta.bin\n#EXT-X-ENDLIST\n', { |
| 2022 | status: 200, |
| 2023 | headers: { 'Content-Type': 'application/vnd.apple.mpegurl' }, |
| 2024 | }); |
| 2025 | } |
| 2026 | |
| 2027 | if (url === 'http://localhost/chunk-alpha.bin' || url === 'http://localhost/chunk-beta.bin') |
| 2028 | { |
| 2029 | return new Response(new Uint8Array([1, 2, 3, 4]), { |
| 2030 | status: 200, |
| 2031 | headers: { 'Content-Type': 'application/octet-stream' }, |
| 2032 | }); |
| 2033 | } |
| 2034 | |
| 2035 | return new Response(null, { status: 404 }); |
| 2036 | }); |
| 2037 | |
| 2038 | const controller = helper.bindStreamAheadPrefetch({ |
| 2039 | playlistUrl: 'http://localhost/master.m3u8', |
| 2040 | pollIntervalMs: 0, |
| 2041 | credentials: 'same-origin', |
| 2042 | }); |
| 2043 | |
| 2044 | expect(controller).toBeTruthy(); |
| 2045 | const unsubscribe = controller.subscribe((status) => |
| 2046 | { |
| 2047 | statusSamples.push(status || {}); |
| 2048 | }); |
| 2049 | |
| 2050 | await controller.done(); |
| 2051 | unsubscribe(); |
| 2052 | |
| 2053 | const finalStatus = statusSamples[statusSamples.length - 1] || {}; |
| 2054 | expect(finalStatus.totalSegmentCount).toBe(2); |
| 2055 | expect(finalStatus.fetchedSegmentCount).toBe(2); |
| 2056 | expect(finalStatus.cachedThroughRatio).toBe(1); |
| 2057 | |
| 2058 | controller.stop(); |
| 2059 | window.close(); |
| 2060 | }); |
| 2061 | |
| 2062 | test('clears cached stream assets for a finished playlist session', async () => |
| 2063 | { |
| 2064 | const window = createWindow(); |
| 2065 | const helper = loadSharedVideoPlayer(window); |
| 2066 | const requests = [ |
| 2067 | new window.Request('http://localhost/api/files/video-stream/sess-a/master.m3u8'), |
| 2068 | new window.Request('http://localhost/api/files/video-stream/sess-a/stream_0_segment_000001.ts'), |
| 2069 | new window.Request('http://localhost/api/files/video-stream/sess-b/master.m3u8'), |
| 2070 | ]; |
| 2071 | const deleteCalls = []; |
| 2072 | const cache = { |
| 2073 | keys: jest.fn(async () => requests), |
| 2074 | delete: jest.fn(async (request) => |
| 2075 | { |
| 2076 | deleteCalls.push(request.url); |
| 2077 | return true; |
| 2078 | }), |
| 2079 | }; |
| 2080 | window.caches = { |
| 2081 | open: jest.fn(async () => cache), |
| 2082 | }; |
| 2083 | |
| 2084 | await helper.clearVideoStreamClientCache('http://localhost/api/files/video-stream/sess-a/master.m3u8'); |
| 2085 | |
| 2086 | expect(window.caches.open).toHaveBeenCalledWith('filemanager-stream-cache-v1'); |
| 2087 | expect(cache.keys).toHaveBeenCalledTimes(1); |
| 2088 | expect(deleteCalls).toEqual(expect.arrayContaining([ |
| 2089 | 'http://localhost/api/files/video-stream/sess-a/master.m3u8', |
| 2090 | 'http://localhost/api/files/video-stream/sess-a/stream_0_segment_000001.ts', |
| 2091 | ])); |
| 2092 | expect(deleteCalls).not.toContain('http://localhost/api/files/video-stream/sess-b/master.m3u8'); |
| 2093 | |
| 2094 | window.close(); |
| 2095 | }); |
| 2096 | }); |