File size: 5,300 Bytes
e1f81b0 | 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 | import {act, fireEvent, render} from "@testing-library/react-native"
import * as Clipboard from "expo-clipboard"
import {TouchableOpacity} from "react-native"
import {VersionInfo} from "./VersionInfo"
const mockSetSuperMode = jest.fn()
jest.mock("@mentra/engine", () => ({
engine: {session: {status: {audioTransport: "test"}, onStatus: jest.fn()}},
SETTINGS: {
debug_mode: {key: "debug_mode"},
super_mode: {key: "super_mode"},
cloud_core_url: {key: "cloud_core_url"},
},
useSetting: (key: string) => [key === "debug_mode", key === "super_mode" ? mockSetSuperMode : jest.fn()],
getAppBuildInfo: () => ({appVersion: "test", buildBranch: "test", buildTime: "test", buildCommit: "test"}),
}))
jest.mock("@/contexts/AuthContext", () => ({
useAuth: () => ({user: {id: "private-id", email: "private@example.test"}}),
}))
jest.mock("@/contexts/ThemeContext", () => ({useAppTheme: () => ({themed: () => ({})})}))
jest.mock("@/hooks/useEngineSnapshot", () => ({useEngineSnapshot: () => ({audioTransport: "test"})}))
jest.mock("@/i18n", () => ({translate: (key: string) => key}))
jest.mock("@/services/cloudClient", () => ({
resolvedEndpoints: () => ({core: "https://private.example.test", runtime: "test"}),
}))
jest.mock("@/utils/AlertUtils", () => ({__esModule: true, default: jest.fn()}))
jest.mock("@/components/ignite", () => ({Text: require("react-native").Text}))
jest.mock("expo-clipboard", () => ({setStringAsync: jest.fn().mockResolvedValue(undefined)}))
jest.mock("react-native-toast-message", () => ({__esModule: true, default: {show: jest.fn()}}))
let now = 0
let info: jest.SpyInstance
beforeEach(() => {
jest.useFakeTimers()
jest.clearAllMocks()
now = 0
jest.spyOn(performance, "now").mockImplementation(() => now)
info = jest.spyOn(console, "info").mockImplementation(() => {})
})
afterEach(() => {
jest.clearAllTimers()
jest.useRealTimers()
jest.restoreAllMocks()
})
it("records timer activation at the existing ten-second threshold without copying on release", () => {
const screen = render(<VersionInfo />)
const footer = screen.UNSAFE_getByType(TouchableOpacity)
fireEvent(footer, "pressIn")
now = 9999
act(() => jest.advanceTimersByTime(9999))
expect(mockSetSuperMode).not.toHaveBeenCalled()
now = 10000
act(() => jest.advanceTimersByTime(1))
fireEvent(footer, "pressOut")
expect(mockSetSuperMode).toHaveBeenCalledTimes(1)
expect(mockSetSuperMode).toHaveBeenCalledWith(true)
expect(Clipboard.setStringAsync).not.toHaveBeenCalled()
expect(info.mock.calls.map(([, fields]) => fields.phase)).toEqual([
"press_in",
"timer_requested",
"timer_fired",
"press_out",
])
expect(info).toHaveBeenCalledWith(
"[SUPER_MODE_PRESS]",
expect.objectContaining({
phase: "timer_fired",
pressId: 1,
elapsedMs: 10000,
requestedDurationMs: 10000,
timerDelayMs: 0,
}),
)
expect(info).toHaveBeenLastCalledWith(
"[SUPER_MODE_PRESS]",
expect.objectContaining({
phase: "press_out",
elapsedMs: 10000,
timerPending: false,
}),
)
})
it("records an overdue pending timer cancelled by release without claiming it fired", () => {
const screen = render(<VersionInfo />)
const footer = screen.UNSAFE_getByType(TouchableOpacity)
fireEvent(footer, "pressIn")
// Simulate the JS callback still queued when PressOut is delivered after the native hold.
now = 10500
fireEvent(footer, "pressOut")
act(() => jest.advanceTimersByTime(20000))
expect(mockSetSuperMode).not.toHaveBeenCalled()
expect(Clipboard.setStringAsync).toHaveBeenCalledTimes(1)
expect(info.mock.calls.map(([, fields]) => fields.phase)).toEqual([
"press_in",
"timer_requested",
"press_out",
"timer_cancelled",
])
expect(info).toHaveBeenCalledWith(
"[SUPER_MODE_PRESS]",
expect.objectContaining({
phase: "press_out",
pressId: 1,
elapsedMs: 10500,
timerPending: true,
}),
)
expect(info).toHaveBeenLastCalledWith(
"[SUPER_MODE_PRESS]",
expect.objectContaining({
phase: "timer_cancelled",
pressId: 1,
elapsedMs: 10500,
reason: "press_out",
}),
)
expect(JSON.stringify(info.mock.calls)).not.toMatch(/private|example\.test/)
})
it("correlates a delayed timer fire with its own press after an earlier cancellation", () => {
const screen = render(<VersionInfo />)
const footer = screen.UNSAFE_getByType(TouchableOpacity)
fireEvent(footer, "pressIn")
now = 1000
fireEvent(footer, "pressOut")
fireEvent(footer, "pressIn")
now = 12000
act(() => jest.advanceTimersByTime(10000))
fireEvent(footer, "pressOut")
expect(mockSetSuperMode).toHaveBeenCalledTimes(1)
expect(Clipboard.setStringAsync).toHaveBeenCalledTimes(1)
expect(info).toHaveBeenCalledWith(
"[SUPER_MODE_PRESS]",
expect.objectContaining({
phase: "timer_cancelled",
pressId: 1,
elapsedMs: 1000,
}),
)
expect(info).toHaveBeenCalledWith(
"[SUPER_MODE_PRESS]",
expect.objectContaining({
phase: "timer_fired",
pressId: 2,
elapsedMs: 11000,
timerDelayMs: 1000,
}),
)
})
|