import test from 'node:test'; import assert from 'node:assert/strict'; import { iceConfiguration, validateDescription, getFingerprints, waitForIceComplete, waitForBufferDrain, MAX_SIGNAL_LENGTH, MAX_BUFFERED } from '../src/lib/webrtc.ts'; const fingerprint = Array.from({ length: 32 }, (_, i) => i.toString(16).padStart(2, '0')).join(':'); const sdp = `v=0\r\nm=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\na=fingerprint:sha-256 ${fingerprint}\r\n`; test('TURN is absent by default and dormant credentials cannot enable it', () => { assert.deepEqual(iceConfiguration(), { iceServers: [] }); const settings = { stunUrl: 'stun:example.com:3478', turnUrl: 'turn:relay.example', username: 'user', credential: 'secret' }; assert.equal(iceConfiguration(settings).iceServers.length, 1); assert.equal(iceConfiguration({ ...settings, allowTurn: true }).iceServers.length, 2); assert.throws(() => iceConfiguration({ stunUrl: 'turn:relay.example' })); assert.throws(() => iceConfiguration({ allowTurn: true })); }); test('remote relay candidates require local explicit opt-in', () => { const relayed = sdp + 'a=candidate:1 1 udp 10 192.0.2.1 5000 typ relay raddr 0.0.0.0 rport 0\r\n'; assert.throws(() => validateDescription(relayed), /explicitly enable TURN/); assert.equal(validateDescription(relayed, { allowTurn: true }), validateDescription(sdp)); }); test('fingerprints show all 32 bytes and reject ambiguous descriptions', () => { const expected = fingerprint.toUpperCase().replaceAll(':', ' '); assert.equal(validateDescription(sdp), expected); assert.deepEqual(getFingerprints({ localDescription: { sdp }, remoteDescription: { sdp } }), { local: expected, remote: expected }); assert.throws(() => validateDescription(sdp + `a=fingerprint:sha-256 ${fingerprint}\r\n`)); assert.throws(() => validateDescription(sdp.replace(fingerprint, 'AA:BB:CC:DD:EE'))); assert.throws(() => validateDescription(sdp + 'm=audio 9 UDP/TLS/RTP/SAVPF 111\r\n')); }); test('descriptions are bounded and printable', () => { assert.throws(() => validateDescription(sdp + 'a'.repeat(MAX_SIGNAL_LENGTH)), /Invalid connection description/); assert.throws(() => validateDescription(sdp + 'a=x:\u00e9\r\n'), /Invalid connection description/); assert.throws(() => validateDescription(42), /Invalid connection description/); }); class Peer extends EventTarget { iceGatheringState: RTCIceGatheringState = 'gathering'; connectionState: RTCPeerConnectionState = 'new'; } test('ICE gathering waits for completion, rejects timeout, and supports cancellation', async () => { const pc = new Peer(); const completed = waitForIceComplete(pc, { timeout: 100 }); pc.iceGatheringState = 'complete'; pc.dispatchEvent(new Event('icegatheringstatechange')); await completed; await assert.rejects(waitForIceComplete(new Peer(), { timeout: 5 }), /timed out/); const controller = new AbortController(); const cancelled = waitForIceComplete(new Peer(), { signal: controller.signal }); controller.abort(); await assert.rejects(cancelled, /cancelled/); }); test('buffer wait resolves when already drained and rejects close or cancellation', async () => { const channel = new class extends EventTarget { readyState: RTCDataChannelState = 'open'; bufferedAmount = 0; bufferedAmountLowThreshold = 0; }(); await waitForBufferDrain(channel); channel.bufferedAmount = MAX_BUFFERED + 1; const wait = waitForBufferDrain(channel); channel.readyState = 'closed'; channel.dispatchEvent(new Event('close')); await assert.rejects(wait, /interrupted/); channel.readyState = 'open'; const controller = new AbortController(); const cancelled = waitForBufferDrain(channel, { signal: controller.signal }); controller.abort(); await assert.rejects(cancelled, /interrupted/); });