// The VoIP push handler: every push is reported to CallKit at once, with the // call's UUID made from the room, before the core is asked anything. import CottonKit import Foundation import Testing @testable import CottonCalls @MainActor @Suite struct VoIPPushTests { private let room = "0123456789abcdef0123456789abcdef" // RULE: a ring is reported to CallKit first, under the room's UUID, then handed to the core as calls.incoming. @Test func aRingIsReportedThenAsked() async { let rig = Rig() let handler = VoIPPushHandler(reporter: rig.system, center: rig.center) let done = Flag() handler.handle(payload: ["room": room, "video": true]) { done.set() } #expect(done.isSet, "PushKit's completion runs once CallKit has the report") #expect(rig.system.rings.map(\.0) == [UUID(uuidString: "01234567-89AB-CDEF-0123-456789ABCDEF")!]) #expect(rig.system.rings.map(\.1) == [true]) #expect(rig.core.methods.isEmpty, "reported before the core is asked") await rig.center.settle() // Said to be a push (CallKit has it: a ring for a call that ended here is ended at once), then acked (CALLS FIX 2). // The ack runs apart from the asks (it never holds up the ring), so it may come first. #expect(rig.core.methods.sorted() == ["calls.ack", "calls.incoming"]) #expect(rig.core.request("calls.incoming") == .object(["room": .string(room), "video": .bool(true), "later": .bool(true), "pushed": .bool(true)])) #expect(rig.core.request("calls.ack") == .object(["room": .string(room)])) } // RULE: a ring's box goes to the core as `ring` with this phone's chat key, asked to answer before the join. @Test func aRingsBoxGoesWithTheChatKey() async { let rig = Rig() let handler = VoIPPushHandler(reporter: rig.system, center: rig.center, chatKey: { "invented-chat-key" }) handler.handle(payload: ["room": room, "video": false, "box": "c1:invented"]) {} #expect(rig.system.rings.count == 1, "reported before the box is opened") await rig.center.settle() #expect(rig.core.request("calls.incoming") == .object([ "room": .string(room), "video": .bool(false), "later": .bool(true), "ring": "c1:invented", "privateKey": "invented-chat-key", "pushed": .bool(true), ])) } // RULE: no box, no key is handed over. @Test func noBoxNoKey() async { let rig = Rig() let handler = VoIPPushHandler(reporter: rig.system, center: rig.center, chatKey: { "invented-chat-key" }) handler.handle(payload: ["room": room]) {} await rig.center.settle() #expect(rig.core.request("calls.incoming")?["privateKey"] == nil) } // RULE: the answer's effects name the caller on the call CallKit holds, and the join it left is asked next. @Test func theCallerIsNamedThenTheJoinIsAsked() async { let rig = Rig() rig.core.reply("calls.incoming", .object([ "call": .object(["phase": "ringing_in"]), "effects": .array([.object(["do": "system_call_incoming", "uuid": "01234567-89AB-CDEF-0123-456789ABCDEF", "title": "An Invented Friend", "handle": "+15550100001", "video": .bool(false)])]), "join": .string(room), ])) rig.center.incoming(room: room, video: false, ring: "c1:invented", privateKey: "invented-chat-key") await rig.center.settle() #expect(rig.system.said == ["incoming An Invented Friend +15550100001"]) #expect(rig.core.methods == ["calls.incoming", "calls.join"]) #expect(rig.core.request("calls.join") == .object(["room": .string(room)])) } // RULE: a stop push is reported too (iOS asks a report for every VoIP push), then handed to the core as calls.stop. @Test func aStopIsReportedThenAsked() async { let rig = Rig() let handler = VoIPPushHandler(reporter: rig.system, center: rig.center) handler.handle(payload: ["room": room, "stop": "answered_elsewhere"]) {} #expect(rig.system.rings.count == 1) await rig.center.settle() #expect(rig.core.methods.sorted() == ["calls.ack", "calls.stop"]) #expect(rig.core.request("calls.stop") == .object(["room": .string(room), "reason": "answered_elsewhere", "pushed": .bool(true)])) } // RULE (CALLS FIX 1): a missed call's stop names who called, sealed: the box and this phone's chat key go to // calls.stop, so a phone that never heard the ring writes its Missed row. @Test func aMissedStopHandsTheCallerToTheCore() async { let rig = Rig() let handler = VoIPPushHandler(reporter: rig.system, center: rig.center, chatKey: { "invented-chat-key" }) handler.handle(payload: ["room": room, "stop": "missed", "box": "c1:invented", "at": 1_790_000_000_000 as Int64, "ended": 1_790_000_045_000 as Int64]) {} await rig.center.settle() // With the ring's own times (CALLS FIX 2): the row says when it rang, not when the push came. #expect(rig.core.request("calls.stop") == .object([ "room": .string(room), "reason": "missed", "ring": "c1:invented", "privateKey": "invented-chat-key", "at": .number(1_790_000_000_000), "ended": .number(1_790_000_045_000), "pushed": .bool(true), ])) } // RULE: a push this build cannot read is still reported, and ended at once as failed; the core is not asked. @Test func anUnreadablePushIsReportedAndEnded() async { let rig = Rig() let handler = VoIPPushHandler(reporter: rig.system, center: rig.center) handler.handle(payload: ["nothing": "here"]) {} #expect(rig.system.rings.count == 1) #expect(rig.system.endedReasons == [1]) await rig.center.settle() #expect(rig.core.methods.isEmpty) } // RULE: the ring's UUID is the room's 32 hex, as the core makes it (uuid_of_room); any other room is none. @Test func theRingUUIDIsTheRoom() { #expect(RingUUID.of(room: room)?.uuidString == "01234567-89AB-CDEF-0123-456789ABCDEF") #expect(RingUUID.of(room: "not-a-room") == nil) } } final class Flag: @unchecked Sendable { private let lock = NSLock() private var value = false func set() { lock.withLock { value = true } } var isSet: Bool { lock.withLock { value } } }