// The gate: what the OS said, what Cotton does about it, and what it says.//// Three crashes and one bad first launch are behind this file, and all four are// the same mistake: the platform object existed before the OS had said yes.// What the tests below pin is the ORDER and the ANSWERS — the mechanism that// makes that mistake hard to make again. The other half, "nobody else may ask",// is a source sweep: `CapabilityGateSweepTests` (scripts/__tests__/swift-sweep-capability-gate.test.ts).//// Nothing here touches a real permission. `ScriptedCapabilityAuthority` IS the// OS, and the mappings are pure functions over the OS's own enums, which is why// a simulator with no microphone can prove all of it.import AVFoundationimportCottonKitimportFoundationimportPhotosimportSpeechimportUserNotificationsimport XCTest@testable importCottonUI// MARK: - Every OS status lands somewherefinalclassCapabilityMappingTests: XCTestCase {}// MARK: - The gate itselffinalclassCapabilityGateTests: XCTestCase {}// MARK: - What a refusal saysfinalclassCapabilityRefusalTests: XCTestCase {}// MARK: - The two callers this was built for@MainActorfinalclassDictationGateTests: XCTestCase {/// **The whole bug, in one assertion.** A blocked microphone ends the/// dictation with a sentence and opens NO session — which means no audio/// session, no engine, no input node and nothing to raise an NSException.func testABlockedMicrophoneOpensNoSession() async {let recogniser = FakeRecogniser() recogniser.permission = false recogniser.refusal = .blocked(.microphone, .denied)let model = DictationModel(recogniser: recogniser)let outcome = await model.start(prefix: "", hints: DictationHints()) XCTAssertEqual(outcome, .dictationDenied) XCTAssertTrue(recogniser.sessions.isEmpty) XCTAssertNotNil(VoiceTake.dictationMessage(.dictationDenied)) }/// **Constructing the real recogniser is FREE, and this test is what says/// so**: it runs on a simulator with no microphone, which is exactly where/// an `AVAudioEngine` built in `init` raised over a 0 Hz input format./// Then it pins the order the owner saw — microphone, then speech.func testTheRealRecogniserIsFreeToBuildAndAsksMicrophoneThenSpeech() asyncthrows {guard #available(iOS 26.0, macOS 26.0, *) else {throw XCTSkip("the recogniser is DictationTranscriber's, iOS 26 / macOS 26") }let os = ScriptedCapabilityAuthority( grants: [.microphone: .authorized, .speech: .authorized] )let recogniser = SystemSpeechRecogniser(capabilities: os) XCTAssertEqual(os.asked, [], "construction asks the OS for nothing")let result = await recogniser.access() XCTAssertEqual(result, .ready) XCTAssertEqual(os.asked, [.microphone, .speech]) }}