import CottonValues import Foundation import XCTest @testable import CottonUI @MainActor final class CallDefinitionTests: XCTestCase { private var resources: Bundle? { Bundle(url: SourceSweep.iosSources.appendingPathComponent("CottonUI/Resources")) } /// RULE: A phone handoff accepts a phone-like handle for tel or sms only and rejects email, URL syntax, control characters and unsupported schemes. func testPhoneHandoffDoesNotBecomeAnArbitraryURL() throws { let call = PageAction(label: "Call back", column: "handle", scheme: .tel) XCTAssertEqual(call.url(for: .text("+1 (202) 555-0100"))?.absoluteString, "tel:+12025550100") XCTAssertEqual(PageAction(label: "Message", column: "handle", scheme: .sms).url(for: .text("+12025550100"))?.absoluteString, "sms:+12025550100") for handle in ["a@example.com", "123?body=hello", "123&foo=bar", "123\n456", "javascript:alert(1)", "", "Withheld number"] { XCTAssertNil(call.url(for: .text(handle)), handle) } // An unsupported scheme is unrepresentable: ActionScheme is the core's closed enum. } /// RULE: Provider-selected FaceTime handoffs keep their audio or video destination, accept safe email handles, and phone keypad characters are encoded as path data. func testProviderHandoffsAndDialCharacters() throws { let action = PageAction(label: "Call back", column: "handle", scheme: .tel, schemeColumn: "provider", schemes: ["facetime_video": .facetime, "facetime_audio": .facetimeAudio]) let video = TableRecord(id: "example-video", values: ["provider": .text("facetime_video")]) let audio = TableRecord(id: "example-audio", values: ["provider": .text("facetime_audio")]) XCTAssertEqual(action.url(for: .text("guest@example.invalid"), record: video)?.absoluteString, "facetime:guest@example.invalid") XCTAssertEqual(action.url(for: .text("+12025550100"), record: audio)?.scheme, "facetime-audio") let dial = try XCTUnwrap(action.url(for: .text("*123#"))) XCTAssertEqual(dial.query, nil) XCTAssertEqual(dial.fragment, nil) XCTAssertEqual(dial.path, "*123#") } }