6 lines
1.9 KiB
JavaScript
6 lines
1.9 KiB
JavaScript
import test from 'node:test';import assert from 'node:assert/strict';import {CasualAudio} from '../audio.mjs';
|
|
function host(){const events=[];class Audio{constructor(){this.currentTime=1;this.state='running';this.destination={};}createOscillator(){return {frequency:{setValueAtTime(f,t){events.push({f,t});}},connect(){},disconnect(){},start(){},stop(){}};}createGain(){return {gain:{setValueAtTime(){},linearRampToValueAtTime(){},exponentialRampToValueAtTime(){}},connect(){},disconnect(){}};}}return {AudioContext:Audio,events};}
|
|
test('six distinct soft cues and muted audio cannot start voices',()=>{const h=host(),memory=new Map(),storage={getItem:k=>memory.get(k),setItem:(k,v)=>memory.set(k,v)},audio=new CasualAudio(h,storage,'sound');const shapes=[];for(const kind of ['move','correct','error','undo','restart','complete']){const at=h.events.length;audio.play(kind);assert.equal(audio.lastEvent,kind);shapes.push(h.events.slice(at).map(e=>e.f).join(','));}assert.equal(new Set(shapes).size,6);audio.toggle();const n=h.events.length;audio.play('complete');assert.equal(h.events.length,n);assert.equal(memory.get('sound'),'off');});
|
|
test('rapid movement and repeated errors are throttled without suppressing correct arrival',()=>{const h=host(),a=new CasualAudio(h,{getItem(){return null},setItem(){}},'k');a.play('move');a.play('move');assert.equal(h.events.length,1);a.play('correct');assert.equal(h.events.length,3);a.play('error');a.play('error');assert.equal(h.events.length,5);a.context.currentTime+=.2;a.play('move');assert.equal(h.events.length,6);});
|
|
test('unsupported audio and blocked resume keep gameplay safe',()=>{assert.doesNotThrow(()=>new CasualAudio({}, {getItem(){throw Error()},setItem(){}},'k').play('move'));class FailingAudio{constructor(){throw Error('not supported')}}assert.doesNotThrow(()=>new CasualAudio({AudioContext:FailingAudio},{getItem(){return null}},'k').play('complete'));});
|