The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import { beforeEach, expect, vi } from "vite-plus/test"; |
| 3 | import { it } from "@effect/vitest"; |
| 4 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 5 | |
| 6 | const native = vi.hoisted(() => ({ |
| 7 | setBadgeCount: vi.fn(), |
| 8 | setOverlayIcon: vi.fn(), |
| 9 | isDestroyed: vi.fn(() => false), |
| 10 | getFocusedWindow: vi.fn(() => null as object | null), |
| 11 | image: { isEmpty: vi.fn(() => false) }, |
| 12 | createFromDataURL: vi.fn(), |
| 13 | webContents: { send: vi.fn() }, |
| 14 | listeners: new Map<string, () => void>(), |
| 15 | })); |
| 16 | vi.mock("electron", () => ({ |
| 17 | app: { |
| 18 | setBadgeCount: native.setBadgeCount, |
| 19 | on: (event: string, listener: () => void) => native.listeners.set(event, listener), |
| 20 | removeListener: (event: string) => native.listeners.delete(event), |
| 21 | }, |
| 22 | BrowserWindow: { |
| 23 | getFocusedWindow: native.getFocusedWindow, |
| 24 | getAllWindows: () => [native], |
| 25 | }, |
| 26 | nativeImage: { createFromDataURL: native.createFromDataURL }, |
| 27 | })); |
| 28 | |
| 29 | import * as ElectronApp from "../../electron/ElectronApp.ts"; |
| 30 | import * as DesktopIpc from "../DesktopIpc.ts"; |
| 31 | import { installNotificationBadge } from "./notificationBadge.ts"; |
| 32 | |
| 33 | const badge = { count: 2, image: "data:image/png;base64,aGVsbG8=" }; |
| 34 | |
| 35 | beforeEach(() => { |
| 36 | vi.clearAllMocks(); |
| 37 | native.getFocusedWindow.mockReturnValue(null); |
| 38 | native.isDestroyed.mockReturnValue(false); |
| 39 | native.image.isEmpty.mockReturnValue(false); |
| 40 | native.createFromDataURL.mockReturnValue(native.image); |
| 41 | native.setBadgeCount.mockImplementation(() => true); |
| 42 | native.listeners.clear(); |
| 43 | }); |
| 44 | |
| 45 | it.effect("validates IPC and clears on native focus, quit, and disposal", () => |
| 46 | Effect.gen(function* () { |
| 47 | const handlers = new Map<string, DesktopIpc.DesktopIpcHandleListener>(); |
| 48 | yield* Effect.scoped( |
| 49 | Effect.gen(function* () { |
| 50 | yield* installNotificationBadge(); |
| 51 | const handler = handlers.get("desktop:set-notification-badge")!; |
| 52 | const event = { sender: { id: 1 } }; |
| 53 | for (const invalid of [ |
| 54 | { ...badge, count: -1 }, |
| 55 | { ...badge, count: 0.5 }, |
| 56 | { ...badge, count: Infinity }, |
| 57 | { ...badge, image: "https://example.com/icon.png" }, |
| 58 | { ...badge, image: `data:image/png;base64,${"a".repeat(16_384)}` }, |
| 59 | ]) { |
| 60 | yield* Effect.promise(() => expect(handler(event, invalid)).rejects.toBeDefined()); |
| 61 | } |
| 62 | expect(native.setBadgeCount).not.toHaveBeenCalled(); |
| 63 | yield* Effect.promise(() => Promise.resolve(handler(event, badge))); |
| 64 | expect(native.setBadgeCount).toHaveBeenLastCalledWith(2); |
| 65 | native.listeners.get("browser-window-focus")!(); |
| 66 | expect(native.setBadgeCount).toHaveBeenLastCalledWith(0); |
| 67 | expect(native.webContents.send).toHaveBeenCalledWith("desktop:set-notification-badge"); |
| 68 | native.getFocusedWindow.mockReturnValue({}); |
| 69 | yield* Effect.promise(() => Promise.resolve(handler(event, badge))); |
| 70 | expect(native.setBadgeCount).toHaveBeenLastCalledWith(0); |
| 71 | expect(native.webContents.send).toHaveBeenCalledTimes(2); |
| 72 | yield* Effect.promise(() => Promise.resolve(handler(event, { count: 0, image: null }))); |
| 73 | expect(native.webContents.send).toHaveBeenCalledTimes(2); |
| 74 | native.getFocusedWindow.mockReturnValue(null); |
| 75 | yield* Effect.promise(() => Promise.resolve(handler(event, badge))); |
| 76 | native.listeners.get("before-quit")!(); |
| 77 | expect(native.setBadgeCount).toHaveBeenLastCalledWith(0); |
| 78 | }), |
| 79 | ).pipe( |
| 80 | Effect.provideService(HostProcess.Platform, "linux"), |
| 81 | Effect.provide([ |
| 82 | ElectronApp.layer, |
| 83 | DesktopIpc.layerTest({ |
| 84 | handle: (channel, handler) => { |
| 85 | handlers.set(channel, handler); |
| 86 | }, |
| 87 | removeHandler: (channel) => { |
| 88 | handlers.delete(channel); |
| 89 | }, |
| 90 | on: vi.fn(), |
| 91 | removeAllListeners: vi.fn(), |
| 92 | }), |
| 93 | ]), |
| 94 | ); |
| 95 | expect(native.setBadgeCount).toHaveBeenLastCalledWith(0); |
| 96 | expect(native.listeners.size).toBe(0); |
| 97 | expect(handlers.size).toBe(0); |
| 98 | }), |
| 99 | ); |