Files
adaptive-music/tests/js/pcm-timeline.test.mjs
T

61 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2026-12-05 09:40:14 -05:00
import assert from 'node:assert/strict';
import {test} from 'node:test';
import {PcmTimeline} from '../../src/audio/pcm-timeline.mjs';
const sampleRate=48000;
const packet=serverTimeUs=>({samples:[new Float32Array(960)],sampleRate,serverTimeUs});
function fixture(){
const sources=[];
const context={currentTime:1,state:'running',
createBuffer:(_channels,frames)=>({data:new Float32Array(frames),getChannelData(){return this.data;}}),
createBufferSource(){
const source={playbackRate:{value:1},connect(){},disconnect(){},start(time){this.time=time;},stop(time){this.stopTime=time;}};
sources.push(source);
return source;
},
};
return {context,sources,timeline:new PcmTimeline(context,{})};
}
test('consecutive PCM stays contiguous under a jittering browser clock',()=>{
const {context,sources,timeline}=fixture();
for(let i=0;i<8;i++){
context.currentTime=1+i*0.02+i%2*0.0025;
timeline.push(packet(2000000+i*20000),1900000+i*20000);
}
for(let i=1;i<sources.length;i++){
const previous=sources[i-1],current=sources[i];
assert.ok(Math.abs(current.time-previous.time-0.02/previous.playbackRate.value)<1e-9);
}
assert.equal(timeline.diagnostics.sourceGaps,0);
assert.equal(timeline.diagnostics.underruns,0);
});
test('partial clear preserves only the valid span and allows replacement at its boundary',()=>{
const {sources,timeline}=fixture();
const start=timeline.push(packet(2000000),1900000);
timeline.push(packet(2020000),1900000);
timeline.push(packet(2040000),1900000);
timeline.clear(2030000);
assert.ok(Math.abs(sources[1].stopTime-start-0.03)<1e-9);
assert.equal(sources[2].stopTime,undefined);
assert.equal(timeline.push(packet(2030000),1900000),sources[1].stopTime);
assert.equal(timeline.diagnostics.sourceOverlaps,0);
});
test('source gaps and late packets are observable separately from suspension',()=>{
const {context,timeline}=fixture();
timeline.push(packet(2000000),1900000);
timeline.push(packet(2030000),1900000);
assert.equal(timeline.diagnostics.sourceGaps,1);
assert.equal(timeline.diagnostics.gapMs,10);
assert.equal(timeline.push(packet(1000000),1900000),false);
assert.equal(timeline.diagnostics.droppedLate,1);
context.state='suspended';
assert.equal(timeline.push(packet(2080000),1900000),false);
assert.equal(timeline.diagnostics.bufferedMs,0);
context.state='running';
assert.ok(timeline.push(packet(2080000),1900000)>0);
assert.equal(timeline.diagnostics.sourceGaps,1);
});