File size: 1,706 Bytes
68465df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/// <reference types="bun-types" />
// eslint-disable-next-line import/no-unresolved
import {describe, expect, mock, test} from "bun:test"

function fakeEngine() {
  return {
    router: {logRing: {snapshot: () => []}, onCrashloop: null as unknown, onRestartToast: null as unknown},
    uiRouter: {bindWebView() {}, unbindWebView() {}, notifyReopen() {}},
    crashController: {},
  }
}

let current = fakeEngine()
const installed: unknown[] = []

mock.module("react-native", () => ({Platform: {OS: "android"}}))
mock.module("@sentry/react-native", () => ({captureMessage() {}, addBreadcrumb() {}}))
mock.module("@mentra/engine", () => ({engine: {miniapps: {list: () => []}}}))
mock.module("@mentra/engine-host-internal", () => ({
  ensureMiniappEngine: () => current,
  getMiniappEngine: () => current,
}))
mock.module("@/services/streamPreview", () => ({
  installStreamPreviewCoordinator: (uiRouter: unknown) => installed.push(uiRouter),
}))
mock.module("@/utils/AlertUtils", () => ({default: () => {}}))

const {bootstrapMentraJS} = await import("./mentraJsBootstrap")

describe("bootstrapMentraJS", () => {
  test("wires the stream-preview channel into a rebuilt engine after logout", () => {
    const first = current
    bootstrapMentraJS()
    bootstrapMentraJS()
    expect(installed).toEqual([first.uiRouter])

    // engine.stop() drops the singletons; the next engine.start() builds new routers.
    current = fakeEngine()
    bootstrapMentraJS()
    expect(installed).toEqual([first.uiRouter, current.uiRouter])
    expect(typeof current.router.onCrashloop).toBe("function")
    expect(typeof current.router.onRestartToast).toBe("function")
  })
})