|
| 1 | +/* @vitest-environment jsdom */ |
| 2 | + |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { createStorageMock } from "../test-helpers/storage.ts"; |
| 5 | +import { |
| 6 | + autoPromptNotificationsOnSend, |
| 7 | + hasActiveNotificationPromptGesture, |
| 8 | + shouldAutoPromptNotificationsOnSend, |
| 9 | +} from "./notifications-auto-prompt.ts"; |
| 10 | + |
| 11 | +const STORAGE_KEY = "openclaw.control.notificationsAutoPrompt.v1"; |
| 12 | + |
| 13 | +type AutoPromptContext = Parameters<typeof autoPromptNotificationsOnSend>[0]; |
| 14 | +type NativePermission = "granted" | "denied" | "notDetermined" | "unknown"; |
| 15 | + |
| 16 | +let storage: Storage; |
| 17 | +let browserRequestPermission: ReturnType<typeof vi.fn>; |
| 18 | + |
| 19 | +beforeEach(() => { |
| 20 | + storage = createStorageMock(); |
| 21 | + vi.stubGlobal("localStorage", storage); |
| 22 | + browserRequestPermission = vi.fn(() => Promise.resolve("granted" as NotificationPermission)); |
| 23 | + vi.stubGlobal("Notification", { requestPermission: browserRequestPermission }); |
| 24 | +}); |
| 25 | + |
| 26 | +afterEach(() => { |
| 27 | + vi.unstubAllGlobals(); |
| 28 | + vi.restoreAllMocks(); |
| 29 | +}); |
| 30 | + |
| 31 | +function createContext( |
| 32 | + overrides: { |
| 33 | + supported?: boolean; |
| 34 | + permission?: NotificationPermission | "unsupported"; |
| 35 | + subscribed?: boolean; |
| 36 | + loading?: boolean; |
| 37 | + nativePermission?: NativePermission | null; |
| 38 | + } = {}, |
| 39 | +) { |
| 40 | + const enable = vi.fn(async () => undefined); |
| 41 | + const requestPermission = vi.fn(); |
| 42 | + const nativePermission = overrides.nativePermission ?? null; |
| 43 | + const context = { |
| 44 | + nativeNotifications: |
| 45 | + nativePermission === null |
| 46 | + ? null |
| 47 | + : { snapshot: { permission: nativePermission }, requestPermission }, |
| 48 | + webPush: { |
| 49 | + snapshot: { |
| 50 | + supported: overrides.supported ?? true, |
| 51 | + permission: overrides.permission ?? "default", |
| 52 | + subscribed: overrides.subscribed ?? false, |
| 53 | + loading: overrides.loading ?? false, |
| 54 | + error: null, |
| 55 | + }, |
| 56 | + enable, |
| 57 | + }, |
| 58 | + } as unknown as AutoPromptContext; |
| 59 | + return { context, enable, requestPermission }; |
| 60 | +} |
| 61 | + |
| 62 | +describe("notification auto-prompt", () => { |
| 63 | + it("requests browser permission synchronously and enables web push only once", async () => { |
| 64 | + const { context, enable } = createContext(); |
| 65 | + |
| 66 | + autoPromptNotificationsOnSend(context); |
| 67 | + autoPromptNotificationsOnSend(context); |
| 68 | + |
| 69 | + expect(browserRequestPermission).toHaveBeenCalledOnce(); |
| 70 | + expect(enable).not.toHaveBeenCalled(); |
| 71 | + await Promise.resolve(); |
| 72 | + expect(enable).toHaveBeenCalledOnce(); |
| 73 | + expect(storage.getItem(STORAGE_KEY)).toBe("1"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("records a dismissed browser prompt without enabling web push", async () => { |
| 77 | + browserRequestPermission.mockResolvedValue("default"); |
| 78 | + const { context, enable } = createContext(); |
| 79 | + |
| 80 | + autoPromptNotificationsOnSend(context); |
| 81 | + autoPromptNotificationsOnSend(context); |
| 82 | + await Promise.resolve(); |
| 83 | + |
| 84 | + expect(browserRequestPermission).toHaveBeenCalledOnce(); |
| 85 | + expect(enable).not.toHaveBeenCalled(); |
| 86 | + expect(storage.getItem(STORAGE_KEY)).toBe("1"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("does nothing when the one-shot flag is already set", () => { |
| 90 | + storage.setItem(STORAGE_KEY, "1"); |
| 91 | + const { context, enable } = createContext(); |
| 92 | + |
| 93 | + autoPromptNotificationsOnSend(context); |
| 94 | + |
| 95 | + expect(enable).not.toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it.each(["denied", "granted"] as const)( |
| 99 | + "does not enable web push when permission is %s", |
| 100 | + (permission) => { |
| 101 | + const { context, enable } = createContext({ permission }); |
| 102 | + |
| 103 | + autoPromptNotificationsOnSend(context); |
| 104 | + |
| 105 | + expect(enable).not.toHaveBeenCalled(); |
| 106 | + expect(storage.getItem(STORAGE_KEY)).toBeNull(); |
| 107 | + }, |
| 108 | + ); |
| 109 | + |
| 110 | + it.each([ |
| 111 | + ["subscribed", { subscribed: true }], |
| 112 | + ["loading", { loading: true }], |
| 113 | + ["unsupported", { supported: false, permission: "unsupported" as const }], |
| 114 | + ])("does not enable web push when it is %s", (_name, overrides) => { |
| 115 | + const { context, enable } = createContext(overrides); |
| 116 | + |
| 117 | + autoPromptNotificationsOnSend(context); |
| 118 | + |
| 119 | + expect(enable).not.toHaveBeenCalled(); |
| 120 | + expect(storage.getItem(STORAGE_KEY)).toBeNull(); |
| 121 | + }); |
| 122 | + |
| 123 | + it("prefers the native permission flow when permission is not determined", () => { |
| 124 | + const { context, enable, requestPermission } = createContext({ |
| 125 | + nativePermission: "notDetermined", |
| 126 | + }); |
| 127 | + |
| 128 | + autoPromptNotificationsOnSend(context); |
| 129 | + |
| 130 | + expect(requestPermission).toHaveBeenCalledOnce(); |
| 131 | + expect(enable).not.toHaveBeenCalled(); |
| 132 | + expect(storage.getItem(STORAGE_KEY)).toBe("1"); |
| 133 | + }); |
| 134 | + |
| 135 | + it.each(["denied", "unknown"] as const)( |
| 136 | + "does not request native permission when it is %s", |
| 137 | + (nativePermission) => { |
| 138 | + const { context, enable, requestPermission } = createContext({ nativePermission }); |
| 139 | + |
| 140 | + autoPromptNotificationsOnSend(context); |
| 141 | + |
| 142 | + expect(requestPermission).not.toHaveBeenCalled(); |
| 143 | + expect(enable).not.toHaveBeenCalled(); |
| 144 | + expect(storage.getItem(STORAGE_KEY)).toBeNull(); |
| 145 | + }, |
| 146 | + ); |
| 147 | + |
| 148 | + it("fails closed when the localStorage getter throws", () => { |
| 149 | + Object.defineProperty(globalThis, "localStorage", { |
| 150 | + configurable: true, |
| 151 | + get() { |
| 152 | + throw new Error("opaque origin"); |
| 153 | + }, |
| 154 | + }); |
| 155 | + const { context, enable, requestPermission } = createContext({ |
| 156 | + nativePermission: "notDetermined", |
| 157 | + }); |
| 158 | + |
| 159 | + expect(() => autoPromptNotificationsOnSend(context)).not.toThrow(); |
| 160 | + expect(requestPermission).not.toHaveBeenCalled(); |
| 161 | + expect(enable).not.toHaveBeenCalled(); |
| 162 | + }); |
| 163 | +}); |
| 164 | + |
| 165 | +describe("notification auto-prompt send boundary", () => { |
| 166 | + const candidate = { |
| 167 | + connected: true, |
| 168 | + directComposerSend: true, |
| 169 | + message: "hello", |
| 170 | + hasAttachments: false, |
| 171 | + isCommand: false, |
| 172 | + }; |
| 173 | + |
| 174 | + it("accepts a direct composer prompt or attachment", () => { |
| 175 | + expect(shouldAutoPromptNotificationsOnSend(candidate)).toBe(true); |
| 176 | + expect( |
| 177 | + shouldAutoPromptNotificationsOnSend({ ...candidate, message: "", hasAttachments: true }), |
| 178 | + ).toBe(true); |
| 179 | + }); |
| 180 | + |
| 181 | + it("recognizes only the synchronous browser event dispatch", async () => { |
| 182 | + const button = document.createElement("button"); |
| 183 | + let duringDispatch = false; |
| 184 | + let afterDispatch = true; |
| 185 | + button.addEventListener("click", () => { |
| 186 | + duringDispatch = hasActiveNotificationPromptGesture(); |
| 187 | + queueMicrotask(() => { |
| 188 | + afterDispatch = hasActiveNotificationPromptGesture(); |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + button.click(); |
| 193 | + await Promise.resolve(); |
| 194 | + |
| 195 | + expect(duringDispatch).toBe(true); |
| 196 | + expect(afterDispatch).toBe(false); |
| 197 | + }); |
| 198 | + |
| 199 | + it.each([ |
| 200 | + ["programmatic send", { directComposerSend: false }], |
| 201 | + ["recognized command", { isCommand: true }], |
| 202 | + ["disconnected composer", { connected: false }], |
| 203 | + ["empty composer", { message: "" }], |
| 204 | + ])("rejects a %s", (_name, override) => { |
| 205 | + expect(shouldAutoPromptNotificationsOnSend({ ...candidate, ...override })).toBe(false); |
| 206 | + }); |
| 207 | +}); |
0 commit comments