Files
NumberSwipe_New/wechat-campaign-200-r2/bgm-duck.test.mjs
T

7 lines
2.2 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';import fs from 'node:fs';import {Music} from './src/music.mjs';import {CasualAudio} from './src/audio.mjs';
function setup(){const contexts=[];const api={getStorageSync(){},setStorageSync(){},createInnerAudioContext(){const a={stop(){},play(){},onError(fn){this.error=fn;},onEnded(fn){this.ended=fn;},offEnded(fn){if(this.ended===fn)this.ended=null;}};contexts.push(a);return a;}};globalThis.wx=api;const m=new Music(api);m.scene('play','one');const sound=new CasualAudio({}, {getItem(){},setItem(){}},'test');sound.beforeComplete=()=>m.duck();sound.afterComplete=()=>m.unduck();return {m,sound,contexts};}
test('jingle end, cancellation and playback error restore BGM without restarting',()=>{const {m,sound}=setup();sound.play('complete');const a=sound.channels[3];assert.equal(m.ducked,true);a.ended();assert.equal(m.audio.volume,.119);sound.play('complete');const late=a.ended;sound.stop();assert.equal(m.ducked,false);sound.play('complete');late();assert.equal(m.ducked,true);a.error({errMsg:'failed'});assert.equal(m.ducked,false);sound.play('complete');sound.toggle();assert.equal(m.ducked,false);});
test('muted effects never duck BGM and unrelated effect errors cannot end jingle ducking',()=>{const {m,sound}=setup();sound.enabled=false;sound.play('complete');assert.equal(m.ducked,false);sound.enabled=true;sound.play('complete');sound.channels[0].error({errMsg:'unrelated'});assert.equal(m.ducked,true);sound.stop();});
test('hide and music preference do not revive playback through a late jingle callback',()=>{const {m,sound}=setup();sound.play('complete');m.suspend();sound.stop();assert.equal(m.playing,null);assert.equal(m.ducked,false);m.resume();assert.equal(m.audio.volume,.119);sound.play('complete');m.toggle();sound.channels[3].ended();assert.equal(m.playing,null);assert.equal(m.enabled,false);m.toggle();assert.equal(m.audio.volume,.119);});
test('BGMB is absent from the runtime manifest and music package',()=>{const manifest=JSON.parse(fs.readFileSync(new URL('./.audio-sources/manifest.json',import.meta.url)));assert.equal(manifest['play-b'],undefined);assert.equal(fs.existsSync(new URL('./music/play-b.wav',import.meta.url)),false);});