Files
adaptive-music/tests/js/development-launcher.test.mjs
T

38 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

2026-12-03 16:11:47 -05:00
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {mkdtemp,mkdir,writeFile,readFile,rm} from 'node:fs/promises';
import {tmpdir} from 'node:os';
import {join} from 'node:path';
import {fileURLToPath} from 'node:url';
import {spawn,spawnSync} from 'node:child_process';
import {createServer} from 'node:http';
test('extracted companion launches privately with fixture credentials, serves catalog diagnostics and listener PCM',async()=>{
const folder=await mkdtemp(join(tmpdir(),'adaptive-companion-')),commands=[];
const ma=createServer(async(req,res)=>{let text='';for await(const chunk of req)text+=chunk;const {command}=JSON.parse(text);commands.push(command);let result;
if(command==='players/get')result={player_id:'dev-only',name:'Adaptive Music DEV',provider:'sendspin-fixture',enabled:true,available:true,group_members:[],group_childs:[],static_group_members:[]};
else if(command.startsWith('player_queues/get'))result={queue_id:'dev-only',active:true,available:true};else result=[];
res.writeHead(200,{'Content-Type':'application/json'});res.end(JSON.stringify(result));});
await new Promise(resolve=>ma.listen(0,'127.0.0.1',resolve));let child;
try{
const source=fileURLToPath(new URL('../../scripts/package_module.py',import.meta.url));
const script="import importlib.util,pathlib,sys,zipfile;s=importlib.util.spec_from_file_location('p',sys.argv[1]);m=importlib.util.module_from_spec(s);s.loader.exec_module(m);p=m.build_companion(pathlib.Path(sys.argv[2])/'companion.zip');zipfile.ZipFile(p).extractall(sys.argv[2])";
const built=spawnSync('python3',['-c',script,source,folder],{encoding:'utf8'});assert.equal(built.status,0,built.stderr);
const root=join(folder,'adaptive-music-companion'),local=join(root,'.local');await mkdir(local,{mode:0o700});
const json=(name,value)=>writeFile(join(local,name),JSON.stringify(value),{mode:0o600});
await writeFile(join(local,'ma-token'),'fixture-MA-secret',{mode:0o600});
await json('environment.json',{ma_url:`http://127.0.0.1:${ma.address().port}`});await json('development-gateway.json',{foundryOrigin:'https://foundry.example.test',syntheticAudio:true});
await json('ma-readonly-results.json',{me:{player_filter:['dev-only'],provider_filter:['approved']},player:{name:'Adaptive Music DEV',provider:'sendspin-fixture'}});
child=spawn(process.execPath,['gateway/run-development.mjs'],{cwd:root,stdio:['ignore','pipe','pipe']});let output='';child.stdout.on('data',chunk=>{output+=chunk;});child.stderr.on('data',chunk=>{output+=chunk;});
for(let attempt=0;attempt<200;attempt++){try{await readFile(join(local,'listener-bootstrap.json'));break;}catch{await new Promise(resolve=>setTimeout(resolve,10));}}
const runtime=JSON.parse(await readFile(join(local,'development-gateway-runtime.json'),'utf8')),controller=JSON.parse(await readFile(join(local,'controller-bootstrap.json'),'utf8')),listener=JSON.parse(await readFile(join(local,'listener-bootstrap.json'),'utf8'));
assert.ok(runtime.url.startsWith('http://127.0.0.1:'));assert.ok(!output.includes('fixture-MA-secret'));
const rpc=async(action)=>fetch(runtime.url+'/rpc',{method:'POST',headers:{Origin:'https://foundry.example.test','Content-Type':'application/json',Authorization:`Bearer ${controller.token}`},body:JSON.stringify({action,campaign:controller.campaign,session:controller.session})});
assert.equal((await rpc('connect')).status,200);const d=await (await rpc('diagnostics')).json();assert.equal(d.target,'verified');assert.equal(d.playbackEnabled,false);assert.deepEqual(await (await rpc('catalog')).json(),[]);
const before=commands.length;
const audio=await fetch(runtime.url+'/audio',{method:'POST',headers:{Origin:'https://foundry.example.test','Content-Type':'application/json',Authorization:`Bearer ${listener.token}`},body:JSON.stringify({campaign:listener.campaign,session:listener.session})});
assert.equal(audio.status,200);const reader=audio.body.getReader();const frame=await reader.read();assert.ok(frame.value.length>=1920);await reader.cancel();assert.equal(commands.length,before);assert.ok(!commands.some(c=>['player_queues/play_media','player_queues/stop'].includes(c)));
}finally{
if(child&&child.exitCode===null){const exited=new Promise(resolve=>child.once('exit',resolve));child.kill('SIGTERM');await exited;}
ma.closeAllConnections();await new Promise(resolve=>ma.close(resolve));await rm(folder,{recursive:true,force:true});
}
});