| 1 | import type { DesktopBridge } from "@t3tools/contracts"; |
| 2 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 3 | |
| 4 | import { |
| 5 | dispatchSnapShotComposerFocus, |
| 6 | getDesktopSnapShotBridge, |
| 7 | subscribeSnapShotComposerFocus, |
| 8 | } from "./desktopSnapShot"; |
| 9 | |
| 10 | beforeEach(() => { |
| 11 | Object.defineProperty(globalThis, "window", { |
| 12 | configurable: true, |
| 13 | value: new EventTarget(), |
| 14 | }); |
| 15 | }); |
| 16 | |
| 17 | afterEach(() => { |
| 18 | Reflect.deleteProperty(globalThis, "window"); |
| 19 | }); |
| 20 | |
| 21 | describe("getDesktopSnapShotBridge", () => { |
| 22 | it("rejects an older desktop bridge without window capture methods", () => { |
| 23 | window.desktopBridge = {} as DesktopBridge; |
| 24 | |
| 25 | expect(getDesktopSnapShotBridge()).toBeUndefined(); |
| 26 | }); |
| 27 | |
| 28 | it("returns a bridge with the complete window capture capability", () => { |
| 29 | const bridge = { |
| 30 | requestSnapShotPermissions: vi.fn(), |
| 31 | getSnapShotState: vi.fn(), |
| 32 | checkSnapShotShortcut: vi.fn(), |
| 33 | setSnapShotShortcutSuppressed: vi.fn(), |
| 34 | listPendingSnapShots: vi.fn(), |
| 35 | readSnapShot: vi.fn(), |
| 36 | acknowledgeSnapShot: vi.fn(), |
| 37 | onSnapShotEvent: vi.fn(), |
| 38 | } as unknown as DesktopBridge; |
| 39 | window.desktopBridge = bridge; |
| 40 | |
| 41 | expect(getDesktopSnapShotBridge()).toBe(bridge); |
| 42 | }); |
| 43 | }); |
| 44 | |
| 45 | describe("window capture composer focus", () => { |
| 46 | it("notifies active subscribers", () => { |
| 47 | const listener = vi.fn(); |
| 48 | const unsubscribe = subscribeSnapShotComposerFocus(listener); |
| 49 | |
| 50 | dispatchSnapShotComposerFocus(); |
| 51 | expect(listener).toHaveBeenCalledOnce(); |
| 52 | |
| 53 | unsubscribe(); |
| 54 | dispatchSnapShotComposerFocus(); |
| 55 | expect(listener).toHaveBeenCalledOnce(); |
| 56 | }); |
| 57 | }); |