Files
adaptive-music/tests/js/native.test.mjs
T

64 lines
4.6 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 { NativeProvider } from '../../src/providers/native/provider.mjs';
import { primaryGM, startNativeRuntime } from '../../src/foundry/runtime.mjs';
const collection = items => { const m=new Map(items.map(x=>[x.id,x])); m[Symbol.iterator]=function*(){yield* this.values();}; return m; };
function fixture() {
const calls=[];
const p={id:'owned',mode:0,sounds:collection([
{id:'a',sort:0,playing:true,pausedTime:null,repeat:true,volume:0.4,sound:{currentTime:42,duration:100}},
{id:'b',sort:1,playing:false,pausedTime:12,repeat:false,volume:0.7,sound:{duration:100}}
]), async updateEmbeddedDocuments(type,updates){calls.push({type,updates}); for(const u of updates)Object.assign(this.sounds.get(u._id),u);} };
const other={id:'household',sounds:collection([]),async updateEmbeddedDocuments(){assert.fail('unowned mutation');}};
const provider=new NativeProvider({playlists:collection([p,other]),ownedPlaylistIds:['owned'],campaign:'c',session:'s'});
const ctx={campaign:'c',session:'s',generation:4,check(){}};
return {provider,p,ctx,calls};
}
test('native captures actual offset and paused state, stops per sound, restores repeat/volume',async()=>{
const {provider,p,ctx,calls}=fixture(); const snap=await provider.snapshot(ctx);
assert.equal(snap.playlists[0].sounds[0].pausedTime,42);
await provider.stopOwned(ctx); assert.equal(calls[0].type,'PlaylistSound'); assert.equal(calls[0].updates.length,1);
await provider.restore(snap,{...ctx,generation:5});
assert.equal(p.sounds.get('a').pausedTime,42); assert.equal(p.sounds.get('a').repeat,true);
assert.equal(p.sounds.get('b').playing,false); assert.equal(p.sounds.get('b').pausedTime,12);
});
test('native resolves sequential playlist and touches only owned documents',async()=>{
const {provider,ctx,calls}=fixture(); const prepared=await provider.resolve({id:'owned'},ctx);
await provider.apply(prepared,ctx); assert.equal(calls[0].updates[0]._id,'a');
await assert.rejects(provider.resolve({id:'household'},ctx),{code:'denied'});
await assert.rejects(provider.resolve({id:'owned'},{...ctx,campaign:'other'}),{code:'denied'});
});
test('native rejects unsupported shuffle and empty playlist',async()=>{
const {provider,p,ctx}=fixture(); p.mode=1;
await assert.rejects(provider.resolve({id:p.id},ctx),{code:'unsupported'});
p.mode=0;p.sounds.clear();await assert.rejects(provider.resolve({id:p.id},ctx),{code:'unavailable'});
});
test('native rejects expired tracks, changed playlist membership and foreign/future snapshots before mutation',async()=>{
const {provider,p,ctx,calls}=fixture();const snap=await provider.snapshot(ctx);
for(const patch of [{campaign:'other'},{session:'other'},{generation:100}])await assert.rejects(provider.restore({...snap,...patch},ctx),{code:'stale'});
p.sounds.delete('b'); await assert.rejects(provider.restore(snap,ctx),{code:'expired-media'});assert.equal(calls.length,0);
});
test('native reports nonseekable restoration and clamps near-end seek',async()=>{
const {provider,p,ctx,calls}=fixture();const snap=await provider.snapshot(ctx);
p.sounds.get('a').sound.duration=Infinity;
await assert.rejects(provider.restore(snap,ctx),{code:'unsupported'});assert.equal(calls.length,0);
p.sounds.get('a').sound.duration=40;await provider.restore(snap,ctx);assert.equal(p.sounds.get('a').pausedTime,39.99);
});
test('scene-linked playlist conflict and lost authority fail before mutation',async()=>{
const {provider,ctx,calls}=fixture();provider.sceneConflict=()=>true;
await assert.rejects(provider.resolve({id:'owned'},ctx),{code:'scene-conflict'});
provider.sceneConflict=()=>false;
await assert.rejects(provider.stopOwned({...ctx,check(){throw new Error('lost');}}));assert.equal(calls.length,0);
});
test('GM election deterministic and no GM means none',()=>{
assert.equal(primaryGM([{id:'z',active:true,isGM:true},{id:'a',active:true,isGM:true},{id:'0',active:false,isGM:true}]),'a');
assert.equal(primaryGM([]),undefined);
});
test('Foundry bootstrap registers local settings/hooks and disposes without ambient or actor access',async()=>{
const callbacks=new Map();let index=0,registered=0;
const Hooks={on(e,cb){callbacks.set(++index,cb);return index;},off(e,id){callbacks.delete(id);}};
const game={ready:true,user:{id:'gm'},users:[{id:'gm',isGM:true,active:true}],playlists:collection([]),scenes:[],combats:[],
settings:{register(){registered++;},get(){return null;}}};
const runtime=startNativeRuntime({game,Hooks,campaign:'c',session:'s',ownedPlaylistIds:[]});
await runtime.refresh();assert.equal(registered,1);assert.ok(callbacks.size>0);runtime.dispose();assert.equal(callbacks.size,0);
});