|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +let mockPlatform = 'linux' |
| 4 | +let mockAppImage: string | undefined |
| 5 | +let mockIsPackaged = true |
| 6 | +let mockExePath = '/opt/ComfyUI Desktop 2.0/comfyui-desktop-2' |
| 7 | + |
| 8 | +vi.mock('electron', () => ({ |
| 9 | + app: { |
| 10 | + get isPackaged() { |
| 11 | + return mockIsPackaged |
| 12 | + }, |
| 13 | + getPath: (name: string) => { |
| 14 | + if (name === 'exe') return mockExePath |
| 15 | + return '' |
| 16 | + }, |
| 17 | + }, |
| 18 | + ipcMain: { |
| 19 | + handle: vi.fn(), |
| 20 | + }, |
| 21 | + BrowserWindow: { |
| 22 | + getAllWindows: () => [], |
| 23 | + }, |
| 24 | +})) |
| 25 | + |
| 26 | +vi.mock('@todesktop/runtime', () => ({ |
| 27 | + default: { autoUpdater: null }, |
| 28 | +})) |
| 29 | + |
| 30 | +vi.mock('../settings', () => ({ |
| 31 | + get: vi.fn(), |
| 32 | +})) |
| 33 | + |
| 34 | +vi.mock('./quit-state', () => ({ |
| 35 | + clearQuitReason: vi.fn(), |
| 36 | + setQuitReason: vi.fn(), |
| 37 | +})) |
| 38 | + |
| 39 | +const originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform')! |
| 40 | + |
| 41 | +describe('isSystemPackageInstall (via get-update-capabilities)', () => { |
| 42 | + let registeredHandlers: Record<string, (...args: unknown[]) => unknown> |
| 43 | + |
| 44 | + beforeEach(async () => { |
| 45 | + registeredHandlers = {} |
| 46 | + const { ipcMain } = await import('electron') |
| 47 | + vi.mocked(ipcMain.handle).mockImplementation(((channel: string, handler: (...args: unknown[]) => unknown) => { |
| 48 | + registeredHandlers[channel] = handler |
| 49 | + }) as typeof ipcMain.handle) |
| 50 | + |
| 51 | + mockPlatform = 'linux' |
| 52 | + mockAppImage = undefined |
| 53 | + mockIsPackaged = true |
| 54 | + mockExePath = '/opt/ComfyUI Desktop 2.0/comfyui-desktop-2' |
| 55 | + |
| 56 | + delete process.env.APPIMAGE |
| 57 | + Object.defineProperty(process, 'platform', { value: mockPlatform, configurable: true }) |
| 58 | + |
| 59 | + vi.resetModules() |
| 60 | + }) |
| 61 | + |
| 62 | + afterEach(() => { |
| 63 | + Object.defineProperty(process, 'platform', originalPlatform) |
| 64 | + }) |
| 65 | + |
| 66 | + async function getCapabilities(): Promise<{ canAutoUpdate: boolean; systemManaged: boolean }> { |
| 67 | + Object.defineProperty(process, 'platform', { value: mockPlatform, configurable: true }) |
| 68 | + if (mockAppImage) { |
| 69 | + process.env.APPIMAGE = mockAppImage |
| 70 | + } else { |
| 71 | + delete process.env.APPIMAGE |
| 72 | + } |
| 73 | + |
| 74 | + vi.resetModules() |
| 75 | + const updater = await import('./updater') |
| 76 | + updater.register() |
| 77 | + const handler = registeredHandlers['get-update-capabilities']! |
| 78 | + return handler() as { canAutoUpdate: boolean; systemManaged: boolean } |
| 79 | + } |
| 80 | + |
| 81 | + it('detects .deb install under /opt/', async () => { |
| 82 | + mockExePath = '/opt/ComfyUI Desktop 2.0/comfyui-desktop-2' |
| 83 | + const caps = await getCapabilities() |
| 84 | + expect(caps).toEqual({ canAutoUpdate: false, systemManaged: true }) |
| 85 | + }) |
| 86 | + |
| 87 | + it('detects .deb install under /usr/', async () => { |
| 88 | + mockExePath = '/usr/lib/comfyui-desktop-2/comfyui-desktop-2' |
| 89 | + const caps = await getCapabilities() |
| 90 | + expect(caps).toEqual({ canAutoUpdate: false, systemManaged: true }) |
| 91 | + }) |
| 92 | + |
| 93 | + it('returns standard for AppImage (APPIMAGE env set)', async () => { |
| 94 | + mockAppImage = '/home/user/ComfyUI-Desktop-2.0.AppImage' |
| 95 | + const caps = await getCapabilities() |
| 96 | + expect(caps).toEqual({ canAutoUpdate: true, systemManaged: false }) |
| 97 | + }) |
| 98 | + |
| 99 | + it('returns standard for Windows', async () => { |
| 100 | + mockPlatform = 'win32' |
| 101 | + const caps = await getCapabilities() |
| 102 | + expect(caps).toEqual({ canAutoUpdate: true, systemManaged: false }) |
| 103 | + }) |
| 104 | + |
| 105 | + it('returns standard for macOS', async () => { |
| 106 | + mockPlatform = 'darwin' |
| 107 | + const caps = await getCapabilities() |
| 108 | + expect(caps).toEqual({ canAutoUpdate: true, systemManaged: false }) |
| 109 | + }) |
| 110 | + |
| 111 | + it('returns standard when not packaged (dev mode)', async () => { |
| 112 | + mockIsPackaged = false |
| 113 | + const caps = await getCapabilities() |
| 114 | + expect(caps).toEqual({ canAutoUpdate: true, systemManaged: false }) |
| 115 | + }) |
| 116 | + |
| 117 | + it('returns standard for Linux exe under /home/ (manual extract)', async () => { |
| 118 | + mockExePath = '/home/user/apps/comfyui-desktop-2' |
| 119 | + const caps = await getCapabilities() |
| 120 | + expect(caps).toEqual({ canAutoUpdate: true, systemManaged: false }) |
| 121 | + }) |
| 122 | + |
| 123 | + it('returns standard for Linux exe under /tmp/ (temp location)', async () => { |
| 124 | + mockExePath = '/tmp/.mount_comfyui/comfyui-desktop-2' |
| 125 | + const caps = await getCapabilities() |
| 126 | + expect(caps).toEqual({ canAutoUpdate: true, systemManaged: false }) |
| 127 | + }) |
| 128 | +}) |
0 commit comments