Preserve accumulated client implementation since v0.1.4, including local identities, multi-server state, chat, administration, audio and streaming controls. Refine official-source layout, original artwork, responsive composer overflow, settings navigation and chat scroll anchoring. Current verification: Node suite passes; isolated packaged chat and responsive control smokes pass without device capture. Native core is reused, not rebuilt in this pass. Strict whole-screen visual parity remains incomplete; no 1:1 or cross-platform acceptance claim. Local checkpoint only; no release or push.
136 lines
7.2 KiB
JavaScript
136 lines
7.2 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { attachApplicationAudio, decodeApplicationPcm } from '../app/renderer/js/stream/appaudio-renderer.js';
|
|
class Track extends EventTarget { constructor(kind) { super(); this.kind = kind; this.readyState = 'live'; } stop() { this.readyState = 'ended'; } }
|
|
class Stream {
|
|
constructor() { this.tracks = [new Track('video')]; }
|
|
getVideoTracks() { return this.tracks.filter(track => track.kind === 'video'); }
|
|
getAudioTracks() { return this.tracks.filter(track => track.kind === 'audio'); }
|
|
addTrack(track) { this.tracks.push(track); }
|
|
removeTrack(track) { this.tracks = this.tracks.filter(item => item !== track); }
|
|
}
|
|
function fixture({ start } = {}) {
|
|
const stream = new Stream();
|
|
const worklets = [];
|
|
const contexts = [];
|
|
let listener;
|
|
let stopped = 0;
|
|
const node = () => ({ gain: { value: 1, setValueAtTime(value) { this.value = value; } }, connect(next) { return next; }, disconnect() {} });
|
|
class Context {
|
|
constructor() { this.sampleRate = 48000; this.currentTime = 0; this.audioWorklet = { addModule: async () => {} }; this.gains = []; contexts.push(this); }
|
|
resume() { return Promise.resolve(); }
|
|
close() { this.closed = true; return Promise.resolve(); }
|
|
createGain() { const gain = node(); this.gains.push(gain); return gain; }
|
|
createMediaStreamDestination() { return { ...node(), stream: { getAudioTracks: () => [new Track('audio')] } }; }
|
|
}
|
|
class Worklet extends EventTarget {
|
|
constructor() {
|
|
super(); Object.assign(this, node()); this.messages = [];
|
|
this.port = { postMessage: value => { this.messages.push(value); if (value.type === 'stop') queueMicrotask(() => this.port.onmessage?.({ data: { type: 'stopped' } })); }, close() {} };
|
|
worklets.push(this);
|
|
}
|
|
}
|
|
const bridge = {
|
|
onAppAudio(callback) { listener = callback; return () => { listener = null; }; },
|
|
ackAppAudio() {},
|
|
async startAppAudio() {
|
|
assert.equal(typeof listener, 'function', 'subscribe before native starts');
|
|
listener({ event: 'pcm', sessionId: 'selected', id: 'app', token: 1, sequence: 0, pcm: Buffer.alloc(3840).toString('base64') });
|
|
if (start) return start();
|
|
return { ok: true };
|
|
},
|
|
async stopAppAudio() { stopped++; return { ok: true }; },
|
|
};
|
|
return { stream, worklets, contexts, bridge, dependencies: { bridge, AudioContext: Context, AudioWorkletNode: Worklet }, emit: packet => listener?.(packet), stopped: () => stopped };
|
|
}
|
|
test('decoder validates exact signed little endian stereo frame', () => {
|
|
const bytes = Buffer.alloc(3840);
|
|
bytes.writeInt16LE(-32768, 0); bytes.writeInt16LE(16384, 2);
|
|
const samples = decodeApplicationPcm(bytes.toString('base64'));
|
|
assert.equal(samples[0], -1); assert.equal(samples[1], 0.5);
|
|
assert.throws(() => decodeApplicationPcm('AAAA'), /Invalid/);
|
|
});
|
|
test('mixer subscribes before capture, gains are isolated and release is idempotent', async () => {
|
|
const f = fixture();
|
|
const mixed = await attachApplicationAudio(f.stream, { sessionId: 'selected', applications: ['app'], gains: { app: 0.5 }, masterGain: 0.8 }, f.dependencies);
|
|
assert.equal(f.worklets[0].messages[0].type, 'pcm');
|
|
assert.equal(f.stream.getAudioTracks().length, 1);
|
|
mixed.setAppGain('app', 0.25);
|
|
assert.equal(f.contexts[0].gains[1].gain.value, 0.25);
|
|
assert.equal(f.contexts[0].gains[0].gain.value, 0.8);
|
|
mixed.setMasterGain(0);
|
|
assert.equal(f.contexts[0].gains[1].gain.value, 0.25);
|
|
await mixed.release(); await mixed.release();
|
|
assert.equal(f.stopped(), 1);
|
|
assert.equal(f.contexts[0].closed, true);
|
|
assert.equal(f.stream.getAudioTracks().length, 0);
|
|
assert.equal(f.stream.getVideoTracks()[0].readyState, 'live');
|
|
});
|
|
test('abort while start is pending disposes resources and a late successful result cannot attach', async () => {
|
|
let finish;
|
|
const f = fixture({ start: () => new Promise(resolve => { finish = resolve; }) });
|
|
const abort = new AbortController();
|
|
const attaching = attachApplicationAudio(f.stream, { sessionId: 'selected', applications: ['app'], signal: abort.signal }, f.dependencies);
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
abort.abort();
|
|
finish({ ok: true });
|
|
await assert.rejects(attaching, /cancelled/);
|
|
assert.equal(f.contexts[0].closed, true);
|
|
assert.equal(f.stream.getAudioTracks().length, 0);
|
|
assert.equal(f.stopped(), 1);
|
|
});
|
|
test('native error and source ended stop the chosen audio without fallback', async () => {
|
|
const f = fixture();
|
|
const errors = [];
|
|
await attachApplicationAudio(f.stream, { sessionId: 'selected', applications: ['app'], onError: cause => errors.push(cause) }, f.dependencies);
|
|
f.emit({ sessionId: 'selected', event: 'error', message: 'Selected app exited' });
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
assert.equal(errors.length, 1);
|
|
assert.equal(f.stopped(), 1);
|
|
assert.equal(f.stream.getAudioTracks().length, 0);
|
|
const other = fixture();
|
|
await attachApplicationAudio(other.stream, { sessionId: 'selected', applications: ['app'] }, other.dependencies);
|
|
other.stream.getVideoTracks()[0].dispatchEvent(new Event('ended'));
|
|
await new Promise(resolve => setImmediate(resolve));
|
|
assert.equal(other.stopped(), 1);
|
|
});
|
|
|
|
test('closed worklet port still closes context and native helper', async () => {
|
|
const f = fixture();
|
|
const mixed = await attachApplicationAudio(f.stream, { sessionId: 'selected', applications: ['app'] }, f.dependencies);
|
|
f.worklets[0].port.postMessage = () => { throw new Error('closed port'); };
|
|
await mixed.release();
|
|
assert.equal(f.contexts[0].closed, true);
|
|
assert.equal(f.stopped(), 1);
|
|
});
|
|
|
|
test('context close failure is observable and explicit release retries retained context', async () => {
|
|
const f = fixture();
|
|
const mixed = await attachApplicationAudio(f.stream, { sessionId: 'selected', applications: ['app'] }, f.dependencies);
|
|
const close = f.contexts[0].close.bind(f.contexts[0]);
|
|
f.contexts[0].close = async () => { throw new Error('close failed'); };
|
|
await assert.rejects(mixed.release(), /close failed/);
|
|
assert.equal(f.stopped(), 1);
|
|
f.contexts[0].close = close;
|
|
await mixed.release();
|
|
assert.equal(f.contexts[0].closed, true);
|
|
});
|
|
|
|
test('all/exclude mixer follows backend application set, not selection ids', async () => {
|
|
for (const scope of ['all', 'exclude']) {
|
|
const f = fixture();
|
|
f.bridge.startAppAudio = async options => {
|
|
assert.equal(options.scope, scope);
|
|
f.emit({ sessionId: 'selected', event: 'applications', applications: [{ id: 'included', label: 'Included' }] });
|
|
return { ok: true };
|
|
};
|
|
const mixed = await attachApplicationAudio(f.stream, { sessionId: 'selected', applications: scope === 'exclude' ? ['excluded'] : [], scope }, f.dependencies);
|
|
assert.equal(f.worklets.length, 1);
|
|
f.emit({ sessionId: 'selected', event: 'pcm', id: 'included', token: 1, sequence: 0, pcm: Buffer.alloc(3840).toString('base64') });
|
|
assert.equal(f.worklets[0].messages[0].type, 'pcm');
|
|
f.emit({ sessionId: 'selected', event: 'applications', applications: [{ id: 'new', label: 'New' }] });
|
|
assert.equal(f.worklets[0].messages.at(-1).type, 'stop'); assert.equal(f.worklets.length, 2);
|
|
await mixed.release(); assert.equal(f.stream.getAudioTracks().length, 0);
|
|
}
|
|
});
|