// The core's words for CallKit, as CallKit reads them. import CottonKit import Foundation import Testing @testable import CottonCalls #if canImport(CallKit) && os(iOS) import CallKit @Suite struct CallKitMappingTests { // RULE: a person's call has their number; a group's call joined late (no number) is the group, by its name. @Test func aGroupsCallIsHandledByItsName() { let person = CallKitSystem.handle("+15550100001", name: "Asha") #expect(person.type == .phoneNumber && person.value == "+15550100001") let group = CallKitSystem.handle("", name: "Invented Family") #expect(group.type == .generic && group.value == "Invented Family") } // RULE: an ended call's `ios` value is CXCallEndedReason's own raw value (cotton_core SystemEnd::ios): failed 1, remoteEnded 2, unanswered 3, answeredElsewhere 4, declinedElsewhere 5. @Test func theEndReasonsAreCallKits() { #expect(CXCallEndedReason(rawValue: 1) == .failed) #expect(CXCallEndedReason(rawValue: 2) == .remoteEnded) #expect(CXCallEndedReason(rawValue: 3) == .unanswered) #expect(CXCallEndedReason(rawValue: 4) == .answeredElsewhere) #expect(CXCallEndedReason(rawValue: 5) == .declinedElsewhere) } } #endif @MainActor @Suite struct CallKitEffectTests { // RULE: an end with an `ios` reason is reported as ended; one without asks an end action (this person ended it); a connected incoming call is not reported again. @Test func endsAndConnectionsMapAsTheCoreSays() async { let rig = Rig() let one = "01234567-89AB-CDEF-0123-456789ABCDEF" rig.core.answer("calls.end", effects: [ .object(["do": "system_call_connected", "uuid": .string(one), "outgoing": .bool(false)]), .object(["do": "system_call_ended", "uuid": .string(one), "cause": "local", "android": .number(2)]), .object(["do": "system_call_ended", "uuid": .string(one), "cause": "missed", "ios": .number(3), "android": .number(5)]), ]) rig.center.end() await rig.center.settle() #expect(rig.system.said == ["ended", "ended"]) #expect(rig.system.endedReasons == [nil, 3]) } // RULE (CALLS FIX 1): CallKit's reset ends only a call CallKit holds: the call daemon quitting while it held none // (a simulator's calls never are CallKit's) hangs up nothing. #if canImport(CallKit) && os(iOS) @Test func aResetWithNoCallCallKitHoldsEndsNothing() async { let rig = Rig() let callKit = CallKitSystem(icon: nil) callKit.center = rig.center callKit.providerDidReset(CXProvider(configuration: CXProviderConfiguration())) await rig.center.settle() #expect(!rig.core.methods.contains("calls.end")) } #endif // RULE: an effect with no UUID CallKit can read is passed over, never invented. @Test func aBadUUIDIsPassedOver() async { let rig = Rig() rig.core.answer("calls.end", effects: [.object(["do": "system_call_ended", "uuid": "nope", "cause": "local"])]) rig.center.end() await rig.center.settle() #expect(rig.system.said.isEmpty) } }