Hole abgelaufene Wiedergabeadressen im Player neu und melde einen Abbruch sichtbar

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>
This commit is contained in:
StommerandClaude Sonnet 5.5 committed 2026-12-01 10:20:35 +01:00
1 parent 100b4e26c3
commit 345e694933
8 files changed
+981 -16

No files matched your search

+223
View File
@@ -0,0 +1,223 @@
import { describe, expect, it, vi } from 'vitest';
import { createRenewer, createRenewingLoader, renewalRequestUrl } from './renewal.js';
const RENEW = 'https://luf.test/bereich/wiedergabe/1/2/3/erneuern';
function response(ok, body) {
return { ok, json: async () => body };
}
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
describe('renewalRequestUrl', () => {
it('fragt für einen Abschnitt nach genau dieser Datei', () => {
const failed = 'https://luf.test/bereich/abschnitt/1/2/3/ableitungen/3/480/segment-001.ts?expires=1&signature=x';
expect(renewalRequestUrl(RENEW, failed)).toBe(RENEW + '?datei=ableitungen%2F3%2F480%2Fsegment-001.ts');
});
it('fragt für die Hauptliste ohne Datei', () => {
expect(renewalRequestUrl(RENEW, 'https://luf.test/bereich/ausgabe/1/2/3/streamen?expires=1&signature=x')).toBe(RENEW);
});
it('hängt die Datei an eine Adresse mit Abfrage an und entschlüsselt den Pfad', () => {
const failed = 'https://luf.test/bereich/abschnitt/1/2/3/ableitungen/3/H%C3%B6fe/a.ts';
expect(renewalRequestUrl(RENEW + '?x=1', failed)).toBe(RENEW + '?x=1&datei=ableitungen%2F3%2FH%C3%B6fe%2Fa.ts');
});
it('fällt bei einer unlesbaren Adresse auf die Hauptliste zurück', () => {
expect(renewalRequestUrl(RENEW, 'http://[kaputt')).toBe(RENEW);
});
});
describe('createRenewer', () => {
it('liefert die frische Adresse', async () => {
const fetchFn = vi.fn(async () => response(true, { url: 'https://luf.test/neu' }));
expect(await createRenewer(RENEW, fetchFn)('https://luf.test/bereich/ausgabe/1/2/3/streamen')).toBe('https://luf.test/neu');
expect(fetchFn).toHaveBeenCalledWith(RENEW, expect.objectContaining({ credentials: 'same-origin' }));
});
it('gibt nach der ersten Ablehnung nie wieder eine Anfrage heraus', async () => {
const fetchFn = vi.fn(async () => response(false, {}));
const renew = createRenewer(RENEW, fetchFn);
expect(await renew('https://luf.test/a')).toBeNull();
expect(await renew('https://luf.test/b')).toBeNull();
expect(fetchFn).toHaveBeenCalledTimes(1);
});
it('behandelt eine Antwort ohne Adresse wie eine Ablehnung', async () => {
const fetchFn = vi.fn(async () => response(true, { url: '' }));
const renew = createRenewer(RENEW, fetchFn);
expect(await renew('https://luf.test/a')).toBeNull();
expect(await renew('https://luf.test/b')).toBeNull();
expect(fetchFn).toHaveBeenCalledTimes(1);
});
it('fängt Netzwerkfehler ab und versucht es beim nächsten Abruf wieder', async () => {
const fetchFn = vi.fn()
.mockRejectedValueOnce(new Error('offline'))
.mockResolvedValueOnce(response(true, { url: 'https://luf.test/neu' }));
const renew = createRenewer(RENEW, fetchFn);
expect(await renew('https://luf.test/a')).toBeNull();
expect(await renew('https://luf.test/a')).toBe('https://luf.test/neu');
});
it('meldet eine Ablehnung des Servers genau einmal, einen Netzwerkfehler nie', async () => {
const refused = vi.fn();
const denied = createRenewer(RENEW, vi.fn(async () => response(false, {})), refused);
await denied('https://luf.test/a');
await denied('https://luf.test/b');
expect(refused).toHaveBeenCalledTimes(1);
const unusable = vi.fn();
await createRenewer(RENEW, vi.fn(async () => response(true, {})), unusable)('https://luf.test/a');
expect(unusable).toHaveBeenCalledTimes(1);
const offline = vi.fn();
await createRenewer(RENEW, vi.fn().mockRejectedValue(new Error('offline')), offline)('https://luf.test/a');
expect(offline).not.toHaveBeenCalled();
});
it('begrenzt die Zahl der Erneuerungen je Wiedergabe', async () => {
const fetchFn = vi.fn(async () => response(true, { url: 'https://luf.test/neu' }));
const renew = createRenewer(RENEW, fetchFn);
let granted = 0;
for (let index = 0; index < 50; index += 1) {
if (await renew('https://luf.test/a') !== null) {
granted += 1;
}
}
expect(granted).toBe(20);
expect(fetchFn).toHaveBeenCalledTimes(20);
});
});
describe('createRenewingLoader', () => {
function fakeBase() {
const instances = [];
class Base {
constructor(config) {
this.config = config;
this.stats = { loaded: 0 };
this.destroyed = false;
this.aborted = false;
instances.push(this);
}
load(context, loaderConfig, callbacks) {
this.context = context;
this.callbacks = callbacks;
}
destroy() {
this.destroyed = true;
}
abort() {
this.aborted = true;
}
}
return { Base, instances };
}
function start(renew) {
const { Base, instances } = fakeBase();
const Loader = createRenewingLoader(Base, renew);
const callbacks = { onSuccess: vi.fn(), onError: vi.fn(), onTimeout: vi.fn(), onAbort: vi.fn() };
const context = { url: 'https://luf.test/bereich/abschnitt/1/2/3/ableitungen/3/480/s.ts', type: 'fragment' };
const loader = new Loader({ id: 'konfiguration' });
loader.load(context, { timeout: 1 }, callbacks);
return { loader, instances, callbacks, context };
}
const forbidden = { code: 403, text: 'Forbidden' };
it('reicht Erfolg und fremde Fehler unverändert durch, ohne zu erneuern', async () => {
const renew = vi.fn();
const { instances, callbacks, context } = start(renew);
instances[0].callbacks.onSuccess({ url: context.url }, {}, context, null);
instances[0].callbacks.onError({ code: 404, text: 'Not Found' }, context, null, {});
await flush();
expect(callbacks.onSuccess).toHaveBeenCalledTimes(1);
expect(callbacks.onError).toHaveBeenCalledWith({ code: 404, text: 'Not Found' }, context, null, {});
expect(renew).not.toHaveBeenCalled();
});
it('wiederholt bei 403 mit der erneuerten Adresse in einem neuen Lader', async () => {
const renew = vi.fn(async () => 'https://luf.test/frisch');
const { instances, callbacks, context } = start(renew);
instances[0].callbacks.onError(forbidden, context, null, {});
await flush();
expect(renew).toHaveBeenCalledWith('https://luf.test/bereich/abschnitt/1/2/3/ableitungen/3/480/s.ts');
expect(instances).toHaveLength(2);
expect(instances[0].destroyed).toBe(true);
expect(instances[1].config).toEqual({ id: 'konfiguration' });
expect(instances[1].context.url).toBe('https://luf.test/frisch');
expect(callbacks.onError).not.toHaveBeenCalled();
});
it('wiederholt nur einmal: ein zweites 403 geht an hls.js weiter', async () => {
const renew = vi.fn(async () => 'https://luf.test/frisch');
const { instances, callbacks, context } = start(renew);
instances[0].callbacks.onError(forbidden, context, null, {});
await flush();
instances[1].callbacks.onError(forbidden, context, null, {});
await flush();
expect(renew).toHaveBeenCalledTimes(1);
expect(instances).toHaveLength(2);
expect(callbacks.onError).toHaveBeenCalledTimes(1);
});
it('meldet den ursprünglichen Fehler, wenn der Server keine Adresse gibt', async () => {
const { instances, callbacks, context } = start(vi.fn(async () => null));
instances[0].callbacks.onError(forbidden, context, null, {});
await flush();
expect(instances).toHaveLength(1);
expect(callbacks.onError).toHaveBeenCalledWith(forbidden, context, null, {});
});
it('tut nach einem Abbruch während der Erneuerung nichts mehr', async () => {
let release;
const renew = vi.fn(() => new Promise((resolve) => { release = resolve; }));
const { loader, instances, callbacks, context } = start(renew);
instances[0].callbacks.onError(forbidden, context, null, {});
loader.abort();
release('https://luf.test/frisch');
await flush();
expect(instances).toHaveLength(1);
expect(callbacks.onError).not.toHaveBeenCalled();
expect(instances[0].aborted).toBe(true);
});
it('zeigt Statistik und Kontext des gerade laufenden Laders', async () => {
const { loader, instances, context } = start(vi.fn(async () => 'https://luf.test/frisch'));
expect(loader.stats).toBe(instances[0].stats);
instances[0].callbacks.onError(forbidden, context, null, {});
await flush();
expect(loader.stats).toBe(instances[1].stats);
expect(loader.context).toBe(instances[1].context);
});
});