Files
ytplayer/frontend/related-videos.test.js

19 lines
1.5 KiB
JavaScript

const { test } = require('node:test');
const assert = require('node:assert/strict');
const { createLoader, cardsFor } = require('./related-videos');
test('related cards exclude the current video, duplicates and invalid cards', () => {
assert.deepEqual(cardsFor([{id:'a',title:'A'},{id:'b',title:'B'},{id:'b',title:'B'},null,{id:'c'}],'a'),[{id:'b',title:'B'}]);
});
test('failed related discovery stays actionable and Retry refreshes the source', async () => {
const states = [], calls = [];
const load = createLoader({ fetchRelated: async (_, options) => { calls.push(options); return calls.length === 1 ? {ok:false,error:'YouTube HTTP 429'} : {ok:true,results:[{id:'b',title:'B'}]}; }, update: state => states.push(state) });
await load({id:'a',title:'A'}); assert.equal(states.at(-1).status,'error'); assert.equal(states.at(-1).detail,'YouTube HTTP 429');
await load({id:'a',title:'A'},{refresh:true}); assert.equal(states.at(-1).status,'ready'); assert.equal(calls[1].refresh,true);
});
test('a slow previous song cannot overwrite the current song recommendations', async () => {
let finish; const states=[];
const load=createLoader({fetchRelated: meta=>meta.id==='a'?new Promise(resolve=>{finish=resolve;}):Promise.resolve({ok:true,results:[{id:'d',title:'D'}]}),update:state=>states.push(state)});
const old=load({id:'a'}); await load({id:'b'}); finish({ok:true,results:[{id:'c',title:'C'}]}); await old;
assert.equal(states.at(-1).id,'b'); assert.equal(states.at(-1).cards[0].id,'d');
});