@mentra/bluetooth-sdk 3.5.0-dev.1518 → 3.5.0-dev.1519

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 (110) hide show
  1. package/MIGRATION.md +9 -0
  2. package/README.md +64 -11
  3. package/android/src/main/java/com/mentra/bluetoothsdk/GeneratedReleaseMetadata.kt +5 -5
  4. package/android/src/main/java/com/mentra/bluetoothsdk/MentraLocalNetworkModule.kt +10 -0
  5. package/build/BluetoothSdk.types.d.ts +23 -5
  6. package/build/BluetoothSdk.types.d.ts.map +1 -1
  7. package/build/BluetoothSdk.types.js +5 -0
  8. package/build/BluetoothSdk.types.js.map +1 -1
  9. package/build/_internal.d.ts +1 -0
  10. package/build/_internal.d.ts.map +1 -1
  11. package/build/_internal.js +1 -0
  12. package/build/_internal.js.map +1 -1
  13. package/build/_private/BluetoothSdkModule.d.ts.map +1 -1
  14. package/build/_private/BluetoothSdkModule.js +20 -9
  15. package/build/_private/BluetoothSdkModule.js.map +1 -1
  16. package/build/_private/MentraLocalNetworkModule.d.ts +21 -0
  17. package/build/_private/MentraLocalNetworkModule.d.ts.map +1 -1
  18. package/build/_private/MentraLocalNetworkModule.js.map +1 -1
  19. package/build/generated/releaseMetadata.js +5 -5
  20. package/build/generated/releaseMetadata.js.map +1 -1
  21. package/build/glasses-wifi/connection.d.ts +6 -0
  22. package/build/glasses-wifi/connection.d.ts.map +1 -0
  23. package/build/glasses-wifi/connection.js +44 -0
  24. package/build/glasses-wifi/connection.js.map +1 -0
  25. package/build/glasses-wifi/continuous.d.ts +39 -0
  26. package/build/glasses-wifi/continuous.d.ts.map +1 -0
  27. package/build/glasses-wifi/continuous.js +151 -0
  28. package/build/glasses-wifi/continuous.js.map +1 -0
  29. package/build/glasses-wifi/files.d.ts +11 -0
  30. package/build/glasses-wifi/files.d.ts.map +1 -0
  31. package/build/glasses-wifi/files.js +33 -0
  32. package/build/glasses-wifi/files.js.map +1 -0
  33. package/build/glasses-wifi/index.d.ts +20 -0
  34. package/build/glasses-wifi/index.d.ts.map +1 -0
  35. package/build/glasses-wifi/index.js +15 -0
  36. package/build/glasses-wifi/index.js.map +1 -0
  37. package/build/glasses-wifi/link.d.ts +31 -0
  38. package/build/glasses-wifi/link.d.ts.map +1 -0
  39. package/build/glasses-wifi/link.js +71 -0
  40. package/build/glasses-wifi/link.js.map +1 -0
  41. package/build/glasses-wifi/native.d.ts +63 -0
  42. package/build/glasses-wifi/native.d.ts.map +1 -0
  43. package/build/glasses-wifi/native.js +138 -0
  44. package/build/glasses-wifi/native.js.map +1 -0
  45. package/build/glasses-wifi/service.d.ts +86 -0
  46. package/build/glasses-wifi/service.d.ts.map +1 -0
  47. package/build/glasses-wifi/service.js +497 -0
  48. package/build/glasses-wifi/service.js.map +1 -0
  49. package/build/index.d.ts +4 -2
  50. package/build/index.d.ts.map +1 -1
  51. package/build/index.js +24 -6
  52. package/build/index.js.map +1 -1
  53. package/build/ota-transport/index.d.ts +2 -43
  54. package/build/ota-transport/index.d.ts.map +1 -1
  55. package/build/ota-transport/index.js +3 -49
  56. package/build/ota-transport/index.js.map +1 -1
  57. package/build/react/useBluetoothStatus.d.ts.map +1 -1
  58. package/build/react/useBluetoothStatus.js +5 -24
  59. package/build/react/useBluetoothStatus.js.map +1 -1
  60. package/build/react/useMentraBluetooth.d.ts +1 -2
  61. package/build/react/useMentraBluetooth.d.ts.map +1 -1
  62. package/build/react/useMentraBluetooth.js +0 -1
  63. package/build/react/useMentraBluetooth.js.map +1 -1
  64. package/expo-module.config.json +1 -1
  65. package/ios/MentraBluetoothSDK.podspec +5 -4
  66. package/ios/MentraLocalNetworkModule.swift +264 -0
  67. package/ios/Source/BluetoothSdkDefaults.swift +1 -1
  68. package/ios/Source/GeneratedReleaseMetadata.swift +5 -5
  69. package/ios/Source/hotspot/GlassesHotspotNetwork.swift +563 -0
  70. package/ios/Source/hotspot/HotspotAccessPurpose.swift +5 -0
  71. package/ios/Source/hotspot/HotspotAddressPolicy.swift +50 -0
  72. package/ios/Source/hotspot/HotspotInterfacePolicy.swift +155 -0
  73. package/ios/Source/hotspot/HotspotJoinFailure.swift +27 -0
  74. package/ios/Source/hotspot/HotspotLeaveCompletion.swift +24 -0
  75. package/ios/Source/hotspot/HotspotNetworkOwnership.swift +22 -0
  76. package/ios/Source/hotspot/HotspotReapplyPolicy.swift +26 -0
  77. package/ios/Source/hotspot/LocalNetworkAccessRequest.swift +160 -0
  78. package/ios/Source/hotspot/MacHotspotAssociation.swift +415 -0
  79. package/ios/Source/hotspot/MacHotspotAuthorization.swift +126 -0
  80. package/ios/Source/hotspot/MacHotspotAuthorizationFacts.swift +51 -0
  81. package/ios/Source/hotspot/MacHotspotCleanupContinuation.swift +35 -0
  82. package/ios/Tests/HotspotAddressPolicyTests.swift +30 -0
  83. package/ios/Tests/HotspotInterfacePolicyTests.swift +304 -0
  84. package/ios/Tests/HotspotJoinFailureTests.swift +37 -0
  85. package/ios/Tests/HotspotLeaveCompletionTests.swift +29 -0
  86. package/ios/Tests/HotspotReapplyPolicyTests.swift +18 -0
  87. package/ios/Tests/LocalNetworkAccessRequestTests.swift +237 -0
  88. package/ios/Tests/MacHotspotAssociationTests.swift +521 -0
  89. package/ios/Tests/MacHotspotAuthorizationFactsTests.swift +62 -0
  90. package/ios/Tests/MacHotspotAuthorizationTests.swift +348 -0
  91. package/ios/Tests/MacWifiInvocationTests.swift +76 -0
  92. package/package.json +7 -2
  93. package/plugin/build/withIos.d.ts +4 -0
  94. package/plugin/build/withIos.js +20 -1
  95. package/src/BluetoothSdk.types.ts +27 -5
  96. package/src/_internal.ts +1 -0
  97. package/src/_private/BluetoothSdkModule.ts +20 -10
  98. package/src/_private/MentraLocalNetworkModule.ts +22 -0
  99. package/src/generated/releaseMetadata.ts +5 -5
  100. package/src/glasses-wifi/connection.ts +47 -0
  101. package/src/glasses-wifi/continuous.ts +177 -0
  102. package/src/glasses-wifi/files.ts +32 -0
  103. package/src/glasses-wifi/index.ts +24 -0
  104. package/src/glasses-wifi/link.ts +94 -0
  105. package/src/glasses-wifi/native.ts +193 -0
  106. package/src/glasses-wifi/service.ts +554 -0
  107. package/src/index.ts +34 -10
  108. package/src/ota-transport/index.ts +3 -85
  109. package/src/react/useBluetoothStatus.ts +5 -26
  110. package/src/react/useMentraBluetooth.ts +0 -3
@@ -0,0 +1,521 @@
1
+ @testable import MentraBluetoothSDK
2
+ import XCTest
3
+
4
+ final class MacHotspotAssociationTests: XCTestCase {
5
+ private final class Wifi: MacWifiInterface {
6
+ var ssid: String?
7
+ var scans = 0
8
+ var joins = 0
9
+ var leaves = 0
10
+ var beforeJoin: (() -> Void)?
11
+ var onScan: (() -> Void)?
12
+ var unreadable = false
13
+ var releasesNetwork = true
14
+ var missingScans = 0
15
+ var scanError: Error?
16
+ var associationError: Error?
17
+ var refusedJoins = 0
18
+ /// Known networks; associating saves the network, as CoreWLAN does.
19
+ var known: Set<String> = []
20
+ var forgets: [String] = []
21
+ var forgetError: Error?
22
+ /// What the interface does on its own after leaving: macOS auto-join, or auto-join after a power cycle.
23
+ var afterLeave: String?
24
+ var afterPowerCycle: String?
25
+ var powerCycles = 0
26
+ var pendingAutoJoinPolls = 0
27
+ private var polls = 0
28
+ func name() throws -> String {
29
+ "en1"
30
+ }
31
+
32
+ func currentSSID() throws -> String? {
33
+ if unreadable { throw HotspotError("Location authorization was revoked") }
34
+ if ssid == nil, let afterLeave {
35
+ polls += 1
36
+ if polls > pendingAutoJoinPolls { ssid = afterLeave; self.afterLeave = nil }
37
+ }
38
+ return ssid
39
+ }
40
+
41
+ var scanned: [MacHotspotScan] = []
42
+ func network(ssid _: String, channels: MacHotspotScan) throws -> NSObject? {
43
+ scans += 1; scanned.append(channels); onScan?()
44
+ if let scanError { throw scanError }
45
+ return scans <= missingScans ? nil : NSObject()
46
+ }
47
+
48
+ func associate(network _: NSObject, password _: String) throws {
49
+ joins += 1; beforeJoin?()
50
+ known.insert("glasses")
51
+ if let associationError { ssid = nil; throw associationError }
52
+ if joins <= refusedJoins { throw NSError(domain: "com.apple.wifi.apple80211API.error", code: -3912) }
53
+ ssid = "glasses"
54
+ }
55
+
56
+ func disassociate() {
57
+ leaves += 1
58
+ if releasesNetwork { ssid = nil }
59
+ }
60
+
61
+ func forget(ssid: String) throws {
62
+ forgets.append(ssid)
63
+ if let forgetError { throw forgetError }
64
+ known.remove(ssid)
65
+ }
66
+
67
+ func setPower(_ on: Bool) throws {
68
+ if !on { powerCycles += 1; ssid = nil } else { ssid = afterPowerCycle }
69
+ }
70
+ }
71
+
72
+ private func joinFromHome(_ wifi: Wifi, error: Error? = nil) -> (MacHotspotAssociation, () -> TimeInterval) {
73
+ wifi.ssid = "home"; wifi.known = ["home"]
74
+ wifi.associationError = error
75
+ var now: TimeInterval = 0
76
+ let join = MacHotspotAssociation(deadline: 60, now: { now }, pause: { now += $0 }, makeInterface: { wifi })
77
+ return (join, { now })
78
+ }
79
+
80
+ // MARK: Leaving restores the Mac's own network (lab air-1, 2026-11-11)
81
+
82
+ func testSuccessfulJoinForgetsHotspotAndWaitsForAutoJoinHome() throws {
83
+ let wifi = Wifi()
84
+ let (join, _) = joinFromHome(wifi)
85
+ _ = try join.associate(ssid: "glasses", password: "private")
86
+ wifi.afterLeave = "home"; wifi.pendingAutoJoinPolls = 3
87
+ let report = try join.leave()
88
+ XCTAssertEqual(report, MacHotspotLeaveReport(disassociated: true, forgotHotspot: true, restore: .autoJoined))
89
+ XCTAssertEqual(wifi.known, ["home"])
90
+ XCTAssertEqual(wifi.powerCycles, 0)
91
+ }
92
+
93
+ func testFailedAssociationThatDroppedHomeNetworkComesBackByAutoJoin() throws {
94
+ let wifi = Wifi()
95
+ let (join, _) = joinFromHome(wifi, error: NSError(domain: "com.apple.wifi.apple80211API.error", code: -3905))
96
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private"))
97
+ XCTAssertNil(wifi.ssid, "macOS dropped the home network for the association request")
98
+ wifi.afterLeave = "home"; wifi.pendingAutoJoinPolls = 5
99
+ let report = try join.leave()
100
+ XCTAssertEqual(report, MacHotspotLeaveReport(disassociated: false, forgotHotspot: true, restore: .autoJoined))
101
+ XCTAssertEqual(wifi.known, ["home"])
102
+ XCTAssertEqual(wifi.powerCycles, 0)
103
+ }
104
+
105
+ func testStrandedMacIsPowerCycledSoAutoJoinStartsOver() throws {
106
+ let wifi = Wifi()
107
+ let (join, now) = joinFromHome(wifi, error: NSError(domain: "com.apple.wifi.apple80211API.error", code: -3905))
108
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private"))
109
+ wifi.afterPowerCycle = "home"
110
+ let before = now()
111
+ let report = try join.leave()
112
+ XCTAssertEqual(report.restore, .powerCycled)
113
+ XCTAssertEqual(wifi.powerCycles, 1)
114
+ XCTAssertEqual(wifi.ssid, "home")
115
+ XCTAssertEqual(wifi.known, ["home"])
116
+ XCTAssertEqual(now() - before, MacHotspotAssociation.autoJoinWait + 1)
117
+ }
118
+
119
+ func testTimedOutJoinCancelledDuringAssociationStillRestoresHomeNetwork() throws {
120
+ let wifi = Wifi()
121
+ let (join, _) = joinFromHome(wifi)
122
+ // The join deadline cancels while macOS is mid-association and has already dropped home.
123
+ wifi.beforeJoin = { wifi.ssid = nil; join.cancel() }
124
+ wifi.associationError = NSError(domain: "com.apple.wifi.apple80211API.error", code: -3905)
125
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private")) {
126
+ XCTAssertTrue($0 is CancellationError)
127
+ }
128
+ wifi.afterPowerCycle = "home"
129
+ let report = try join.leave()
130
+ XCTAssertEqual(report.restore, .powerCycled, "cancel() must not cut cleanup's pacing short")
131
+ XCTAssertEqual(wifi.known, ["home"])
132
+ }
133
+
134
+ func testRestoreGivesUpWithoutDisconnectingAnything() throws {
135
+ let wifi = Wifi()
136
+ let (join, _) = joinFromHome(wifi, error: NSError(domain: "com.apple.wifi", code: 9))
137
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private"))
138
+ let report = try join.leave()
139
+ XCTAssertEqual(report.restore, .notRestored)
140
+ XCTAssertEqual(wifi.powerCycles, 1)
141
+ XCTAssertEqual(wifi.leaves, 0)
142
+ }
143
+
144
+ func testMacOnAnotherNetworkAfterLeaveIsLeftAlone() throws {
145
+ let wifi = Wifi()
146
+ let (join, _) = joinFromHome(wifi)
147
+ _ = try join.associate(ssid: "glasses", password: "private")
148
+ wifi.afterLeave = "phone-hotspot"
149
+ let report = try join.leave()
150
+ XCTAssertEqual(report.restore, .otherNetwork)
151
+ XCTAssertEqual(wifi.ssid, "phone-hotspot")
152
+ XCTAssertEqual(wifi.powerCycles, 0)
153
+ }
154
+
155
+ func testForgetFailureIsReportedButHomeIsStillRestored() throws {
156
+ let wifi = Wifi(); wifi.forgetError = HotspotError("denied")
157
+ let (join, _) = joinFromHome(wifi)
158
+ _ = try join.associate(ssid: "glasses", password: "private")
159
+ wifi.afterLeave = "home"
160
+ let report = try join.leave()
161
+ XCTAssertFalse(report.forgotHotspot)
162
+ XCTAssertEqual(report.restore, .autoJoined)
163
+ XCTAssertTrue(report.summary.contains("forget: denied"))
164
+ XCTAssertFalse(report.summary.contains("home"), "The log line never names a network")
165
+ }
166
+
167
+ func testMacWithoutPreviousWifiIsNotPowerCycled() throws {
168
+ let wifi = Wifi()
169
+ let join = MacHotspotAssociation(pause: { _ in }, makeInterface: { wifi })
170
+ XCTAssertThrowsError(try { wifi.associationError = NSError(domain: "x", code: 1); return try join.associate(ssid: "glasses", password: "private") }())
171
+ let report = try join.leave()
172
+ XCTAssertEqual(report.restore, .noPreviousNetwork)
173
+ XCTAssertEqual(wifi.powerCycles, 0)
174
+ XCTAssertEqual(wifi.forgets, ["glasses"])
175
+ }
176
+
177
+ func testJoinThatNeverAssociatedLeavesWifiUntouched() throws {
178
+ let wifi = Wifi(); wifi.missingScans = .max
179
+ let (join, _) = joinFromHome(wifi)
180
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private"))
181
+ let report = try join.leave()
182
+ XCTAssertEqual(report, MacHotspotLeaveReport())
183
+ XCTAssertEqual(wifi.forgets, [])
184
+ XCTAssertEqual(wifi.ssid, "home")
185
+ }
186
+
187
+ func testAutomaticJoinUsesActualWifiInterfaceAndCleansOwnedAssociation() throws {
188
+ let wifi = Wifi()
189
+ let join = MacHotspotAssociation(makeInterface: { wifi })
190
+ XCTAssertEqual(try join.associate(ssid: "glasses", password: "private"), "en1")
191
+ XCTAssertEqual(wifi.scans, 1)
192
+ XCTAssertEqual(wifi.joins, 1)
193
+ try join.leave()
194
+ XCTAssertEqual(wifi.leaves, 1)
195
+ }
196
+
197
+ func testExistingUserAssociationIsReusedWithoutOwningItsRelease() throws {
198
+ let wifi = Wifi(); wifi.ssid = "glasses"
199
+ let join = MacHotspotAssociation(makeInterface: { wifi })
200
+ _ = try join.associate(ssid: "glasses", password: "private")
201
+ try join.leave()
202
+ XCTAssertEqual(wifi.joins, 0)
203
+ XCTAssertEqual(wifi.leaves, 0)
204
+ }
205
+
206
+ func testUserReplacementConnectionIsNotDisconnected() throws {
207
+ let wifi = Wifi()
208
+ let join = MacHotspotAssociation(makeInterface: { wifi })
209
+ _ = try join.associate(ssid: "glasses", password: "private")
210
+ wifi.ssid = "user-network"
211
+ try join.leave()
212
+ XCTAssertEqual(wifi.leaves, 0)
213
+ XCTAssertEqual(wifi.ssid, "user-network")
214
+ }
215
+
216
+ func testAlreadyDisconnectedNetworkDoesNotBlockRelease() throws {
217
+ let wifi = Wifi()
218
+ let join = MacHotspotAssociation(makeInterface: { wifi })
219
+ _ = try join.associate(ssid: "glasses", password: "private")
220
+ wifi.ssid = nil
221
+ try join.leave()
222
+ XCTAssertEqual(wifi.leaves, 0)
223
+ }
224
+
225
+ func testUnreadableCurrentNetworkRetainsCleanupForRetry() throws {
226
+ let wifi = Wifi()
227
+ let join = MacHotspotAssociation(makeInterface: { wifi })
228
+ _ = try join.associate(ssid: "glasses", password: "private")
229
+ wifi.unreadable = true
230
+ XCTAssertThrowsError(try join.leave())
231
+ XCTAssertEqual(wifi.leaves, 0)
232
+ wifi.unreadable = false
233
+ try join.leave()
234
+ XCTAssertEqual(wifi.leaves, 1)
235
+ }
236
+
237
+ func testUnfinishedDisassociationRejectsAndCanBeRetried() throws {
238
+ let wifi = Wifi()
239
+ let join = MacHotspotAssociation(makeInterface: { wifi })
240
+ _ = try join.associate(ssid: "glasses", password: "private")
241
+ wifi.releasesNetwork = false
242
+ XCTAssertThrowsError(try join.leave())
243
+ wifi.releasesNetwork = true
244
+ try join.leave()
245
+ XCTAssertEqual(wifi.leaves, 2)
246
+ }
247
+
248
+ func testCancelDuringScanPreventsLaterAssociation() {
249
+ let wifi = Wifi()
250
+ let join = MacHotspotAssociation(makeInterface: { wifi })
251
+ wifi.onScan = { join.cancel() }
252
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private")) {
253
+ XCTAssertTrue($0 is CancellationError)
254
+ }
255
+ XCTAssertEqual(wifi.joins, 0)
256
+ }
257
+
258
+ func testEmptyScansRescanWhenMacOSAllowsTheNextLiveScan() throws {
259
+ let wifi = Wifi(); wifi.missingScans = 2
260
+ var now: TimeInterval = 0
261
+ let join = MacHotspotAssociation(deadline: 60, now: { now }, pause: { now += $0 }, makeInterface: { wifi })
262
+ XCTAssertEqual(try join.associate(ssid: "glasses", password: "private"), "en1")
263
+ XCTAssertEqual(wifi.scans, 3)
264
+ XCTAssertEqual(wifi.joins, 1)
265
+ XCTAssertEqual(now, 2 * MacHotspotAssociation.scanInterval)
266
+ try join.leave()
267
+ XCTAssertEqual(wifi.leaves, 1)
268
+ }
269
+
270
+ func testJoinWindowOutlastsTwoMissedMacScans() throws {
271
+ // A live scan takes about 22 s while the Mac is on another network; two missed the AP (2026-11-17).
272
+ let wifi = Wifi(); wifi.missingScans = 2
273
+ var now: TimeInterval = 0
274
+ wifi.onScan = { now += 22 }
275
+ let join = MacHotspotAssociation(deadline: MacHotspotAssociation.joinWindow, now: { now }, pause: { now += $0 }, makeInterface: { wifi })
276
+ XCTAssertEqual(try join.associate(ssid: "glasses", password: "private"), "en1")
277
+ XCTAssertEqual(wifi.scans, 3)
278
+ XCTAssertGreaterThan(now, 60)
279
+ }
280
+
281
+ // MARK: Scan selection
282
+
283
+ func testReportedChannelIsScannedAloneThenEveryChannelBelow6GHz() throws {
284
+ let wifi = Wifi(); wifi.missingScans = 1
285
+ var now: TimeInterval = 0
286
+ let join = MacHotspotAssociation(deadline: 60, now: { now }, pause: { now += $0 }, makeInterface: { wifi })
287
+ XCTAssertEqual(try join.associate(ssid: "glasses", password: "private", frequency: 5180), "en1")
288
+ XCTAssertEqual(wifi.scanned, [.channel(band: MacHotspotScan.band5, number: 36), .below6GHz])
289
+ }
290
+
291
+ func testWithoutAReportedChannelThe2GHzBandIsScannedFirst() throws {
292
+ let wifi = Wifi(); wifi.missingScans = 2
293
+ var now: TimeInterval = 0
294
+ let join = MacHotspotAssociation(deadline: 60, now: { now }, pause: { now += $0 }, makeInterface: { wifi })
295
+ XCTAssertEqual(try join.associate(ssid: "glasses", password: "private"), "en1")
296
+ XCTAssertEqual(wifi.scanned, [.band2GHz, .below6GHz, .below6GHz])
297
+ }
298
+
299
+ func testRescanAfterRefusedJoinCoversEveryChannelBelow6GHz() throws {
300
+ let wifi = Wifi(); wifi.refusedJoins = 1
301
+ let (join, _) = joinFromHome(wifi)
302
+ XCTAssertEqual(try join.associate(ssid: "glasses", password: "private", frequency: 2437), "en1")
303
+ XCTAssertEqual(wifi.scanned, [.channel(band: MacHotspotScan.band2, number: 6), .below6GHz])
304
+ }
305
+
306
+ func testFrequenciesMapToTheirChannels() {
307
+ XCTAssertEqual(MacHotspotScan.channel(frequency: 2412), .channel(band: MacHotspotScan.band2, number: 1))
308
+ XCTAssertEqual(MacHotspotScan.channel(frequency: 2484), .channel(band: MacHotspotScan.band2, number: 14))
309
+ XCTAssertEqual(MacHotspotScan.channel(frequency: 5745), .channel(band: MacHotspotScan.band5, number: 149))
310
+ // 6 GHz, which these glasses cannot use for their AP, and nonsense values fall back to the 2.4 GHz band.
311
+ XCTAssertNil(MacHotspotScan.channel(frequency: 5955))
312
+ XCTAssertNil(MacHotspotScan.channel(frequency: 0))
313
+ XCTAssertEqual(MacHotspotScan.next(after: 0, frequency: 5955), .band2GHz)
314
+ }
315
+
316
+ func testScanSetsCoverOnlyTheirBands() {
317
+ XCTAssertTrue(MacHotspotScan.band2GHz.includes(band: MacHotspotScan.band2, number: 1))
318
+ XCTAssertFalse(MacHotspotScan.band2GHz.includes(band: MacHotspotScan.band5, number: 36))
319
+ XCTAssertTrue(MacHotspotScan.below6GHz.includes(band: MacHotspotScan.band5, number: 36))
320
+ XCTAssertFalse(MacHotspotScan.below6GHz.includes(band: 3, number: 37))
321
+ XCTAssertFalse(MacHotspotScan.channel(band: MacHotspotScan.band5, number: 36).includes(band: MacHotspotScan.band5, number: 40))
322
+ }
323
+
324
+ func testAbsentHotspotStopsAtOriginalDeadlineWithoutAssociating() {
325
+ let wifi = Wifi(); wifi.missingScans = .max
326
+ var now: TimeInterval = 3 // Authorization already used three of the five seconds.
327
+ let join = MacHotspotAssociation(deadline: 5, now: { now }, pause: { now += $0 }, makeInterface: { wifi })
328
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private")) {
329
+ XCTAssertTrue($0.localizedDescription.contains("hotspot join timed out"))
330
+ }
331
+ XCTAssertEqual(now, 5)
332
+ XCTAssertEqual(wifi.scans, 1)
333
+ XCTAssertEqual(wifi.joins, 0)
334
+ }
335
+
336
+ func testExactAutomaticAssociationDuringScanIsReusedWithoutOwningRelease() throws {
337
+ let wifi = Wifi(); wifi.missingScans = .max
338
+ wifi.onScan = { wifi.ssid = "glasses" }
339
+ let join = MacHotspotAssociation(makeInterface: { wifi })
340
+ XCTAssertEqual(try join.associate(ssid: "glasses", password: "private"), "en1")
341
+ XCTAssertEqual(wifi.scans, 1)
342
+ XCTAssertEqual(wifi.joins, 0)
343
+ try join.leave()
344
+ XCTAssertEqual(wifi.leaves, 0)
345
+ }
346
+
347
+ func testExactAutomaticAssociationBetweenScansNeedsNoFurtherScanOrJoin() throws {
348
+ let wifi = Wifi(); wifi.missingScans = .max
349
+ var now: TimeInterval = 0
350
+ let join = MacHotspotAssociation(deadline: 60, now: { now }, pause: { now += $0; wifi.ssid = "glasses" }, makeInterface: { wifi })
351
+ XCTAssertEqual(try join.associate(ssid: "glasses", password: "private"), "en1")
352
+ XCTAssertEqual(wifi.scans, 1)
353
+ XCTAssertEqual(wifi.joins, 0)
354
+ try join.leave()
355
+ XCTAssertEqual(wifi.leaves, 0)
356
+ }
357
+
358
+ func testNativeScanFailureIsTerminalWithoutRetry() {
359
+ let wifi = Wifi(); wifi.scanError = NSError(domain: "com.apple.wifi", code: 7)
360
+ var pauses = 0
361
+ let join = MacHotspotAssociation(pause: { _ in pauses += 1 }, makeInterface: { wifi })
362
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private")) {
363
+ XCTAssertEqual(($0 as NSError).code, 7)
364
+ }
365
+ XCTAssertEqual(wifi.scans, 1)
366
+ XCTAssertEqual(pauses, 0)
367
+ XCTAssertEqual(wifi.joins, 0)
368
+ }
369
+
370
+ func testPermissionLossAfterScanIsTerminalWithoutRetry() {
371
+ let wifi = Wifi(); wifi.missingScans = .max
372
+ wifi.onScan = { wifi.unreadable = true }
373
+ var pauses = 0
374
+ let join = MacHotspotAssociation(pause: { _ in pauses += 1 }, makeInterface: { wifi })
375
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private"))
376
+ XCTAssertEqual(wifi.scans, 1)
377
+ XCTAssertEqual(pauses, 0)
378
+ XCTAssertEqual(wifi.joins, 0)
379
+ }
380
+
381
+ func testNetworkReturnedAfterDeadlineCannotStartAssociation() {
382
+ let wifi = Wifi()
383
+ var now: TimeInterval = 0
384
+ wifi.onScan = { now = 5 }
385
+ let join = MacHotspotAssociation(deadline: 5, now: { now }, makeInterface: { wifi })
386
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private"))
387
+ XCTAssertEqual(wifi.scans, 1)
388
+ XCTAssertEqual(wifi.joins, 0)
389
+ }
390
+
391
+ func testCancelBetweenEmptyScansPreventsAnotherScanOrAssociation() {
392
+ let wifi = Wifi(); wifi.missingScans = .max
393
+ var join: MacHotspotAssociation!
394
+ join = MacHotspotAssociation(pause: { _ in join.cancel() }, makeInterface: { wifi })
395
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private")) {
396
+ XCTAssertTrue($0 is CancellationError)
397
+ }
398
+ XCTAssertEqual(wifi.scans, 1)
399
+ XCTAssertEqual(wifi.joins, 0)
400
+ }
401
+
402
+ func testRefusedAssociationRescansAndJoinsAgain() throws {
403
+ let wifi = Wifi(); wifi.refusedJoins = 1
404
+ var now: TimeInterval = 0
405
+ let join = MacHotspotAssociation(deadline: 60, now: { now }, pause: { now += $0 }, makeInterface: { wifi })
406
+ XCTAssertEqual(try join.associate(ssid: "glasses", password: "private"), "en1")
407
+ XCTAssertEqual(wifi.scans, 2)
408
+ XCTAssertEqual(wifi.joins, 2)
409
+ XCTAssertEqual(now, MacHotspotAssociation.associationRetryDelay)
410
+ try join.leave()
411
+ XCTAssertEqual(wifi.leaves, 1)
412
+ }
413
+
414
+ func testAssociationFailureEndsAfterItsLastAttempt() {
415
+ let wifi = Wifi(); wifi.associationError = NSError(domain: "com.apple.wifi", code: 9)
416
+ var now: TimeInterval = 0
417
+ let join = MacHotspotAssociation(deadline: 60, now: { now }, pause: { now += $0 }, makeInterface: { wifi })
418
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private")) {
419
+ XCTAssertEqual(($0 as NSError).code, 9)
420
+ }
421
+ XCTAssertEqual(wifi.scans, MacHotspotAssociation.associationAttempts)
422
+ XCTAssertEqual(wifi.joins, MacHotspotAssociation.associationAttempts)
423
+ }
424
+
425
+ func testAssociationFailureIsNotRetriedPastTheDeadline() {
426
+ let wifi = Wifi(); wifi.associationError = NSError(domain: "com.apple.wifi", code: 9)
427
+ var now: TimeInterval = 0
428
+ wifi.beforeJoin = { now = 59 }
429
+ let join = MacHotspotAssociation(deadline: 60, now: { now }, pause: { now += $0 }, makeInterface: { wifi })
430
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private")) {
431
+ XCTAssertEqual(($0 as NSError).code, 9)
432
+ }
433
+ XCTAssertEqual(wifi.joins, 1)
434
+ }
435
+
436
+ func testPermissionLossAfterRefusedAssociationIsTerminal() {
437
+ let wifi = Wifi(); wifi.refusedJoins = 1
438
+ wifi.beforeJoin = { wifi.unreadable = true }
439
+ var now: TimeInterval = 0
440
+ let join = MacHotspotAssociation(deadline: 60, now: { now }, pause: { now += $0 }, makeInterface: { wifi })
441
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private")) {
442
+ XCTAssertTrue($0.localizedDescription.contains("Location"))
443
+ }
444
+ XCTAssertEqual(wifi.joins, 1)
445
+ }
446
+
447
+ func testCancelDuringRefusedAssociationPreventsAnotherJoin() {
448
+ let wifi = Wifi(); wifi.refusedJoins = 1
449
+ var join: MacHotspotAssociation!
450
+ wifi.beforeJoin = { join.cancel() }
451
+ join = MacHotspotAssociation(makeInterface: { wifi })
452
+ XCTAssertThrowsError(try join.associate(ssid: "glasses", password: "private")) {
453
+ XCTAssertTrue($0 is CancellationError)
454
+ }
455
+ XCTAssertEqual(wifi.joins, 1)
456
+ }
457
+
458
+ func testCancellationWakesEmptyScanPacingAndSettlesBeforeCustodyRelease() throws {
459
+ let wifi = Wifi(); wifi.missingScans = .max
460
+ let scanned = expectation(description: "first empty scan completed")
461
+ let stopped = expectation(description: "cancelled discovery settled")
462
+ wifi.onScan = { if wifi.scans == 1 { scanned.fulfill() } }
463
+ let join = MacHotspotAssociation(makeInterface: { wifi })
464
+ let ownership = HotspotNetworkOwnership()
465
+ let token = try XCTUnwrap(ownership.acquire())
466
+ DispatchQueue(label: "test.mac-wifi-empty").async {
467
+ do { _ = try join.associate(ssid: "glasses", password: "private"); XCTFail("Cancelled discovery succeeded") }
468
+ catch { XCTAssertTrue(error is CancellationError) }
469
+ do { try join.leave() } catch { XCTFail("Cleanup failed: \(error)") }
470
+ ownership.release(token)
471
+ stopped.fulfill()
472
+ }
473
+ wait(for: [scanned], timeout: 1)
474
+ XCTAssertNil(ownership.acquire())
475
+ join.cancel()
476
+ wait(for: [stopped], timeout: 0.5)
477
+ XCTAssertEqual(wifi.scans, 1)
478
+ XCTAssertEqual(wifi.joins, 0)
479
+ XCTAssertEqual(wifi.leaves, 0)
480
+ XCTAssertNotNil(ownership.acquire())
481
+ }
482
+
483
+ func testCancelDuringBlockingAssociationWaitsForLateOwnedCleanup() throws {
484
+ let wifi = Wifi()
485
+ let join = MacHotspotAssociation(makeInterface: { wifi })
486
+ let started = expectation(description: "native association started")
487
+ let cleaned = expectation(description: "late association cleaned up")
488
+ let releaseNativeCall = DispatchSemaphore(value: 0)
489
+ let ownership = HotspotNetworkOwnership()
490
+ let token = try XCTUnwrap(ownership.acquire())
491
+ wifi.beforeJoin = {
492
+ started.fulfill()
493
+ _ = releaseNativeCall.wait(timeout: .now() + 2)
494
+ }
495
+ DispatchQueue(label: "test.mac-wifi").async {
496
+ do { _ = try join.associate(ssid: "glasses", password: "private"); XCTFail("Cancelled association succeeded") }
497
+ catch { XCTAssertTrue(error is CancellationError) }
498
+ do { try join.leave() } catch { XCTFail("Cleanup failed: \(error)") }
499
+ ownership.release(token)
500
+ cleaned.fulfill()
501
+ }
502
+ wait(for: [started], timeout: 1)
503
+ join.cancel()
504
+ XCTAssertNil(ownership.acquire(), "A timed-out writer must retain custody")
505
+ releaseNativeCall.signal()
506
+ wait(for: [cleaned], timeout: 1)
507
+ XCTAssertEqual(wifi.leaves, 1)
508
+ XCTAssertNotNil(ownership.acquire())
509
+ }
510
+
511
+ func testStaleReleaseCannotReleaseAnotherOwner() throws {
512
+ let ownership = HotspotNetworkOwnership()
513
+ let old = try XCTUnwrap(ownership.acquire())
514
+ ownership.release(old)
515
+ let current = try XCTUnwrap(ownership.acquire())
516
+ ownership.release(old)
517
+ XCTAssertNil(ownership.acquire())
518
+ ownership.release(current)
519
+ XCTAssertNotNil(ownership.acquire())
520
+ }
521
+ }
@@ -0,0 +1,62 @@
1
+ import CoreLocation
2
+ @testable import MentraBluetoothSDK
3
+ import XCTest
4
+
5
+ final class MacHotspotAuthorizationFactsTests: XCTestCase {
6
+ func testStaticRejectionRetainsMeasuredInstanceStatusWithoutInventingCallback() {
7
+ let facts = MacHotspotAuthorizationFacts()
8
+ facts.observeInstance(raw: CLAuthorizationStatus.authorizedAlways.rawValue, getter: .instanceInitial, foreground: .active)
9
+ let failure = HotspotJoinFailure(facts.failure("Location access is required to verify the current Mac Wi-Fi network",
10
+ raw: CLAuthorizationStatus.notDetermined.rawValue, getter: .staticSSID, foreground: .unknown))
11
+ XCTAssertEqual(failure.code, "ERR_HOTSPOT_JOIN")
12
+ XCTAssertTrue(failure.message.contains("raw=0 getter=static-ssid foreground=unknown"))
13
+ XCTAssertTrue(failure.message.contains("instanceRaw=3 instanceGetter=instance-initial instanceForeground=active"))
14
+ XCTAssertTrue(failure.message.contains("initialCallbackObserved=false"))
15
+ XCTAssertTrue(failure.message.contains("pid=\(ProcessInfo.processInfo.processIdentifier)"))
16
+ XCTAssertLessThanOrEqual(failure.message.utf8.count, 512)
17
+ }
18
+
19
+ func testActualCallbackAndLaterDeniedStatusRemainDistinct() {
20
+ let facts = MacHotspotAuthorizationFacts()
21
+ facts.observeInstance(raw: CLAuthorizationStatus.notDetermined.rawValue, getter: .instanceInitial, foreground: .inactive)
22
+ facts.observeInstance(raw: CLAuthorizationStatus.authorizedAlways.rawValue, getter: .instanceCallback, foreground: .active)
23
+ let failure = facts.failure("Location access is required to verify the current Mac Wi-Fi network",
24
+ raw: CLAuthorizationStatus.denied.rawValue, getter: .staticSSID, foreground: .unknown)
25
+ XCTAssertTrue(failure.message.contains("raw=2 getter=static-ssid"))
26
+ XCTAssertTrue(failure.message.contains("instanceRaw=3 instanceGetter=instance-callback"))
27
+ XCTAssertTrue(failure.message.contains("initialCallbackObserved=true"))
28
+ XCTAssertFalse(MacHotspotAuthorization.isAuthorized(.denied))
29
+ XCTAssertFalse(MacHotspotAuthorization.isAuthorized(.restricted))
30
+ XCTAssertFalse(MacHotspotAuthorization.isAuthorized(.notDetermined))
31
+ }
32
+
33
+ func testAnIndependentSSIDReadHasNoInventedPriorAuthorization() {
34
+ let facts = MacHotspotAuthorizationFacts()
35
+ let failure = facts.failure("Location access is required to verify the current Mac Wi-Fi network",
36
+ raw: CLAuthorizationStatus.restricted.rawValue, getter: .staticSSID, foreground: .unknown)
37
+ XCTAssertTrue(failure.message.contains("instanceRaw=unknown instanceGetter=unknown instanceForeground=unknown"))
38
+ XCTAssertTrue(failure.message.contains("initialCallbackObserved=false"))
39
+ }
40
+
41
+ func testFailureFactsCannotOverrideCancellationOrIncludeNativeUserInfo() {
42
+ let facts = MacHotspotAuthorizationFacts()
43
+ facts.observeInstance(raw: 3, getter: .instanceCallback, foreground: .active)
44
+ let cancelled = HotspotJoinFailure(CancellationError())
45
+ XCTAssertEqual(cancelled.code, "ERR_HOTSPOT_CANCELLED")
46
+ XCTAssertEqual(cancelled.message, "Hotspot join cancelled")
47
+ XCTAssertFalse(cancelled.message.contains("location raw="))
48
+ let native = HotspotJoinFailure(NSError(domain: "NSPOSIXErrorDomain", code: 61,
49
+ userInfo: ["ssid": "secret-network", "password": "secret-password"]))
50
+ XCTAssertFalse(native.message.contains("secret"))
51
+ XCTAssertFalse(native.message.contains("location raw="))
52
+ }
53
+
54
+ func testForegroundReadOffOwningThreadStaysUnknown() {
55
+ let observed = expectation(description: "worker observation")
56
+ DispatchQueue.global().async {
57
+ XCTAssertEqual(MacHotspotAuthorizationFacts.foreground(), .unknown)
58
+ observed.fulfill()
59
+ }
60
+ wait(for: [observed], timeout: 2)
61
+ }
62
+ }