// Jasmine spec for voice-mesh.js's `tuneOpus` — the pure SDP-munging helper
// that turns on Opus in-band FEC + DTX and caps the bitrate for voice
// (Phase C of the my-sea invite/voice sprint). The RTCPeerConnection mesh
// itself is verified by hand; this pins the one deterministic, testable part.
describe('voice-mesh tuneOpus', function () {
    var SDP_WITH_FMTP =
        'v=0\r\n' +
        'm=audio 9 UDP/TLS/RTP/SAVPF 111\r\n' +
        'a=rtpmap:111 opus/48000/2\r\n' +
        'a=fmtp:111 minptime=10\r\n';

    var SDP_NO_FMTP =
        'v=0\r\n' +
        'm=audio 9 UDP/TLS/RTP/SAVPF 111\r\n' +
        'a=rtpmap:111 opus/48000/2\r\n';

    it('exposes tuneOpus globally', function () {
        expect(typeof window.tuneOpus).toBe('function');
    });

    it('appends FEC/DTX/bitrate to an existing opus fmtp line', function () {
        var out = window.tuneOpus(SDP_WITH_FMTP);
        expect(out).toContain('a=fmtp:111 minptime=10;');
        expect(out).toContain('useinbandfec=1');
        expect(out).toContain('usedtx=1');
        expect(out).toContain('maxaveragebitrate=40000');
    });

    it('adds an fmtp line when opus has none', function () {
        var out = window.tuneOpus(SDP_NO_FMTP);
        expect(out).toMatch(/a=fmtp:111 [^\r\n]*useinbandfec=1/);
    });

    it('leaves SDP without opus untouched', function () {
        var pcmu = 'v=0\r\nm=audio 9 RTP/AVP 0\r\na=rtpmap:0 PCMU/8000\r\n';
        expect(window.tuneOpus(pcmu)).toBe(pcmu);
    });

    it('is null-safe', function () {
        expect(window.tuneOpus('')).toBe('');
    });
});
