Files
adaptive-music/tests/js/setup-audio.test.mjs
T

37 lines
4.1 KiB
JavaScript
Raw Normal View History

2026-12-03 16:11:47 -05:00
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {createSetupClass} from '../../src/foundry/setup.mjs';
import {ListenerAudio} from '../../src/foundry/listener-audio.mjs';
class Element{
constructor(tag,dom){this.tag=tag;this.ownerDocument=dom;this.children=[];this.listeners={};this.dataset={};this.value='';}
append(...children){this.children.push(...children);if(this.tag==='select'&&!this.value&&children[0]?.tag==='option')this.value=children[0].value;}
replaceChildren(...children){this.children=[];this.append(...children);}
setAttribute(){}
addEventListener(name,fn){this.listeners[name]=fn;}
}
const descendants=node=>[node,...node.children.flatMap(descendants)];
const collection=items=>{const map=new Map(items.map(i=>[i.id,i]));map[Symbol.iterator]=function*(){yield*map.values();};return map;};
test('GM setup connects catalog into independent scene editors and never persists its connection code',async()=>{
const dom={createElement:tag=>new Element(tag,dom)};globalThis.document=dom;
let flag={},catalog=[];const settings={ownedPlaylistIds:'owned',musicDefaults:{},enabled:false},writes=[];
const scene={id:'scene',name:'Scene',active:true,getFlag:()=>flag,setFlag:async(ns,k,value)=>{flag=value;}};
const game={user:{isGM:true},playlists:collection([{id:'owned',name:'Owned'}]),scenes:collection([scene]),combats:[],settings:{get:(ns,k)=>settings[k],set:async(ns,k,value)=>{settings[k]=value;writes.push({k,value});}}};
const gateway={status:null,async connect(code){assert.equal(code,'capability-fixture');catalog=[{provider:'ma',id:'opaque-ma',name:'MA fixture',kind:'playlist'}];this.status={target:'verified',playbackEnabled:false,audioSource:'none'};},refresh:async()=>{},disconnect:async()=>{}};
try{
const Setup=createSetupClass({game,gateway,audio:{},getChoices:async()=>[{provider:'native',id:'owned',name:'Owned'},...catalog],start(){},stop(){},setOverrides(){},onError:assert.fail,Base:class{}});
const root=await new Setup()._renderHTML();const code=descendants(root).find(e=>e.tag==='input'&&e.type==='password');code.value='capability-fixture';
await descendants(root).find(e=>e.tag==='button'&&e.textContent==='Connect and load MA catalog').listeners.click();assert.equal(code.value,'');
const panel=descendants(root).find(e=>e.tag==='fieldset'&&e.children.some(c=>c.tag==='legend'&&c.textContent==='Scene music'));
const selects=descendants(panel).filter(e=>e.tag==='select');assert.equal(selects.length,4);assert.ok(selects[0].children.some(o=>o.textContent.includes('MA fixture')));
selects[0].value='["ma","opaque-ma"]';selects[1].value='["native","owned"]';selects[3].value='["native","owned"]';
await descendants(panel).find(e=>e.tag==='button').listeners.click();assert.deepEqual(flag.ambient,{provider:'ma',id:'opaque-ma'});assert.deepEqual(flag.combat,{provider:'native',id:'owned'});assert.ok(!JSON.stringify({flag,writes}).includes('capability-fixture'));
}finally{delete globalThis.document;}
});
test('browser PCM sink resumes from gesture, decodes stereo frames and closes its context on stream end',async()=>{
const calls=[],buffers=[];const context={currentTime:0,destination:{},resume(){calls.push('resume');return Promise.resolve();},close(){calls.push('close');return Promise.resolve();},
createBuffer(channels,length,rate){assert.equal(rate,24000);const channelsData=[new Float32Array(length),new Float32Array(length)];buffers.push(channelsData);return {getChannelData:i=>channelsData[i]};},
createBufferSource(){return {connect(){},start(){calls.push('start');},stop(){calls.push('stop');}};}};
const pcm=new Uint8Array(1920),view=new DataView(pcm.buffer);for(let i=0;i<960;i++)view.setInt16(i*2,16384,true);
const audio=new ListenerAudio({createContext:()=>context,fetchImpl:async()=>{calls.push('fetch');return new Response(pcm,{headers:{'X-Audio-Format':'pcm16le;rate=24000;channels=2'}});}});
const pending=audio.start({url:'/audio',options:{}});assert.equal(calls[0],'resume');await pending;assert.equal(buffers[0][0][0],0.5);assert.equal(buffers[0][1][0],0.5);assert.ok(calls.includes('close'));assert.ok(calls.indexOf('resume')<calls.indexOf('fetch'));
});