import { describe, expect, it, vi } from "vitest"; import type { SignalPayload } from "../src/shared/protocol"; import { NativeSenderEdge, type NativeEdgeControl, } from "../src/client/native/native-sender-edge"; import type { NativeClientEvent } from "../src/client/native/wire"; function fixture(natPrediction = false, extraStun = false, retry = false) { let listener: ((event: NativeClientEvent) => void) | null = null; const prepareEdge = vi.fn(async () => ({ type: "offer", sdp: "v=0\r\n", })); const control: NativeEdgeControl = { preparePunch: vi.fn(async () => null), runPunch: vi.fn(async () => false), closePunch: vi.fn(async () => undefined), updateShare: vi.fn(async () => undefined), prepareEdge, acceptSignal: vi.fn(async () => undefined), closeEdge: vi.fn(async () => undefined), onEvent: vi.fn((next) => { listener = next; return () => { listener = null; }; }), }; const sent: SignalPayload[] = []; const states: RTCPeerConnectionState[] = []; const sendSignal = vi.fn((_peerId: string, payload: SignalPayload) => { sent.push(payload); return true; }); const edge = new NativeSenderEdge( "viewer_123456", "edge_12345678", "share_1234567", natPrediction ? { iceServers: [{ urls: ["stun:share.example.test:3478", ...(extraStun ? ["stun:other.example.test:3478"] : [])] }], natPredictionStunUrls: [ "stun:share.example.test:3479", "stun:share.example.test:3480", ], } : { iceServers: [], natPredictionStunUrls: [] }, natPrediction, control, { sendSignal, onState: (state) => states.push(state), }, undefined, retry, ); return { edge, control, sent, states, sendSignal, emit: (event: NativeClientEvent) => listener?.(event), }; } describe("native sender edge adapter", () => { it("keeps initial ICE immediate and falls through immediately for a Browser retry", async () => { const initial = fixture(true); expect(await initial.edge.start()).toBe(true); expect(initial.control.preparePunch).not.toHaveBeenCalled(); initial.edge.dispose(); const retry = fixture(true, false, true); const started = retry.edge.start(); expect(retry.sent[0].kind).toBe("punch"); await retry.edge.acceptSignal({ kind: "punch", connectionId: "edge_12345678", punch: { phase: "ready" } }); expect(await started).toBe(true); expect(retry.control.runPunch).not.toHaveBeenCalled(); expect(retry.control.prepareEdge).toHaveBeenCalledWith("share_1234567", "edge_12345678", expect.anything(), undefined, false); expect(retry.sent.find(signal => signal.kind === "description")).not.toHaveProperty("punched"); retry.edge.dispose(); }); it.each([null, { candidate: "candidate:1 1 udp 1 127.0.0.1 9 typ host" }])( "keeps connected media when trickle signaling is unavailable: %j", async (candidate) => { const current = fixture(); expect(await current.edge.start()).toBe(true); current.emit({ version: 10, type: "edge-state", shareId: "share_1234567", connectionId: "edge_12345678", state: "connected" }); current.sendSignal.mockReturnValue(false); current.emit({ version: 10, type: "edge-candidate", shareId: "share_1234567", connectionId: "edge_12345678", candidate }); expect(current.edge.isConnected()).toBe(true); expect(current.control.closeEdge).not.toHaveBeenCalled(); expect(current.states).toEqual(["connected"]); current.emit({ version: 10, type: "edge-state", shareId: "share_1234567", connectionId: "edge_12345678", state: "failed" }); expect(current.edge.isConnected()).toBe(false); expect(current.states).toEqual(["connected", "failed"]); current.edge.dispose(); current.emit({ version: 10, type: "edge-state", shareId: "share_1234567", connectionId: "edge_12345678", state: "connected" }); expect(current.edge.isConnected()).toBe(false); expect(current.control.closeEdge).toHaveBeenCalledOnce(); }, ); it("retires an offer that could not be sent, but leaves gathered candidates to edge recovery", async () => { const rejected = fixture(); rejected.sendSignal.mockReturnValue(false); expect(await rejected.edge.start()).toBe(false); expect(rejected.control.closeEdge).toHaveBeenCalledOnce(); const current = fixture(); vi.mocked(current.control.prepareEdge).mockImplementationOnce(async () => { current.emit({ version: 10, type: "edge-candidate", shareId: "share_1234567", connectionId: "edge_12345678", candidate: null }); return { type: "offer", sdp: "v=0\r\n" }; }); current.sendSignal.mockImplementation((_peer, payload) => payload.kind === "description"); expect(await current.edge.start()).toBe(true); expect(current.control.closeEdge).not.toHaveBeenCalled(); current.edge.dispose(); }); it("sends the offer before candidates gathered during preparation", async () => { const current = fixture(); const prepare = vi.mocked(current.control.prepareEdge); prepare.mockImplementationOnce(async () => { current.emit({ version: 10, type: "edge-candidate", shareId: "share_1234567", connectionId: "edge_12345678", candidate: { candidate: "candidate:1 1 udp 1 127.0.0.1 9 typ host", }, }); return { type: "offer", sdp: "v=0\r\n" }; }); expect(await current.edge.start()).toBe(true); expect(current.sent.map((payload) => payload.kind)).toEqual([ "description", "candidate", ]); }); it("forwards only exact-generation signals and state", async () => { const current = fixture(); expect(await current.edge.start()).toBe(true); current.emit({ version: 10, type: "edge-state", shareId: "share_1234567", connectionId: "edge_12345678", state: "connected", }); expect(current.edge.isConnected()).toBe(true); await current.edge.acceptSignal({ kind: "candidate", connectionId: "other_123456", candidate: null, }); expect(current.control.acceptSignal).not.toHaveBeenCalled(); await current.edge.acceptSignal({ kind: "candidate", connectionId: "edge_12345678", candidate: null, }); expect(current.control.acceptSignal).toHaveBeenCalledOnce(); }); it("reuses the Site survey for Native candidate prediction", async () => { const current = fixture(true); expect(await current.edge.start()).toBe(true); expect(vi.mocked(current.control.prepareEdge).mock.calls[0]?.[2].iceServers) .toHaveLength(3); for (const [index, port] of [40_000, 40_003, 40_006].entries()) { current.emit({ version: 10, type: "edge-candidate", shareId: "share_1234567", connectionId: "edge_12345678", candidate: { candidate: `candidate:ns${index + 1} 1 udp 1 203.0.113.7 ${port} typ srflx`, }, }); } const candidates = current.sent.flatMap((payload) => payload.kind === "candidate" && payload.candidate ? [payload.candidate.candidate] : [], ); expect(candidates.filter((candidate) => /^candidate:s[pm]\d+ /.test(candidate))) .toHaveLength(8); }); it("forwards extra STUN candidates without mistaking them for prediction evidence", async () => { const current = fixture(true, true); expect(await current.edge.start()).toBe(true); const emit = (index: number, port: number) => current.emit({ version: 10, type: "edge-candidate", shareId: "share_1234567", connectionId: "edge_12345678", candidate: { candidate: `candidate:ns${index} 1 udp 1 203.0.113.7 ${port} typ srflx` }, }); const candidates = () => current.sent.flatMap(payload => payload.kind === "candidate" && payload.candidate ? [payload.candidate.candidate] : []); // Deliberately receive out of list order, with three seemingly sequential ports. emit(3, 40_006); emit(2, 40_003); emit(1, 40_000); expect(candidates()).toHaveLength(3); emit(4, 40_012); expect(candidates().filter(value => /^candidate:ns/.test(value))).toHaveLength(4); expect(candidates().filter(value => /^candidate:s[pm]/.test(value))).toHaveLength(8); current.edge.dispose(); }); });