File size: 7,082 Bytes
8a29801
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/// <reference types="bun-types" />

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

import {bluetoothSdk} from "./bluetoothSdkTestMock"

const mockGetDefaultDevice = mock((): Promise<Record<string, unknown> | null> => Promise.resolve(null))
bluetoothSdk.getDefaultDevice = mockGetDefaultDevice

const okResult = {is_error: () => false} as const
const settingsValues: Record<string, unknown> = {}
const mockLoadAllSettings = mock(() => Promise.resolve(okResult as {is_error: () => boolean; error?: unknown}))
const mockSetSetting = mock((key: string, value: unknown) => {
  settingsValues[key] = value
  return Promise.resolve(okResult)
})
mock.module("../../stores/settings", () => ({
  SETTINGS: {
    default_wearable: {key: "default_wearable"},
    pending_wearable: {key: "pending_wearable"},
    device_name: {key: "device_name"},
    device_address: {key: "device_address"},
  },
  useSettingsStore: {
    getState: () => ({
      loadAllSettings: mockLoadAllSettings,
      getSetting: (key: string) => settingsValues[key],
      setSetting: mockSetSetting,
    }),
  },
}))

let connection: {state: string} = {state: "disconnected"}
mock.module("../../stores/glasses", () => ({
  useGlassesStore: {
    getState: () => ({connection}),
  },
}))

const mockPushAllBluetoothSettings = mock(() => Promise.resolve())
mock.module("../GlassesSettingsSync", () => ({
  pushAllBluetoothSettings: mockPushAllBluetoothSettings,
}))

// Import AFTER the mocks are registered
const {
  hydrateDeviceStore,
  hasDefaultDevice,
  demoteOrphanedDefaultWearable,
  resetDeviceStoreHydrationForTests,
} = require("../DeviceStoreHydration")

function resetSettings(values: Record<string, unknown>) {
  for (const key of Object.keys(settingsValues)) delete settingsValues[key]
  Object.assign(settingsValues, values)
}

describe("DeviceStoreHydration", () => {
  beforeEach(() => {
    bluetoothSdk.getDefaultDevice = mockGetDefaultDevice
    resetDeviceStoreHydrationForTests()
    mockGetDefaultDevice.mockClear()
    mockLoadAllSettings.mockClear()
    mockSetSetting.mockClear()
    mockPushAllBluetoothSettings.mockClear()
    mockLoadAllSettings.mockImplementation(() => Promise.resolve(okResult))
    mockGetDefaultDevice.mockImplementation(() => Promise.resolve(null))
    connection = {state: "disconnected"}
    resetSettings({})
  })

  test("hydration retries after a failed settings load, then single-flights", async () => {
    mockLoadAllSettings.mockImplementation(() =>
      Promise.resolve({is_error: () => true, error: new Error("disk unavailable")}),
    )
    // The failure surfaces as a REJECTION (never a false "absent"), and
    // hasDefaultDevice propagates it so callers can fail open.
    await expect(hasDefaultDevice()).rejects.toThrow("settings load failed")
    expect(mockPushAllBluetoothSettings).not.toHaveBeenCalled()

    // The memo cleared on failure: a later call retries and heals.
    mockLoadAllSettings.mockImplementation(() => Promise.resolve(okResult))
    await hydrateDeviceStore()
    expect(mockPushAllBluetoothSettings).toHaveBeenCalledTimes(1)

    // Completed hydration is cached: further calls run nothing again.
    await hydrateDeviceStore()
    await hydrateDeviceStore()
    expect(mockPushAllBluetoothSettings).toHaveBeenCalledTimes(1)
  })

  test("hasDefaultDevice awaits hydration and reports the native two-state answer", async () => {
    mockGetDefaultDevice.mockImplementation(() => Promise.resolve(null))
    expect(await hasDefaultDevice()).toBe(false)

    mockGetDefaultDevice.mockImplementation(() =>
      Promise.resolve({model: "Even Realities G1", name: "G1_123"}),
    )
    expect(await hasDefaultDevice()).toBe(true)
  })

  test("demotes an orphaned default_wearable to pending and clears the identity", async () => {
    resetSettings({default_wearable: "Mentra Live", device_name: "LIVE_1", device_address: "aa:bb"})
    mockGetDefaultDevice.mockImplementation(() => Promise.resolve(null))

    await demoteOrphanedDefaultWearable()

    expect(settingsValues.pending_wearable).toBe("Mentra Live")
    expect(settingsValues.default_wearable).toBe("")
    expect(settingsValues.device_name).toBe("")
    expect(settingsValues.device_address).toBe("")
    // Two pushes: the hydration seed (pre-demotion values), then the re-push
    // so native mirrors the demoted identity.
    expect(mockPushAllBluetoothSettings).toHaveBeenCalledTimes(2)
  })

  test("demotion backfills pending only when empty — a fresher selection wins", async () => {
    resetSettings({default_wearable: "Mentra Live", pending_wearable: "Even Realities G1"})
    mockGetDefaultDevice.mockImplementation(() => Promise.resolve(null))

    await demoteOrphanedDefaultWearable()

    // The user's later selection (G1) survives; the stale orphan is only cleared.
    expect(settingsValues.pending_wearable).toBe("Even Realities G1")
    expect(settingsValues.default_wearable).toBe("")
  })

  test("does not demote when the native default device exists", async () => {
    resetSettings({default_wearable: "Mentra Live", device_name: "LIVE_1"})
    mockGetDefaultDevice.mockImplementation(() =>
      Promise.resolve({model: "Mentra Live", name: "LIVE_1"}),
    )

    await demoteOrphanedDefaultWearable()

    expect(mockSetSetting).not.toHaveBeenCalled()
  })

  test("does not demote the simulated wearable", async () => {
    resetSettings({default_wearable: "Simulated Glasses"})

    await demoteOrphanedDefaultWearable()

    expect(mockSetSetting).not.toHaveBeenCalled()
    // Hydration runs (the internal await), but the native default is never read.
    expect(mockGetDefaultDevice).not.toHaveBeenCalled()
  })

  test("demotion refuses to run against an unhydrated store", async () => {
    resetSettings({default_wearable: "Mentra Live", device_name: "LIVE_1"})
    mockLoadAllSettings.mockImplementation(() =>
      Promise.resolve({is_error: () => true, error: new Error("disk unavailable")}),
    )

    await expect(demoteOrphanedDefaultWearable()).rejects.toThrow("settings load failed")

    // Nothing was demoted on the unseeded store.
    expect(mockSetSetting).not.toHaveBeenCalled()
    expect(settingsValues.default_wearable).toBe("Mentra Live")
  })

  test("does not demote while the link layer is busy or connected (a pairing owns the identity)", async () => {
    resetSettings({default_wearable: "Mentra Live"})

    connection = {state: "connecting"}
    await demoteOrphanedDefaultWearable()
    expect(mockSetSetting).not.toHaveBeenCalled()

    connection = {state: "connected"}
    await demoteOrphanedDefaultWearable()
    expect(mockSetSetting).not.toHaveBeenCalled()
  })

  test("does nothing when no default_wearable is set", async () => {
    resetSettings({pending_wearable: "Mentra Live"})

    await demoteOrphanedDefaultWearable()

    expect(mockSetSetting).not.toHaveBeenCalled()
  })
})