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 { afterEach, beforeEach, expect, it, vi } from "vite-plus/test"; |
| 2 | import { MacPermissionHelper } from "./MacPermissionHelper.ts"; |
| 3 | import type { SettingsWindow } from "./MacSettingsWindow.ts"; |
| 4 | |
| 5 | const mocks = vi.hoisted(() => ({ |
| 6 | granted: false, |
| 7 | createFromPath: vi.fn(), |
| 8 | startDrag: vi.fn(), |
| 9 | showItemInFolder: vi.fn(), |
| 10 | send: vi.fn(), |
| 11 | loadURL: vi.fn(), |
| 12 | stopTracking: vi.fn(), |
| 13 | trackingFailed: undefined as (() => void) | undefined, |
| 14 | settingsChanged: undefined as ((state: SettingsWindow) => void) | undefined, |
| 15 | })); |
| 16 | const windows = vi.hoisted( |
| 17 | () => |
| 18 | [] as Array<{ |
| 19 | destroyed: boolean; |
| 20 | webContents: { mainFrame: object }; |
| 21 | setBounds: ReturnType<typeof vi.fn>; |
| 22 | hide: ReturnType<typeof vi.fn>; |
| 23 | showInactive: ReturnType<typeof vi.fn>; |
| 24 | }>, |
| 25 | ); |
| 26 | vi.mock("electron", async () => { |
| 27 | const { EventEmitter } = await import("node:events"); |
| 28 | class MockWindow extends EventEmitter { |
| 29 | destroyed = false; |
| 30 | webContents = Object.assign(new EventEmitter(), { |
| 31 | mainFrame: {}, |
| 32 | startDrag: mocks.startDrag, |
| 33 | send: mocks.send, |
| 34 | setWindowOpenHandler: vi.fn(), |
| 35 | }); |
| 36 | constructor(_options: unknown) { |
| 37 | super(); |
| 38 | windows.push(this); |
| 39 | } |
| 40 | isDestroyed() { |
| 41 | return this.destroyed; |
| 42 | } |
| 43 | destroy() { |
| 44 | this.destroyed = true; |
| 45 | this.emit("closed"); |
| 46 | } |
| 47 | close() { |
| 48 | this.destroy(); |
| 49 | } |
| 50 | loadURL = mocks.loadURL; |
| 51 | showInactive = vi.fn(); |
| 52 | hide = vi.fn(); |
| 53 | setBounds = vi.fn(); |
| 54 | isVisible = () => false; |
| 55 | isFocused = () => false; |
| 56 | show = vi.fn(); |
| 57 | focus = vi.fn(); |
| 58 | getBounds = () => ({ x: 0, y: 0, width: 800, height: 600 }); |
| 59 | } |
| 60 | return { |
| 61 | app: { |
| 62 | getPath: () => "/Applications/T3 Code (Nightly).app/Contents/MacOS/T3 Code", |
| 63 | }, |
| 64 | nativeImage: { createFromPath: mocks.createFromPath }, |
| 65 | BrowserWindow: class extends MockWindow {}, |
| 66 | ipcMain: new EventEmitter(), |
| 67 | screen: { |
| 68 | getDisplayMatching: () => ({ workArea: { x: 0, y: 0, width: 1200, height: 900 } }), |
| 69 | getCursorScreenPoint: () => ({ x: 10, y: 10 }), |
| 70 | getDisplayNearestPoint: () => ({ workArea: { x: -1200, y: 0, width: 1200, height: 900 } }), |
| 71 | }, |
| 72 | systemPreferences: { |
| 73 | getMediaAccessStatus: () => (mocks.granted ? "granted" : "denied"), |
| 74 | isTrustedAccessibilityClient: () => mocks.granted, |
| 75 | }, |
| 76 | shell: { showItemInFolder: mocks.showItemInFolder }, |
| 77 | }; |
| 78 | }); |
| 79 | vi.mock("./MacSettingsWindow.ts", async (importOriginal) => { |
| 80 | const actual = await importOriginal<typeof import("./MacSettingsWindow.ts")>(); |
| 81 | return { |
| 82 | ...actual, |
| 83 | watchMacSettingsWindow: ( |
| 84 | onChange: (state: SettingsWindow) => void, |
| 85 | onUnavailable: () => void, |
| 86 | ) => { |
| 87 | mocks.settingsChanged = onChange; |
| 88 | mocks.trackingFailed = onUnavailable; |
| 89 | return mocks.stopTracking; |
| 90 | }, |
| 91 | }; |
| 92 | }); |
| 93 | let helper: MacPermissionHelper; |
| 94 | beforeEach(() => { |
| 95 | vi.useFakeTimers(); |
| 96 | vi.clearAllMocks(); |
| 97 | mocks.granted = false; |
| 98 | const icon = { toDataURL: () => "data:image/png;base64,abc" }; |
| 99 | mocks.createFromPath.mockReturnValue({ isEmpty: () => false, resize: () => icon }); |
| 100 | mocks.loadURL.mockResolvedValue(undefined); |
| 101 | windows.length = 0; |
| 102 | helper = new MacPermissionHelper(); |
| 103 | }); |
| 104 | afterEach(() => { |
| 105 | helper.close(); |
| 106 | vi.useRealTimers(); |
| 107 | }); |
| 108 | const iconPaths = ["/bundle/prod-resources/icon.png"]; |
| 109 | const open = () => |
| 110 | helper.show("accessibility", "/bundle/mac-permission-preload.cjs", null, iconPaths); |
| 111 | it("does not reopen a superseded helper when its initial probe completes", async () => { |
| 112 | const pending = Promise.withResolvers<boolean>(); |
| 113 | const first = helper.show( |
| 114 | "full-disk-access", |
| 115 | "/bundle/mac-permission-preload.cjs", |
| 116 | null, |
| 117 | iconPaths, |
| 118 | () => pending.promise, |
| 119 | ); |
| 120 | await open(); |
| 121 | pending.resolve(false); |
| 122 | await first; |
| 123 | expect(windows).toHaveLength(1); |
| 124 | expect(windows[0]!.destroyed).toBe(false); |
| 125 | }); |