| 1 | import { |
| 2 | DEFAULT_CLIENT_SETTINGS, |
| 3 | type ConfirmDialogOptions, |
| 4 | type ContextMenuItem, |
| 5 | type DesktopBridge, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 8 | |
| 9 | const showContextMenuFallbackMock = |
| 10 | vi.fn< |
| 11 | <T extends string>( |
| 12 | items: readonly ContextMenuItem<T>[], |
| 13 | position?: { x: number; y: number }, |
| 14 | ) => Promise<T | null> |
| 15 | >(); |
| 16 | const dismissContextMenuMock = vi.fn<() => void>(); |
| 17 | |
| 18 | const requestConfirmDialogMock = |
| 19 | vi.fn<(message: string, options?: ConfirmDialogOptions) => Promise<boolean> | undefined>(); |
| 20 | |
| 21 | vi.mock("./contextMenuFallback", () => ({ |
| 22 | showContextMenuFallback: showContextMenuFallbackMock, |
| 23 | dismissContextMenu: dismissContextMenuMock, |
| 24 | })); |
| 25 | |
| 26 | vi.mock("./confirmDialog", () => ({ |
| 27 | requestConfirmDialog: requestConfirmDialogMock, |
| 28 | })); |
| 29 | |
| 30 | function createLocalStorageStub(): Storage { |
| 31 | const values = new Map<string, string>(); |
| 32 | return { |
| 33 | getItem: (key) => values.get(key) ?? null, |
| 34 | setItem: (key, value) => { |
| 35 | values.set(key, value); |
| 36 | }, |
| 37 | removeItem: (key) => { |
| 38 | values.delete(key); |
| 39 | }, |
| 40 | clear: () => values.clear(), |
| 41 | key: (index) => [...values.keys()][index] ?? null, |
| 42 | get length() { |
| 43 | return values.size; |
| 44 | }, |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | function testWindow(): Window & typeof globalThis { |
| 49 | return globalThis.window ?? (globalThis as unknown as Window & typeof globalThis); |
| 50 | } |
| 51 | |
| 52 | beforeEach(() => { |
| 53 | vi.resetModules(); |
| 54 | vi.clearAllMocks(); |
| 55 | if (globalThis.window === undefined) { |
| 56 | Object.defineProperty(globalThis, "window", { |
| 57 | configurable: true, |
| 58 | value: globalThis, |
| 59 | }); |
| 60 | } |
| 61 | Reflect.deleteProperty(testWindow(), "desktopBridge"); |
| 62 | Object.defineProperty(testWindow(), "localStorage", { |
| 63 | configurable: true, |
| 64 | value: createLocalStorageStub(), |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | afterEach(() => { |
| 69 | vi.restoreAllMocks(); |
| 70 | }); |
| 71 | |
| 72 | describe("LocalApi", () => { |
| 73 | it("keeps backend operations out of the local host facade", async () => { |
| 74 | const { createLocalApi } = await import("./localApi"); |
| 75 | const api = createLocalApi(); |
| 76 | |
| 77 | expect(api).not.toHaveProperty("server"); |
| 78 | expect(api.shell).not.toHaveProperty("openInEditor"); |
| 79 | }); |
| 80 | |
| 81 | it("uses the browser context-menu fallback without a desktop bridge", async () => { |
| 82 | showContextMenuFallbackMock.mockResolvedValue("rename"); |
| 83 | const { createLocalApi } = await import("./localApi"); |
| 84 | const items = [{ id: "rename", label: "Rename" }] as const; |
| 85 | |
| 86 | await expect(createLocalApi().contextMenu.show(items, { x: 4, y: 5 })).resolves.toBe("rename"); |
| 87 | expect(showContextMenuFallbackMock).toHaveBeenCalledWith(items, { x: 4, y: 5 }); |
| 88 | }); |
| 89 | |
| 90 | it("dismisses an open browser context menu without a desktop bridge", async () => { |
| 91 | const { createLocalApi } = await import("./localApi"); |
| 92 | |
| 93 | await createLocalApi().contextMenu.close(); |
| 94 | |
| 95 | expect(dismissContextMenuMock).toHaveBeenCalledOnce(); |
| 96 | }); |
| 97 | |
| 98 | it("uses the themed confirmation host when it is available", async () => { |
| 99 | requestConfirmDialogMock.mockResolvedValue(true); |
| 100 | const { createLocalApi } = await import("./localApi"); |
| 101 | const options = { variant: "destructive" } as const; |
| 102 | |
| 103 | await expect(createLocalApi().dialogs.confirm("Delete this thread?", options)).resolves.toBe( |
| 104 | true, |
| 105 | ); |
| 106 | expect(requestConfirmDialogMock).toHaveBeenCalledWith("Delete this thread?", options); |
| 107 | }); |
| 108 | |
| 109 | it("fails closed in a browser when no themed host is available", async () => { |
| 110 | requestConfirmDialogMock.mockReturnValue(undefined); |
| 111 | const { createLocalApi } = await import("./localApi"); |
| 112 | |
| 113 | await expect(createLocalApi().dialogs.confirm("Delete this thread?")).resolves.toBe(false); |
| 114 | }); |
| 115 | |
| 116 | it("rejects opening System Settings when the desktop bridge is unavailable", async () => { |
| 117 | const { createLocalApi } = await import("./localApi"); |
| 118 | |
| 119 | await expect(createLocalApi().shell.openSystemSettings("full-disk-access")).rejects.toThrow( |
| 120 | "Unable to open System Settings.", |
| 121 | ); |
| 122 | }); |
| 123 | |
| 124 | it("delegates host capabilities and persistence to the desktop bridge", async () => { |
| 125 | const showContextMenu = vi.fn().mockResolvedValue("delete"); |
| 126 | const pickFolder = vi.fn().mockResolvedValue("/tmp/project"); |
| 127 | const getClientSettings = vi.fn().mockResolvedValue(DEFAULT_CLIENT_SETTINGS); |
| 128 | const setClientSettings = vi.fn().mockResolvedValue(undefined); |
| 129 | testWindow().desktopBridge = { |
| 130 | showContextMenu, |
| 131 | pickFolder, |
| 132 | getClientSettings, |
| 133 | setClientSettings, |
| 134 | } as unknown as DesktopBridge; |
| 135 | |
| 136 | const { createLocalApi } = await import("./localApi"); |
| 137 | const api = createLocalApi(); |
| 138 | const items = [{ id: "delete", label: "Delete" }] as const; |
| 139 | |
| 140 | await expect(api.contextMenu.show(items)).resolves.toBe("delete"); |
| 141 | requestConfirmDialogMock.mockReturnValue(undefined); |
| 142 | await expect(api.dialogs.confirm("Install update?")).resolves.toBe(false); |
| 143 | await expect(api.dialogs.pickFolder({ initialPath: "/tmp" })).resolves.toBe("/tmp/project"); |
| 144 | await expect(api.persistence.getClientSettings()).resolves.toEqual(DEFAULT_CLIENT_SETTINGS); |
| 145 | await api.persistence.setClientSettings(DEFAULT_CLIENT_SETTINGS); |
| 146 | |
| 147 | expect(showContextMenu).toHaveBeenCalledWith(items, undefined); |
| 148 | expect(pickFolder).toHaveBeenCalledWith({ initialPath: "/tmp" }); |
| 149 | expect(getClientSettings).toHaveBeenCalledTimes(1); |
| 150 | expect(setClientSettings).toHaveBeenCalledWith(DEFAULT_CLIENT_SETTINGS); |
| 151 | }); |
| 152 | |
| 153 | it("persists client settings in browser storage", async () => { |
| 154 | const { createLocalApi } = await import("./localApi"); |
| 155 | const api = createLocalApi(); |
| 156 | const settings = { |
| 157 | ...DEFAULT_CLIENT_SETTINGS, |
| 158 | timestampFormat: "12-hour" as const, |
| 159 | }; |
| 160 | |
| 161 | await api.persistence.setClientSettings(settings); |
| 162 | await expect(api.persistence.getClientSettings()).resolves.toEqual(settings); |
| 163 | }); |
| 164 | }); |