diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index c8e7e98723..9f07a38be5 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -2185,8 +2185,9 @@ section leaving the page removes the player, so nothing plays unseen. ON AIR with a blinking red dot while it plays; LOADING with an amber dot while it starts; an offline card ("Stream is offline", with Try again) when the player reports the video ended, errored or did not start within 25 s, -which is how an off-air broadcast shows; "This stream cannot play here" when -the owner refuses embedding or the API does not load. Stepping to another +which is how an off-air broadcast shows, or when YouTube's player API does not +load (Try again loads it afresh); "This stream cannot play here" when the +owner refuses embedding. Stepping to another stream hands a playing one over; a selection that moves to an object without that stream, clearing it or turning the layer off removes the player, while another entry of the same station (a module, a docked vehicle, diff --git a/scripts/format-scope.json b/scripts/format-scope.json index 03d77fa9b0..b1b66c874d 100644 --- a/scripts/format-scope.json +++ b/scripts/format-scope.json @@ -1666,6 +1666,7 @@ "server/providers/space/issTelemetry.test.mjs", "src/data/liveStreams.test.mjs", "src/data/youtubeLivePlayer.test.mjs", + "src/data/youtubeApi.test.mjs", "src/layers/installations/labelBoxClick.test.mjs", "src/layers/satellites/cockpitSubject.test.mjs", "src/layers/satellites/issArticulation.test.mjs", diff --git a/src/data/youtubeApi.test.mjs b/src/data/youtubeApi.test.mjs new file mode 100644 index 0000000000..6ffec7094f --- /dev/null +++ b/src/data/youtubeApi.test.mjs @@ -0,0 +1,84 @@ +// src/data/youtubeApi.test.mjs — YouTube's IFrame API loader, with the page +// faked: one load per document, and a load that fails is not kept, so the +// LIVE section's Try again loads the API afresh. +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { loadYouTubeApi } from './youtubeApi.js'; + +/** A document whose head records the scripts put in it. */ +function fakePage() { + const scripts = []; + const timers = []; + const documentRef = { + createElement(tag) { + const listeners = {}; + return { + tag, + removed: false, + addEventListener(name, listener) { + listeners[name] = listener; + }, + fire(name) { + listeners[name]?.(); + }, + remove() { + this.removed = true; + }, + }; + }, + head: { + append(node) { + scripts.push(node); + }, + }, + }; + const globalRef = { + setTimeout(callback, delay) { + timers.push({ callback, delay, cleared: false }); + return timers.length; + }, + clearTimeout(id) { + if (timers[id - 1]) timers[id - 1].cleared = true; + }, + }; + return { documentRef, globalRef, scripts, timers }; +} + +class Player {} + +test('the API loads once per document, and a loaded one is reused', async () => { + const { documentRef, globalRef, scripts } = fakePage(); + const first = loadYouTubeApi(documentRef, globalRef); + assert.equal(loadYouTubeApi(documentRef, globalRef), first); + assert.equal(scripts.length, 1); + assert.equal(scripts[0].src, 'https://www.youtube.com/iframe_api'); + globalRef.YT = { Player }; + globalRef.onYouTubeIframeAPIReady(); + assert.equal(await first, globalRef.YT); + assert.equal(await loadYouTubeApi(documentRef, globalRef), globalRef.YT); + assert.equal(scripts.length, 1); +}); + +test('a script that fails to load is removed, and the next call loads afresh', async () => { + const { documentRef, globalRef, scripts } = fakePage(); + const failed = loadYouTubeApi(documentRef, globalRef); + scripts[0].fire('error'); + await assert.rejects(failed, /YouTube API unavailable/); + assert.equal(scripts[0].removed, true); + const again = loadYouTubeApi(documentRef, globalRef); + assert.notEqual(again, failed); + assert.equal(scripts.length, 2); + globalRef.YT = { Player }; + globalRef.onYouTubeIframeAPIReady(); + assert.equal(await again, globalRef.YT); +}); + +test('a load slower than 5 s fails, and the next call loads afresh', async () => { + const { documentRef, globalRef, scripts, timers } = fakePage(); + const slow = loadYouTubeApi(documentRef, globalRef); + assert.equal(timers[0].delay, 5000); + timers[0].callback(); + await assert.rejects(slow, /YouTube API timed out/); + loadYouTubeApi(documentRef, globalRef); + assert.equal(scripts.length, 2); +}); diff --git a/src/data/youtubeLivePlayer.js b/src/data/youtubeLivePlayer.js index 6c95823178..06adb33a2f 100644 --- a/src/data/youtubeLivePlayer.js +++ b/src/data/youtubeLivePlayer.js @@ -8,8 +8,9 @@ import { YOUTUBE_EMBED_HOST, youtubeLivePlayerVars } from './liveStreams.js'; * never starts. The player cannot say "this broadcast is off air" in so * many words: a live stream that is off air is reported as a video that * cannot play, or that never reaches playing, so both read as offline here. - * Embedding refused by the video's owner is `unavailable`, which no retry - * will change. + * An API that does not load (blocked, or slower than its 5 s) is offline too, + * reason `api`: Try again loads it afresh. Embedding refused by the video's + * owner is `unavailable`, which no retry will change. */ /** A stream that has not started playing this long after its player was ready is offline. */ @@ -102,7 +103,7 @@ export function createYouTubeLivePlayer({ } }, () => { - if (!destroyed) set('unavailable', 'api'); + if (!destroyed) set('offline', 'api'); }, ); diff --git a/src/data/youtubeLivePlayer.test.mjs b/src/data/youtubeLivePlayer.test.mjs index 3b525e9f1b..eeedae1950 100644 --- a/src/data/youtubeLivePlayer.test.mjs +++ b/src/data/youtubeLivePlayer.test.mjs @@ -157,10 +157,10 @@ test('a timer that fires after playback began changes nothing', async () => { assert.deepEqual(states.at(-1), { state: 'playing', reason: null }); }); -test('an API that does not load leaves the stream unavailable', async () => { +test('an API that does not load reads as offline, which Try again can change', async () => { const { states, settle } = fixture({ apiFails: true }); await settle(); - assert.deepEqual(states.at(-1), { state: 'unavailable', reason: 'api' }); + assert.deepEqual(states.at(-1), { state: 'offline', reason: 'api' }); }); test('destroy empties the host, destroys the player and silences later events', async () => {