@mentra/bluetooth-sdk 3.5.0-dev.1258 → 3.5.0-dev.1261

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.
@@ -88,6 +88,28 @@ final class IncidentLogDispatchTests: XCTestCase {
88
88
  }
89
89
  }
90
90
 
91
+ func testCoreUrlStaysOnThePhone() throws {
92
+ try withToken("synthetic") {
93
+ let live = IncidentLogTransport()
94
+ try live.sendIncidentId(incident, apiBaseUrl: apiBase)
95
+ XCTAssertTrue(sentMessages(live).joined().contains("upload_incident_logs"))
96
+ XCTAssertFalse(sentMessages(live).joined().contains("apiBaseUrl"))
97
+ }
98
+ }
99
+
100
+ func testNoCoreUrlRejectsInsteadOfSendingLogsAnywhereElse() throws {
101
+ try withToken("synthetic") {
102
+ let live = IncidentLogTransport()
103
+ for base in [nil, " "] as [String?] {
104
+ XCTAssertThrowsError(try live.sendIncidentId(incident, apiBaseUrl: base)) { error in
105
+ XCTAssertEqual((error as? BluetoothSdkError)?.code, "core_url_unavailable")
106
+ }
107
+ }
108
+ XCTAssertTrue(live.writes.isEmpty)
109
+ XCTAssertTrue(live.bleIncidentLogRelays.isEmpty)
110
+ }
111
+ }
112
+
91
113
  func testCurrentTokenPrecedesUploadAndNeverAppearsInLogs() throws {
92
114
  let token = "synthetic-\(UUID().uuidString)"
93
115
  try withToken(token) {
@@ -147,20 +169,216 @@ final class IncidentLogDispatchTests: XCTestCase {
147
169
 
148
170
  let logcat = "Lrep_01m4hbvt88d"
149
171
  try await receive(live, name, "owner")
150
- XCTAssertEqual(CloudStub.requests, ["/api/client/reports/\(incident)/artifacts owner"])
172
+ XCTAssertTrue(CloudStub.requests.isEmpty)
151
173
  let confirmation = try XCTUnwrap(sentMessages(live).last { $0.contains("transfer_complete") })
152
174
  XCTAssertTrue(confirmation.contains(name) && confirmation.contains("\"success\":true"))
153
175
  XCTAssertThrowsError(try live.sendIncidentId(collidingIncident, apiBaseUrl: cloudBase)) { error in
154
176
  XCTAssertEqual((error as? BluetoothSdkError)?.code, "incident_relay_busy")
155
177
  }
156
178
  try await receive(live, logcat, "owner-logcat")
179
+ try await waitFor { CloudStub.requests.count == 2 }
180
+ XCTAssertEqual(CloudStub.requests, [
181
+ "/api/client/reports/\(incident)/artifacts owner-logcat",
182
+ "/api/client/reports/\(incident)/artifacts owner",
183
+ ])
157
184
 
158
185
  try live.sendIncidentId(collidingIncident, apiBaseUrl: cloudBase)
159
186
  try await receive(live, name, "second")
187
+ XCTAssertEqual(CloudStub.requests.count, 2)
188
+ try await receive(live, logcat, "second-logcat")
189
+ try await waitFor { CloudStub.requests.count == 4 }
190
+ XCTAssertEqual(CloudStub.requests[2], "/api/client/reports/\(collidingIncident)/artifacts second-logcat")
160
191
  XCTAssertEqual(CloudStub.requests.last, "/api/client/reports/\(collidingIncident)/artifacts second")
161
192
  }
162
193
  }
163
194
 
195
+ func testSavesTheBesLogAndUploadsTheMtkLogBeforeIt() async throws {
196
+ try await withCloud(status: 200) {
197
+ let live = IncidentLogTransport()
198
+ let name = "Brep_01m4hbvt88d"
199
+ let logcat = "Lrep_01m4hbvt88d"
200
+ try live.sendIncidentId(incident, apiBaseUrl: cloudBase)
201
+ let bes = "{\"retryKey\":\"bes-crash\",\"entries\":[{\"source\":\"BES\"}]}"
202
+ try await receive(live, name, bes)
203
+ XCTAssertTrue(CloudStub.requests.isEmpty)
204
+ XCTAssertEqual(live.bleIncidentLogRelays[name]?.incidentId, incident)
205
+
206
+ try await receive(live, logcat, "{\"entries\":[{\"source\":\"MTK\"}]}")
207
+ try await waitFor { CloudStub.requests.count == 2 }
208
+ XCTAssertEqual(
209
+ CloudStub.requests,
210
+ [
211
+ "/api/client/reports/\(incident)/artifacts {\"entries\":[{\"source\":\"MTK\"}]}",
212
+ "/api/client/reports/\(incident)/artifacts \(bes)",
213
+ ]
214
+ )
215
+ XCTAssertNil(live.bleIncidentLogRelays[name])
216
+ XCTAssertNil(live.bleIncidentLogRelays[logcat])
217
+ }
218
+ }
219
+
220
+ func testHeldBesLogIsDurablyPersistedAndResumedAfterProcessDeath() async throws {
221
+ clearHeldBesDir()
222
+ try await withCloud(status: 200) {
223
+ let live = IncidentLogTransport()
224
+ let name = "Brep_01m4hbvt88d"
225
+ try live.sendIncidentId(incident, apiBaseUrl: cloudBase)
226
+ let bes = "{\"source\":\"BES\"}"
227
+ try await receive(live, name, bes)
228
+ // Acknowledged to the glasses but not uploaded yet: the bytes must already be on disk.
229
+ XCTAssertTrue(CloudStub.requests.isEmpty)
230
+ XCTAssertTrue(besHeldFileExists(incident))
231
+
232
+ // Process death before the MTK log arrives: a fresh instance owns no relay and the
233
+ // glasses will not resend, so it must finish the upload from the durable copy.
234
+ let revived = IncidentLogTransport()
235
+ revived.resumePersistedBesUploads()
236
+ try await waitFor {
237
+ CloudStub.requests.contains("/api/client/reports/\(self.incident)/artifacts \(bes)")
238
+ }
239
+ try await waitFor { !self.besHeldFileExists(self.incident) }
240
+ }
241
+ }
242
+
243
+ func testSavedBesLogWaitsForItsOwnAccountAndCore() async throws {
244
+ clearHeldBesDir()
245
+ defer { clearHeldBesDir() }
246
+ try await withCloud(status: 200) {
247
+ let store = DeviceStore.shared.store
248
+ store.set("bluetooth", "core_token", jwt(iss: "https://core-a", sub: "user-a"))
249
+ let live = IncidentLogTransport()
250
+ let bes = "{\"source\":\"BES\"}"
251
+ try live.sendIncidentId(incident, apiBaseUrl: cloudBase)
252
+ try await receive(live, "Brep_01m4hbvt88d", bes)
253
+
254
+ let revived = IncidentLogTransport()
255
+ revived.besRetrySeconds = 0.05
256
+ store.set("bluetooth", "core_token", jwt(iss: "https://core-b", sub: "user-a"))
257
+ revived.resumePersistedBesUploads()
258
+ try await Task.sleep(nanoseconds: 200_000_000)
259
+ store.set("bluetooth", "core_token", jwt(iss: "https://core-a", sub: "user-b"))
260
+ try await Task.sleep(nanoseconds: 200_000_000)
261
+ XCTAssertTrue(CloudStub.requests.isEmpty)
262
+ XCTAssertTrue(besHeldFileExists(incident))
263
+
264
+ store.set("bluetooth", "core_token", jwt(iss: "https://core-a", sub: "user-a"))
265
+ try await waitFor { !self.besHeldFileExists(self.incident) }
266
+ XCTAssertEqual(CloudStub.requests.count, 1)
267
+ }
268
+ }
269
+
270
+ private func jwt(iss: String, sub: String) -> String {
271
+ let claims = Data("{\"iss\":\"\(iss)\",\"sub\":\"\(sub)\"}".utf8).base64EncodedString()
272
+ .replacingOccurrences(of: "+", with: "-").replacingOccurrences(of: "/", with: "_")
273
+ .replacingOccurrences(of: "=", with: "")
274
+ return "h.\(claims).s"
275
+ }
276
+
277
+ func testMtkTransferCutOffPartwayStopsHoldingTheBesLog() async throws {
278
+ clearHeldBesDir()
279
+ defer { clearHeldBesDir() }
280
+ try await withCloud(status: 200) {
281
+ let live = IncidentLogTransport()
282
+ live.besHoldSeconds = 0.05
283
+ live.besMtkWaitMaxSeconds = 0.3
284
+ let bes = "{\"source\":\"BES\"}"
285
+ try live.sendIncidentId(incident, apiBaseUrl: cloudBase)
286
+ // Part of the MTK file arrived, then the link dropped: nothing will ever finish it.
287
+ let mtk = try XCTUnwrap(live.bleIncidentLogRelays["Lrep_01m4hbvt88d"])
288
+ mtk.session = MentraLiveFileTransferSession(
289
+ fileName: "Lrep_01m4hbvt88d", fileSize: 100_000, announcedPackets: 1000
290
+ )
291
+ try await receive(live, "Brep_01m4hbvt88d", bes)
292
+
293
+ try await Task.sleep(nanoseconds: 150_000_000)
294
+ XCTAssertTrue(CloudStub.requests.isEmpty)
295
+
296
+ try await waitFor { !self.besHeldFileExists(self.incident) }
297
+ XCTAssertEqual(CloudStub.requests, ["/api/client/reports/\(incident)/artifacts \(bes)"])
298
+ }
299
+ }
300
+
301
+ func testBesLogThatCannotBeSavedIsRefusedSoTheGlassesKeepTheirCopy() async throws {
302
+ clearHeldBesDir()
303
+ defer { clearHeldBesDir() }
304
+ // A file where the directory belongs makes every save fail.
305
+ let dir = try XCTUnwrap(besHeldDirURL())
306
+ try FileManager.default.createDirectory(at: dir.deletingLastPathComponent(), withIntermediateDirectories: true)
307
+ try Data("not a directory".utf8).write(to: dir)
308
+ try await withCloud(status: 200) {
309
+ let live = IncidentLogTransport()
310
+ let name = "Brep_01m4hbvt88d"
311
+ try live.sendIncidentId(incident, apiBaseUrl: cloudBase)
312
+ try await receive(live, name, "{\"source\":\"BES\"}")
313
+ let confirmations = sentMessages(live).filter { $0.contains("transfer_complete") }
314
+ XCTAssertEqual(confirmations.count, 1)
315
+ XCTAssertTrue(confirmations[0].contains("false"))
316
+ XCTAssertEqual(live.bleIncidentLogRelays[name]?.incidentId, incident)
317
+ }
318
+ }
319
+
320
+ func testBesUploadThatFailsDuringAnOutageIsRetriedUntilCloudConfirmsIt() async throws {
321
+ clearHeldBesDir()
322
+ defer { clearHeldBesDir() }
323
+ try await withCloud(status: 500) {
324
+ let live = IncidentLogTransport()
325
+ live.besHoldSeconds = 0.01
326
+ live.besRetrySeconds = 0.05
327
+ let name = "Brep_01m4hbvt88d"
328
+ let bes = "{\"source\":\"BES\"}"
329
+ try live.sendIncidentId(incident, apiBaseUrl: cloudBase)
330
+ try await receive(live, name, bes)
331
+ // No MTK log arrives, so after the hold the phone uploads the BES itself.
332
+ try await waitFor { CloudStub.requests.count >= 2 }
333
+ XCTAssertEqual(live.bleIncidentLogRelays[name]?.incidentId, incident)
334
+ XCTAssertTrue(besHeldFileExists(incident))
335
+
336
+ CloudStub.status = 200
337
+ try await waitFor { !self.besHeldFileExists(self.incident) }
338
+ XCTAssertNil(live.bleIncidentLogRelays[name])
339
+ }
340
+ }
341
+
342
+ func testResumedBesUploadRetriesAfterFailureAndReconnectDoesNotDuplicateIt() async throws {
343
+ clearHeldBesDir()
344
+ defer { clearHeldBesDir() }
345
+ try await withCloud(status: 500) {
346
+ let live = IncidentLogTransport()
347
+ try live.sendIncidentId(incident, apiBaseUrl: cloudBase)
348
+ try await receive(live, "Brep_01m4hbvt88d", "{\"source\":\"BES\"}")
349
+
350
+ let revived = IncidentLogTransport()
351
+ revived.besRetrySeconds = 0.2
352
+ revived.resumePersistedBesUploads()
353
+ try await waitFor { CloudStub.requests.count == 2 } // Both attempts of one try.
354
+ revived.resumePersistedBesUploads()
355
+ revived.resumePersistedBesUploads()
356
+ XCTAssertEqual(CloudStub.requests.count, 2)
357
+
358
+ CloudStub.status = 200
359
+ CloudStub.requests = []
360
+ try await waitFor { !self.besHeldFileExists(self.incident) }
361
+ XCTAssertEqual(CloudStub.requests.count, 1)
362
+ }
363
+ }
364
+
365
+ func testBesLogCloudPermanentlyRefusesIsDroppedInsteadOfRetriedForever() async throws {
366
+ clearHeldBesDir()
367
+ defer { clearHeldBesDir() }
368
+ try await withCloud(status: 404) {
369
+ let live = IncidentLogTransport()
370
+ try live.sendIncidentId(incident, apiBaseUrl: cloudBase)
371
+ try await receive(live, "Brep_01m4hbvt88d", "{\"source\":\"BES\"}")
372
+
373
+ let revived = IncidentLogTransport()
374
+ revived.besRetrySeconds = 0.05
375
+ revived.resumePersistedBesUploads()
376
+ try await waitFor { !self.besHeldFileExists(self.incident) }
377
+ try await Task.sleep(nanoseconds: 200_000_000)
378
+ XCTAssertEqual(CloudStub.requests.count, 1)
379
+ }
380
+ }
381
+
164
382
  func testStaleCallbackOfFinishedOwnerCannotConfirmCollidingReportsFile() async throws {
165
383
  try await withCloud(status: 200) {
166
384
  let live = IncidentLogTransport()
@@ -170,15 +388,16 @@ final class IncidentLogDispatchTests: XCTestCase {
170
388
  let staleRelay = try XCTUnwrap(live.bleIncidentLogRelays[name])
171
389
  try await receive(live, name, "owner")
172
390
  try await receive(live, logcat, "owner-logcat")
391
+ try await waitFor { CloudStub.requests.count == 2 }
173
392
  try live.sendIncidentId(collidingIncident, apiBaseUrl: cloudBase)
174
393
  let confirmationsBefore = sentMessages(live).filter { $0.contains("transfer_complete") }.count
175
394
  let requestsBefore = CloudStub.requests.count
176
395
 
177
- // A late callback of the first report's request arrives while the colliding report owns the name.
396
+ // A late callback of the first report's file arrives while the colliding report owns the name.
178
397
  live.uploadBleIncidentLogRelay(relay: staleRelay, fileName: name, data: Data("late".utf8))
179
- try await waitFor { CloudStub.requests.count == requestsBefore + 1 }
180
398
  try await Task.sleep(nanoseconds: 300_000_000)
181
399
 
400
+ XCTAssertEqual(CloudStub.requests.count, requestsBefore)
182
401
  XCTAssertEqual(sentMessages(live).filter { $0.contains("transfer_complete") }.count, confirmationsBefore)
183
402
  XCTAssertEqual(live.bleIncidentLogRelays[name]?.incidentId, collidingIncident)
184
403
  }
@@ -190,12 +409,22 @@ final class IncidentLogDispatchTests: XCTestCase {
190
409
  var now: TimeInterval = 1000
191
410
  live.incidentRelayClock = { now }
192
411
  let name = "Brep_01m4hbvt88d"
412
+ let logcat = "Lrep_01m4hbvt88d"
193
413
  try live.sendIncidentId(incident, apiBaseUrl: cloudBase)
194
414
  live.uploadBleIncidentLogRelay(
195
415
  relay: try XCTUnwrap(live.bleIncidentLogRelays[name]), fileName: name, data: Data("owner".utf8)
196
416
  )
197
- try await waitFor { wire(live).contains("transfer_complete") }
417
+ try await waitFor { wire(live).contains("\"success\":true") }
418
+ XCTAssertTrue(CloudStub.requests.isEmpty)
419
+ live.uploadBleIncidentLogRelay(
420
+ relay: try XCTUnwrap(live.bleIncidentLogRelays[logcat]),
421
+ fileName: logcat,
422
+ data: Data("owner-logcat".utf8)
423
+ )
424
+ try await waitFor { wire(live).contains("\"success\":false") }
425
+ XCTAssertEqual(CloudStub.requests, ["/api/client/reports/\(incident)/artifacts owner-logcat"])
198
426
  XCTAssertEqual(live.bleIncidentLogRelays[name]?.incidentId, incident)
427
+ XCTAssertEqual(live.bleIncidentLogRelays[logcat]?.incidentId, incident)
199
428
  XCTAssertThrowsError(try live.sendIncidentId(collidingIncident, apiBaseUrl: cloudBase)) { error in
200
429
  XCTAssertEqual((error as? BluetoothSdkError)?.code, "incident_relay_busy")
201
430
  }
@@ -228,6 +457,22 @@ final class IncidentLogDispatchTests: XCTestCase {
228
457
  sentMessages(live).joined()
229
458
  }
230
459
 
460
+ private func besHeldDirURL() -> URL? {
461
+ FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first?
462
+ .appendingPathComponent("MentraLive_HeldBes", isDirectory: true)
463
+ }
464
+
465
+ private func besHeldFileExists(_ incidentId: String) -> Bool {
466
+ guard let dir = besHeldDirURL() else { return false }
467
+ let safe = String(incidentId.map { $0.isLetter || $0.isNumber || $0 == "." || $0 == "-" || $0 == "_" ? $0 : "_" })
468
+ return FileManager.default.fileExists(atPath: dir.appendingPathComponent(safe + ".json").path)
469
+ }
470
+
471
+ private func clearHeldBesDir() {
472
+ guard let dir = besHeldDirURL() else { return }
473
+ try? FileManager.default.removeItem(at: dir)
474
+ }
475
+
231
476
  private func withCloud(status: Int, body: () async throws -> Void) async throws {
232
477
  CloudStub.status = status
233
478
  CloudStub.requests = []
@@ -245,8 +490,15 @@ final class IncidentLogDispatchTests: XCTestCase {
245
490
 
246
491
  private func receive(_ live: IncidentLogTransport, _ name: String, _ payload: String) async throws {
247
492
  let relay = try XCTUnwrap(live.bleIncidentLogRelays[name])
493
+ let confirmations = sentMessages(live).filter { $0.contains("transfer_complete") }.count
248
494
  live.uploadBleIncidentLogRelay(relay: relay, fileName: name, data: Data(payload.utf8))
249
- try await waitFor { live.bleIncidentLogRelays[name] !== relay }
495
+ if relay.kind == .firmware {
496
+ try await waitFor {
497
+ sentMessages(live).filter { $0.contains("transfer_complete") }.count > confirmations
498
+ }
499
+ } else {
500
+ try await waitFor { live.bleIncidentLogRelays[name] !== relay }
501
+ }
250
502
  }
251
503
 
252
504
  private func waitFor(_ condition: () -> Bool) async throws {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mentra/bluetooth-sdk",
3
- "version": "3.5.0-dev.1258",
3
+ "version": "3.5.0-dev.1261",
4
4
  "description": "SDK for communicating with smart glasses",
5
5
  "main": "build/index.js",
6
6
  "react-native": "src/index.ts",
@@ -115,7 +115,7 @@
115
115
  "registry": "https://registry.npmjs.org/"
116
116
  },
117
117
  "dependencies": {
118
- "@mentra/cloud-protocol": "3.5.0-dev.1258"
118
+ "@mentra/cloud-protocol": "3.5.0-dev.1261"
119
119
  },
120
120
  "devDependencies": {
121
121
  "@types/node": "^25.9.3",
@@ -12,9 +12,9 @@ export interface BluetoothSdkReleaseMetadata {
12
12
  export const BLUETOOTH_SDK_RELEASE_METADATA: Readonly<BluetoothSdkReleaseMetadata> = Object.freeze({
13
13
  "schemaVersion": 1,
14
14
  "familyBaseVersion": "3.5.0",
15
- "releaseIdentity": "3.5.0-dev.1258",
16
- "releaseSetId": "mentra-3.5.0-dev.1258",
17
- "sourceCommit": "09b5ef8797f62dba102c9788625bb203cdf64c28",
18
- "otaManifestUrl": "https://artifactscdn.mentraglass.com/Mentra-Community/MentraOS/releases/mentra-builds-v3.5.0/mentra-live-ota-3.5.0-dev.1258.json",
19
- "otaManifestSha256": "392e31fe9404b3fb5831a29307e9eb76f5ce85f5686774210e9ef79dd8c3bd0b"
15
+ "releaseIdentity": "3.5.0-dev.1261",
16
+ "releaseSetId": "mentra-3.5.0-dev.1261",
17
+ "sourceCommit": "c8f6349da74f3a0ae62c979a59aafbf62c644f7c",
18
+ "otaManifestUrl": "https://artifactscdn.mentraglass.com/Mentra-Community/MentraOS/releases/mentra-builds-v3.5.0/mentra-live-ota-3.5.0-dev.1261.json",
19
+ "otaManifestSha256": "fc13b964185dce4340c5e6f2481c3894c7b53ee1d57d6119dbcaad64e8ff0496"
20
20
  })