@mentra/bluetooth-sdk 3.3.0 → 3.4.0-beta.1237
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MIGRATION.md +61 -0
- package/README.md +80 -11
- package/android/build.gradle +60 -51
- package/android/gradle.properties +3 -0
- package/android/settings.gradle +47 -3
- package/android/src/main/java/com/mentra/bluetoothsdk/BesCrashReportWaiters.kt +68 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkDefaults.kt +5 -2
- package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkModule.kt +85 -6
- package/android/src/main/java/com/mentra/bluetoothsdk/Bridge.kt +35 -1
- package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +154 -110
- package/android/src/main/java/com/mentra/bluetoothsdk/DeviceStore.kt +21 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedChangelogCatalog.kt +1 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +6 -6
- package/android/src/main/java/com/mentra/bluetoothsdk/MentraBluetoothSdk.kt +27 -2
- package/android/src/main/java/com/mentra/bluetoothsdk/connection/ConnectionModels.kt +5 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/events/BluetoothEvents.kt +16 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/GlassesAudioSetupPolicy.kt +64 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLive.kt +925 -155
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicy.kt +227 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingAdvertisement.kt +19 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingReadinessPolicy.kt +94 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateMachine.kt +411 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateStore.kt +124 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/Nimo.kt +54 -59
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinator.kt +5 -32
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasEncoder.kt +8 -1
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSession.kt +3 -10
- package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/SGCManager.kt +6 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/status/DeviceStatus.kt +50 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueue.kt +92 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHost.kt +169 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/STTTools.kt +74 -115
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SherpaOnnxTranscriber.kt +348 -357
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetector.kt +211 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngine.kt +137 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/SttTextFormatter.kt +52 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/WakeWordDetector.kt +235 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/AvifExifStripper.java +2 -2
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/BlePhotoUploadService.java +3 -3
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNaming.java +11 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/AudioRouteWatcher.kt +120 -0
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmStreamPlayer.kt +181 -16
- package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlayback.kt +177 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/BesCrashReportWaitersTest.kt +112 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/DeviceManagerOwnerLossTest.kt +146 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/GlassesStatusAudioSetupTest.kt +51 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/GlassesStatusClassicAudioTest.kt +2 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/WifiAdbPeerIntentTest.kt +19 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/GlassesAudioSetupPolicyTest.kt +65 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveAudioSetupReadinessTest.kt +231 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveGlassesReadyBurstTest.kt +5 -1
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveIncidentDispatchTest.kt +183 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicyTest.kt +368 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingAdvertisementParserTest.kt +20 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingReadinessPolicyTest.kt +136 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateMachineTest.kt +346 -0
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinatorTest.kt +1 -56
- package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSessionTest.kt +0 -22
- package/android/src/test/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNamingTest.kt +23 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueueTest.kt +81 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHostTest.kt +216 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/STTToolsExtractionTest.kt +108 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetectorTest.kt +126 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngineTest.kt +245 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SttTextFormatterTest.kt +67 -0
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlaybackTest.kt +76 -0
- package/build/BluetoothSdk.types.d.ts +201 -1
- package/build/BluetoothSdk.types.d.ts.map +1 -1
- package/build/BluetoothSdk.types.js.map +1 -1
- package/build/_private/BluetoothSdkModule.d.ts +43 -5
- package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
- package/build/_private/BluetoothSdkModule.js +2 -0
- package/build/_private/BluetoothSdkModule.js.map +1 -1
- package/build/generated/changelogCatalog.d.ts +3 -0
- package/build/generated/changelogCatalog.d.ts.map +1 -1
- package/build/generated/changelogCatalog.js +4 -0
- package/build/generated/changelogCatalog.js.map +1 -1
- package/build/generated/releaseMetadata.js +6 -6
- package/build/generated/releaseMetadata.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +9 -0
- package/build/index.js.map +1 -1
- package/ios/BluetoothSdkModule.swift +76 -12
- package/ios/MentraBluetoothSDK.podspec +4 -0
- package/ios/Packages/SherpaOnnx/SherpaOnnx.swift +23 -5
- package/ios/Resources/SileroVad/silero_vad.onnx +0 -0
- package/ios/Source/BesCrashReportWaiters.swift +64 -0
- package/ios/Source/BluetoothSdkDefaults.swift +2 -2
- package/ios/Source/Bridge.swift +11 -3
- package/ios/Source/DeviceManager.swift +131 -18
- package/ios/Source/DeviceStore.swift +27 -0
- package/ios/Source/GeneratedChangelogCatalog.swift +1 -0
- package/ios/Source/GeneratedReleaseMetadata.swift +6 -6
- package/ios/Source/MentraBluetoothSDK.swift +40 -5
- package/ios/Source/audio/PcmStreamPlayer.swift +290 -57
- package/ios/Source/audio/PcmUrlPlayback.swift +231 -0
- package/ios/Source/audio/SpeechActivity.swift +277 -0
- package/ios/Source/errors/BluetoothSdkError.swift +5 -1
- package/ios/Source/events/BluetoothEvents.swift +41 -0
- package/ios/Source/internal/BleTraceLogger.swift +9 -6
- package/ios/Source/internal/BluetoothAvailability.swift +15 -5
- package/ios/Source/sgcs/GlassesAudioSetupPolicy.swift +186 -0
- package/ios/Source/sgcs/MentraLive.swift +1270 -189
- package/ios/Source/sgcs/MentraLivePairingAdvertisement.swift +260 -1
- package/ios/Source/sgcs/MentraLivePairingStateMachine.swift +385 -0
- package/ios/Source/sgcs/MentraLivePairingStateStore.swift +121 -0
- package/ios/Source/sgcs/Nimo.swift +10 -3
- package/ios/Source/sgcs/NimoCanvasCoordinator.swift +39 -9
- package/ios/Source/sgcs/SGCManager.swift +13 -0
- package/ios/Source/sgcs/Simulated.swift +2 -0
- package/ios/Source/status/DeviceStatus.swift +35 -0
- package/ios/Source/stt/STTTools.swift +13 -31
- package/ios/Source/stt/SherpaOnnxTranscriber.swift +204 -162
- package/ios/Source/stt/SileroSpeechActivity.swift +87 -0
- package/ios/Source/stt/WakeWordDetector.swift +145 -0
- package/ios/Source/types/DeviceModels.swift +8 -1
- package/ios/Source/utils/AudioSessionMonitor.swift +158 -5
- package/ios/Source/utils/BoundedWait.swift +44 -0
- package/ios/Source/utils/DemandDrivenAudioWorker.swift +79 -0
- package/ios/Source/utils/SttModelTraits.swift +62 -0
- package/ios/Tests/BesCrashReportTests.swift +142 -0
- package/ios/Tests/BluetoothAudioRouteTests.swift +82 -0
- package/ios/Tests/DemandDrivenAudioWorkerTests.swift +171 -0
- package/ios/Tests/DeviceManagerReconnectTests.swift +4 -2
- package/ios/Tests/GlassesAudioSetupPolicyTests.swift +197 -0
- package/ios/Tests/IncidentLogDispatchTests.swift +158 -0
- package/ios/Tests/MentraLiveAudioSetupTests.swift +209 -0
- package/ios/Tests/MentraLiveConnectionOptionsTests.swift +55 -0
- package/ios/Tests/MentraLivePairingAdvertisementTests.swift +327 -0
- package/ios/Tests/MentraLivePairingStateMachineTests.swift +402 -0
- package/ios/Tests/MentraLiveSessionReadinessTests.swift +368 -26
- package/ios/Tests/NimoCanvasTransportTests.swift +58 -0
- package/ios/Tests/PcmStreamTests.swift +299 -0
- package/ios/Tests/SpeechActivityTests.swift +238 -0
- package/ios/Tests/SttModelTraitsTests.swift +62 -0
- package/ios/Tests/WifiAdbDispatchTests.swift +118 -0
- package/package.json +3 -2
- package/plugin/build/withAndroid.js +9 -11
- package/plugin/build/withKotlinVersion.d.ts +2 -0
- package/plugin/build/withKotlinVersion.js +46 -0
- package/src/BluetoothSdk.types.ts +201 -1
- package/src/_private/BluetoothSdkModule.ts +47 -5
- package/src/generated/changelogCatalog.ts +4 -0
- package/src/generated/releaseMetadata.ts +6 -6
- package/src/index.ts +22 -0
- package/android/silero/LICENSE.md +0 -17
- package/android/silero/build.gradle +0 -50
- package/android/silero/proguard-rules.pro +0 -24
- package/android/silero/src/androidTest/assets/hello.wav +0 -0
- package/android/silero/src/androidTest/java/com/konovalov/vad/silero/VadSileroTest.kt +0 -79
- package/android/silero/src/main/java/com/konovalov/vad/silero/Vad.kt +0 -160
- package/android/silero/src/main/java/com/konovalov/vad/silero/VadSilero.kt +0 -449
- package/android/silero/src/main/java/com/konovalov/vad/silero/config/FrameSize.kt +0 -15
- package/android/silero/src/main/java/com/konovalov/vad/silero/config/Mode.kt +0 -14
- package/android/silero/src/main/java/com/konovalov/vad/silero/config/SampleRate.kt +0 -12
- package/android/silero/src/main/java/com/konovalov/vad/silero/utils/AudioUtils.kt +0 -47
- package/android/silero/src/main/java/com/konovalov/vad/silero/utils/TensorMap.kt +0 -41
- package/android/silero/src/test/java/com/konovalov/vad/silero/utils/AudioUtilsTest.kt +0 -44
- package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -368
- package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocol.kt +0 -156
- package/android/src/main/java/com/mentra/bluetoothsdk/stt/VadGateSpeechPolicy.kt +0 -229
- package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -20
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnosticsTest.kt +0 -287
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocolTest.kt +0 -127
- package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoLifecycleTest.kt +0 -288
- /package/android/{silero/src/main/assets/LICENSE → src/main/assets/vad/LICENSE.silero_vad} +0 -0
- /package/android/{silero/src/main/assets → src/main/assets/vad}/silero_vad.onnx +0 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
@testable import MentraBluetoothSDK
|
|
2
|
+
import XCTest
|
|
3
|
+
|
|
4
|
+
/// Keep native serialization and fragment packing; replace only the radio writes.
|
|
5
|
+
@MainActor
|
|
6
|
+
private final class BesCrashTransport: MentraLive {
|
|
7
|
+
var ready = true
|
|
8
|
+
var writes: [(Data, BleWriteTrace?)] = []
|
|
9
|
+
|
|
10
|
+
override var incidentLogTransportReady: Bool {
|
|
11
|
+
ready
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
override func queueSend(_ data: Data, id _: String, trace: BleWriteTrace?) {
|
|
15
|
+
writes.append((data, trace))
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@MainActor
|
|
20
|
+
final class BesCrashReportTests: XCTestCase {
|
|
21
|
+
private let keyA = "ML395018B-dump-0000002a-1f2e3d4c"
|
|
22
|
+
private let keyB = "ML395018B-reset-9f3a0c1b2d4e5f60"
|
|
23
|
+
|
|
24
|
+
private func ack(_ key: String, _ reportId: String?, ok: Bool, error: String? = nil) -> [String: Any] {
|
|
25
|
+
var values: [String: Any] = ["type": "bes_crash_reported_ack", "incidentKey": key, "ok": ok]
|
|
26
|
+
if let reportId { values["reportId"] = reportId }
|
|
27
|
+
if let error { values["error"] = error }
|
|
28
|
+
return values
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private func expectCode(_ code: String, _ body: () async throws -> Void) async -> BluetoothSdkError? {
|
|
32
|
+
do {
|
|
33
|
+
try await body()
|
|
34
|
+
XCTFail("expected \(code)")
|
|
35
|
+
return nil
|
|
36
|
+
} catch {
|
|
37
|
+
let sdkError = error as? BluetoothSdkError
|
|
38
|
+
XCTAssertEqual(sdkError?.code, code)
|
|
39
|
+
return sdkError
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
func testAckDeliveredDuringSendResolves() async throws {
|
|
44
|
+
let waiters = BesCrashReportWaiters()
|
|
45
|
+
try await waiters.report(incidentKey: keyA, reportId: "rep_1", timeoutMs: 1000) {
|
|
46
|
+
XCTAssertEqual(waiters.onAck(ack(keyA, "rep_1", ok: true)), 1)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
func testConcurrentIncidentsSettleOnlyTheirOwnCall() async throws {
|
|
51
|
+
let waiters = BesCrashReportWaiters()
|
|
52
|
+
let b = Task { @MainActor in
|
|
53
|
+
try await waiters.report(incidentKey: keyB, reportId: "rep_b", timeoutMs: 300) {}
|
|
54
|
+
}
|
|
55
|
+
try await waiters.report(incidentKey: keyA, reportId: "rep_a", timeoutMs: 1000) {
|
|
56
|
+
XCTAssertEqual(waiters.onAck(ack(keyA, "rep_a", ok: true)), 1)
|
|
57
|
+
}
|
|
58
|
+
_ = await expectCode("request_timeout") { try await b.value }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
func testStaleReportIdIsIgnored() async {
|
|
62
|
+
let waiters = BesCrashReportWaiters()
|
|
63
|
+
_ = await expectCode("request_timeout") {
|
|
64
|
+
try await waiters.report(incidentKey: keyA, reportId: "rep_new", timeoutMs: 200) {
|
|
65
|
+
XCTAssertEqual(waiters.onAck(ack(keyA, "rep_old", ok: true)), 0)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
func testOkFalseRejectsWithGlassesError() async {
|
|
71
|
+
let waiters = BesCrashReportWaiters()
|
|
72
|
+
let error = await expectCode("bes_crash_report_rejected") {
|
|
73
|
+
try await waiters.report(incidentKey: keyA, reportId: "rep_1", timeoutMs: 1000) {
|
|
74
|
+
XCTAssertEqual(waiters.onAck(ack(keyA, "rep_1", ok: false, error: "unknown_incident")), 1)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
XCTAssertTrue(error?.message.contains("unknown_incident") == true)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
func testOkTrueWithoutReportIdDoesNotResolve() async {
|
|
81
|
+
let waiters = BesCrashReportWaiters()
|
|
82
|
+
_ = await expectCode("request_timeout") {
|
|
83
|
+
try await waiters.report(incidentKey: keyA, reportId: "rep_1", timeoutMs: 200) {
|
|
84
|
+
XCTAssertEqual(waiters.onAck(ack(keyA, nil, ok: true)), 0)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
func testDisconnectRejectsPendingCalls() async {
|
|
90
|
+
let waiters = BesCrashReportWaiters()
|
|
91
|
+
_ = await expectCode("glasses_not_connected") {
|
|
92
|
+
try await waiters.report(incidentKey: keyA, reportId: "rep_1", timeoutMs: 5000) {
|
|
93
|
+
waiters.rejectAll(BluetoothSdkError(code: "glasses_not_connected", message: "disconnected"))
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
func testSendFailureLeavesNoWaiter() async {
|
|
99
|
+
let waiters = BesCrashReportWaiters()
|
|
100
|
+
_ = await expectCode("glasses_not_connected") {
|
|
101
|
+
try await waiters.report(incidentKey: keyA, reportId: "rep_1", timeoutMs: 1000) {
|
|
102
|
+
throw BluetoothSdkError(code: "glasses_not_connected", message: "no link")
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
XCTAssertEqual(waiters.onAck(ack(keyA, "rep_1", ok: true)), 0)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
func testCommandIsQueuedWithWakeUp() throws {
|
|
109
|
+
let live = BesCrashTransport()
|
|
110
|
+
try live.sendBesCrashReported(incidentKey: keyA, reportId: "rep_01M4HBVT88DFSN8TYNZ1WTV4PB")
|
|
111
|
+
XCTAssertEqual(live.writes.compactMap { $0.1?.commandType }, ["bes_crash_reported"])
|
|
112
|
+
XCTAssertTrue(live.writes.allSatisfy { $0.1?.wakeup == true })
|
|
113
|
+
let wire = live.writes.map { String(decoding: $0.0, as: UTF8.self) }.joined()
|
|
114
|
+
XCTAssertTrue(wire.contains(keyA))
|
|
115
|
+
XCTAssertTrue(wire.contains("rep_01M4HBVT88DFSN8TYNZ1WTV4PB"))
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
func testCommandRejectsWithoutLink() {
|
|
119
|
+
let live = BesCrashTransport()
|
|
120
|
+
live.ready = false
|
|
121
|
+
XCTAssertThrowsError(try live.sendBesCrashReported(incidentKey: keyA, reportId: "rep_1")) { error in
|
|
122
|
+
XCTAssertEqual((error as? BluetoothSdkError)?.code, "glasses_not_connected")
|
|
123
|
+
}
|
|
124
|
+
XCTAssertTrue(live.writes.isEmpty)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
func testDetectedEventReportsNullBeforeAReport() {
|
|
128
|
+
let event = BesCrashDetectedEvent(values: ["incidentKey": keyA, "reportId": NSNull(), "summary": ["traceGaps": 0]])
|
|
129
|
+
XCTAssertNil(event.reportId)
|
|
130
|
+
XCTAssertTrue(event.values["reportId"] is NSNull)
|
|
131
|
+
XCTAssertEqual(event.summary["traceGaps"] as? Int, 0)
|
|
132
|
+
XCTAssertEqual(BesCrashDetectedEvent(values: ["reportId": "rep_1"]).reportId, "rep_1")
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
func testRelayFileNamesAreRecognized() {
|
|
136
|
+
XCTAssertTrue(MentraLive.isIncidentRelayFileName("Brep_01m4hbvt88d"))
|
|
137
|
+
XCTAssertTrue(MentraLive.isIncidentRelayFileName("Lrep_01m4hbvt88d"))
|
|
138
|
+
for name in ["I000123456", "I000123456.jpg", "ble_1234567890", "Brep_01m4hbvt88d.json", "Xrep_01m4hbvt88d", ""] {
|
|
139
|
+
XCTAssertFalse(MentraLive.isIncidentRelayFileName(name), name)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -60,4 +60,86 @@ final class BluetoothAudioRouteTests: XCTestCase {
|
|
|
60
60
|
))
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
|
+
|
|
64
|
+
// MARK: - Profile observation
|
|
65
|
+
|
|
66
|
+
private func profiles(
|
|
67
|
+
outputs: [(name: String, portType: String)] = [],
|
|
68
|
+
inputs: [(name: String, portType: String)]? = [],
|
|
69
|
+
isIOSAppOnMac: Bool = false
|
|
70
|
+
) -> BluetoothAudioRoute.ProfileObservation {
|
|
71
|
+
BluetoothAudioRoute.observeProfiles(
|
|
72
|
+
outputs: outputs, inputs: inputs, target: "03BE", isIOSAppOnMac: isIOSAppOnMac
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
func testActiveA2dpOutputIsConnectedAndRouted() {
|
|
77
|
+
let result = profiles(outputs: [(name: "Mentra_Live_03BE", portType: "BluetoothA2DPOutput")], inputs: nil)
|
|
78
|
+
XCTAssertEqual(result, .init(profilesConnected: true, audioRouted: true))
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
func testHfpInputOnlyIsConnectedButNotRouted() {
|
|
82
|
+
let result = profiles(
|
|
83
|
+
outputs: [(name: "Speaker", portType: "Speaker")],
|
|
84
|
+
inputs: [(name: "Mentra_Live_03BE", portType: "BluetoothHFP")]
|
|
85
|
+
)
|
|
86
|
+
XCTAssertEqual(result, .init(profilesConnected: true, audioRouted: false))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
func testNoMatchIsNeitherConnectedNorRouted() {
|
|
90
|
+
let result = profiles(
|
|
91
|
+
outputs: [(name: "Speaker", portType: "Speaker")],
|
|
92
|
+
inputs: [(name: "iPhone Microphone", portType: "MicrophoneBuiltIn")]
|
|
93
|
+
)
|
|
94
|
+
XCTAssertEqual(result, .init(profilesConnected: false, audioRouted: false))
|
|
95
|
+
XCTAssertEqual(profiles(), .init(profilesConnected: false, audioRouted: false))
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
func testNilInputsDoNotHideActiveOutputAndAreNotAMatch() {
|
|
99
|
+
XCTAssertEqual(
|
|
100
|
+
profiles(outputs: [(name: "Speaker", portType: "Speaker")], inputs: nil),
|
|
101
|
+
.init(profilesConnected: false, audioRouted: false)
|
|
102
|
+
)
|
|
103
|
+
XCTAssertEqual(
|
|
104
|
+
profiles(outputs: [(name: "Mentra_Live_03BE", portType: "BluetoothHFP")], inputs: nil),
|
|
105
|
+
.init(profilesConnected: true, audioRouted: true)
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
func testOtherDevicesDoNotMatch() {
|
|
110
|
+
let result = profiles(
|
|
111
|
+
outputs: [(name: "Mentra_Live_023B", portType: "BluetoothA2DPOutput")],
|
|
112
|
+
inputs: [(name: "Mentra_Live_023B", portType: "BluetoothHFP")]
|
|
113
|
+
)
|
|
114
|
+
XCTAssertEqual(result, .init(profilesConnected: false, audioRouted: false))
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
func testIdentityAndTransportBothRequiredForInputsAndOutputs() {
|
|
118
|
+
let result = profiles(
|
|
119
|
+
outputs: [(name: "Mentra_Live_03BE", portType: "USBAudio")],
|
|
120
|
+
inputs: [(name: "Mentra_Live_03BE", portType: "BluetoothA2DPOutput")]
|
|
121
|
+
)
|
|
122
|
+
XCTAssertEqual(result, .init(profilesConnected: false, audioRouted: false))
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
func testIOSOnMacBluetoothPortIsRoutedOnlyOnMac() {
|
|
126
|
+
let output = [(name: "Mentra_Live_03BE", portType: "Bluetooth")]
|
|
127
|
+
XCTAssertEqual(
|
|
128
|
+
profiles(outputs: output, isIOSAppOnMac: true),
|
|
129
|
+
.init(profilesConnected: true, audioRouted: true)
|
|
130
|
+
)
|
|
131
|
+
XCTAssertEqual(
|
|
132
|
+
profiles(outputs: output, isIOSAppOnMac: false),
|
|
133
|
+
.init(profilesConnected: false, audioRouted: false)
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
func testEmptyTargetNeverMatches() {
|
|
138
|
+
let result = BluetoothAudioRoute.observeProfiles(
|
|
139
|
+
outputs: [(name: "Mentra_Live_03BE", portType: "BluetoothA2DPOutput")],
|
|
140
|
+
inputs: [(name: "Mentra_Live_03BE", portType: "BluetoothHFP")],
|
|
141
|
+
target: "", isIOSAppOnMac: false
|
|
142
|
+
)
|
|
143
|
+
XCTAssertEqual(result, .init(profilesConnected: false, audioRouted: false))
|
|
144
|
+
}
|
|
63
145
|
}
|
|
@@ -154,4 +154,175 @@ final class DemandDrivenAudioWorkerTests: XCTestCase {
|
|
|
154
154
|
worker.setActive(false)
|
|
155
155
|
queue.sync {}
|
|
156
156
|
}
|
|
157
|
+
|
|
158
|
+
func testControlOperationDoesNotEnableMicrophoneDemand() async {
|
|
159
|
+
let worker = DemandDrivenAudioWorker<Processor>(makeProcessor: { Processor() }, process: { _, _, _ in })
|
|
160
|
+
let prepared = await worker.perform { true }
|
|
161
|
+
XCTAssertTrue(prepared)
|
|
162
|
+
XCTAssertFalse(worker.snapshot().active)
|
|
163
|
+
XCTAssertEqual(worker.snapshot().processedBuffers, 0)
|
|
164
|
+
let ran = await worker.withCurrentProcessor { _, _ in true }
|
|
165
|
+
XCTAssertNil(ran)
|
|
166
|
+
XCTAssertFalse(worker.snapshot().initialized)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
func testCurrentProcessorOperationRunsAfterQueuedAudio() async {
|
|
170
|
+
var events = [String]()
|
|
171
|
+
let worker = DemandDrivenAudioWorker<Processor>(makeProcessor: { Processor() }, process: { _, _, _ in
|
|
172
|
+
events.append("audio")
|
|
173
|
+
})
|
|
174
|
+
worker.setActive(true)
|
|
175
|
+
worker.accept(Data([1, 0]))
|
|
176
|
+
let finished = await worker.withCurrentProcessor { _, token -> Bool in
|
|
177
|
+
events.append("flush")
|
|
178
|
+
return worker.isCurrent(token)
|
|
179
|
+
}
|
|
180
|
+
XCTAssertEqual(finished, true)
|
|
181
|
+
XCTAssertEqual(events, ["audio", "flush"])
|
|
182
|
+
worker.setActive(false)
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
func testDemandEndReleasesProcessorAndNextDemandLoadsAgain() async {
|
|
186
|
+
var creations = 0
|
|
187
|
+
var releases = 0
|
|
188
|
+
let worker = DemandDrivenAudioWorker<Processor>(makeProcessor: {
|
|
189
|
+
creations += 1
|
|
190
|
+
return Processor { releases += 1 }
|
|
191
|
+
}, process: { _, _, _ in })
|
|
192
|
+
let idle = await worker.awaitLoad()
|
|
193
|
+
XCTAssertEqual(idle, .noDemand)
|
|
194
|
+
XCTAssertEqual(creations, 0)
|
|
195
|
+
|
|
196
|
+
worker.setActive(true)
|
|
197
|
+
let loaded = await worker.awaitLoad()
|
|
198
|
+
XCTAssertEqual(loaded, .loaded)
|
|
199
|
+
XCTAssertTrue(worker.snapshot().initialized)
|
|
200
|
+
XCTAssertEqual(creations, 1)
|
|
201
|
+
|
|
202
|
+
worker.setActive(false)
|
|
203
|
+
// Release is serialized on the worker queue, after which no processor is reachable.
|
|
204
|
+
let afterRelease = await worker.withCurrentProcessor { _, _ in true }
|
|
205
|
+
XCTAssertNil(afterRelease)
|
|
206
|
+
XCTAssertEqual(releases, 1)
|
|
207
|
+
XCTAssertFalse(worker.snapshot().initialized)
|
|
208
|
+
let ended = await worker.awaitLoad()
|
|
209
|
+
XCTAssertEqual(ended, .noDemand)
|
|
210
|
+
|
|
211
|
+
worker.setActive(true)
|
|
212
|
+
let reloaded = await worker.awaitLoad()
|
|
213
|
+
XCTAssertEqual(reloaded, .loaded)
|
|
214
|
+
XCTAssertEqual(creations, 2)
|
|
215
|
+
worker.setActive(false)
|
|
216
|
+
_ = await worker.perform { true }
|
|
217
|
+
XCTAssertEqual(releases, 2)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
func testRestartAndAwaitLoadReportsFailureAndSkipsLoadingWithoutDemand() async {
|
|
221
|
+
var attempts = 0
|
|
222
|
+
var succeed = true
|
|
223
|
+
let worker = DemandDrivenAudioWorker<Processor>(makeProcessor: {
|
|
224
|
+
attempts += 1
|
|
225
|
+
return succeed ? Processor() : nil
|
|
226
|
+
}, process: { _, _, _ in })
|
|
227
|
+
let withoutDemand = await worker.restartAndAwaitLoad()
|
|
228
|
+
XCTAssertEqual(withoutDemand, .noDemand)
|
|
229
|
+
_ = await worker.perform { true }
|
|
230
|
+
XCTAssertEqual(attempts, 0)
|
|
231
|
+
|
|
232
|
+
worker.setActive(true)
|
|
233
|
+
let first = await worker.awaitLoad()
|
|
234
|
+
XCTAssertEqual(first, .loaded)
|
|
235
|
+
succeed = false
|
|
236
|
+
let failed = await worker.restartAndAwaitLoad()
|
|
237
|
+
XCTAssertEqual(failed, .failed)
|
|
238
|
+
XCTAssertFalse(worker.snapshot().initialized)
|
|
239
|
+
succeed = true
|
|
240
|
+
let recovered = await worker.restartAndAwaitLoad()
|
|
241
|
+
XCTAssertEqual(recovered, .loaded)
|
|
242
|
+
XCTAssertEqual(attempts, 3)
|
|
243
|
+
worker.setActive(false)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
func testControlOperationRunsWhileAudioArrivesFasterThanInference() async {
|
|
247
|
+
let queue = DispatchQueue(label: "test.speech.slow")
|
|
248
|
+
let worker = DemandDrivenAudioWorker<Processor>(queue: queue, makeProcessor: { Processor() }, process: { _, _, _ in
|
|
249
|
+
Thread.sleep(forTimeInterval: 0.004)
|
|
250
|
+
})
|
|
251
|
+
let loaded = await worker.activateAndAwaitLoad()
|
|
252
|
+
XCTAssertEqual(loaded, .loaded)
|
|
253
|
+
|
|
254
|
+
let feeding = ManagedAtomicFlag()
|
|
255
|
+
let feeder = Thread {
|
|
256
|
+
// One buffer per millisecond against 4 ms of inference each: the queue never empties.
|
|
257
|
+
while !feeding.isSet {
|
|
258
|
+
worker.accept(Data([1]))
|
|
259
|
+
Thread.sleep(forTimeInterval: 0.001)
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
feeder.start()
|
|
263
|
+
let started = Date()
|
|
264
|
+
let ran = await worker.perform { true }
|
|
265
|
+
let waited = Date().timeIntervalSince(started)
|
|
266
|
+
feeding.set()
|
|
267
|
+
XCTAssertTrue(ran)
|
|
268
|
+
XCTAssertLessThan(waited, 1.0)
|
|
269
|
+
worker.setActive(false)
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
func testEnablingAgainRetriesAFailedLoadButNotAWorkingOne() async {
|
|
273
|
+
var attempts = 0
|
|
274
|
+
var succeed = false
|
|
275
|
+
let worker = DemandDrivenAudioWorker<Processor>(makeProcessor: {
|
|
276
|
+
attempts += 1
|
|
277
|
+
return succeed ? Processor() : nil
|
|
278
|
+
}, process: { _, _, _ in })
|
|
279
|
+
let failed = await worker.activateAndAwaitLoad()
|
|
280
|
+
XCTAssertEqual(failed, .failed)
|
|
281
|
+
XCTAssertEqual(attempts, 1)
|
|
282
|
+
|
|
283
|
+
succeed = true
|
|
284
|
+
let recovered = await worker.activateAndAwaitLoad()
|
|
285
|
+
XCTAssertEqual(recovered, .loaded)
|
|
286
|
+
XCTAssertEqual(attempts, 2)
|
|
287
|
+
|
|
288
|
+
let again = await worker.activateAndAwaitLoad()
|
|
289
|
+
XCTAssertEqual(again, .loaded)
|
|
290
|
+
XCTAssertEqual(attempts, 2)
|
|
291
|
+
worker.setActive(false)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
func testAwaitLoadWaitsForASlowLoad() async {
|
|
295
|
+
let loading = DispatchSemaphore(value: 0)
|
|
296
|
+
let release = DispatchSemaphore(value: 0)
|
|
297
|
+
let worker = DemandDrivenAudioWorker<Processor>(makeProcessor: {
|
|
298
|
+
loading.signal()
|
|
299
|
+
release.wait()
|
|
300
|
+
return Processor()
|
|
301
|
+
}, process: { _, _, _ in })
|
|
302
|
+
worker.setActive(true)
|
|
303
|
+
XCTAssertEqual(loading.wait(timeout: .now() + 2), .success)
|
|
304
|
+
XCTAssertFalse(worker.snapshot().initialized)
|
|
305
|
+
DispatchQueue.global().asyncAfter(deadline: .now() + 0.05) { release.signal() }
|
|
306
|
+
let state = await worker.awaitLoad()
|
|
307
|
+
XCTAssertEqual(state, .loaded)
|
|
308
|
+
XCTAssertTrue(worker.snapshot().initialized)
|
|
309
|
+
worker.setActive(false)
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/// A tiny thread-safe stop flag for the feeder thread.
|
|
314
|
+
private final class ManagedAtomicFlag: @unchecked Sendable {
|
|
315
|
+
private let lock = NSLock()
|
|
316
|
+
private var value = false
|
|
317
|
+
var isSet: Bool {
|
|
318
|
+
lock.lock()
|
|
319
|
+
defer { lock.unlock() }
|
|
320
|
+
return value
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
func set() {
|
|
324
|
+
lock.lock()
|
|
325
|
+
value = true
|
|
326
|
+
lock.unlock()
|
|
327
|
+
}
|
|
157
328
|
}
|
|
@@ -280,6 +280,8 @@ final class DeviceManagerReconnectTests: XCTestCase {
|
|
|
280
280
|
|
|
281
281
|
@MainActor
|
|
282
282
|
private final class ReconnectRecordingDevice: SGCManager {
|
|
283
|
+
var onCleanup: (() -> Void)?
|
|
284
|
+
var onDisconnect: (() -> Void)?
|
|
283
285
|
var type = "Reconnect test glasses"
|
|
284
286
|
let hasMic = true
|
|
285
287
|
let showConnectionConfirmation = false
|
|
@@ -333,7 +335,7 @@ private final class ReconnectRecordingDevice: SGCManager {
|
|
|
333
335
|
func sendShutdown() {}
|
|
334
336
|
func sendReboot() {}
|
|
335
337
|
func sendRgbLedControl(requestId _: String, packageName _: String?, action _: String, color _: String?, onDurationMs _: Int, offDurationMs _: Int, count _: Int) {}
|
|
336
|
-
func disconnect() {}
|
|
338
|
+
func disconnect() { onDisconnect?() }
|
|
337
339
|
func forget() {}
|
|
338
340
|
func findCompatibleDevices() {}
|
|
339
341
|
func stopScan() {}
|
|
@@ -342,7 +344,7 @@ private final class ReconnectRecordingDevice: SGCManager {
|
|
|
342
344
|
nil
|
|
343
345
|
}
|
|
344
346
|
|
|
345
|
-
func cleanup() {}
|
|
347
|
+
func cleanup() { onCleanup?() }
|
|
346
348
|
func ping() {}
|
|
347
349
|
func dbg1() {}
|
|
348
350
|
func dbg2() {}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
@testable import MentraBluetoothSDK
|
|
2
|
+
import XCTest
|
|
3
|
+
|
|
4
|
+
/// Table tests for the iOS audio-setup rules (Android reports `not_applicable` while ready).
|
|
5
|
+
final class GlassesAudioSetupPolicyTests: XCTestCase {
|
|
6
|
+
private typealias Policy = GlassesAudioSetupPolicy
|
|
7
|
+
private let t0 = 1_000_000
|
|
8
|
+
|
|
9
|
+
private func inputs(
|
|
10
|
+
ready: Bool = true,
|
|
11
|
+
report: (a2dp: Bool, hfp: Bool)? = nil,
|
|
12
|
+
phone: Bool = false,
|
|
13
|
+
phase: GlassesAudioSetupPhase = .reconnect,
|
|
14
|
+
startedAt: Int? = nil
|
|
15
|
+
) -> GlassesAudioSetupInputs {
|
|
16
|
+
GlassesAudioSetupInputs(
|
|
17
|
+
sessionReady: ready,
|
|
18
|
+
glassesReportsClassicLink: report != nil,
|
|
19
|
+
glassesA2dp: report?.a2dp ?? false,
|
|
20
|
+
glassesHfp: report?.hfp ?? false,
|
|
21
|
+
phoneAudioUp: phone,
|
|
22
|
+
phase: phase,
|
|
23
|
+
phaseStartedAtMs: startedAt ?? t0
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
private func state(_ inputs: GlassesAudioSetupInputs, at offsetMs: Int) -> GlassesAudioSetupState {
|
|
28
|
+
Policy.decide(inputs, nowMs: t0 + offsetMs).state
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
func testConstantsMatchTheContract() {
|
|
32
|
+
XCTAssertEqual(Policy.pairingAutoAudioWaitMs, 8000)
|
|
33
|
+
XCTAssertEqual(Policy.reconnectAudioGraceMs, 10000)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
func testNoSessionIsIdle() {
|
|
37
|
+
XCTAssertEqual(
|
|
38
|
+
Policy.decide(inputs(ready: false, report: (true, true), phone: true), nowMs: t0),
|
|
39
|
+
GlassesAudioSetupDecision(state: .idle, nextDeadlineMs: nil)
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
func testPairingWithAutoAudioWaitsEightSecondsThenNeedsTheUser() {
|
|
44
|
+
let pairing = inputs(phase: .pairing(autoAudio: true))
|
|
45
|
+
XCTAssertEqual(
|
|
46
|
+
Policy.decide(pairing, nowMs: t0),
|
|
47
|
+
GlassesAudioSetupDecision(state: .connecting, nextDeadlineMs: t0 + 8000)
|
|
48
|
+
)
|
|
49
|
+
XCTAssertEqual(state(pairing, at: 7999), .connecting)
|
|
50
|
+
XCTAssertEqual(state(pairing, at: 8000), .needsUserConnectInSettings)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
func testPairingWithoutAutoAudioNeedsTheUserAtOnce() {
|
|
54
|
+
XCTAssertEqual(
|
|
55
|
+
Policy.decide(inputs(phase: .pairing(autoAudio: false)), nowMs: t0),
|
|
56
|
+
GlassesAudioSetupDecision(state: .needsUserConnectInSettings, nextDeadlineMs: nil)
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
func testReconnectWaitsTenSecondsThenNeedsTheUser() {
|
|
61
|
+
let reconnect = inputs(phase: .reconnect)
|
|
62
|
+
XCTAssertEqual(
|
|
63
|
+
Policy.decide(reconnect, nowMs: t0 + 1000),
|
|
64
|
+
GlassesAudioSetupDecision(state: .connecting, nextDeadlineMs: t0 + 10000)
|
|
65
|
+
)
|
|
66
|
+
XCTAssertEqual(state(reconnect, at: 9999), .connecting)
|
|
67
|
+
XCTAssertEqual(state(reconnect, at: 10000), .needsUserConnectInSettings)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
func testEitherAudioSourceIsEnoughInEveryPhase() {
|
|
71
|
+
for phase in [GlassesAudioSetupPhase.pairing(autoAudio: true), .pairing(autoAudio: false), .reconnect] {
|
|
72
|
+
for offset in [0, 30000] {
|
|
73
|
+
XCTAssertEqual(state(inputs(phone: true, phase: phase), at: offset), .connected, "phone \(phase)")
|
|
74
|
+
XCTAssertEqual(state(inputs(report: (true, false), phase: phase), at: offset), .connected, "a2dp \(phase)")
|
|
75
|
+
XCTAssertEqual(state(inputs(report: (false, true), phase: phase), at: offset), .connected, "hfp \(phase)")
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
XCTAssertEqual(Policy.decide(inputs(phone: true), nowMs: t0).nextDeadlineMs, nil)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
func testAGlassesReportOfBothLinksDownNeverShortensTheWait() {
|
|
82
|
+
let down = inputs(report: (false, false), phase: .pairing(autoAudio: true))
|
|
83
|
+
XCTAssertEqual(state(down, at: 1), .connecting)
|
|
84
|
+
XCTAssertEqual(Policy.decide(down, nowMs: t0 + 1).nextDeadlineMs, t0 + 8000)
|
|
85
|
+
XCTAssertEqual(state(inputs(report: (false, false), phase: .reconnect), at: 9000), .connecting)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
func testABothDownReportDoesNotVetoThePhone() {
|
|
89
|
+
XCTAssertEqual(state(inputs(report: (false, false), phone: true), at: 0), .connected)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
func testTheGlassesReportArrivingBeforeThePhoneSignals() {
|
|
93
|
+
var tracker = GlassesAudioSetupTracker()
|
|
94
|
+
tracker.startPhase(.pairing(autoAudio: true), nowMs: t0)
|
|
95
|
+
tracker.classicLink(a2dp: true, hfp: true)
|
|
96
|
+
XCTAssertEqual(tracker.evaluate(sessionReady: true, phoneAudioUp: false, nowMs: t0 + 100).state, .connected)
|
|
97
|
+
// The phone's route catches up later: still connected, nothing to re-arm.
|
|
98
|
+
XCTAssertEqual(
|
|
99
|
+
tracker.evaluate(sessionReady: true, phoneAudioUp: true, nowMs: t0 + 900),
|
|
100
|
+
GlassesAudioSetupDecision(state: .connected, nextDeadlineMs: nil)
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
func testTheGlassesReportArrivingAfterThePhoneSignals() {
|
|
105
|
+
var tracker = GlassesAudioSetupTracker()
|
|
106
|
+
tracker.startPhase(.reconnect, nowMs: t0)
|
|
107
|
+
XCTAssertEqual(tracker.evaluate(sessionReady: true, phoneAudioUp: true, nowMs: t0 + 100).state, .connected)
|
|
108
|
+
// The glasses then report both down (for example A2DP closing while HFP reconnects).
|
|
109
|
+
// The phone still sees audio, so the state stays connected.
|
|
110
|
+
tracker.classicLink(a2dp: false, hfp: false)
|
|
111
|
+
XCTAssertEqual(tracker.evaluate(sessionReady: true, phoneAudioUp: true, nowMs: t0 + 200).state, .connected)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
func testAReportAfterNeedsUserMovesToConnected() {
|
|
115
|
+
var tracker = GlassesAudioSetupTracker()
|
|
116
|
+
tracker.startPhase(.pairing(autoAudio: false), nowMs: t0)
|
|
117
|
+
XCTAssertEqual(tracker.evaluate(sessionReady: true, phoneAudioUp: false, nowMs: t0).state, .needsUserConnectInSettings)
|
|
118
|
+
tracker.classicLink(a2dp: false, hfp: true)
|
|
119
|
+
XCTAssertEqual(tracker.evaluate(sessionReady: true, phoneAudioUp: false, nowMs: t0 + 60000).state, .connected)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
func testLosingAudioAfterConnectedStartsTheReconnectWaitAgain() {
|
|
123
|
+
var tracker = GlassesAudioSetupTracker()
|
|
124
|
+
tracker.startPhase(.pairing(autoAudio: false), nowMs: t0)
|
|
125
|
+
XCTAssertEqual(tracker.evaluate(sessionReady: true, phoneAudioUp: true, nowMs: t0 + 500).state, .connected)
|
|
126
|
+
|
|
127
|
+
let lostAt = t0 + 60000
|
|
128
|
+
XCTAssertEqual(
|
|
129
|
+
tracker.evaluate(sessionReady: true, phoneAudioUp: false, nowMs: lostAt),
|
|
130
|
+
GlassesAudioSetupDecision(state: .connecting, nextDeadlineMs: lostAt + 10000)
|
|
131
|
+
)
|
|
132
|
+
XCTAssertEqual(tracker.phase, .reconnect)
|
|
133
|
+
XCTAssertEqual(tracker.phaseStartedAtMs, lostAt)
|
|
134
|
+
XCTAssertEqual(tracker.evaluate(sessionReady: true, phoneAudioUp: false, nowMs: lostAt + 9999).state, .connecting)
|
|
135
|
+
XCTAssertEqual(
|
|
136
|
+
tracker.evaluate(sessionReady: true, phoneAudioUp: false, nowMs: lostAt + 10000).state,
|
|
137
|
+
.needsUserConnectInSettings
|
|
138
|
+
)
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
func testPairingProtocolGlassesNeverAskTheUserAfterSetup() {
|
|
142
|
+
var tracker = GlassesAudioSetupTracker()
|
|
143
|
+
tracker.startPhase(.reconnect, nowMs: t0)
|
|
144
|
+
tracker.classicLink(a2dp: true, hfp: true)
|
|
145
|
+
XCTAssertEqual(tracker.evaluate(sessionReady: true, phoneAudioUp: false, nowMs: t0).state, .connected)
|
|
146
|
+
tracker.classicLink(a2dp: false, hfp: false)
|
|
147
|
+
XCTAssertEqual(
|
|
148
|
+
tracker.evaluate(sessionReady: true, phoneAudioUp: false, nowMs: t0 + 3_600_000),
|
|
149
|
+
GlassesAudioSetupDecision(state: .connecting, nextDeadlineMs: nil)
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
func testPairingProtocolGlassesStillAskWhenAudioNeverCameUpAfterPairing() {
|
|
154
|
+
let pairing = inputs(report: (false, false), phase: .pairing(autoAudio: true))
|
|
155
|
+
XCTAssertEqual(state(pairing, at: 7999), .connecting)
|
|
156
|
+
XCTAssertEqual(state(pairing, at: 8000), .needsUserConnectInSettings)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
func testASessionDropIsIdleAndForgetsTheGlassesReport() {
|
|
160
|
+
var tracker = GlassesAudioSetupTracker()
|
|
161
|
+
tracker.startPhase(.reconnect, nowMs: t0)
|
|
162
|
+
tracker.classicLink(a2dp: true, hfp: false)
|
|
163
|
+
XCTAssertEqual(tracker.evaluate(sessionReady: true, phoneAudioUp: false, nowMs: t0).state, .connected)
|
|
164
|
+
|
|
165
|
+
tracker.endSession()
|
|
166
|
+
XCTAssertEqual(
|
|
167
|
+
tracker.evaluate(sessionReady: false, phoneAudioUp: false, nowMs: t0 + 1000),
|
|
168
|
+
GlassesAudioSetupDecision(state: .idle, nextDeadlineMs: nil)
|
|
169
|
+
)
|
|
170
|
+
XCTAssertFalse(tracker.glassesReportsClassicLink)
|
|
171
|
+
|
|
172
|
+
// The next session has not reported its link yet.
|
|
173
|
+
tracker.startPhase(.reconnect, nowMs: t0 + 2000)
|
|
174
|
+
XCTAssertEqual(tracker.evaluate(sessionReady: true, phoneAudioUp: false, nowMs: t0 + 2000).state, .connecting)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
func testMapEncodingMatchesTheContract() {
|
|
178
|
+
XCTAssertEqual(GlassesAudioSetupState.idle.toMap() as NSDictionary, ["state": "idle"])
|
|
179
|
+
XCTAssertEqual(GlassesAudioSetupState.notApplicable.toMap() as NSDictionary, ["state": "not_applicable"])
|
|
180
|
+
XCTAssertEqual(GlassesAudioSetupState.connecting.toMap() as NSDictionary, ["state": "connecting"])
|
|
181
|
+
XCTAssertEqual(GlassesAudioSetupState.connected.toMap() as NSDictionary, ["state": "connected"])
|
|
182
|
+
XCTAssertEqual(
|
|
183
|
+
GlassesAudioSetupState.needsUserConnectInSettings.toMap() as NSDictionary,
|
|
184
|
+
["state": "needs_user", "action": "connect_in_settings"]
|
|
185
|
+
)
|
|
186
|
+
for state in [GlassesAudioSetupState.idle, .notApplicable, .connecting, .connected, .needsUserConnectInSettings] {
|
|
187
|
+
XCTAssertEqual(GlassesAudioSetupState(map: state.toMap()), state)
|
|
188
|
+
}
|
|
189
|
+
XCTAssertNil(GlassesAudioSetupState(map: ["state": "unknown"]))
|
|
190
|
+
XCTAssertNil(GlassesAudioSetupState(map: nil))
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
func testModelsWithoutADriverRuleAreNotApplicableWhileReady() {
|
|
194
|
+
XCTAssertEqual(GlassesAudioSetupState.modelDefault(ready: true), .notApplicable)
|
|
195
|
+
XCTAssertEqual(GlassesAudioSetupState.modelDefault(ready: false), .idle)
|
|
196
|
+
}
|
|
197
|
+
}
|