const test = require('node:test'); const assert = require('node:assert/strict'); const { createReporter } = require('./server-backfill.js'); const meta={id:'0gfX0dFLaBc',title:'Because You are God',artist:'Cathedral of Praise Worship',duration:240,thumbnail:'blob:device-art'}; test('first playback reports known metadata once per video per session',()=>{ const requests=[];const reporter=createReporter({fetch:(url,options)=>{requests.push({url,options});return Promise.resolve({ok:true});}}); reporter.report(meta);reporter.report(meta);reporter.report({...meta,id:'bbbbbbbbbbb'}); assert.equal(requests.length,2);assert.equal(requests[0].url,'/api/media/0gfX0dFLaBc/meta'); const card=JSON.parse(requests[0].options.body);assert.equal(card.channel,meta.artist);assert.equal(card.duration,240);assert.equal(card.thumbnail,undefined); assert.equal(requests[0].options.method,'POST'); }); test('offline plays defer until online and repeated events do not multiply reports',()=>{ let online=false;const requests=[];const reporter=createReporter({online:()=>online,fetch:(...args)=>{requests.push(args);return Promise.resolve();}}); reporter.report(meta);reporter.report(meta);reporter.flush();assert.equal(requests.length,0); online=true;reporter.flush();reporter.flush();reporter.report(meta);assert.equal(requests.length,1); }); test('uploads, edits, unknown titles and disabled native shells never report',()=>{ const requests=[];const fetch=(...args)=>{requests.push(args);};const reporter=createReporter({fetch}); for(const item of [{...meta,id:'upl_123'},{...meta,custom:true},{...meta,upload:true},{...meta,title:meta.id},{...meta,title:''}])reporter.report(item); createReporter({fetch,enabled:()=>false}).report(meta);assert.equal(requests.length,0); }); test('synchronous and rejected network errors are isolated from playback',async()=>{ assert.doesNotThrow(()=>createReporter({fetch:()=>{throw new Error('offline');}}).report(meta)); assert.doesNotThrow(()=>createReporter({fetch:()=>Promise.reject(new Error('offline'))}).report(meta)); await new Promise(resolve=>setImmediate(resolve)); });