Files

43 lines
3.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 { savePlaylistConfiguration, mountPlaylistConfiguration, startConfigurationInterfaces } from '../../src/foundry/configuration.mjs';
const choices=[{provider:'native',id:'owned',name:'Scene music'},{provider:'ma',id:'opaque',name:'Catalog music'}];
function fixture(){
let flag={boss:true,priority:7};const writes=[];
const document={getFlag:()=>flag,async setFlag(ns,k,value){writes.push({ns,k,value});flag=value;}};
return {document,writes,game:{user:{isGM:true}}};
}
test('scene/combat configuration preserves independent flags and stores only tagged references',async()=>{
for(const choice of choices){const f=fixture();await savePlaylistConfiguration({...f,choices,selected:JSON.stringify([choice.provider,choice.id])});
assert.deepEqual(f.writes,[{ns:'adaptive-music',k:'music',value:{boss:true,priority:7,playlist:{provider:choice.provider,id:choice.id}}}]);
await savePlaylistConfiguration({...f,choices,selected:''});assert.equal(f.writes[1].value.playlist,null);
}
});
test('role loss, stale selection and arbitrary URL/ID fail before document write',async()=>{
const f=fixture();for(const selected of ['https://example.test/audio',JSON.stringify(['native','household']),JSON.stringify(['ma','stale'])])await assert.rejects(savePlaylistConfiguration({...f,choices,selected}),{code:'denied'});
f.game.user.isGM=false;await assert.rejects(savePlaylistConfiguration({...f,choices,selected:''}),{code:'denied'});assert.equal(f.writes.length,0);
});
// Tiny DOM harness exercises event wiring/text sinks; it is not Foundry/browser evidence.
class Element {
constructor(tag,ownerDocument){this.tag=tag;this.ownerDocument=ownerDocument;this.children=[];this.dataset={};this.listeners={};}
append(...children){this.children.push(...children);}
querySelector(){return this.children.find(c=>c.dataset?.adaptiveMusic);}
setAttribute(){}
addEventListener(name,fn){this.listeners[name]=fn;}
remove(){this.removed=true;}
}
function container(){const dom={createElement:tag=>new Element(tag,dom),createTextNode:text=>({textContent:text})};return new Element('div',dom);}
test('editor uses text for catalog labels and rechecks GM on actual save event',async()=>{
const f=fixture(),root=container();const panel=mountPlaylistConfiguration({...f,container:root,choices:[...choices,{provider:'ma',id:'x',name:'<img src=x onerror=attack()>'}]});
assert.equal(mountPlaylistConfiguration({...f,container:root,choices}),null);
const label=panel.children.find(c=>c.tag==='label'),select=label.children.find(c=>c.tag==='select');
assert.ok(select.children.some(c=>c.textContent.includes('<img')));
select.value=JSON.stringify(['native','owned']);const button=panel.children.find(c=>c.tag==='button');
await button.listeners.click();assert.equal(f.writes.length,1);
f.game.user.isGM=false;await button.listeners.click();assert.equal(f.writes.length,1);
});
test('configuration hook disposal prevents pending asynchronous render from mounting',async()=>{
const f=fixture(),callbacks=new Map();let resolve;const off=[];
const ui=startConfigurationInterfaces({...f,Hooks:{on(e,fn){callbacks.set(e,fn);return e;},off(...args){off.push(args);}},getChoices:()=>new Promise(r=>{resolve=r;})});
const root=container();const pending=callbacks.get('renderSceneConfig')({document:f.document},root);ui.dispose();resolve(choices);await pending;assert.equal(root.children.length,0);assert.equal(off.length,2);
});