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

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

// Where engine.start() and engine.stop() run the Fleet reporter, against every other runtime service.

const calls: string[] = []
function step(label: string, result?: () => unknown) {
  return mock((..._args: unknown[]) => {
    calls.push(label)
    return result?.()
  })
}

let hasCore = true
const startFleetReporter = step("fleet.start")
const stopFleetReporter = step("fleet.stop", async () => {})
const startAssignmentReconciler = step("reconciler.start")
const stopAssignmentReconciler = step("reconciler.stop")
const setFleetInventorySource = step("inventory.set")
const fleetDesiredStateClient = {fetch: async () => ({})}
const registryInventorySource = {list: () => null, subscribe: () => () => {}}

mock.module("../runtime/bootstrap", () => ({
  configure: step("configure"),
  getConfigValues: () => ({runtimeRealtimeSession: false}),
  start: step("bootstrap.start", async () => {}),
  stop: step("bootstrap.stop"),
  updateMiniappPolicy: step("updateMiniappPolicy"),
  updateUiSeams: step("updateUiSeams"),
}))
mock.module("../services/CloudClientService", () => ({
  cloudClientService: {
    init: step("cloud.init"),
    hasCore: () => hasCore,
    syncCoreTokenToBluetooth: step("cloud.syncCoreToken", async () => "token"),
    stop: step("cloud.stop"),
  },
  fleetDesiredStateClient,
}))
mock.module("../services/fleet/FleetReporter", () => ({startFleetReporter, stopFleetReporter}))
mock.module("../services/fleet/assignmentReconcilerHost", () => ({startAssignmentReconciler, stopAssignmentReconciler}))
mock.module("../services/fleet/fleetCollector", () => ({setFleetInventorySource}))
mock.module("../services/fleet/registryInventorySource", () => ({registryInventorySource}))
mock.module("../services/DeviceStoreHydration", () => ({
  hydrateDeviceStore: step("hydrateDeviceStore", async () => {}),
  demoteOrphanedDefaultWearable: step("demoteOrphanedDefaultWearable", async () => {}),
}))
mock.module("../services/GlassesSettingsSync", () => ({
  startGlassesSettingsSync: step("glassesSettings.start"),
  stopGlassesSettingsSync: step("glassesSettings.stop"),
}))
mock.module("../services/GlassesStatusProjection", () => ({
  startGlassesStatusProjection: step("statusProjection.start"),
  stopGlassesStatusProjection: step("statusProjection.stop"),
  toMiniappConnectionData: () => null,
}))
mock.module("../services/OtaService", () => ({startOtaService: step("ota.start"), stopOtaService: step("ota.stop")}))
mock.module("../services/AudioCloudUplink", () => ({
  startAudioCloudUplink: step("uplink.start"),
  stopAudioCloudUplink: step("uplink.stop"),
}))
mock.module("../services/SupportProfileSync", () => ({
  startSupportProfileSync: step("supportProfile.start"),
  stopSupportProfileSync: step("supportProfile.stop"),
}))
mock.module("../services/DeviceEventRouter", () => ({
  startDeviceEventRouter: step("deviceEvents.start"),
  stopDeviceEventRouter: step("deviceEvents.stop"),
}))
mock.module("../services/PhoneNotificationsSync", () => ({
  startPhoneNotificationsSync: step("phoneNotifications.start"),
  stopPhoneNotificationsSync: step("phoneNotifications.stop"),
}))
mock.module("../services/MentraJSCrashloopReportService", () => ({
  startMentraJSCrashloopReportService: step("crashloop.start"),
  stopMentraJSCrashloopReportService: step("crashloop.stop"),
}))
mock.module("../services/BesCrashReportService", () => ({
  startBesCrashReportService: step("besCrash.start"),
  stopBesCrashReportService: step("besCrash.stop"),
}))
mock.module("../services/MiniappEngine", () => ({
  ensureMiniappEngine: step("miniappEngine.ensure"),
  stopMiniappEngine: step("miniappEngine.stop"),
}))
mock.module("../services/LocalMiniappRuntime", () => ({
  default: {
    initialize: step("localMiniappRuntime.initialize"),
    cleanup: step("localMiniappRuntime.cleanup"),
    forwardEvent: step("localMiniappRuntime.forwardEvent"),
  },
}))
mock.module("../services/DisplayProcessor", () => ({
  default: {attachToRuntime: step("display.attach"), detachFromRuntime: step("display.detach")},
}))
mock.module("../services/asg/gallerySyncService", () => ({
  gallerySyncService: {initialize: step("gallery.initialize"), cleanup: step("gallery.cleanup")},
}))
for (const facade of [
  "glasses",
  "display",
  "speech",
  "session",
  "settings",
  "dev",
  "reports",
  "ota",
  "gallery",
  "miniapps",
  "pairing",
  "phoneNotifications",
  "permissions",
  "notifications",
]) {
  mock.module(`../facades/${facade}`, () => ({[facade]: {}}))
}
mock.module("../utils/devLogging", () => ({logBuffer: {startConsoleInterception: step("logBuffer.intercept")}}))

const {engine} = await import("../engine")

beforeEach(() => {
  calls.length = 0
  hasCore = true
  startFleetReporter.mockClear()
  stopFleetReporter.mockClear()
  startAssignmentReconciler.mockClear()
  stopAssignmentReconciler.mockClear()
  setFleetInventorySource.mockClear()
})

describe("engine.start()", () => {
  test("starts the Fleet reporter before it resolves: after the Core token sync, before the miniapp runtime", async () => {
    await engine.start()
    calls.push("engine.start resolved")
    const at = (label: string) => calls.indexOf(label)
    expect(startFleetReporter).toHaveBeenCalledTimes(1)
    expect(at("cloud.syncCoreToken")).toBeLessThan(at("fleet.start"))
    for (const later of ["localMiniappRuntime.initialize", "miniappEngine.ensure", "engine.start resolved"]) {
      expect({later, fleetFirst: at("fleet.start") < at(later)}).toEqual({later, fleetFirst: true})
    }
  })

  test("wires the registry's inventory before the reporter, and starts the reconciler with the desired-state client after it", async () => {
    await engine.start()
    const at = (label: string) => calls.indexOf(label)
    expect(setFleetInventorySource).toHaveBeenCalledWith(registryInventorySource)
    expect(startAssignmentReconciler).toHaveBeenCalledTimes(1)
    expect(startAssignmentReconciler).toHaveBeenCalledWith(fleetDesiredStateClient)
    expect(at("inventory.set")).toBeLessThan(at("fleet.start"))
    expect(at("fleet.start")).toBeLessThan(at("reconciler.start"))
    expect(at("reconciler.start")).toBeLessThan(at("localMiniappRuntime.initialize"))
  })

  test("does not start Fleet without a Core", async () => {
    hasCore = false
    await engine.start()
    expect(startFleetReporter).not.toHaveBeenCalled()
    expect(startAssignmentReconciler).not.toHaveBeenCalled()
    expect(setFleetInventorySource).not.toHaveBeenCalled()
  })
})

describe("engine.stop()", () => {
  test("stops the reporter before the cloud client, and a failing stop does not skip the rest", async () => {
    stopFleetReporter.mockImplementationOnce(async () => {
      calls.push("fleet.stop")
      throw new Error("reporter stop failed")
    })
    const warn = console.warn
    console.warn = () => {}
    try {
      await engine.stop()
    } finally {
      console.warn = warn
    }
    expect(stopFleetReporter).toHaveBeenCalledTimes(1)
    expect(calls.indexOf("fleet.stop")).toBeLessThan(calls.indexOf("cloud.stop"))
    expect(calls).toContain("bootstrap.stop")
  })

  test("stops the reconciler and unwires the inventory, both before the cloud client, even when the reconciler stop throws", async () => {
    stopAssignmentReconciler.mockImplementationOnce(() => {
      calls.push("reconciler.stop")
      throw new Error("reconciler stop failed")
    })
    const warn = console.warn
    console.warn = () => {}
    try {
      await engine.stop()
    } finally {
      console.warn = warn
    }
    const at = (label: string) => calls.indexOf(label)
    expect(stopAssignmentReconciler).toHaveBeenCalledTimes(1)
    expect(setFleetInventorySource).toHaveBeenCalledWith(null)
    expect(at("reconciler.stop")).toBeLessThan(at("fleet.stop"))
    expect(at("fleet.stop")).toBeLessThan(at("inventory.set"))
    expect(at("inventory.set")).toBeLessThan(at("cloud.stop"))
  })
})