File size: 23,937 Bytes
e079a73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/// <reference types="bun-types" />

import {beforeEach, describe, expect, mock, test} from "bun:test"

import {FakeMmkv, fakeCreateMMKV, resetFakeMmkv} from "../../../services/fleet/__tests__/fakeMmkv"

mock.module("react-native-mmkv", () => ({createMMKV: fakeCreateMMKV}))

const {createCredentialVault} = await import("../credentialVault")
const {mmkvLegacyCredentials} = await import("../platform")
type CredentialVault = import("../credentialVault").CredentialVault
type LegacyCredentials = import("../credentialVault").LegacyCredentials
type SecureItemStore = import("../credentialVault").SecureItemStore

const ACCESS = "mentra.account.accessToken"
const REFRESH = "mentra.account.refreshToken"
const CLOUD = "mentra.cloud-client.refreshToken"
const WORKSPACE = "mentra.cloud-client.enterprise.https%3A%2F%2Facme.example.refreshToken"
const ITEM = "mentra.credentials.test"
const SCRUB_KEY = "mentra.credentials.scrub"

/** An MMKV instance that also records the scrub. */
class LegacyMmkv extends FakeMmkv {
  readonly writes: Array<{key: string; length: number}> = []
  trims = 0
  override set(key: string, value: string | number | boolean): void {
    this.writes.push({key, length: String(value).length})
    super.set(key, value)
  }
  trim(): void {
    this.trims += 1
  }
}

/**

 * The platform secure store. Its items outlive the app's MMKV, as the iOS Keychain outlives uninstalling the app.

 * Failures are switched on per test.

 */
class FakeSecureStore implements SecureItemStore {
  readonly items = new Map<string, string>()
  writes = 0
  deletes = 0
  failDeletes = false
  failWrites = false
  failReads = false
  /** Read back something other than what was written. */
  corruptReads = false
  /** The process dies while it waits for this read. */
  hangReads = false
  /** Writes wait for this, when set. */
  writeGate: Promise<void> | null = null

  async getItem(key: string): Promise<string | null> {
    if (this.hangReads) return new Promise<never>(() => {})
    if (this.failReads) throw new Error(`errSecInteractionNotAllowed reading ${key}`)
    const value = this.items.get(key) ?? null
    return this.corruptReads && value !== null ? value.slice(0, -2) : value
  }

  async setItem(key: string, value: string): Promise<void> {
    if (this.writeGate) await this.writeGate
    if (this.failWrites) throw new Error(`Keystore encryption failed for ${key}`)
    this.writes += 1
    this.items.set(key, value)
  }

  async deleteItem(key: string): Promise<void> {
    if (this.failDeletes) throw new Error(`Keychain delete failed for ${key}`)
    this.deletes += 1
    this.items.delete(key)
  }
}

/** The app's data that uninstalling removes: MMKV, with the move record. */
class AppData {
  legacy = new LegacyMmkv()
  record = new FakeMmkv()
  fileSize: number | null = 16384
}

let secure: FakeSecureStore
let data: AppData
let logs: string[]

function legacyFor(app: AppData, encoding: "json" | "raw" = "raw"): LegacyCredentials {
  return mmkvLegacyCredentials({
    keys: encoding === "json" ? [ACCESS, REFRESH] : "every key",
    encoding,
    isRetiredKey: encoding === "json" ? (key) => key.startsWith("supabase.auth.") : undefined,
    open: () => app.legacy,
    fileSize: () => app.fileSize,
  })
}

/** One launch of the app: a new process opens the same stores. */
function launch(options: {app?: AppData; legacy?: LegacyCredentials; encoding?: "json" | "raw"} = {}): CredentialVault {
  const app = options.app ?? data
  return createCredentialVault({
    name: "test",
    secure,
    moveRecord: () => app.record,
    legacy: options.legacy ?? legacyFor(app, options.encoding),
    log: (message) => logs.push(message),
  })
}

function legacyKeys(app: AppData = data): string[] {
  return app.legacy.getAllKeys().filter((key) => key !== SCRUB_KEY)
}

beforeEach(() => {
  resetFakeMmkv()
  secure = new FakeSecureStore()
  data = new AppData()
  logs = []
})

describe("the one-time move", () => {
  test("moves a signed-in person's credentials and deletes the MMKV copies", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    data.legacy.set(WORKSPACE, "workspace-refresh-1")

    const vault = launch()
    expect(await vault.get(CLOUD)).toBe("cloud-refresh-1")
    expect(await vault.get(WORKSPACE)).toBe("workspace-refresh-1")
    expect(await vault.location()).toBe("secure-store")
    expect(legacyKeys()).toEqual([])
    expect(secure.items.has(ITEM)).toBe(true)
    expect(logs).toContain("CREDENTIALS(test): moved 2 credential(s) from MMKV to the secure store")
  })

  test("overwrites the removed copies in the MMKV file", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    data.fileSize = 32768

    await launch().location()
    // A value longer than the file makes MMKV write back first, then write it over everything left behind.
    const scrub = data.legacy.writes.find((write) => write.key === SCRUB_KEY)
    expect(scrub?.length).toBeGreaterThan(32768)
    expect(data.legacy.contains(SCRUB_KEY)).toBe(false)
    expect(data.legacy.trims).toBe(1)
    expect(data.record.getString("test.secureStore")).toBe("scrubbed-v1")
  })

  test("runs once: a later launch reads the secure store and writes nothing", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    await launch().location()
    const writes = secure.writes
    const scrubs = data.legacy.trims

    const next = launch()
    expect(await next.get(CLOUD)).toBe("cloud-refresh-1")
    expect(await next.location()).toBe("secure-store")
    expect(secure.writes).toBe(writes)
    expect(data.legacy.trims).toBe(scrubs)
    expect(logs).toContain("CREDENTIALS(test): credentials are in the secure store")
  })

  test("a signed-out person moves nothing and stays signed out", async () => {
    const vault = launch()
    expect(await vault.get(CLOUD)).toBeNull()
    expect(await vault.location()).toBe("secure-store")
    expect(logs).toContain("CREDENTIALS(test): moved 0 credential(s) from MMKV to the secure store")

    await vault.set(CLOUD, "after-sign-in")
    expect(await launch().get(CLOUD)).toBe("after-sign-in")
  })

  test("a fresh install never signs in with a previous installation's secure-store credentials", async () => {
    data.legacy.set(CLOUD, "previous-installation")
    await launch().location()

    // Uninstalling removes MMKV, not the Keychain item.
    const reinstalled = new AppData()
    const vault = launch({app: reinstalled})
    expect(await vault.get(CLOUD)).toBeNull()
    expect([...secure.items.values()].join()).not.toContain("previous-installation")
  })

  test("a failed write keeps the MMKV copies, keeps the person signed in, and retries next launch", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    secure.failWrites = true

    const failed = launch()
    expect(await failed.get(CLOUD)).toBe("cloud-refresh-1")
    expect(await failed.location()).toBe("mmkv")
    expect(data.legacy.getString(CLOUD)).toBe("cloud-refresh-1")
    expect(data.record.getString("test.secureStore")).toBeUndefined()
    // The token rotates while the store is unavailable: MMKV keeps the newest value.
    await failed.set(CLOUD, "cloud-refresh-2")
    expect(data.legacy.getString(CLOUD)).toBe("cloud-refresh-2")
    expect(logs.some((line) => line.includes("move failed; keeping the MMKV copies and retrying next launch"))).toBe(
      true,
    )

    secure.failWrites = false
    const retried = launch()
    expect(await retried.get(CLOUD)).toBe("cloud-refresh-2")
    expect(await retried.location()).toBe("secure-store")
    expect(legacyKeys()).toEqual([])
  })

  test("a read-back mismatch keeps the MMKV copies and the session", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    secure.corruptReads = true

    const vault = launch()
    expect(await vault.get(CLOUD)).toBe("cloud-refresh-1")
    expect(await vault.location()).toBe("mmkv")
    expect(data.legacy.getString(CLOUD)).toBe("cloud-refresh-1")
    expect(logs.some((line) => line.includes("the secure store read back other data"))).toBe(true)

    secure.corruptReads = false
    expect(await launch().get(CLOUD)).toBe("cloud-refresh-1")
    expect(legacyKeys()).toEqual([])
  })

  test("while the move is pending, a store read error stays an error for keys MMKV no longer has", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    data.legacy.set(WORKSPACE, "workspace-refresh-1")
    await launch().location()
    // An older build wrote one copy; this launch cannot read the store to merge it.
    data.legacy.set(CLOUD, "cloud-refresh-from-older-build")
    secure.failReads = true
    const vault = launch()
    expect(await vault.location()).toBe("mmkv")
    expect(await vault.get(CLOUD)).toBe("cloud-refresh-from-older-build")
    await expect(vault.get(WORKSPACE)).rejects.toThrow("errSecInteractionNotAllowed")
  })

  test("a scrub filler a crash left behind is not moved as a credential", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    data.legacy.set(SCRUB_KEY, "0".repeat(1000))
    const vault = launch()
    expect(await vault.get(CLOUD)).toBe("cloud-refresh-1")
    expect(logs).toContain("CREDENTIALS(test): moved 1 credential(s) from MMKV to the secure store")
    expect(secure.items.get(ITEM)).not.toContain("000")
    expect(data.legacy.contains(SCRUB_KEY)).toBe(false)
  })

  test("a store that cannot be read at all keeps the MMKV copies", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    secure.failReads = true

    const vault = launch()
    expect(await vault.get(CLOUD)).toBe("cloud-refresh-1")
    expect(await vault.location()).toBe("mmkv")
    expect(data.record.getString("test.secureStore")).toBeUndefined()
  })

  test("a crash after the write, before the move is recorded, moves again next launch", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    secure.hangReads = true
    // The process dies during the read-back.
    void launch().get(CLOUD)
    await Promise.resolve()
    expect(data.record.getString("test.secureStore")).toBeUndefined()
    expect(data.legacy.getString(CLOUD)).toBe("cloud-refresh-1")

    secure.hangReads = false
    const next = launch()
    expect(await next.get(CLOUD)).toBe("cloud-refresh-1")
    expect(await next.location()).toBe("secure-store")
    expect(legacyKeys()).toEqual([])
  })

  test("a crash after the move is recorded, before the copies are deleted, loses nothing", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    const real = legacyFor(data)
    // The process dies before it deletes the copies, so it never scrubs either.
    await launch({legacy: {...real, eraseAll: () => {}, scrub: () => false}}).location()
    expect(data.record.getString("test.secureStore")).toBe("moved-v1")
    expect(data.legacy.getString(CLOUD)).toBe("cloud-refresh-1")

    const next = launch()
    expect(await next.get(CLOUD)).toBe("cloud-refresh-1")
    expect(await next.location()).toBe("secure-store")
    expect(legacyKeys()).toEqual([])
    expect(data.record.getString("test.secureStore")).toBe("scrubbed-v1")
  })

  test("a crash before the scrub retries it next launch", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    data.fileSize = null
    await launch().location()
    expect(data.record.getString("test.secureStore")).toBe("moved-v1")
    expect(data.legacy.trims).toBe(0)

    data.fileSize = 16384
    expect(await launch().get(CLOUD)).toBe("cloud-refresh-1")
    expect(data.legacy.trims).toBe(1)
    expect(data.record.getString("test.secureStore")).toBe("scrubbed-v1")
  })

  test("copies an older build wrote after the move win over the stored ones", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    data.legacy.set(WORKSPACE, "workspace-refresh-1")
    await launch().location()

    // The person ran an older build, which signed in again through MMKV.
    data.legacy.set(CLOUD, "cloud-refresh-from-older-build")
    const next = launch()
    expect(await next.get(CLOUD)).toBe("cloud-refresh-from-older-build")
    expect(await next.get(WORKSPACE)).toBe("workspace-refresh-1")
    expect(legacyKeys()).toEqual([])
  })

  test("an MMKV copy that could not be deleted stays current until a launch deletes it", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    const real = legacyFor(data)
    const vault = launch({
      legacy: {
        ...real,
        eraseAll: () => {
          throw new Error("MMKV is read-only")
        },
      },
    })
    expect(await vault.location()).toBe("mmkv")
    await vault.set(CLOUD, "cloud-refresh-2")
    expect(data.legacy.getString(CLOUD)).toBe("cloud-refresh-2")

    expect(await launch().get(CLOUD)).toBe("cloud-refresh-2")
    expect(legacyKeys()).toEqual([])
  })

  test("a sign-out while the store is unusable reaches the stored copy too", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    data.legacy.set(WORKSPACE, "workspace-refresh-1")
    await launch().location()
    // An older build ran; then the store fails while moving its copy.
    data.legacy.set(CLOUD, "cloud-refresh-from-older-build")
    secure.failWrites = true

    const vault = launch()
    expect(await vault.location()).toBe("mmkv")
    expect(await vault.get(CLOUD)).toBe("cloud-refresh-from-older-build")
    // The store still holds this installation's credentials for keys MMKV no longer has.
    expect(await vault.get(WORKSPACE)).toBe("workspace-refresh-1")

    secure.failWrites = false
    await vault.delete(CLOUD)
    const next = launch()
    expect(await next.get(CLOUD)).toBeNull()
    expect(await next.get(WORKSPACE)).toBe("workspace-refresh-1")
  })
})

describe("token rotation and read errors", () => {
  test("a refresh during the move lands in the secure store, after the move", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    let open!: () => void
    secure.writeGate = new Promise<void>((resolve) => {
      open = resolve
    })
    const vault = launch()
    // cloud-client reads the token, Core rotates it, cloud-client saves the new one, all while the move waits.
    const read = vault.get(CLOUD)
    const rotated = vault.set(CLOUD, "cloud-refresh-2")
    await Promise.resolve()
    expect(data.legacy.getString(CLOUD)).toBe("cloud-refresh-1")
    secure.writeGate = null
    open()
    expect(await read).toBe("cloud-refresh-1")
    await rotated
    expect(data.legacy.getString(CLOUD)).toBeUndefined()
    expect(await launch().get(CLOUD)).toBe("cloud-refresh-2")
  })

  test("a read error is 'unavailable', never 'signed out'", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    await launch().location()
    // The device has not been unlocked since boot.
    secure.failReads = true
    const locked = launch()
    await expect(locked.get(CLOUD)).rejects.toThrow("errSecInteractionNotAllowed")
    expect(await locked.location()).toBe("secure-store")
    await expect(locked.set(WORKSPACE, "workspace-refresh-1")).rejects.toThrow("errSecInteractionNotAllowed")

    secure.failReads = false
    expect(await locked.get(CLOUD)).toBe("cloud-refresh-1")
    expect(await launch().get(CLOUD)).toBe("cloud-refresh-1")
  })
})

describe("sign-out", () => {
  test("a delete the store cannot take stays deleted across relaunches until the store confirms it", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    data.legacy.set(WORKSPACE, "workspace-refresh-1")
    await launch().location()

    secure.failReads = true
    secure.failWrites = true
    const vault = launch()
    await vault.delete(CLOUD)
    expect(logs.some((line) => line.includes("it stays deleted and is retried"))).toBe(true)
    expect(data.record.getString("test.deleted")).toBe(JSON.stringify([CLOUD]))

    // Readable again, the store still holds the token until a launch writes it out.
    secure.failReads = false
    const relaunched = launch()
    secure.failWrites = true
    expect(await relaunched.get(CLOUD)).toBeNull()
    expect(await relaunched.get(WORKSPACE)).toBe("workspace-refresh-1")

    secure.failWrites = false
    const recovered = launch()
    expect(await recovered.get(CLOUD)).toBeNull()
    expect(secure.items.get(ITEM)).not.toContain("cloud-refresh-1")
    expect(data.record.getString("test.deleted")).toBeUndefined()
    expect(await recovered.get(WORKSPACE)).toBe("workspace-refresh-1")
  })

  test("a clear the store cannot take stays in force across relaunches, and a new sign-in replaces it", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    await launch().location()
    secure.failDeletes = true
    await launch().clear()

    const relaunched = launch()
    expect(await relaunched.get(CLOUD)).toBeNull()
    // Signing in again writes a fresh item, which settles the clear.
    await relaunched.set(WORKSPACE, "workspace-refresh-2")
    expect(data.record.getString("test.deleted")).toBeUndefined()
    secure.failDeletes = false
    const next = launch()
    expect(await next.get(CLOUD)).toBeNull()
    expect(await next.get(WORKSPACE)).toBe("workspace-refresh-2")
  })

  test("clear deletes every credential without reading the item", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    await launch().location()
    secure.failReads = true
    const vault = launch()
    await vault.clear()
    expect(secure.items.has(ITEM)).toBe(false)
    expect(await vault.get(CLOUD)).toBeNull()
    secure.failReads = false
    expect(await launch().get(CLOUD)).toBeNull()
  })

  test("a sign-out reaches the store even when this launch could not read MMKV to know the move had finished", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    await launch().location()
    const getString = data.record.getString.bind(data.record)
    let failures = 1
    data.record.getString = (key: string) => {
      if (failures-- > 0) throw new Error("MMKV could not load")
      return getString(key)
    }
    const vault = launch()
    expect(await vault.location()).toBe("mmkv")
    await vault.clear()
    data.record.getString = getString
    expect(await launch().get(CLOUD)).toBeNull()
  })

  test("clear during a pending move deletes the MMKV copies too", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    secure.failWrites = true
    const vault = launch()
    expect(await vault.location()).toBe("mmkv")
    await vault.clear()
    expect(legacyKeys()).toEqual([])
    secure.failWrites = false
    expect(await launch().get(CLOUD)).toBeNull()
  })
})

describe("after the move", () => {
  test("nothing reads MMKV, and every write removes a stray MMKV copy", async () => {
    const vault = launch()
    await vault.set(CLOUD, "cloud-refresh-1")
    data.legacy.set(WORKSPACE, "planted-after-the-move")
    const reads: string[] = []
    const getString = data.legacy.getString.bind(data.legacy)
    data.legacy.getString = (key: string) => {
      reads.push(key)
      return getString(key)
    }

    expect(await vault.get(CLOUD)).toBe("cloud-refresh-1")
    expect(await vault.get(WORKSPACE)).toBeNull()
    await vault.set(WORKSPACE, "workspace-refresh-1")
    await vault.delete(CLOUD)
    expect(reads).toEqual([])
    expect(data.legacy.contains(WORKSPACE)).toBe(false)
    expect(await vault.get(WORKSPACE)).toBe("workspace-refresh-1")
    expect(await vault.get(CLOUD)).toBeNull()
  })

  test("keeps every write when calls overlap, including calls made during the move", async () => {
    data.legacy.set(CLOUD, "cloud-refresh-1")
    const vault = launch()
    const keys = Array.from({length: 20}, (_, index) => `mentra.cloud-client.deployment-${index}.refreshToken`)
    await Promise.all(keys.map((key, index) => vault.set(key, `token-${index}`)))
    await vault.delete(keys[3])

    const next = launch()
    expect(await next.get(CLOUD)).toBe("cloud-refresh-1")
    expect(await next.get(keys[0])).toBe("token-0")
    expect(await next.get(keys[3])).toBeNull()
    expect(await next.get(keys[19])).toBe("token-19")
  })

  test("a failed write surfaces and the next read reflects the stored item", async () => {
    const vault = launch()
    await vault.set(CLOUD, "cloud-refresh-1")
    secure.failWrites = true
    await expect(vault.set(CLOUD, "cloud-refresh-2")).rejects.toThrow("Keystore encryption failed")
    secure.failWrites = false
    expect(await vault.get(CLOUD)).toBe("cloud-refresh-1")
  })

  test("deletes retired credentials whenever they turn up, and scrubs them", async () => {
    data.legacy.set(ACCESS, JSON.stringify("account-access"))
    await launch({encoding: "json"}).location()
    const scrubs = data.legacy.trims

    data.legacy.set("supabase.auth.token", JSON.stringify({access_token: "supabase-access"}))
    const next = launch({encoding: "json"})
    expect(await next.get(ACCESS)).toBe("account-access")
    expect(data.legacy.contains("supabase.auth.token")).toBe(false)
    expect(data.legacy.trims).toBe(scrubs + 1)
    expect(data.record.getString("test.secureStore")).toBe("scrubbed-v1")
    expect(logs).toContain("CREDENTIALS(test): deleted retired credentials from MMKV")
  })

  test("an unreadable item is treated as empty, so the person can sign in again", async () => {
    await launch().location()
    secure.items.set(ITEM, "not json")
    const vault = launch()
    expect(await vault.get(CLOUD)).toBeNull()
    await vault.set(CLOUD, "cloud-refresh-1")
    expect(await launch().get(CLOUD)).toBe("cloud-refresh-1")
  })
})

describe("app storage copies", () => {
  test("decodes values the app wrote with storage.save and deletes retired Supabase keys", async () => {
    data.legacy.set(ACCESS, JSON.stringify("account-access"))
    data.legacy.set(REFRESH, JSON.stringify("account-refresh"))
    data.legacy.set("supabase.auth.token", JSON.stringify({access_token: "supabase-access"}))
    data.legacy.set("mentra.account.userProfile", JSON.stringify(JSON.stringify({id: "user-1"})))

    const vault = launch({encoding: "json"})
    expect(await vault.get(ACCESS)).toBe("account-access")
    expect(await vault.get(REFRESH)).toBe("account-refresh")
    expect(legacyKeys()).toEqual(["mentra.account.userProfile"])
  })

  test("keeps a saved object as its JSON text", () => {
    const authing = {access_token: "authing-access", refresh_token: "authing-refresh", expires_at: 1}
    data.legacy.set("authing_session", JSON.stringify(authing))
    const legacy = mmkvLegacyCredentials({keys: ["authing_session"], encoding: "json", open: () => data.legacy})
    expect(JSON.parse(legacy.readAll().authing_session)).toEqual(authing)
  })

  test("writes while the move is pending stay readable by an older build", async () => {
    data.legacy.set(ACCESS, JSON.stringify("account-access"))
    secure.failWrites = true
    const vault = launch({encoding: "json"})
    await vault.set(ACCESS, "account-access-2")
    expect(JSON.parse(data.legacy.getString(ACCESS)!)).toBe("account-access-2")
  })
})

test("never logs a credential", async () => {
  data.legacy.set(CLOUD, "secret-cloud-refresh")
  data.legacy.set(WORKSPACE, "secret-workspace-refresh")
  secure.failWrites = true
  await launch().location()
  secure.failWrites = false
  const vault = launch()
  await vault.set(CLOUD, "secret-rotated")
  secure.items.set(ITEM, "secret-garbage")
  await launch().get(CLOUD)

  expect(logs.length).toBeGreaterThan(0)
  for (const line of logs) expect(line).not.toContain("secret")
})