Files
FamilyMealPlanner/MealMoodTests/SpeechDictationServiceTests.swift
T
alexandrev-tibco 896c78d260 fix: crash al dictar por closures de callbacks aislados al main actor
Crashlytics: dispatch_assert_queue_fail en com.apple.root.default-qos, dentro de
closure #1 in SpeechDictationService.start, llamado desde __TCCAccessRequest.

El proyecto compila en Swift 6 (activo desde febrero, antes del dictado). Los
callbacks de APIs ObjC pre-concurrency escritos dentro de este tipo @MainActor se
infieren aislados al main actor, y Swift 6 inserta una comprobacion de aislamiento
al entrar. TCC invoca el handler en cola de fondo, la comprobacion falla y mata el
proceso. El Task { @MainActor } de dentro no ayuda: el crash es antes de llegar.

Cuatro sitios con el mismo patron, tres enmascarados detras del primero porque
nadie pasaba de los permisos:

- requestAuthorization y requestRecordPermission -> wrappers nonisolated con
  continuation, el handler deja de estar aislado
- recognitionTask -> closure @Sendable explicito; solo cruzan valores Sendable
  porque SFSpeechRecognitionResult no lo es
- installTap -> captura el request local en vez de ir por self, que el hilo de
  audio en tiempo real no debe tocar estado del main actor

requestAuthorization llama al handler aunque el permiso ya este concedido, asi
que afectaba tambien a usuarios recurrentes: el dictado probablemente no ha
funcionado nunca desde la 2.0.

Los tests que acompanan NO reproducen el crash: el simulador entrega el callback
sincrono en el hilo principal y el codigo previo pasa igual. Comprobado
revirtiendo el fix. Queda pendiente validarlo en dispositivo real.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Ks8uUcMA9mjypVK7F2Pkt
2026-08-19 21:28:29 +02:00

42 lines
1.7 KiB
Swift

import XCTest
@testable import MealMood
/// Smoke cover for the dictation entry point.
///
/// **This does not reproduce the crash it was written for.** The production
/// crash (`dispatch_assert_queue` inside the `requestAuthorization` handler)
/// needs TCC to deliver the callback asynchronously on a background queue, which
/// is what happens on device when the permission prompt is answered. The
/// simulator delivers it synchronously on the calling thread instead, so the
/// isolation check passes and the pre-fix code runs clean here verified by
/// reverting the fix and watching these tests still pass.
///
/// What it does cover: `start` settles instead of hanging, and does not report
/// recording when permission is unavailable. Verifying the crash itself needs a
/// real device with the speech and microphone permissions reset.
@MainActor
final class SpeechDictationServiceTests: XCTestCase {
func testStartSettlesWithoutRecordingWhenPermissionIsUnavailable() async throws {
let service = SpeechDictationService()
XCTAssertEqual(service.state, .idle)
service.start(localeIdentifier: "en-US")
let deadline = Date().addingTimeInterval(10)
while service.state == .idle && Date() < deadline {
try await Task.sleep(nanoseconds: 100_000_000)
}
XCTAssertNotEqual(service.state, .recording,
"No permission is granted in this environment, so it must not record")
}
func testStartTwiceDoesNotBlowUp() {
let service = SpeechDictationService()
service.start(localeIdentifier: "en-US")
service.start(localeIdentifier: "en-US")
XCTAssertNotEqual(service.state, .recording)
}
}