|
| 1 | +/* @vitest-environment jsdom */ |
| 2 | + |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { |
| 5 | + createNativeNotificationsCapability, |
| 6 | + NATIVE_NOTIFICATIONS_STATUS_EVENT, |
| 7 | + type NativeNotificationsCapability, |
| 8 | +} from "./native-notifications.ts"; |
| 9 | + |
| 10 | +type NativeNotificationsMessage = { |
| 11 | + type: "status" | "request-permission" | "send-test"; |
| 12 | +}; |
| 13 | + |
| 14 | +type NativeNotificationsTestWindow = Window & { |
| 15 | + __OPENCLAW_NATIVE_NOTIFICATIONS__?: unknown; |
| 16 | +}; |
| 17 | + |
| 18 | +let capability: NativeNotificationsCapability | null = null; |
| 19 | + |
| 20 | +afterEach(() => { |
| 21 | + capability?.dispose(); |
| 22 | + capability = null; |
| 23 | + Reflect.deleteProperty( |
| 24 | + window as NativeNotificationsTestWindow, |
| 25 | + "__OPENCLAW_NATIVE_NOTIFICATIONS__", |
| 26 | + ); |
| 27 | + vi.unstubAllGlobals(); |
| 28 | +}); |
| 29 | + |
| 30 | +function installBridge() { |
| 31 | + const postMessage = vi.fn<(message: NativeNotificationsMessage) => void>(); |
| 32 | + vi.stubGlobal("webkit", { |
| 33 | + messageHandlers: { openclawNotifications: { postMessage } }, |
| 34 | + }); |
| 35 | + return postMessage; |
| 36 | +} |
| 37 | + |
| 38 | +describe("native notifications", () => { |
| 39 | + it("returns null without the WebKit bridge", () => { |
| 40 | + expect(createNativeNotificationsCapability()).toBeNull(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("posts status on create", () => { |
| 44 | + const postMessage = installBridge(); |
| 45 | + |
| 46 | + capability = createNativeNotificationsCapability(); |
| 47 | + |
| 48 | + expect(capability?.snapshot).toEqual({ permission: "unknown" }); |
| 49 | + expect(postMessage).toHaveBeenCalledWith({ type: "status" }); |
| 50 | + }); |
| 51 | + |
| 52 | + it("seeds status from the native snapshot", () => { |
| 53 | + installBridge(); |
| 54 | + (window as NativeNotificationsTestWindow).__OPENCLAW_NATIVE_NOTIFICATIONS__ = { |
| 55 | + permission: "granted", |
| 56 | + }; |
| 57 | + |
| 58 | + capability = createNativeNotificationsCapability(); |
| 59 | + |
| 60 | + expect(capability?.snapshot).toEqual({ permission: "granted" }); |
| 61 | + }); |
| 62 | + |
| 63 | + it("publishes valid status events", () => { |
| 64 | + installBridge(); |
| 65 | + capability = createNativeNotificationsCapability(); |
| 66 | + const listener = vi.fn(); |
| 67 | + capability?.subscribe(listener); |
| 68 | + |
| 69 | + window.dispatchEvent( |
| 70 | + new CustomEvent(NATIVE_NOTIFICATIONS_STATUS_EVENT, { |
| 71 | + detail: { permission: "denied" }, |
| 72 | + }), |
| 73 | + ); |
| 74 | + |
| 75 | + expect(capability?.snapshot).toEqual({ permission: "denied" }); |
| 76 | + expect(listener).toHaveBeenCalledWith({ permission: "denied" }); |
| 77 | + }); |
| 78 | + |
| 79 | + it("ignores invalid status event details", () => { |
| 80 | + installBridge(); |
| 81 | + capability = createNativeNotificationsCapability(); |
| 82 | + const listener = vi.fn(); |
| 83 | + capability?.subscribe(listener); |
| 84 | + |
| 85 | + window.dispatchEvent( |
| 86 | + new CustomEvent(NATIVE_NOTIFICATIONS_STATUS_EVENT, { |
| 87 | + detail: { permission: "authorized" }, |
| 88 | + }), |
| 89 | + ); |
| 90 | + |
| 91 | + expect(capability?.snapshot).toEqual({ permission: "unknown" }); |
| 92 | + expect(listener).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it("reposts status when the window focuses", () => { |
| 96 | + const postMessage = installBridge(); |
| 97 | + capability = createNativeNotificationsCapability(); |
| 98 | + postMessage.mockClear(); |
| 99 | + |
| 100 | + window.dispatchEvent(new Event("focus")); |
| 101 | + |
| 102 | + expect(postMessage).toHaveBeenCalledWith({ type: "status" }); |
| 103 | + }); |
| 104 | + |
| 105 | + it("posts permission and test actions", () => { |
| 106 | + const postMessage = installBridge(); |
| 107 | + capability = createNativeNotificationsCapability(); |
| 108 | + postMessage.mockClear(); |
| 109 | + |
| 110 | + capability?.requestPermission(); |
| 111 | + capability?.sendTest(); |
| 112 | + |
| 113 | + expect(postMessage.mock.calls).toEqual([ |
| 114 | + [{ type: "request-permission" }], |
| 115 | + [{ type: "send-test" }], |
| 116 | + ]); |
| 117 | + }); |
| 118 | + |
| 119 | + it("removes listeners on dispose", () => { |
| 120 | + const postMessage = installBridge(); |
| 121 | + capability = createNativeNotificationsCapability(); |
| 122 | + const listener = vi.fn(); |
| 123 | + capability?.subscribe(listener); |
| 124 | + capability?.dispose(); |
| 125 | + capability = null; |
| 126 | + postMessage.mockClear(); |
| 127 | + |
| 128 | + window.dispatchEvent(new Event("focus")); |
| 129 | + window.dispatchEvent( |
| 130 | + new CustomEvent(NATIVE_NOTIFICATIONS_STATUS_EVENT, { |
| 131 | + detail: { permission: "granted" }, |
| 132 | + }), |
| 133 | + ); |
| 134 | + |
| 135 | + expect(postMessage).not.toHaveBeenCalled(); |
| 136 | + expect(listener).not.toHaveBeenCalled(); |
| 137 | + }); |
| 138 | +}); |
0 commit comments