2026-12-03 16:11:47 -05:00
import { test } from 'node:test' ;
import assert from 'node:assert/strict' ;
2026-12-03 20:11:10 -05:00
import { createSetupClass , createListenerClass } from '../../src/foundry/setup.mjs' ;
2026-12-03 16:11:47 -05:00
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 ;};
2026-12-03 20:11:10 -05:00
test ( 'GM setup connects catalog into independent scene editors and never persists private GM authentication' , async ()=>{
2026-12-03 16:11:47 -05:00
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 });}}};
2026-12-03 20:11:10 -05:00
const connection = { status : { state : 'disconnected' , catalog : 0 , audioContext : 'closed' }, async test ( profile ){ assert . equal ( profile . token , 'capability-fixture' ); assert . equal ( profile . maUrl , 'https://ma.example.test' ); catalog = [{ provider : 'ma' , id : 'opaque-ma' , name : 'MA fixture' , kind : 'playlist' }]; return { target : 'dedicated' , targetName : 'Adaptive Music DEV' };}, async connect ( target ){ assert . equal ( target , 'dedicated' );}, refresh : async ()=>{}, disconnect : async ()=>{}};
2026-12-03 16:11:47 -05:00
try {
2026-12-03 20:11:10 -05:00
const Setup = createSetupClass ({ game , connection , audio : {}, getChoices : async ()=>[{ provider : 'native' , id : 'owned' , name : 'Owned' },... catalog ], start (){}, stop (){}, setOverrides (){}, onError : assert . fail , Base : class {}});
2026-12-04 08:58:47 -05:00
const root = await new Setup (). _renderHTML (); const code = descendants ( root ). find ( e => e . tag === 'input' && e . type === 'password' ); code . value = 'capability-fixture' ; descendants ( root ). find ( e => e . type === 'url' ). value = 'https://ma.example.test' ;
await descendants ( root ). find ( e => e . tag === 'button' && e . textContent === 'Authenticate' ). listeners . click (); assert . equal ( code . value , '' ); assert . ok ( ! descendants ( root ). some ( e => e . textContent === 'Connect optional companion' )); await descendants ( root ). find ( e => e . textContent === 'Connect audio' ). listeners . click ();
2026-12-03 16:11:47 -05:00
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' ));
});
2026-12-03 20:11:10 -05:00
test ( 'normal listener has one activation action and no token or invitation entry' , async ()=>{
const dom = { createElement : tag => new Element ( tag , dom )}; globalThis . document = dom ; let clicks = 0 ;
try { const Listener = createListenerClass ({ game : { settings : { get : ()=> false }}, audio : { stop (){}}, resumeListener : ()=>{ clicks ++ ;}, Base : class {}}); const root = await new Listener (). _renderHTML (); assert . ok ( ! descendants ( root ). some ( e => e . tag === 'input' )); await descendants ( root ). find ( e => e . textContent === 'Enable Audio' ). listeners . click (); assert . equal ( clicks , 1 );} finally { delete globalThis . document ;}
});
2026-12-04 08:58:47 -05:00
test ( 'failed wizard authentication can retry without reentering token' , async ()=>{
const { connectionDraft } = await import ( '../../src/foundry/connection-draft.mjs' );
const { mountConnectionWizard } = await import ( '../../src/foundry/connection-wizard.mjs' );
const dom = { createElement : tag => new Element ( tag , dom )}; const container = new Element ( 'div' , dom ); let draft , attempts = 0 , errors = 0 ;
const connection = { get status (){ return { hasPrivateDraft :!! draft , state : 'disconnected' , catalog : 0 };}, async test ( profile ){ draft = connectionDraft ( draft , profile , 'world' ); attempts ++ ; assert . equal ( draft . token , 'private-fixture' ); if ( attempts === 1 ) throw { code : 'ws-auth-denied' }; return { target : 'dedicated' , targetName : 'Campaign receiver' };}};
mountConnectionWizard ({ game : { user : { isGM : true }}, container , connection , onError : ()=> errors ++ });
const all = ()=> descendants ( container ); all (). find ( e => e . type === 'url' ). value = 'https://ma.example.test' ; const token = all (). find ( e => e . type === 'password' ); token . value = 'private-fixture' ;
assert . equal ( all (). filter ( e => e . type === 'file' ). length , 0 );
const submit = all (). find ( e => e . textContent === 'Authenticate' ); await submit . listeners . click (); assert . equal ( errors , 1 ); assert . equal ( token . value , '' );
await submit . listeners . click (); assert . equal ( attempts , 2 ); assert . equal ( errors , 1 );
});