|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +import { |
| 4 | + isPermissionGranted, |
| 5 | + permissionStatusFor, |
| 6 | + requestAndVerifyPermission, |
| 7 | +} from "~/utils/os-permissions"; |
| 8 | + |
| 9 | +describe("os-permissions", () => { |
| 10 | + it("treats only granted and not-needed statuses as permitted", () => { |
| 11 | + expect(isPermissionGranted("granted")).toBe(true); |
| 12 | + expect(isPermissionGranted("notNeeded")).toBe(true); |
| 13 | + expect(isPermissionGranted("empty")).toBe(false); |
| 14 | + expect(isPermissionGranted("denied")).toBe(false); |
| 15 | + }); |
| 16 | + |
| 17 | + it("maps a permission key to the matching OS permission status", () => { |
| 18 | + const check = { |
| 19 | + screenRecording: "granted", |
| 20 | + microphone: "empty", |
| 21 | + camera: "denied", |
| 22 | + accessibility: "notNeeded", |
| 23 | + } as const; |
| 24 | + |
| 25 | + expect(permissionStatusFor(check, "screenRecording")).toBe("granted"); |
| 26 | + expect(permissionStatusFor(check, "microphone")).toBe("empty"); |
| 27 | + expect(permissionStatusFor(check, "camera")).toBe("denied"); |
| 28 | + expect(permissionStatusFor(check, "accessibility")).toBe("notNeeded"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("does not open settings after a successful permission request", async () => { |
| 32 | + const client = { |
| 33 | + requestPermission: vi.fn().mockResolvedValue(undefined), |
| 34 | + openPermissionSettings: vi.fn().mockResolvedValue(undefined), |
| 35 | + doPermissionsCheck: vi.fn().mockResolvedValue({ |
| 36 | + screenRecording: "empty", |
| 37 | + microphone: "granted", |
| 38 | + camera: "empty", |
| 39 | + accessibility: "empty", |
| 40 | + }), |
| 41 | + }; |
| 42 | + |
| 43 | + const result = await requestAndVerifyPermission(client, "microphone"); |
| 44 | + |
| 45 | + expect(client.requestPermission).toHaveBeenCalledWith("microphone"); |
| 46 | + expect(client.openPermissionSettings).not.toHaveBeenCalled(); |
| 47 | + expect(result.status).toBe("granted"); |
| 48 | + expect(result.openedSettings).toBe(false); |
| 49 | + }); |
| 50 | + |
| 51 | + it("opens settings when the OS still reports the permission as ungranted", async () => { |
| 52 | + const client = { |
| 53 | + requestPermission: vi.fn().mockResolvedValue(undefined), |
| 54 | + openPermissionSettings: vi.fn().mockResolvedValue(undefined), |
| 55 | + doPermissionsCheck: vi.fn().mockResolvedValue({ |
| 56 | + screenRecording: "denied", |
| 57 | + microphone: "empty", |
| 58 | + camera: "empty", |
| 59 | + accessibility: "empty", |
| 60 | + }), |
| 61 | + }; |
| 62 | + |
| 63 | + const result = await requestAndVerifyPermission(client, "screenRecording"); |
| 64 | + |
| 65 | + expect(client.requestPermission).toHaveBeenCalledWith("screenRecording"); |
| 66 | + expect(client.openPermissionSettings).toHaveBeenCalledWith( |
| 67 | + "screenRecording", |
| 68 | + ); |
| 69 | + expect(result.status).toBe("denied"); |
| 70 | + expect(result.openedSettings).toBe(true); |
| 71 | + }); |
| 72 | + |
| 73 | + it("skips the native request and goes straight to settings for denied permissions", async () => { |
| 74 | + const client = { |
| 75 | + requestPermission: vi.fn().mockResolvedValue(undefined), |
| 76 | + openPermissionSettings: vi.fn().mockResolvedValue(undefined), |
| 77 | + doPermissionsCheck: vi.fn().mockResolvedValue({ |
| 78 | + screenRecording: "empty", |
| 79 | + microphone: "empty", |
| 80 | + camera: "empty", |
| 81 | + accessibility: "denied", |
| 82 | + }), |
| 83 | + }; |
| 84 | + |
| 85 | + const result = await requestAndVerifyPermission( |
| 86 | + client, |
| 87 | + "accessibility", |
| 88 | + "denied", |
| 89 | + ); |
| 90 | + |
| 91 | + expect(client.requestPermission).not.toHaveBeenCalled(); |
| 92 | + expect(client.openPermissionSettings).toHaveBeenCalledWith("accessibility"); |
| 93 | + expect(result.status).toBe("denied"); |
| 94 | + expect(result.openedSettings).toBe(true); |
| 95 | + }); |
| 96 | +}); |
0 commit comments