MentraOS / mobile /src /utils /pairing /preparePairingScan.test.ts
Anirudh1950's picture
Add files using upload-large-folder tool
68465df verified
Raw History Blame Contribute Delete
6.31 kB
import {PermissionsAndroid, Platform} from "react-native"
import {engine} from "@mentra/engine"
import {showAlert} from "@/utils/AlertUtils"
import {PermissionFeatures, checkConnectivityRequirementsUI, requestFeaturePermissions} from "@/utils/PermissionsUtils"
import {preparePairingScan} from "./preparePairingScan"
jest.mock("@mentra/engine", () => ({
DeviceTypes: {SIMULATED: "Simulated"},
engine: {miniapps: {stopAll: jest.fn()}},
}))
jest.mock("@/i18n", () => ({translate: (key: string) => key}))
jest.mock("@/utils/AlertUtils", () => ({showAlert: jest.fn()}))
jest.mock("@/utils/PermissionsUtils", () => ({
PermissionFeatures: {
BLUETOOTH: "bluetooth",
LOCATION: "location",
MICROPHONE: "microphone",
PHONE_STATE: "phone_state",
},
checkConnectivityRequirementsUI: jest.fn(),
requestFeaturePermissions: jest.fn(),
}))
const originalPlatformOS = Platform.OS
const originalPlatformVersion = Platform.Version
describe("preparePairingScan", () => {
beforeEach(() => {
jest.clearAllMocks()
Object.defineProperty(Platform, "OS", {value: "ios", configurable: true})
;(checkConnectivityRequirementsUI as jest.Mock).mockResolvedValue(true)
;(requestFeaturePermissions as jest.Mock).mockResolvedValue(true)
;(engine.miniapps.stopAll as jest.Mock).mockResolvedValue(undefined)
})
afterAll(() => {
Object.defineProperty(Platform, "OS", {value: originalPlatformOS, configurable: true})
Object.defineProperty(Platform, "Version", {value: originalPlatformVersion, configurable: true})
})
it("checks iOS connectivity and permissions before allowing the scan", async () => {
await expect(preparePairingScan("Mentra Live")).resolves.toBe(true)
expect(checkConnectivityRequirementsUI).toHaveBeenCalledTimes(1)
expect(requestFeaturePermissions).toHaveBeenNthCalledWith(1, PermissionFeatures.BLUETOOTH)
expect(requestFeaturePermissions).toHaveBeenCalledTimes(1)
expect(engine.miniapps.stopAll).toHaveBeenCalledTimes(1)
})
it.each(["Mentra Live", "Even Realities G1", "Simulated Glasses"])(
"allows iOS pairing for %s without phone microphone access",
async (model) => {
;(requestFeaturePermissions as jest.Mock).mockImplementation(
async (feature) => feature !== PermissionFeatures.MICROPHONE,
)
await expect(preparePairingScan(model)).resolves.toBe(true)
expect(requestFeaturePermissions).not.toHaveBeenCalledWith(PermissionFeatures.MICROPHONE)
expect(showAlert).not.toHaveBeenCalled()
},
)
it("does not stop miniapps or continue when connectivity is unavailable", async () => {
;(checkConnectivityRequirementsUI as jest.Mock).mockResolvedValue(false)
await expect(preparePairingScan("Mentra Live")).resolves.toBe(false)
expect(requestFeaturePermissions).not.toHaveBeenCalled()
expect(engine.miniapps.stopAll).not.toHaveBeenCalled()
})
it("surfaces a Bluetooth denial and leaves pairing where it started", async () => {
;(requestFeaturePermissions as jest.Mock).mockResolvedValueOnce(false)
await expect(preparePairingScan("Mentra Live")).resolves.toBe(false)
expect(showAlert).toHaveBeenCalledWith(
"pairing:bluetoothPermissionRequiredTitle",
"pairing:bluetoothPermissionRequiredMessageAlt",
[{text: "common:ok"}],
)
expect(engine.miniapps.stopAll).not.toHaveBeenCalled()
})
it.each([30, 33, 36])("allows Android %s pairing without microphone access", async (version) => {
Object.defineProperty(Platform, "OS", {value: "android", configurable: true})
Object.defineProperty(Platform, "Version", {value: version, configurable: true})
const requestMultiple = jest
.spyOn(PermissionsAndroid, "requestMultiple")
.mockImplementation(
async (permissions) =>
Object.fromEntries(
permissions.map((permission) => [permission, PermissionsAndroid.RESULTS.GRANTED]),
) as Awaited<ReturnType<typeof PermissionsAndroid.requestMultiple>>,
)
;(requestFeaturePermissions as jest.Mock).mockImplementation(
async (feature) => feature !== PermissionFeatures.MICROPHONE,
)
for (const model of ["Mentra Live", "Even Realities G1", "Simulated Glasses"]) {
await expect(preparePairingScan(model)).resolves.toBe(true)
}
expect(engine.miniapps.stopAll).toHaveBeenCalledTimes(3)
expect(requestFeaturePermissions).not.toHaveBeenCalledWith(PermissionFeatures.MICROPHONE)
expect(requestFeaturePermissions).toHaveBeenCalledWith(PermissionFeatures.LOCATION)
requestMultiple.mockRestore()
})
it("preserves the Android permission order before checking connectivity", async () => {
Object.defineProperty(Platform, "OS", {value: "android", configurable: true})
Object.defineProperty(Platform, "Version", {value: 33, configurable: true})
const grantedBluetoothPermissions = {
[PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN]: PermissionsAndroid.RESULTS.GRANTED,
[PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT]: PermissionsAndroid.RESULTS.GRANTED,
[PermissionsAndroid.PERMISSIONS.BLUETOOTH_ADVERTISE]: PermissionsAndroid.RESULTS.GRANTED,
} as Awaited<ReturnType<typeof PermissionsAndroid.requestMultiple>>
const requestMultiple = jest
.spyOn(PermissionsAndroid, "requestMultiple")
.mockResolvedValue(grantedBluetoothPermissions)
await expect(preparePairingScan("Mentra Live")).resolves.toBe(true)
expect(requestFeaturePermissions).toHaveBeenNthCalledWith(1, PermissionFeatures.PHONE_STATE)
expect(requestMultiple).toHaveBeenCalledWith([
PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN,
PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT,
PermissionsAndroid.PERMISSIONS.BLUETOOTH_ADVERTISE,
])
expect(requestFeaturePermissions).toHaveBeenNthCalledWith(2, PermissionFeatures.BLUETOOTH)
expect(requestFeaturePermissions).toHaveBeenNthCalledWith(3, PermissionFeatures.LOCATION)
expect(requestFeaturePermissions).not.toHaveBeenCalledWith(PermissionFeatures.MICROPHONE)
expect(checkConnectivityRequirementsUI).toHaveBeenCalledTimes(1)
requestMultiple.mockRestore()
})
})