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