resources/js/player/renewal.js: erneuernder hls.js-Lader (je Abruf eine Wiederholung bei 403, höchstens 20 Erneuerungen, nach Ablehnung keine weitere). source.js bindet die Quelle (hls.js oder nativ in Safari, mit Wiederaufnahme an der Stelle) und zeigt bei endgültigem Fehler oder abgelehnter Erneuerung eine Meldung. Ohne data-hls-renew (Vorschau, Auswahl) bleibt das Verhalten wie bisher. Vitest als Entwicklungsabhängigkeit, 24 Tests der Logik. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
220 lines
7.6 KiB
JavaScript
220 lines
7.6 KiB
JavaScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { attachSource } from './source.js';
|
|
|
|
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
|
|
function fakeHls({ supported = true } = {}) {
|
|
const instances = [];
|
|
|
|
class Hls {
|
|
static isSupported() {
|
|
return supported;
|
|
}
|
|
|
|
constructor(config) {
|
|
this.config = config;
|
|
this.handlers = {};
|
|
this.calls = [];
|
|
instances.push(this);
|
|
}
|
|
|
|
on(name, handler) {
|
|
this.handlers[name] = handler;
|
|
}
|
|
|
|
loadSource(url) {
|
|
this.calls.push(['loadSource', url]);
|
|
}
|
|
|
|
attachMedia(video) {
|
|
this.calls.push(['attachMedia', video]);
|
|
}
|
|
|
|
recoverMediaError() {
|
|
this.calls.push(['recoverMediaError']);
|
|
}
|
|
|
|
destroy() {
|
|
this.calls.push(['destroy']);
|
|
}
|
|
}
|
|
|
|
Hls.Events = { ERROR: 'hlsError' };
|
|
Hls.ErrorTypes = { MEDIA_ERROR: 'mediaError', NETWORK_ERROR: 'networkError' };
|
|
Hls.DefaultConfig = { loader: class DefaultLoader {} };
|
|
|
|
return { Hls, instances };
|
|
}
|
|
|
|
function fakeVideo({ native = false } = {}) {
|
|
const listeners = {};
|
|
const inserted = [];
|
|
|
|
return {
|
|
dataset: {},
|
|
paused: true,
|
|
currentTime: 0,
|
|
src: '',
|
|
inserted,
|
|
listeners,
|
|
canPlayType: () => (native ? 'maybe' : ''),
|
|
addEventListener(name, handler) {
|
|
listeners[name] = handler;
|
|
},
|
|
play: vi.fn(async () => undefined),
|
|
closest: () => ({ insertAdjacentElement: (where, element) => inserted.push([where, element]) }),
|
|
};
|
|
}
|
|
|
|
describe('attachSource ohne Erneuerungsadresse (Vorschau, Auswahl)', () => {
|
|
it('bindet hls.js wie bisher, ohne eigenen Lader und ohne Fehlerhaken', () => {
|
|
const { Hls, instances } = fakeHls();
|
|
const video = fakeVideo();
|
|
|
|
attachSource(video, { Hls, source: 'https://luf.test/m.m3u8' });
|
|
|
|
expect(instances[0].config).toEqual({});
|
|
expect(instances[0].handlers).toEqual({});
|
|
expect(instances[0].calls).toEqual([['loadSource', 'https://luf.test/m.m3u8'], ['attachMedia', video]]);
|
|
});
|
|
|
|
it('setzt in Safari die Quelle direkt und hängt nichts an', () => {
|
|
const { Hls } = fakeHls({ supported: false });
|
|
const video = fakeVideo({ native: true });
|
|
|
|
attachSource(video, { Hls, source: 'https://luf.test/m.m3u8' });
|
|
|
|
expect(video.src).toBe('https://luf.test/m.m3u8');
|
|
expect(video.listeners.error).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('attachSource mit Erneuerungsadresse (Mitgliederbereich)', () => {
|
|
beforeEach(() => {
|
|
vi.stubGlobal('document', {
|
|
createElement: () => ({
|
|
className: '',
|
|
textContent: '',
|
|
attributes: {},
|
|
setAttribute(name, value) {
|
|
this.attributes[name] = value;
|
|
},
|
|
}),
|
|
});
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it('nutzt den erneuernden Lader', () => {
|
|
const { Hls, instances } = fakeHls();
|
|
|
|
attachSource(fakeVideo(), { Hls, source: 'https://luf.test/m.m3u8', renewUrl: 'https://luf.test/erneuern' });
|
|
|
|
expect(typeof instances[0].config.loader).toBe('function');
|
|
expect(instances[0].handlers.hlsError).toBeTypeOf('function');
|
|
});
|
|
|
|
it('versucht bei einem Medienfehler einmal die Wiederherstellung, danach Meldung und Abbau', () => {
|
|
const { Hls, instances } = fakeHls();
|
|
const video = fakeVideo();
|
|
|
|
attachSource(video, { Hls, source: 'https://luf.test/m.m3u8', renewUrl: 'https://luf.test/erneuern' });
|
|
const onError = instances[0].handlers.hlsError;
|
|
|
|
onError('hlsError', { fatal: false, type: 'networkError' });
|
|
expect(video.inserted).toHaveLength(0);
|
|
|
|
onError('hlsError', { fatal: true, type: 'mediaError' });
|
|
expect(instances[0].calls).toContainEqual(['recoverMediaError']);
|
|
expect(video.inserted).toHaveLength(0);
|
|
|
|
onError('hlsError', { fatal: true, type: 'mediaError' });
|
|
expect(instances[0].calls).toContainEqual(['destroy']);
|
|
expect(video.inserted).toHaveLength(1);
|
|
expect(video.inserted[0][1].attributes.role).toBe('alert');
|
|
|
|
onError('hlsError', { fatal: true, type: 'networkError' });
|
|
expect(video.inserted).toHaveLength(1);
|
|
});
|
|
|
|
it('meldet einen endgültigen Netzwerkfehler sofort', () => {
|
|
const { Hls, instances } = fakeHls();
|
|
const video = fakeVideo();
|
|
|
|
attachSource(video, { Hls, source: 'https://luf.test/m.m3u8', renewUrl: 'https://luf.test/erneuern' });
|
|
instances[0].handlers.hlsError('hlsError', { fatal: true, type: 'networkError' });
|
|
|
|
expect(video.inserted).toHaveLength(1);
|
|
});
|
|
|
|
it('lädt in Safari bei einem Fehler die Hauptliste neu und setzt an der Stelle fort', async () => {
|
|
vi.stubGlobal('window', {
|
|
fetch: vi.fn(async () => ({ ok: true, json: async () => ({ url: 'https://luf.test/frisch.m3u8' }) })),
|
|
});
|
|
const { Hls } = fakeHls({ supported: false });
|
|
const video = fakeVideo({ native: true });
|
|
video.currentTime = 95;
|
|
video.paused = false;
|
|
|
|
attachSource(video, { Hls, source: 'https://luf.test/m.m3u8', renewUrl: 'https://luf.test/erneuern' });
|
|
await video.listeners.error();
|
|
expect(video.src).toBe('https://luf.test/frisch.m3u8');
|
|
|
|
video.listeners.loadedmetadata();
|
|
expect(video.currentTime).toBe(95);
|
|
expect(video.play).toHaveBeenCalledTimes(1);
|
|
expect(video.inserted).toHaveLength(0);
|
|
|
|
await video.listeners.error();
|
|
expect(video.inserted).toHaveLength(1);
|
|
});
|
|
|
|
it('zeigt die Meldung sofort und baut hls.js ab, wenn der Server die Erneuerung ablehnt', async () => {
|
|
vi.stubGlobal('window', { fetch: vi.fn(async () => ({ ok: false, json: async () => ({}) })) });
|
|
const { Hls, instances } = fakeHls();
|
|
const bases = [];
|
|
|
|
Hls.DefaultConfig.loader = class {
|
|
constructor() {
|
|
this.stats = {};
|
|
bases.push(this);
|
|
}
|
|
|
|
load(context, loaderConfig, callbacks) {
|
|
this.callbacks = callbacks;
|
|
}
|
|
|
|
destroy() {}
|
|
|
|
abort() {}
|
|
};
|
|
const video = fakeVideo();
|
|
const callbacks = { onSuccess: vi.fn(), onError: vi.fn(), onTimeout: vi.fn() };
|
|
const context = { url: 'https://luf.test/bereich/abschnitt/1/2/3/ableitungen/3/480/s.ts' };
|
|
|
|
attachSource(video, { Hls, source: 'https://luf.test/m.m3u8', renewUrl: 'https://luf.test/erneuern' });
|
|
new instances[0].config.loader({}).load(context, {}, callbacks);
|
|
bases[0].callbacks.onError({ code: 403, text: 'Forbidden' }, context, null, {});
|
|
await flush();
|
|
|
|
expect(video.inserted).toHaveLength(1);
|
|
expect(instances[0].calls).toContainEqual(['destroy']);
|
|
expect(callbacks.onError).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('meldet in Safari, wenn der Server keine neue Adresse gibt', async () => {
|
|
vi.stubGlobal('window', { fetch: vi.fn(async () => ({ ok: false, json: async () => ({}) })) });
|
|
const { Hls } = fakeHls({ supported: false });
|
|
const video = fakeVideo({ native: true });
|
|
|
|
attachSource(video, { Hls, source: 'https://luf.test/m.m3u8', renewUrl: 'https://luf.test/erneuern' });
|
|
await video.listeners.error();
|
|
await flush();
|
|
|
|
expect(video.src).toBe('https://luf.test/m.m3u8');
|
|
expect(video.inserted).toHaveLength(1);
|
|
});
|
|
});
|