|
| 1 | +import { mkdtempSync } from 'node:fs' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import { join } from 'node:path' |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +vi.mock('electron', () => import('@/test/electron-mock')) |
| 7 | + |
| 8 | +import { app, BrowserWindow } from 'electron' |
| 9 | +import { createConfigStore } from '@/main/config' |
| 10 | +import { createDesktopSettingsService } from '@/main/desktop-settings' |
| 11 | +import { Notification } from '@/test/electron-mock' |
| 12 | + |
| 13 | +function makeService() { |
| 14 | + const config = createConfigStore( |
| 15 | + join(mkdtempSync(join(tmpdir(), 'sim-desktop-settings-')), 'settings.json'), |
| 16 | + {} |
| 17 | + ) |
| 18 | + const window = new BrowserWindow() |
| 19 | + const openMainWindowAt = vi.fn() |
| 20 | + const setAutoDownloadUpdates = vi.fn() |
| 21 | + const service = createDesktopSettingsService({ |
| 22 | + config, |
| 23 | + getMainWindow: () => window, |
| 24 | + openMainWindowAt, |
| 25 | + setAutoDownloadUpdates, |
| 26 | + }) |
| 27 | + return { config, window, openMainWindowAt, setAutoDownloadUpdates, service } |
| 28 | +} |
| 29 | + |
| 30 | +describe('desktop settings service', () => { |
| 31 | + beforeEach(() => { |
| 32 | + Notification.instances.length = 0 |
| 33 | + Notification.isSupported.mockReturnValue(true) |
| 34 | + vi.mocked(app.setLoginItemSettings).mockClear() |
| 35 | + Object.defineProperty(app, 'isPackaged', { configurable: true, value: false }) |
| 36 | + }) |
| 37 | + |
| 38 | + it('persists preferences and applies live updater changes', () => { |
| 39 | + const { config, service, setAutoDownloadUpdates } = makeService() |
| 40 | + expect(service.getPreferences()).toMatchObject({ |
| 41 | + notificationsEnabled: true, |
| 42 | + notificationsOnlyWhenUnfocused: true, |
| 43 | + autoDownloadUpdates: true, |
| 44 | + }) |
| 45 | + |
| 46 | + service.setPreference('autoDownloadUpdates', false) |
| 47 | + expect(config.get('autoDownloadUpdates')).toBe(false) |
| 48 | + expect(setAutoDownloadUpdates).toHaveBeenCalledWith(false) |
| 49 | + }) |
| 50 | + |
| 51 | + it('applies login-item changes only for packaged builds', () => { |
| 52 | + const { service } = makeService() |
| 53 | + service.setPreference('launchAtLogin', true) |
| 54 | + expect(app.setLoginItemSettings).not.toHaveBeenCalled() |
| 55 | + |
| 56 | + Object.defineProperty(app, 'isPackaged', { configurable: true, value: true }) |
| 57 | + service.setPreference('launchAtLogin', false) |
| 58 | + expect(app.setLoginItemSettings).toHaveBeenCalledWith({ openAtLogin: false }) |
| 59 | + }) |
| 60 | + |
| 61 | + it('shows notifications only when allowed and opens their route on click', () => { |
| 62 | + const { window, openMainWindowAt, service } = makeService() |
| 63 | + vi.mocked(window.isFocused).mockReturnValue(true) |
| 64 | + expect(service.notify({ title: 'Done', body: 'Ready' })).toBe(false) |
| 65 | + |
| 66 | + vi.mocked(window.isFocused).mockReturnValue(false) |
| 67 | + expect( |
| 68 | + service.notify({ |
| 69 | + title: 'Task complete', |
| 70 | + body: 'Sim finished responding.', |
| 71 | + route: '/workspace/ws1/chat/c1', |
| 72 | + }) |
| 73 | + ).toBe(true) |
| 74 | + |
| 75 | + const notification = Notification.instances[0] |
| 76 | + expect(notification.options).toMatchObject({ silent: false }) |
| 77 | + expect(notification.show).toHaveBeenCalled() |
| 78 | + const click = notification.on.mock.calls.find(([event]) => event === 'click')?.[1] |
| 79 | + expect(click).toBeTypeOf('function') |
| 80 | + ;(click as () => void)() |
| 81 | + expect(openMainWindowAt).toHaveBeenCalledWith('/workspace/ws1/chat/c1') |
| 82 | + }) |
| 83 | +}) |
0 commit comments