@mentra/bluetooth-sdk 3.3.0 → 3.4.0-beta.1241

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.
Files changed (168) hide show
  1. package/MIGRATION.md +61 -0
  2. package/README.md +80 -11
  3. package/android/build.gradle +60 -51
  4. package/android/gradle.properties +3 -0
  5. package/android/settings.gradle +47 -3
  6. package/android/src/main/java/com/mentra/bluetoothsdk/BesCrashReportWaiters.kt +68 -0
  7. package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkDefaults.kt +5 -2
  8. package/android/src/main/java/com/mentra/bluetoothsdk/BluetoothSdkModule.kt +85 -6
  9. package/android/src/main/java/com/mentra/bluetoothsdk/Bridge.kt +35 -1
  10. package/android/src/main/java/com/mentra/bluetoothsdk/DeviceManager.kt +154 -110
  11. package/android/src/main/java/com/mentra/bluetoothsdk/DeviceStore.kt +21 -0
  12. package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedChangelogCatalog.kt +1 -0
  13. package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +6 -6
  14. package/android/src/main/java/com/mentra/bluetoothsdk/MentraBluetoothSdk.kt +27 -2
  15. package/android/src/main/java/com/mentra/bluetoothsdk/connection/ConnectionModels.kt +5 -0
  16. package/android/src/main/java/com/mentra/bluetoothsdk/events/BluetoothEvents.kt +16 -0
  17. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/GlassesAudioSetupPolicy.kt +64 -0
  18. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLive.kt +925 -155
  19. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicy.kt +227 -0
  20. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingAdvertisement.kt +19 -0
  21. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingReadinessPolicy.kt +94 -0
  22. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateMachine.kt +411 -0
  23. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateStore.kt +124 -0
  24. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/Nimo.kt +54 -59
  25. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinator.kt +5 -32
  26. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasEncoder.kt +8 -1
  27. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSession.kt +3 -10
  28. package/android/src/main/java/com/mentra/bluetoothsdk/sgcs/SGCManager.kt +6 -0
  29. package/android/src/main/java/com/mentra/bluetoothsdk/status/DeviceStatus.kt +50 -0
  30. package/android/src/main/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueue.kt +92 -0
  31. package/android/src/main/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHost.kt +169 -0
  32. package/android/src/main/java/com/mentra/bluetoothsdk/stt/STTTools.kt +74 -115
  33. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SherpaOnnxTranscriber.kt +348 -357
  34. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetector.kt +211 -0
  35. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngine.kt +137 -0
  36. package/android/src/main/java/com/mentra/bluetoothsdk/stt/SttTextFormatter.kt +52 -0
  37. package/android/src/main/java/com/mentra/bluetoothsdk/stt/WakeWordDetector.kt +235 -0
  38. package/android/src/main/java/com/mentra/bluetoothsdk/utils/AvifExifStripper.java +2 -2
  39. package/android/src/main/java/com/mentra/bluetoothsdk/utils/BlePhotoUploadService.java +3 -3
  40. package/android/src/main/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNaming.java +11 -0
  41. package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/AudioRouteWatcher.kt +120 -0
  42. package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmStreamPlayer.kt +181 -16
  43. package/android/src/main/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlayback.kt +177 -0
  44. package/android/src/test/java/com/mentra/bluetoothsdk/BesCrashReportWaitersTest.kt +112 -0
  45. package/android/src/test/java/com/mentra/bluetoothsdk/DeviceManagerOwnerLossTest.kt +146 -0
  46. package/android/src/test/java/com/mentra/bluetoothsdk/GlassesStatusAudioSetupTest.kt +51 -0
  47. package/android/src/test/java/com/mentra/bluetoothsdk/GlassesStatusClassicAudioTest.kt +2 -0
  48. package/android/src/test/java/com/mentra/bluetoothsdk/WifiAdbPeerIntentTest.kt +19 -0
  49. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/GlassesAudioSetupPolicyTest.kt +65 -0
  50. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveAudioSetupReadinessTest.kt +231 -0
  51. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveGlassesReadyBurstTest.kt +5 -1
  52. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveIncidentDispatchTest.kt +183 -0
  53. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLiveOwnerLossPolicyTest.kt +368 -0
  54. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingAdvertisementParserTest.kt +20 -0
  55. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingReadinessPolicyTest.kt +136 -0
  56. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/MentraLivePairingStateMachineTest.kt +346 -0
  57. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasCoordinatorTest.kt +1 -56
  58. package/android/src/test/java/com/mentra/bluetoothsdk/sgcs/NimoCanvasSessionTest.kt +0 -22
  59. package/android/src/test/java/com/mentra/bluetoothsdk/utils/IncidentLogBleRelayNamingTest.kt +23 -0
  60. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/BoundedAudioQueueTest.kt +81 -0
  61. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/OnDeviceRecognizerHostTest.kt +216 -0
  62. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/STTToolsExtractionTest.kt +108 -0
  63. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityDetectorTest.kt +126 -0
  64. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SpeechActivityEngineTest.kt +245 -0
  65. package/android/src/testDebug/java/com/mentra/bluetoothsdk/stt/SttTextFormatterTest.kt +67 -0
  66. package/android/src/testDebug/java/com/mentra/bluetoothsdk/utils/audio/PcmUrlPlaybackTest.kt +76 -0
  67. package/build/BluetoothSdk.types.d.ts +201 -1
  68. package/build/BluetoothSdk.types.d.ts.map +1 -1
  69. package/build/BluetoothSdk.types.js.map +1 -1
  70. package/build/_private/BluetoothSdkModule.d.ts +43 -5
  71. package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
  72. package/build/_private/BluetoothSdkModule.js +2 -0
  73. package/build/_private/BluetoothSdkModule.js.map +1 -1
  74. package/build/generated/changelogCatalog.d.ts +3 -0
  75. package/build/generated/changelogCatalog.d.ts.map +1 -1
  76. package/build/generated/changelogCatalog.js +4 -0
  77. package/build/generated/changelogCatalog.js.map +1 -1
  78. package/build/generated/releaseMetadata.js +6 -6
  79. package/build/generated/releaseMetadata.js.map +1 -1
  80. package/build/index.d.ts +1 -1
  81. package/build/index.d.ts.map +1 -1
  82. package/build/index.js +9 -0
  83. package/build/index.js.map +1 -1
  84. package/ios/BluetoothSdkModule.swift +76 -12
  85. package/ios/MentraBluetoothSDK.podspec +4 -0
  86. package/ios/Packages/SherpaOnnx/SherpaOnnx.swift +23 -5
  87. package/ios/Resources/SileroVad/silero_vad.onnx +0 -0
  88. package/ios/Source/BesCrashReportWaiters.swift +64 -0
  89. package/ios/Source/BluetoothSdkDefaults.swift +2 -2
  90. package/ios/Source/Bridge.swift +11 -3
  91. package/ios/Source/DeviceManager.swift +131 -18
  92. package/ios/Source/DeviceStore.swift +27 -0
  93. package/ios/Source/GeneratedChangelogCatalog.swift +1 -0
  94. package/ios/Source/GeneratedReleaseMetadata.swift +6 -6
  95. package/ios/Source/MentraBluetoothSDK.swift +40 -5
  96. package/ios/Source/audio/PcmStreamPlayer.swift +290 -57
  97. package/ios/Source/audio/PcmUrlPlayback.swift +231 -0
  98. package/ios/Source/audio/SpeechActivity.swift +277 -0
  99. package/ios/Source/errors/BluetoothSdkError.swift +5 -1
  100. package/ios/Source/events/BluetoothEvents.swift +41 -0
  101. package/ios/Source/internal/BleTraceLogger.swift +9 -6
  102. package/ios/Source/internal/BluetoothAvailability.swift +15 -5
  103. package/ios/Source/sgcs/GlassesAudioSetupPolicy.swift +186 -0
  104. package/ios/Source/sgcs/MentraLive.swift +1270 -189
  105. package/ios/Source/sgcs/MentraLivePairingAdvertisement.swift +260 -1
  106. package/ios/Source/sgcs/MentraLivePairingStateMachine.swift +385 -0
  107. package/ios/Source/sgcs/MentraLivePairingStateStore.swift +121 -0
  108. package/ios/Source/sgcs/Nimo.swift +10 -3
  109. package/ios/Source/sgcs/NimoCanvasCoordinator.swift +39 -9
  110. package/ios/Source/sgcs/SGCManager.swift +13 -0
  111. package/ios/Source/sgcs/Simulated.swift +2 -0
  112. package/ios/Source/status/DeviceStatus.swift +35 -0
  113. package/ios/Source/stt/STTTools.swift +13 -31
  114. package/ios/Source/stt/SherpaOnnxTranscriber.swift +204 -162
  115. package/ios/Source/stt/SileroSpeechActivity.swift +87 -0
  116. package/ios/Source/stt/WakeWordDetector.swift +145 -0
  117. package/ios/Source/types/DeviceModels.swift +8 -1
  118. package/ios/Source/utils/AudioSessionMonitor.swift +158 -5
  119. package/ios/Source/utils/BoundedWait.swift +44 -0
  120. package/ios/Source/utils/DemandDrivenAudioWorker.swift +79 -0
  121. package/ios/Source/utils/SttModelTraits.swift +62 -0
  122. package/ios/Tests/BesCrashReportTests.swift +142 -0
  123. package/ios/Tests/BluetoothAudioRouteTests.swift +82 -0
  124. package/ios/Tests/DemandDrivenAudioWorkerTests.swift +171 -0
  125. package/ios/Tests/DeviceManagerReconnectTests.swift +4 -2
  126. package/ios/Tests/GlassesAudioSetupPolicyTests.swift +197 -0
  127. package/ios/Tests/IncidentLogDispatchTests.swift +158 -0
  128. package/ios/Tests/MentraLiveAudioSetupTests.swift +209 -0
  129. package/ios/Tests/MentraLiveConnectionOptionsTests.swift +55 -0
  130. package/ios/Tests/MentraLivePairingAdvertisementTests.swift +327 -0
  131. package/ios/Tests/MentraLivePairingStateMachineTests.swift +402 -0
  132. package/ios/Tests/MentraLiveSessionReadinessTests.swift +368 -26
  133. package/ios/Tests/NimoCanvasTransportTests.swift +58 -0
  134. package/ios/Tests/PcmStreamTests.swift +299 -0
  135. package/ios/Tests/SpeechActivityTests.swift +238 -0
  136. package/ios/Tests/SttModelTraitsTests.swift +62 -0
  137. package/ios/Tests/WifiAdbDispatchTests.swift +118 -0
  138. package/package.json +3 -2
  139. package/plugin/build/withAndroid.js +9 -11
  140. package/plugin/build/withKotlinVersion.d.ts +2 -0
  141. package/plugin/build/withKotlinVersion.js +46 -0
  142. package/src/BluetoothSdk.types.ts +201 -1
  143. package/src/_private/BluetoothSdkModule.ts +47 -5
  144. package/src/generated/changelogCatalog.ts +4 -0
  145. package/src/generated/releaseMetadata.ts +6 -6
  146. package/src/index.ts +22 -0
  147. package/android/silero/LICENSE.md +0 -17
  148. package/android/silero/build.gradle +0 -50
  149. package/android/silero/proguard-rules.pro +0 -24
  150. package/android/silero/src/androidTest/assets/hello.wav +0 -0
  151. package/android/silero/src/androidTest/java/com/konovalov/vad/silero/VadSileroTest.kt +0 -79
  152. package/android/silero/src/main/java/com/konovalov/vad/silero/Vad.kt +0 -160
  153. package/android/silero/src/main/java/com/konovalov/vad/silero/VadSilero.kt +0 -449
  154. package/android/silero/src/main/java/com/konovalov/vad/silero/config/FrameSize.kt +0 -15
  155. package/android/silero/src/main/java/com/konovalov/vad/silero/config/Mode.kt +0 -14
  156. package/android/silero/src/main/java/com/konovalov/vad/silero/config/SampleRate.kt +0 -12
  157. package/android/silero/src/main/java/com/konovalov/vad/silero/utils/AudioUtils.kt +0 -47
  158. package/android/silero/src/main/java/com/konovalov/vad/silero/utils/TensorMap.kt +0 -41
  159. package/android/silero/src/test/java/com/konovalov/vad/silero/utils/AudioUtilsTest.kt +0 -44
  160. package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -368
  161. package/android/src/debug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocol.kt +0 -156
  162. package/android/src/main/java/com/mentra/bluetoothsdk/stt/VadGateSpeechPolicy.kt +0 -229
  163. package/android/src/release/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnostics.kt +0 -20
  164. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoDiagnosticsTest.kt +0 -287
  165. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoFramebufferProtocolTest.kt +0 -127
  166. package/android/src/testDebug/java/com/mentra/bluetoothsdk/sgcs/NimoLifecycleTest.kt +0 -288
  167. /package/android/{silero/src/main/assets/LICENSE → src/main/assets/vad/LICENSE.silero_vad} +0 -0
  168. /package/android/{silero/src/main/assets → src/main/assets/vad}/silero_vad.onnx +0 -0
@@ -0,0 +1,136 @@
1
+ package com.mentra.bluetoothsdk.sgcs
2
+
3
+ import android.bluetooth.BluetoothDevice
4
+ import com.mentra.bluetoothsdk.sgcs.MentraLivePairingReadinessPolicy.Readiness
5
+ import org.junit.Assert.assertEquals
6
+ import org.junit.Assert.assertFalse
7
+ import org.junit.Assert.assertNotEquals
8
+ import org.junit.Assert.assertNull
9
+ import org.junit.Assert.assertTrue
10
+ import org.junit.Test
11
+
12
+ class MentraLivePairingReadinessPolicyTest {
13
+ private val bonded = BluetoothDevice.BOND_BONDED
14
+ private val unbonded = BluetoothDevice.BOND_NONE
15
+
16
+ @Test
17
+ fun `constants match the spec`() {
18
+ assertEquals(5_000L, MentraLivePairingReadinessPolicy.UNKNOWN_CAPABILITY_PAIRING_INFO_WAIT_MS)
19
+ assertEquals(15_000L, MentraLivePairingReadinessPolicy.SECURE_FIRMWARE_REFRESH_MS)
20
+ }
21
+
22
+ @Test
23
+ fun `the selected row decides and session pairing_info only fills an unknown row`() {
24
+ val policy = MentraLivePairingReadinessPolicy
25
+ assertTrue(policy.isSecurePairingTarget(true, null))
26
+ assertTrue(policy.isSecurePairingTarget(true, false))
27
+ // A row listed as legacy stays legacy (a rolled-back unit is never re-read as secure).
28
+ assertFalse(policy.isSecurePairingTarget(false, true))
29
+ assertFalse(policy.isSecurePairingTarget(false, null))
30
+ assertTrue(policy.isSecurePairingTarget(null, true))
31
+ assertFalse(policy.isSecurePairingTarget(null, false))
32
+ assertFalse(policy.isSecurePairingTarget(null, null))
33
+ }
34
+
35
+ @Test
36
+ fun `automatic reconnects are never gated`() {
37
+ for (selected in listOf(true, false, null)) for (session in listOf(true, false, null)) {
38
+ assertEquals(
39
+ Readiness.READY,
40
+ readiness(explicit = false, selected = selected, session = session, bond = unbonded),
41
+ )
42
+ }
43
+ }
44
+
45
+ @Test
46
+ fun `unknown capability waits for pairing_info, then is legacy and ready without one`() {
47
+ assertEquals(Readiness.AWAIT_PAIRING_INFO, readiness(selected = null, session = null))
48
+ assertEquals(Readiness.READY, readiness(selected = null, session = null, expired = true))
49
+ }
50
+
51
+ @Test
52
+ fun `unknown capability with a secure pairing_info follows the secure rule`() {
53
+ assertEquals(Readiness.AWAIT_OWNER_COMMIT, readiness(selected = null, session = true, bond = unbonded))
54
+ assertEquals(
55
+ Readiness.AWAIT_OWNER_COMMIT,
56
+ readiness(selected = null, session = true, bond = bonded, committed = false),
57
+ )
58
+ assertEquals(Readiness.READY, readiness(selected = null, session = true, bond = bonded, committed = true))
59
+ // A late secure report after the wait expired still applies the secure rule.
60
+ assertEquals(
61
+ Readiness.AWAIT_OWNER_COMMIT,
62
+ readiness(selected = null, session = true, expired = true, bond = unbonded),
63
+ )
64
+ }
65
+
66
+ @Test
67
+ fun `unknown capability with a legacy pairing_info is ready at once`() {
68
+ assertEquals(Readiness.READY, readiness(selected = null, session = false, bond = unbonded))
69
+ }
70
+
71
+ @Test
72
+ fun `known rows never wait for pairing_info`() {
73
+ assertEquals(Readiness.AWAIT_OWNER_COMMIT, readiness(selected = true, session = null, bond = unbonded))
74
+ assertEquals(Readiness.READY, readiness(selected = true, session = null, bond = bonded, committed = true))
75
+ assertEquals(Readiness.READY, readiness(selected = false, session = null, bond = unbonded))
76
+ assertEquals(Readiness.READY, readiness(selected = false, session = true, bond = unbonded))
77
+ }
78
+
79
+ @Test
80
+ fun `readiness never waits without a deadline`() {
81
+ for (explicit in listOf(true, false)) for (selected in listOf(true, false, null)) {
82
+ for (session in listOf(true, false, null)) for (expired in listOf(true, false)) {
83
+ for (bond in listOf(bonded, unbonded, BluetoothDevice.BOND_BONDING, null)) {
84
+ for (committed in listOf(true, false)) {
85
+ val result = readiness(explicit, selected, session, expired, bond, committed)
86
+ // The pairing_info wait is bounded: once it expired it never applies.
87
+ if (expired) assertNotEquals(Readiness.AWAIT_PAIRING_INFO, result)
88
+ // The owner-commit wait always runs the secure bond wait deadline.
89
+ if (result == Readiness.AWAIT_OWNER_COMMIT) {
90
+ assertTrue(
91
+ MentraLiveOwnerLossPolicy.shouldRunSecureBondWait(
92
+ explicit,
93
+ MentraLivePairingReadinessPolicy.isSecurePairingTarget(selected, session),
94
+ )
95
+ )
96
+ }
97
+ }
98
+ }
99
+ }
100
+ }
101
+ }
102
+
103
+ @Test
104
+ fun `a ready session without pairing_info clears the secure firmware memory`() {
105
+ assertEquals(false, MentraLivePairingReadinessPolicy.secureFirmwareAfterReadySession(pairingInfoSeen = false))
106
+ assertNull(MentraLivePairingReadinessPolicy.secureFirmwareAfterReadySession(pairingInfoSeen = true))
107
+ }
108
+
109
+ @Test
110
+ fun `secure trailer memory starts empty with each scan`() {
111
+ val memory = MentraLiveSecureTrailerScanMemory()
112
+ memory.noteSecureTrailer("AA:BB:CC:DD:EE:01")
113
+ assertTrue(memory.contains("AA:BB:CC:DD:EE:01"))
114
+ assertFalse(memory.contains("AA:BB:CC:DD:EE:02"))
115
+ assertFalse(memory.contains(null))
116
+ memory.onScanStart()
117
+ assertFalse(memory.contains("AA:BB:CC:DD:EE:01"))
118
+ }
119
+
120
+ private fun readiness(
121
+ explicit: Boolean = true,
122
+ selected: Boolean?,
123
+ session: Boolean?,
124
+ expired: Boolean = false,
125
+ bond: Int? = unbonded,
126
+ committed: Boolean = false,
127
+ ): Readiness =
128
+ MentraLivePairingReadinessPolicy.readiness(
129
+ explicitPairing = explicit,
130
+ selectedCapability = selected,
131
+ sessionPairingInfoSecure = session,
132
+ unknownCapabilityWaitExpired = expired,
133
+ bondState = bond,
134
+ ownerCommitted = committed,
135
+ )
136
+ }
@@ -0,0 +1,346 @@
1
+ package com.mentra.bluetoothsdk.sgcs
2
+
3
+ import org.junit.Assert.assertEquals
4
+ import org.junit.Assert.assertFalse
5
+ import org.junit.Assert.assertNull
6
+ import org.junit.Assert.assertTrue
7
+ import org.junit.Test
8
+
9
+ class MentraLivePairingStateMachineTest {
10
+ private val t0 = 1_700_000_000_000L
11
+ private val device = "MENTRA_LIVE_BLE_ABCD"
12
+
13
+ private fun enter(
14
+ machine: MentraLivePairingStateMachine,
15
+ windowId: Int? = 7,
16
+ windowMs: Long? = 120_000L,
17
+ ownerRetained: Boolean = true,
18
+ explicit: Boolean = false,
19
+ now: Long = t0,
20
+ ) = machine.onEnteringPairingMode(windowMs, windowId, ownerRetained, explicit, device, now)
21
+
22
+ private fun pausedMachine(windowId: Int = 7): MentraLivePairingStateMachine =
23
+ MentraLivePairingStateMachine().also { enter(it, windowId = windowId) }
24
+
25
+ private fun reject(machine: MentraLivePairingStateMachine, now: Long, explicit: Boolean = false) =
26
+ machine.onAuthRejection(secureFirmware = true, explicitAttempt = explicit, nowMs = now)
27
+
28
+ @Test
29
+ fun v4NoticePausesForTheWindowPlusMarginWithoutA300sClamp() {
30
+ val machine = MentraLivePairingStateMachine()
31
+ val transition = enter(machine)
32
+ assertEquals(MentraLivePairingEffect.PAUSE, transition.effect)
33
+ assertEquals(MentraLivePairingState.PairingMode(7, t0 + 150_000L), machine.state)
34
+ assertFalse(machine.allowsAutomaticReconnect)
35
+ assertEquals(150_000L, machine.remainingPauseMs(t0))
36
+
37
+ val long = MentraLivePairingStateMachine()
38
+ enter(long, windowMs = 900_000L)
39
+ assertEquals(MentraLivePairingState.PairingMode(7, t0 + 930_000L), long.state)
40
+ }
41
+
42
+ @Test
43
+ fun publicStateMapsMatchTheTypeScriptContract() {
44
+ assertEquals(mapOf("state" to "normal"), MentraLivePairingState.Normal.toMap())
45
+ assertEquals(mapOf("state" to "pairing_rejected"), MentraLivePairingState.Rejected.toMap())
46
+ assertEquals(
47
+ mapOf("state" to "pairing_mode", "windowId" to 7, "endsAtMs" to t0),
48
+ MentraLivePairingState.PairingMode(7, t0).toMap(),
49
+ )
50
+ }
51
+
52
+ @Test
53
+ fun aDuplicateNoticeForTheSameWindowIsIgnored() {
54
+ val machine = pausedMachine()
55
+ val again = enter(machine, now = t0 + 5_000L)
56
+ assertEquals(MentraLivePairingEffect.NONE, again.effect)
57
+ assertEquals(MentraLivePairingState.PairingMode(7, t0 + 150_000L), machine.state)
58
+ }
59
+
60
+ @Test
61
+ fun anExplicitAttemptIsNeverPausedByTheNotice() {
62
+ val machine = MentraLivePairingStateMachine()
63
+ assertEquals(MentraLivePairingEffect.NONE, enter(machine, explicit = true).effect)
64
+ assertEquals(MentraLivePairingState.Normal, machine.state)
65
+ }
66
+
67
+ @Test
68
+ fun flagOffFirmwareForgetsThisPhoneOnEntry() {
69
+ val machine = MentraLivePairingStateMachine()
70
+ val transition = enter(machine, windowId = null, ownerRetained = false)
71
+ assertEquals(MentraLivePairingEffect.REJECT, transition.effect)
72
+ assertEquals(MentraLivePairingState.Rejected, machine.state)
73
+
74
+ val pairing = MentraLivePairingStateMachine()
75
+ enter(pairing, windowId = null, ownerRetained = false, explicit = true)
76
+ assertEquals(MentraLivePairingState.Normal, pairing.state)
77
+ }
78
+
79
+ @Test
80
+ fun pairingModeEndedResumesEarlyForTheMatchingWindowOnly() {
81
+ val machine = pausedMachine()
82
+ assertEquals(MentraLivePairingEffect.NONE, machine.onPairingModeEnded("timeout", 8).effect)
83
+ assertTrue(machine.isPaused)
84
+ val ended = machine.onPairingModeEnded("paired", 7)
85
+ assertEquals(MentraLivePairingEffect.RESUME, ended.effect)
86
+ assertEquals(MentraLivePairingState.Normal, machine.state)
87
+ // The connected phone was admitted, so there is nothing to verify.
88
+ assertNull(machine.endedWindowId)
89
+ }
90
+
91
+ @Test
92
+ fun aClearedAdvertisementFlagResumesEarlyAfterTheGrace() {
93
+ val machine = pausedMachine()
94
+ // An advertisement from before the window opened can still be in flight.
95
+ val early = machine.onAdvertisement(false, false, 6, t0 + 1_000L)
96
+ assertEquals(MentraLivePairingEffect.NONE, early.effect)
97
+ assertTrue(machine.isPaused)
98
+ assertEquals(MentraLivePairingEffect.NONE, machine.onAdvertisement(true, false, 7, t0 + 20_000L).effect)
99
+ val cleared = machine.onAdvertisement(false, false, 7, t0 + 60_000L)
100
+ assertEquals(MentraLivePairingEffect.RESUME, cleared.effect)
101
+ assertEquals(MentraLivePairingState.Normal, machine.state)
102
+ assertEquals(7, machine.endedWindowId)
103
+ }
104
+
105
+ @Test
106
+ fun theDeadlineResumes() {
107
+ val machine = pausedMachine()
108
+ assertEquals(MentraLivePairingEffect.NONE, machine.onTick(t0 + 149_999L).effect)
109
+ assertEquals(MentraLivePairingEffect.RESUME, machine.onTick(t0 + 150_000L).effect)
110
+ assertTrue(machine.allowsAutomaticReconnect)
111
+ assertTrue(machine.awaitingTakeoverCheck)
112
+ }
113
+
114
+ @Test
115
+ fun ownerChangedForTheEndedWindowRejectsOnTheFirstFailedVerification() {
116
+ val machine = pausedMachine()
117
+ machine.onTick(t0 + 150_000L)
118
+ machine.onAdvertisement(false, ownerChanged = true, generation = 7, nowMs = t0 + 151_000L)
119
+ assertTrue(machine.verificationArmed)
120
+ assertFalse(machine.awaitingTakeoverCheck)
121
+ val verdict = reject(machine, t0 + 152_000L)
122
+ assertEquals(MentraLivePairingEffect.REJECT, verdict.effect)
123
+ assertEquals(MentraLivePairingState.Rejected, machine.state)
124
+ }
125
+
126
+ @Test
127
+ fun ownerChangedCarriedOnTheFlagClearingAdvertisementArmsTheVerification() {
128
+ val machine = pausedMachine()
129
+ val cleared = machine.onAdvertisement(false, ownerChanged = true, generation = 7, nowMs = t0 + 60_000L)
130
+ assertEquals(MentraLivePairingEffect.RESUME, cleared.effect)
131
+ assertTrue(machine.verificationArmed)
132
+ }
133
+
134
+ @Test
135
+ fun aSpoofedOwnerChangedBitFollowedByASuccessfulReconnectDoesNotStrandThePhone() {
136
+ val machine = pausedMachine()
137
+ machine.onTick(t0 + 150_000L)
138
+ machine.onAdvertisement(false, ownerChanged = true, generation = 7, nowMs = t0 + 151_000L)
139
+ assertTrue(machine.verificationArmed)
140
+ machine.onSessionReady(explicitAttempt = false)
141
+ assertEquals(MentraLivePairingState.Normal, machine.state)
142
+ assertFalse(machine.verificationArmed)
143
+ // The same bit again arms nothing: exactly one verification per window.
144
+ machine.onAdvertisement(false, ownerChanged = true, generation = 7, nowMs = t0 + 200_000L)
145
+ assertFalse(machine.verificationArmed)
146
+ assertEquals(MentraLivePairingEffect.NONE, reject(machine, t0 + 201_000L).effect)
147
+ assertEquals(MentraLivePairingState.Normal, machine.state)
148
+ }
149
+
150
+ @Test
151
+ fun ownerChangedForAnotherWindowOrStillInPairingModeArmsNothing() {
152
+ val other = pausedMachine()
153
+ other.onTick(t0 + 150_000L)
154
+ other.onAdvertisement(false, ownerChanged = true, generation = 9, nowMs = t0 + 151_000L)
155
+ assertFalse(other.verificationArmed)
156
+
157
+ val stillPairing = pausedMachine()
158
+ stillPairing.onAdvertisement(true, ownerChanged = true, generation = 7, nowMs = t0 + 60_000L)
159
+ assertFalse(stillPairing.verificationArmed)
160
+
161
+ // Without a window this phone paused for, the bit is not this phone's business.
162
+ val idle = MentraLivePairingStateMachine()
163
+ idle.onAdvertisement(false, ownerChanged = true, generation = 7, nowMs = t0)
164
+ assertFalse(idle.verificationArmed)
165
+ }
166
+
167
+ @Test
168
+ fun aVerificationThatFailsWithoutAnAuthVerdictFallsBackToTheStreak() {
169
+ val machine = pausedMachine()
170
+ machine.onTick(t0 + 150_000L)
171
+ machine.onAdvertisement(false, ownerChanged = true, generation = 7, nowMs = t0 + 151_000L)
172
+ machine.onConnectAttemptFailed()
173
+ assertFalse(machine.verificationArmed)
174
+ assertEquals(MentraLivePairingEffect.NONE, reject(machine, t0 + 160_000L).effect)
175
+ assertEquals(MentraLivePairingState.Normal, machine.state)
176
+ }
177
+
178
+ @Test
179
+ fun withoutTheBitThreeRejectionsSpanningTheMinimumReject() {
180
+ val machine = MentraLivePairingStateMachine()
181
+ assertEquals(MentraLivePairingEffect.NONE, reject(machine, t0).effect)
182
+ assertEquals(MentraLivePairingEffect.NONE, reject(machine, t0 + 20_000L).effect)
183
+ val third = reject(machine, t0 + MentraLiveOwnerLossPolicy.MIN_REJECTION_SPAN_MS)
184
+ assertEquals(MentraLivePairingEffect.REJECT, third.effect)
185
+ assertEquals(MentraLivePairingState.Rejected, machine.state)
186
+ }
187
+
188
+ @Test
189
+ fun rejectionsDuringThePauseOrWhileThePairingAdvertIsFreshNeverCount() {
190
+ val machine = pausedMachine()
191
+ // The intentional disconnect and every 0x14/auth refusal inside the window.
192
+ var now = t0
193
+ repeat(6) {
194
+ assertEquals(MentraLivePairingEffect.NONE, reject(machine, now).effect)
195
+ now += 20_000L
196
+ }
197
+ assertTrue(machine.isPaused)
198
+
199
+ val advertising = MentraLivePairingStateMachine()
200
+ repeat(6) { i ->
201
+ val at = t0 + i * 20_000L
202
+ advertising.onAdvertisement(true, false, 3, at)
203
+ assertEquals(MentraLivePairingEffect.NONE, reject(advertising, at + 1_000L).effect)
204
+ }
205
+ assertEquals(MentraLivePairingState.Normal, advertising.state)
206
+ // The streak starts fresh once pairing mode is no longer advertised.
207
+ advertising.onAdvertisement(false, false, 3, t0 + 200_000L)
208
+ assertEquals(MentraLivePairingEffect.NONE, reject(advertising, t0 + 200_500L).effect)
209
+ assertEquals(MentraLivePairingState.Normal, advertising.state)
210
+ }
211
+
212
+ @Test
213
+ fun legacyFirmwareAndExplicitAttemptsNeverCount() {
214
+ val machine = MentraLivePairingStateMachine()
215
+ var now = t0
216
+ repeat(6) {
217
+ machine.onAuthRejection(secureFirmware = false, explicitAttempt = false, nowMs = now)
218
+ reject(machine, now + 1, explicit = true)
219
+ now += 20_000L
220
+ }
221
+ assertEquals(MentraLivePairingState.Normal, machine.state)
222
+ }
223
+
224
+ @Test
225
+ fun onlyAnExplicitPairOrAForgetClearsARejection() {
226
+ val machine = MentraLivePairingStateMachine()
227
+ enter(machine, windowId = null, ownerRetained = false)
228
+ machine.onSessionReady(explicitAttempt = false)
229
+ assertEquals(MentraLivePairingState.Rejected, machine.state)
230
+ machine.onPairingModeEnded("timeout", 1)
231
+ machine.onTick(t0 + 1_000_000L)
232
+ machine.onAdvertisement(false, false, 1, t0 + 1_000_000L)
233
+ assertEquals(MentraLivePairingState.Rejected, machine.state)
234
+ val paired = machine.onSessionReady(explicitAttempt = true)
235
+ assertTrue(paired.stateChanged)
236
+ assertEquals(MentraLivePairingState.Normal, machine.state)
237
+
238
+ val forgotten = MentraLivePairingStateMachine()
239
+ enter(forgotten, windowId = null, ownerRetained = false)
240
+ forgotten.onForget()
241
+ assertEquals(MentraLivePairingState.Normal, forgotten.state)
242
+ assertEquals("", forgotten.deviceId)
243
+ }
244
+
245
+ @Test
246
+ fun aPauseSurvivesARestartWithTheSameWindowId() {
247
+ val machine = pausedMachine(windowId = 42)
248
+ val persisted = machine.persisted()
249
+ val restored = MentraLivePairingStateMachine()
250
+ val transition = restored.restore(persisted, device, t0 + 30_000L)
251
+ assertEquals(MentraLivePairingEffect.PAUSE, transition.effect)
252
+ assertEquals(MentraLivePairingState.PairingMode(42, t0 + 150_000L), restored.state)
253
+ assertEquals(120_000L, restored.remainingPauseMs(t0 + 30_000L))
254
+ }
255
+
256
+ @Test
257
+ fun aPauseThatEndedWhileTheAppWasAwayStillVerifiesTheTakeover() {
258
+ val persisted = pausedMachine(windowId = 42).persisted()
259
+ val restored = MentraLivePairingStateMachine()
260
+ val transition = restored.restore(persisted, device, t0 + 400_000L)
261
+ assertEquals(MentraLivePairingEffect.NONE, transition.effect)
262
+ assertEquals(MentraLivePairingState.Normal, restored.state)
263
+ assertEquals(42, restored.endedWindowId)
264
+ restored.onAdvertisement(false, ownerChanged = true, generation = 42, nowMs = t0 + 401_000L)
265
+ assertTrue(restored.verificationArmed)
266
+ }
267
+
268
+ @Test
269
+ fun anArmedVerificationIsReArmedAfterARestart() {
270
+ val machine = pausedMachine(windowId = 42)
271
+ machine.onTick(t0 + 150_000L)
272
+ machine.onAdvertisement(false, ownerChanged = true, generation = 42, nowMs = t0 + 151_000L)
273
+ assertTrue(machine.verificationArmed)
274
+ val restored = MentraLivePairingStateMachine()
275
+ restored.restore(machine.persisted(), device, t0 + 160_000L)
276
+ assertFalse(restored.verificationArmed)
277
+ restored.onAdvertisement(false, ownerChanged = true, generation = 42, nowMs = t0 + 161_000L)
278
+ assertTrue(restored.verificationArmed)
279
+ }
280
+
281
+ @Test
282
+ fun anImplausibleRestoredDeadlineDoesNotPause() {
283
+ val persisted = pausedMachine().persisted().toMutableMap()
284
+ persisted[MentraLivePairingStateMachine.KEY_ENDS_AT_MS] = t0 + 10_000_000L
285
+ val restored = MentraLivePairingStateMachine()
286
+ restored.restore(persisted, device, t0)
287
+ assertEquals(MentraLivePairingState.Normal, restored.state)
288
+ }
289
+
290
+ @Test
291
+ fun aRejectionSurvivesARestart() {
292
+ val machine = MentraLivePairingStateMachine()
293
+ enter(machine, windowId = null, ownerRetained = false)
294
+ val restored = MentraLivePairingStateMachine()
295
+ val transition = restored.restore(machine.persisted(), device, t0 + 86_400_000L)
296
+ assertEquals(MentraLivePairingEffect.REJECT, transition.effect)
297
+ assertEquals(MentraLivePairingState.Rejected, restored.state)
298
+ }
299
+
300
+ @Test
301
+ fun restoreIgnoresUnknownKeysAndMalformedValues() {
302
+ val values =
303
+ mapOf(
304
+ MentraLivePairingStateMachine.KEY_DEVICE_ID to device,
305
+ MentraLivePairingStateMachine.KEY_STATE to "pairing_mode",
306
+ MentraLivePairingStateMachine.KEY_WINDOW_ID to "not a number",
307
+ MentraLivePairingStateMachine.KEY_ENDS_AT_MS to t0 + 60_000L,
308
+ "pairing_v9_future_key" to "whatever",
309
+ "unrelated" to 12,
310
+ )
311
+ val restored = MentraLivePairingStateMachine()
312
+ restored.restore(values, device, t0)
313
+ assertEquals(MentraLivePairingState.Normal, restored.state)
314
+
315
+ val future = MentraLivePairingStateMachine()
316
+ future.restore(mapOf(MentraLivePairingStateMachine.KEY_STATE to "pairing_v9_state"), device, t0)
317
+ assertEquals(MentraLivePairingState.Normal, future.state)
318
+ }
319
+
320
+ @Test
321
+ fun aRecordForOtherGlassesDoesNotApply() {
322
+ val persisted = pausedMachine().persisted()
323
+ val restored = MentraLivePairingStateMachine()
324
+ restored.restore(persisted, "MENTRA_LIVE_BLE_OTHER", t0 + 1_000L)
325
+ assertEquals(MentraLivePairingState.Normal, restored.state)
326
+ assertEquals("MENTRA_LIVE_BLE_OTHER", restored.deviceId)
327
+
328
+ val bound = MentraLivePairingStateMachine()
329
+ enter(bound, windowId = null, ownerRetained = false)
330
+ assertTrue(bound.bindDevice("MENTRA_LIVE_BLE_OTHER").stateChanged)
331
+ assertEquals(MentraLivePairingState.Normal, bound.state)
332
+ }
333
+
334
+ @Test
335
+ fun theLegacyStandDownPhaseMigratesToRejected() {
336
+ val restored = MentraLivePairingStateMachine()
337
+ val transition =
338
+ restored.restore(
339
+ mapOf(MentraLivePairingStateMachine.LEGACY_REJECTION_PHASE_KEY to "waiting"),
340
+ device,
341
+ t0,
342
+ )
343
+ assertEquals(MentraLivePairingEffect.REJECT, transition.effect)
344
+ assertEquals(MentraLivePairingState.Rejected, restored.state)
345
+ }
346
+ }
@@ -31,12 +31,11 @@ class NimoCanvasCoordinatorTest {
31
31
  val writes = mutableListOf<ByteArray>()
32
32
  val failures = mutableListOf<String>()
33
33
  val rejections = mutableListOf<Int>()
34
- var holdPreemptions = 0
35
34
  var rejectWrites = false
36
35
  val queue = NimoGattQueue<Any>(clock, { _, bytes -> writes += bytes; !rejectWrites }, { failures += it })
37
36
  val canvas = NimoCanvasCoordinator(clock, { 20 },
38
37
  { frames, started, completed -> queue.enqueue(connection, characteristic, frames, started, completed) },
39
- { queue.reset(); failures += it }, { rejections += it }, { holdPreemptions++ })
38
+ { queue.reset(); failures += it }, { rejections += it })
40
39
  init { queue.connected(connection) }
41
40
  fun written(success: Boolean = true) {
42
41
  queue.written(connection, characteristic, success)
@@ -244,60 +243,6 @@ class NimoCanvasCoordinatorTest {
244
243
  assertTrue(f.failures.isEmpty())
245
244
  }
246
245
 
247
- @Test fun diagnosticHoldWaitsForCurrentBusinessAckAndThenResumesLatestScene() {
248
- val f = Fixture()
249
- val initial = byteArrayOf(0, 0, 1)
250
- val latest = byteArrayOf(2, 0, 1)
251
- f.launch(initial)
252
- var ready = 0
253
- f.canvas.hold { ready++ }
254
- f.canvas.offer(byteArrayOf(1, 0, 1), "one:1")
255
- f.canvas.offer(latest, "one:1")
256
-
257
- assertEquals(0, ready)
258
- f.finishUpdate(initial)
259
- assertEquals(1, ready)
260
- val beforeRelease = f.writes.size
261
-
262
- f.canvas.releaseHold()
263
-
264
- assertEquals(beforeRelease + 1, f.writes.size)
265
- assertArrayEquals(NimoCanvasCodec.frames(4, latest, 20).first(), f.writes.last())
266
- }
267
-
268
- @Test fun scopeTakeoverPreemptsHoldWithoutFlushingHeldScene() {
269
- val f = Fixture()
270
- val initial = byteArrayOf(0, 0, 1)
271
- val held = byteArrayOf(1, 0, 1)
272
- val takeover = byteArrayOf(2, 0, 1)
273
- f.launch(initial); f.finishUpdate(initial)
274
- var ready = 0
275
- f.canvas.hold { ready++ }
276
- f.canvas.offer(held, "one:1")
277
- val beforeTakeover = f.writes.size
278
-
279
- f.canvas.offer(takeover, "two:1")
280
-
281
- assertEquals(1, ready)
282
- assertEquals(1, f.holdPreemptions)
283
- assertEquals(beforeTakeover + 1, f.writes.size)
284
- assertArrayEquals(NimoCanvasCodec.frames(4, takeover, 20).first(), f.writes.last())
285
- }
286
-
287
- @Test fun firstScopePreemptsHoldAcquiredBeforeAnyCanvasScene() {
288
- val f = Fixture()
289
- var ready = 0
290
- f.canvas.readiness(true)
291
- f.canvas.hold { ready++ }
292
- assertEquals(1, ready)
293
-
294
- f.canvas.offer(byteArrayOf(0, 0, 1), "first:1")
295
-
296
- assertEquals(1, f.holdPreemptions)
297
- assertEquals(1, f.writes.size)
298
- assertArrayEquals(NimoCanvasCodec.frames(1, writeCapacity = 20).single(), f.writes.single())
299
- }
300
-
301
246
  @Test fun reentrantCompletionCannotReleaseNewConnectionWrite() {
302
247
  val f = Fixture(); val old = f.connection
303
248
  f.queue.enqueue(old, f.characteristic, listOf(byteArrayOf(1)), completed = {
@@ -314,26 +314,4 @@ class NimoCanvasSessionTest {
314
314
  assertThrows(IllegalArgumentException::class.java) { session.offer(ByteArray(12281), "one:1") }
315
315
  }
316
316
 
317
- @Test fun diagnosticHoldRetainsOnlyLatestSceneUntilReleased() {
318
- val session = NimoCanvasSession()
319
- launched(session); success(session, 4)
320
-
321
- assertTrue(session.hold(true).isEmpty())
322
- assertTrue(session.offer(frameB, "two:1").isEmpty())
323
- assertTrue(session.offer(frameC, "two:2", force = true).isEmpty())
324
-
325
- val update = send(session.hold(false))
326
- assertEquals(4, update.key)
327
- assertArrayEquals(frameC, update.frame)
328
- }
329
-
330
- @Test fun preemptedHoldDoesNotFlushStaleSceneBeforeExit() {
331
- val session = NimoCanvasSession()
332
- launched(session); success(session, 4)
333
- session.hold(true)
334
- session.offer(frameB, "two:1")
335
-
336
- assertTrue(session.hold(false, resume = false).isEmpty())
337
- assertEquals(3, send(session.exit()).key)
338
- }
339
317
  }
@@ -0,0 +1,23 @@
1
+ package com.mentra.bluetoothsdk.utils
2
+
3
+ import org.junit.Assert.assertEquals
4
+ import org.junit.Assert.assertFalse
5
+ import org.junit.Assert.assertTrue
6
+ import org.junit.Test
7
+
8
+ class IncidentLogBleRelayNamingTest {
9
+ @Test fun `relay names built for a report are recognized`() {
10
+ val firmware = IncidentLogBleRelayNaming.bleFileBaseName("rep_01M4HBVT88DFSN8TYNZ1WTV4PB", 'B')
11
+ val logcat = IncidentLogBleRelayNaming.bleFileBaseName("rep_01M4HBVT88DFSN8TYNZ1WTV4PB", 'L')
12
+ assertEquals("Brep_01m4hbvt88d", firmware)
13
+ assertTrue(IncidentLogBleRelayNaming.isRelayFileName(firmware))
14
+ assertTrue(IncidentLogBleRelayNaming.isRelayFileName(logcat))
15
+ assertTrue(IncidentLogBleRelayNaming.isRelayFileName(IncidentLogBleRelayNaming.bleFileBaseName("short", 'B')))
16
+ }
17
+
18
+ @Test fun `photo and other transfers are not relays`() {
19
+ for (name in listOf("I000123456", "I000123456.jpg", "ble_1234567890", "Brep_01m4hbvt88d.json", "Xrep_01m4hbvt88d", "", null)) {
20
+ assertFalse(name.toString(), IncidentLogBleRelayNaming.isRelayFileName(name))
21
+ }
22
+ }
23
+ }
@@ -0,0 +1,81 @@
1
+ package com.mentra.bluetoothsdk.stt
2
+
3
+ import org.junit.Assert.assertEquals
4
+ import org.junit.Assert.assertNull
5
+ import org.junit.Test
6
+
7
+ class BoundedAudioQueueTest {
8
+ private sealed class Item {
9
+ data class Audio(val n: Int) : Item()
10
+ data class Control(val name: String) : Item()
11
+ }
12
+
13
+ private fun queue(capacity: Int) = BoundedAudioQueue<Item>(capacity) { it is Item.Audio }
14
+
15
+ @Test
16
+ fun dropsOnlyTheOldestAudioWhenFull() {
17
+ val q = queue(2)
18
+ q.offer(Item.Audio(1))
19
+ q.offer(Item.Control("finish"))
20
+ q.offer(Item.Audio(2))
21
+ val dropped = q.offer(Item.Audio(3))
22
+
23
+ assertEquals(Item.Audio(1), dropped)
24
+ assertEquals(1L, q.droppedAudio)
25
+ assertEquals(Item.Control("finish"), q.poll(0))
26
+ assertEquals(Item.Audio(2), q.poll(0))
27
+ assertEquals(Item.Audio(3), q.poll(0))
28
+ assertNull(q.poll(0))
29
+ }
30
+
31
+ @Test
32
+ fun neverDropsControlItemsEvenWhenTheWorkerFallsBehind() {
33
+ val q = queue(1)
34
+ repeat(50) { i ->
35
+ q.offer(Item.Control("c$i"))
36
+ q.offer(Item.Audio(i))
37
+ }
38
+ val drained = q.drain()
39
+ assertEquals(50, drained.count { it is Item.Control })
40
+ // Only the newest audio frame fits.
41
+ assertEquals(listOf(Item.Audio(49)), drained.filterIsInstance<Item.Audio>())
42
+ }
43
+
44
+ @Test
45
+ fun keepsControlItemsInOrderAfterEarlierAudio() {
46
+ val q = queue(10)
47
+ q.offer(Item.Audio(1))
48
+ q.offer(Item.Audio(2))
49
+ q.offer(Item.Control("finish"))
50
+ q.offer(Item.Audio(3))
51
+
52
+ assertEquals(
53
+ listOf(Item.Audio(1), Item.Audio(2), Item.Control("finish"), Item.Audio(3)),
54
+ List(4) { q.poll(0) },
55
+ )
56
+ }
57
+
58
+ @Test
59
+ fun clearAudioKeepsControlItems() {
60
+ val q = queue(10)
61
+ q.offer(Item.Audio(1))
62
+ q.offer(Item.Control("reset"))
63
+ q.offer(Item.Audio(2))
64
+
65
+ assertEquals(2, q.clearAudio())
66
+ assertEquals(Item.Control("reset"), q.poll(0))
67
+ assertNull(q.poll(0))
68
+ // Capacity is available again after clearing.
69
+ repeat(10) { q.offer(Item.Audio(it)) }
70
+ assertEquals(0L, q.droppedAudio)
71
+ }
72
+
73
+ @Test
74
+ fun pollTimesOutWhenEmpty() {
75
+ val q = queue(1)
76
+ val started = System.nanoTime()
77
+ assertNull(q.poll(30))
78
+ val elapsedMs = (System.nanoTime() - started) / 1_000_000
79
+ assert(elapsedMs >= 25) { "poll returned after ${elapsedMs}ms" }
80
+ }
81
+ }