74 lines
3.9 KiB
TypeScript
74 lines
3.9 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { ConnectionDiagnostics, summarizeCandidates, summarizeStats } from '../src/lib/connection-diagnostics.ts';
|
|
|
|
test('candidate summaries keep types and end marker, never addresses or SDP secrets', () => {
|
|
const sdp = 'a=ice-pwd:private-password\r\na=fingerprint:sha-256 SECRET\r\n' +
|
|
'a=candidate:private-id 1 udp 123 192.0.2.20 5000 typ host\r\n' +
|
|
'a=candidate:other-id 1 udp 123 example.local 5000 typ host\r\n' +
|
|
'a=candidate:another-id 1 udp 123 2001:db8::1 5000 typ srflx\r\n' +
|
|
'a=candidate:last-id 1 udp 123 192.0.2.22 5000 typ private-value\r\na=end-of-candidates\r\n';
|
|
assert.equal(summarizeCandidates(sdp), 'host=2 srflx=1 prflx=0 relay=0 unknown=1 end=yes');
|
|
assert.equal(summarizeCandidates(undefined), 'host=0 srflx=0 prflx=0 relay=0 unknown=0 end=no');
|
|
});
|
|
|
|
test('stats use only allowlisted types, pair states and nonnegative integer counters', () => {
|
|
const stats = new Map([
|
|
['private-local-id', { type: 'local-candidate', candidateType: 'srflx', protocol: 'udp', address: '192.0.2.20', usernameFragment: 'private-user' }],
|
|
['private-remote-id', { type: 'remote-candidate', candidateType: 'host', protocol: 'private-protocol', address: '192.0.2.21' }],
|
|
['private-pair-id', { id: 'private-pair-id', type: 'candidate-pair', localCandidateId: 'private-local-id', remoteCandidateId: 'private-remote-id', state: 'failed', nominated: false, requestsSent: 8, requestsReceived: 0, responsesReceived: -1, responsesSent: 'private-text' }],
|
|
['transport', { type: 'transport', selectedCandidatePairId: 'private-pair-id' }],
|
|
]);
|
|
const lines = summarizeStats(stats);
|
|
assert.equal(lines[3], 'pair srflx/udp → host/unknown failed selected=true nominated=false requestsSent=8 requestsReceived=0 responsesSent=? responsesReceived=?');
|
|
assert.doesNotMatch(lines.join('\n'), /private|192\.0\.2/);
|
|
});
|
|
|
|
class Peer extends EventTarget {
|
|
connectionState = 'failed';
|
|
iceConnectionState = 'failed';
|
|
iceGatheringState = 'complete';
|
|
closed = false;
|
|
getStats: () => Promise<Map<string, Record<string, unknown>>> = async () => new Map();
|
|
}
|
|
const diagnose = (peer: Peer) => new ConnectionDiagnostics(peer as unknown as RTCPeerConnection, () => {});
|
|
|
|
test('final stats are collected before close, close is called once, and listeners stop', async () => {
|
|
const peer = new Peer();
|
|
let resolve!: (stats: Map<string, Record<string, unknown>>) => void;
|
|
peer.getStats = () => {
|
|
assert.equal(peer.closed, false);
|
|
return new Promise(done => { resolve = done; });
|
|
};
|
|
const diagnostics = diagnose(peer);
|
|
let closes = 0;
|
|
const close = () => { peer.closed = true; closes++; };
|
|
const finishing = diagnostics.finish(close);
|
|
assert.equal(diagnostics.finish(close), finishing);
|
|
assert.equal(closes, 0);
|
|
resolve(new Map());
|
|
await finishing;
|
|
assert.equal(closes, 1);
|
|
assert.ok(diagnostics.lines.some(line => line.endsWith('pairs=0')));
|
|
const saved = [...diagnostics.lines];
|
|
peer.dispatchEvent(new Event('connectionstatechange'));
|
|
assert.deepEqual(diagnostics.lines, saved);
|
|
});
|
|
|
|
test('rejected and hung stats cannot prevent peer cleanup or leak error details', async () => {
|
|
for (const getStats of [async () => { throw new Error('private credential'); }, () => new Promise<Map<string, Record<string, unknown>>>(() => {})]) {
|
|
const peer = new Peer(); peer.getStats = getStats;
|
|
const diagnostics = diagnose(peer);
|
|
await diagnostics.finish(() => { peer.closed = true; });
|
|
assert.equal(peer.closed, true);
|
|
assert.match(diagnostics.lines.join('\n'), /stats unavailable/);
|
|
assert.doesNotMatch(diagnostics.lines.join('\n'), /private credential/);
|
|
}
|
|
});
|
|
|
|
test('event history remains bounded', () => {
|
|
const peer = new Peer(); const diagnostics = diagnose(peer);
|
|
for (let i = 0; i < 100; i++) peer.dispatchEvent(new Event('iceconnectionstatechange'));
|
|
assert.equal(diagnostics.lines.length, 80);
|
|
});
|