import Foundation
import Testing
import WebRTC
@testable import IrdP2P
@testable import IrdP2PSignaling
import IrdP2PCrypto
@Suite struct GatheringPolicyTests {
@Test func theFlagMapsToTheWebRTCPolicy() {
#expect(RoomOptions().continualGathering)
#expect(WebRTCPeerConnection.gatheringPolicy(continual: true) == .gatherContinually)
#expect(WebRTCPeerConnection.gatheringPolicy(continual: false) == .gatherOnce)
}
@Test(arguments: [true, false]) func theOptionReachesTheFactoryOnBothSides(continual: Bool) async throws {
let h = Harness()
var options = h.options()
options.continualGathering = continual
let (a, ca, fa) = try await h.join(peer: lowPeer, options: options) // offers
let (b, cb, fb) = try await h.join(peer: highPeer, options: options) // answers
#expect(await ca.waitFor(.peerReady(highPeer, .direct)))
#expect(await cb.waitFor(.peerReady(lowPeer, .direct)))
#expect(fa.continualGathering == [continual] && fb.continualGathering == [continual])
await a.leave(); await b.leave()
}
/// A factory written against the one-method protocol keeps compiling and is still what rooms get.
struct LegacyFactory: PeerConnectionFactory {
let inner: InMemoryPeerConnectionFactory
let made = Counter()
func makePeerConnection(ice: IceConfiguration) async throws -> any PeerConnectionHandle {
made.increment()
return try await inner.makePeerConnection(ice: ice)
}
}
final class Counter: @unchecked Sendable {
private let lock = NSLock()
private var n = 0
var value: Int { lock.withLock { n } }
func increment() { lock.withLock { n += 1 } }
}
@Test func factoriesWithoutTheNewMethodAreForwardedTo() async throws {
let h = Harness()
let legacy = LegacyFactory(inner: h.webrtc)
var options = h.options()
options.continualGathering = false
let a = try await h.app(peerConnections: legacy).join(secret: h.secret, peerId: lowPeer, options: options)
let (b, cb) = try await h.join(peer: highPeer)
#expect(await cb.waitFor(.peerReady(lowPeer, .direct)))
#expect(legacy.made.value == 1)
await a.leave(); await b.leave()
}
}