2026-12-03 20:11:10 -05:00
// Offline Chromium UI fixture. No Foundry, MA, Git host, token or production access.
import { createServer } from 'node:http' ;
2026-12-05 09:40:14 -05:00
import { readFile , mkdir , realpath } from 'node:fs/promises' ;
2026-12-03 20:11:10 -05:00
import { resolve , extname } from 'node:path' ;
import assert from 'node:assert/strict' ;
2026-12-05 09:40:14 -05:00
const playwright = await import ( process . env . AM_PLAYWRIGHT_MODULE ?? 'playwright' );
const engine = process . argv . includes ( '--firefox' ) ? 'firefox' : process . argv . includes ( '--webkit' ) ? 'webkit' : 'chromium' ;
2026-12-03 20:11:10 -05:00
const root = resolve ( import . meta . dirname , '..' );
2026-12-05 09:40:14 -05:00
const isFixtureAsset = path => path === '/tests/browser/setup.html' || /^\/src\/[\w/-]+\.mjs$/ . test ( path ) || path === '/styles/adaptive-music.css' ;
const server = createServer ( async ( req , res )=>{
try {
const asset = new URL ( req . url , 'http://localhost' ). pathname ;
if ( req . method !== 'GET' ||! isFixtureAsset ( asset )) throw Error ();
const path = await realpath ( resolve ( root , '.' + asset ));
if ( path !== root + asset ) throw Error ();
const data = await readFile ( path );
res . setHeader ( 'Content-Type' , extname ( path ) === '.mjs' ? 'text/javascript' : extname ( path ) === '.css' ? 'text/css' : 'text/html' );
res . end ( data );
} catch { res . writeHead ( 404 ); res . end ();}
});
2026-12-03 20:11:10 -05:00
await new Promise ( resolve => server . listen ( 0 , '127.0.0.1' , resolve )); let browser ;
try {
2026-12-05 09:40:14 -05:00
browser = await playwright [ engine ]. launch ({ headless : true ,...( engine === 'chromium' && { args : [ '--no-sandbox' ]})});
const page = await browser . newPage ({ viewport : { width : 1000 , height : 1000 }}), errors = []; page . on ( 'pageerror' , e => errors . push ( e . message ));
2026-12-03 20:11:10 -05:00
await page . goto ( `http://127.0.0.1: ${ server . address (). port } /tests/browser/setup.html` ); await page . waitForFunction (()=> window . ready );
assert . equal ( await page . getByText ( 'Connect optional companion' ,{ exact : true }). count (), 0 );
2026-12-04 08:58:47 -05:00
for ( const width of [ 380 , 640 , 1000 ]){
await page . setViewportSize ({ width , height : 1000 });
assert . equal ( await page . locator ( '.adaptive-music-setup' ). evaluate ( el => el . scrollWidth <= el . clientWidth + 1 ), true , 'setup must fit narrow window' );
2026-12-04 11:08:09 -05:00
assert . equal ( await page . getByLabel ( 'ICE server URL' ). isVisible (), false , 'advanced fields must remain hidden' );
2026-12-04 08:58:47 -05:00
}
2026-12-05 09:40:14 -05:00
await page . evaluate (()=> window . nativeConfiguration ());
const sidebar = page . locator ( '#fixture-playlists' );
await sidebar . locator ( 'summary' ). click ();
assert . equal ( await sidebar . locator ( 'li' ). count (), 0 );
2026-12-05 13:37:05 -05:00
await page . getByLabel ( 'Music Assistant server address' ). fill ( 'https://ma.example.test' );
await page . evaluate (()=> Object . defineProperty ( navigator . clipboard , 'writeText' ,{ value : async text =>{ window . copiedDiagnostics = text ;}}));
await page . getByLabel ( 'Private GM access token' ). fill ( 'synthetic-secret-sentinel' );
const copyDiagnostics = page . getByRole ( 'button' ,{ name : 'Copy connection diagnostics' });
await page . getByRole ( 'button' ,{ name : 'Connect' , exact : true }). click ();
await page . getByText ( /^Connection failed\./ ). waitFor ();
await copyDiagnostics . click ();
let copied = await page . evaluate (()=> window . copiedDiagnostics );
const { at ,... failure } = JSON . parse ( copied ). lastConnectFailure ;
assert . ok ( Number . isFinite ( Date . parse ( at )));
assert . deepEqual ( failure ,{ code : 'ws-upgrade-failed' , stage : 'API authentication' });
assert . doesNotMatch ( copied , /synthetic-secret-sentinel|ma\.example\.test/ );
await page . getByLabel ( 'Private GM access token' ). fill ( 'fixture-token' );
2026-12-04 11:08:09 -05:00
await page . getByRole ( 'button' ,{ name : 'Connect' , exact : true }). click (); await page . getByText ( 'Campaign audio connected.' ,{ exact : false }). waitFor ();
2026-12-05 13:37:05 -05:00
await copyDiagnostics . click ();
copied = await page . evaluate (()=> window . copiedDiagnostics );
assert . equal ( JSON . parse ( copied ). lastConnectFailure , undefined );
2026-12-03 20:11:10 -05:00
assert . equal ( await page . getByLabel ( 'Private GM access token' ). inputValue (), '' );
2026-12-05 09:40:14 -05:00
await page . evaluate (()=> window . refreshNativeConfiguration ());
const source = sidebar . getByText ( 'Forest at night' ,{ exact : true }), scene = page . locator ( '#fixture-scene' );
await source . waitFor ();
const ambient = scene . getByLabel ( 'Ambient playlist' );
const target = ambient . locator ( '..' );
await source . dragTo ( target );
assert . equal ( await ambient . inputValue (), '["ma","ambient"]' );
await scene . getByRole ( 'button' ,{ name : 'Save assignments' }). click ();
assert . deepEqual ( await page . evaluate (()=> window . saved . ambient ),{ provider : 'ma' , id : 'ambient' });
await page . evaluate (()=> window . closeNativeConfiguration ());
2026-12-04 11:08:09 -05:00
await page . getByRole ( 'button' ,{ name : 'Choose scene music' , exact : true }). click ();
2026-12-03 20:11:10 -05:00
const panel = page . locator ( 'fieldset' ). filter ({ has : page . locator ( 'legend' ,{ hasText : 'Scene music' })});
await panel . locator ( 'select' ). nth ( 0 ). selectOption ( '["ma","ambient"]' ); await panel . locator ( 'select' ). nth ( 1 ). selectOption ( '["ma","combat"]' ); await panel . getByRole ( 'button' ,{ name : 'Save assignments' }). click ();
assert . deepEqual ( await page . evaluate (()=> window . saved . ambient ),{ provider : 'ma' , id : 'ambient' }); assert . deepEqual ( await page . evaluate (()=> window . saved . combat ),{ provider : 'ma' , id : 'combat' });
2026-12-04 11:08:09 -05:00
await page . getByRole ( 'button' ,{ name : 'Go to Playback' , exact : true }). click (); await page . getByRole ( 'heading' ,{ name : 'Forest at night' }). waitFor ();
await page . getByText ( '2 connected listeners' ,{ exact : true }). waitFor ();
await page . getByRole ( 'button' ,{ name : 'Pause' , exact : true }). click (); await page . getByRole ( 'button' ,{ name : 'Resume' , exact : true }). click ();
await page . getByLabel ( 'Playback position' ). press ( 'ArrowRight' ); await page . getByText ( '0:13 / 1:30' ,{ exact : true }). waitFor ();
2026-12-05 09:40:14 -05:00
await page . getByLabel ( 'Repeat' ,{ exact : true }). selectOption ( 'all' );
await page . waitForFunction (()=> document . querySelector ( 'select[aria-label="Repeat"]' ). value === 'all' );
2026-12-04 11:08:09 -05:00
await page . getByLabel ( 'Campaign volume' ). press ( 'ArrowLeft' );
assert . equal ( await page . getByLabel ( 'Campaign volume' ). inputValue (), '99' );
await page . getByRole ( 'button' ,{ name : 'Stop playback' , exact : true }). click (); await page . getByText ( 'Stopped' ,{ exact : true }). waitFor ();
2026-12-05 09:40:14 -05:00
assert . deepEqual ( await page . evaluate (()=> window . controlActions ),[ 'pause' , 'resume' , 'seek' , 'repeat' , 'stop' ]);
if ( engine === 'chromium' ){ await mkdir ( resolve ( root , 'dist/previews' ),{ recursive : true }); await page . screenshot ({ path : resolve ( root , 'dist/previews/am006-setup.png' ), fullPage : true });}
2026-12-03 20:11:10 -05:00
await page . evaluate (()=> window . listener ()); assert . equal ( await page . locator ( 'input' ). count (), 0 ); await page . getByRole ( 'button' ,{ name : 'Enable Audio' }). click (); assert . equal ( await page . evaluate (()=> window . activated ), true ); assert . deepEqual ( errors ,[]);
2026-12-05 13:37:05 -05:00
console . log ( JSON . stringify ({ status : 'pass' , engine , browser : browser . version (), scope : 'offline DOM fixture; no live Foundry or MA' , checks : [ '380/640/1000px layout' , 'three setup tabs' , 'failed connection diagnostics exclude secrets and clear after retry' , 'private field clearing' , 'catalog selectors' , 'ambient/combat save' , 'advanced controls hidden' , 'sidebar catalog refresh and pointer drag' , 'track and listeners' , 'pause/resume/seek/repeat/volume/stop' , 'listener activation' ], pageErrors : errors }, null , 2 ));
2026-12-03 20:11:10 -05:00
} finally { await browser ? . close (); await new Promise ( resolve => server . close ( resolve ));}