|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +vi.mock('$lib/commands', () => ({ |
| 4 | + getConfig: vi.fn(), |
| 5 | +})); |
| 6 | + |
| 7 | +vi.mock('$lib/events', () => ({ |
| 8 | + onConfigChanged: vi.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +const { getConfig } = await import('$lib/commands'); |
| 12 | +const { onConfigChanged } = await import('$lib/events'); |
| 13 | +const { configStore } = await import('../config.svelte'); |
| 14 | + |
| 15 | +const SNAPSHOT_CONFIG = { |
| 16 | + timer: { |
| 17 | + work_minutes: 30, |
| 18 | + rest_seconds: 25, |
| 19 | + pre_alert_seconds: 10, |
| 20 | + alert_timeout_seconds: 45, |
| 21 | + mode: 'twenty_twenty_twenty' as const, |
| 22 | + }, |
| 23 | + behavior: { |
| 24 | + sound_enabled: true, |
| 25 | + fullscreen_skip: true, |
| 26 | + afk_skip_enabled: false, |
| 27 | + afk_threshold_minutes: 5, |
| 28 | + auto_start: false, |
| 29 | + process_whitelist_enabled: false, |
| 30 | + process_whitelist: [], |
| 31 | + }, |
| 32 | + display: { language: 'zh-CN' as const, theme: 'light' as const }, |
| 33 | + schedule: { |
| 34 | + enabled: false, |
| 35 | + active_days: [true, true, true, true, true, false, false] as [ |
| 36 | + boolean, |
| 37 | + boolean, |
| 38 | + boolean, |
| 39 | + boolean, |
| 40 | + boolean, |
| 41 | + boolean, |
| 42 | + boolean, |
| 43 | + ], |
| 44 | + }, |
| 45 | + hotkeys: { |
| 46 | + start_rest: 'CommandOrControl+Alt+B', |
| 47 | + skip_rest: 'CommandOrControl+Alt+S', |
| 48 | + toggle_pause: 'CommandOrControl+Alt+P', |
| 49 | + }, |
| 50 | + pomodoro: { |
| 51 | + focus_minutes: 25, |
| 52 | + short_break_minutes: 5, |
| 53 | + long_break_minutes: 15, |
| 54 | + cycles_per_long: 4, |
| 55 | + }, |
| 56 | +}; |
| 57 | + |
| 58 | +beforeEach(() => { |
| 59 | + vi.clearAllMocks(); |
| 60 | +}); |
| 61 | + |
| 62 | +afterEach(() => { |
| 63 | + configStore.destroy(); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('configStore basics', () => { |
| 67 | + it('exposes version and loaded getters and reflects loaded=true after init', async () => { |
| 68 | + vi.mocked(onConfigChanged).mockResolvedValue(() => {}); |
| 69 | + vi.mocked(getConfig).mockResolvedValue(SNAPSHOT_CONFIG); |
| 70 | + |
| 71 | + const before = configStore.version; |
| 72 | + expect(typeof before).toBe('number'); |
| 73 | + |
| 74 | + await configStore.init(); |
| 75 | + |
| 76 | + expect(configStore.loaded).toBe(true); |
| 77 | + expect(configStore.current.timer.work_minutes).toBe(30); |
| 78 | + // version should have incremented at least once during init |
| 79 | + expect(configStore.version).toBeGreaterThan(before); |
| 80 | + }); |
| 81 | + |
| 82 | + it('event callback updates current and marks store as loaded', async () => { |
| 83 | + let capturedCallback: ((payload: unknown) => void) | null = null; |
| 84 | + vi.mocked(onConfigChanged).mockImplementation((cb) => { |
| 85 | + capturedCallback = cb as (payload: unknown) => void; |
| 86 | + return Promise.resolve(() => {}); |
| 87 | + }); |
| 88 | + vi.mocked(getConfig).mockResolvedValue(SNAPSHOT_CONFIG); |
| 89 | + |
| 90 | + await configStore.init(); |
| 91 | + |
| 92 | + const eventPayload = { |
| 93 | + ...SNAPSHOT_CONFIG, |
| 94 | + timer: { ...SNAPSHOT_CONFIG.timer, work_minutes: 99 }, |
| 95 | + }; |
| 96 | + |
| 97 | + expect(capturedCallback).not.toBeNull(); |
| 98 | + capturedCallback!(eventPayload); |
| 99 | + |
| 100 | + expect(configStore.current.timer.work_minutes).toBe(99); |
| 101 | + expect(configStore.loaded).toBe(true); |
| 102 | + }); |
| 103 | + |
| 104 | + it('rolls back the event listener when getConfig throws', async () => { |
| 105 | + const unlisten = vi.fn(); |
| 106 | + vi.mocked(onConfigChanged).mockResolvedValue(unlisten); |
| 107 | + vi.mocked(getConfig).mockRejectedValueOnce(new Error('snapshot failed')); |
| 108 | + |
| 109 | + await expect(configStore.init()).rejects.toThrow('snapshot failed'); |
| 110 | + |
| 111 | + // unlisten MUST be called as part of the failure rollback |
| 112 | + expect(unlisten).toHaveBeenCalled(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('cleans up the previous subscription when init is called twice', async () => { |
| 116 | + const firstUnlisten = vi.fn(); |
| 117 | + const secondUnlisten = vi.fn(); |
| 118 | + vi.mocked(onConfigChanged) |
| 119 | + .mockResolvedValueOnce(firstUnlisten) |
| 120 | + .mockResolvedValueOnce(secondUnlisten); |
| 121 | + vi.mocked(getConfig).mockResolvedValue(SNAPSHOT_CONFIG); |
| 122 | + |
| 123 | + await configStore.init(); |
| 124 | + await configStore.init(); |
| 125 | + |
| 126 | + expect(firstUnlisten).toHaveBeenCalled(); |
| 127 | + expect(secondUnlisten).not.toHaveBeenCalled(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('destroy unlistens the active subscription', async () => { |
| 131 | + const unlisten = vi.fn(); |
| 132 | + vi.mocked(onConfigChanged).mockResolvedValue(unlisten); |
| 133 | + vi.mocked(getConfig).mockResolvedValue(SNAPSHOT_CONFIG); |
| 134 | + |
| 135 | + await configStore.init(); |
| 136 | + configStore.destroy(); |
| 137 | + |
| 138 | + expect(unlisten).toHaveBeenCalled(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('skips applying snapshot when an event fires before the snapshot resolves', async () => { |
| 142 | + let capturedCallback: ((payload: unknown) => void) | null = null; |
| 143 | + vi.mocked(onConfigChanged).mockImplementation((cb) => { |
| 144 | + capturedCallback = cb as (payload: unknown) => void; |
| 145 | + return Promise.resolve(() => {}); |
| 146 | + }); |
| 147 | + |
| 148 | + const eventFirst = { |
| 149 | + ...SNAPSHOT_CONFIG, |
| 150 | + timer: { ...SNAPSHOT_CONFIG.timer, work_minutes: 77 }, |
| 151 | + }; |
| 152 | + let resolveSnapshot: (config: typeof SNAPSHOT_CONFIG) => void = () => {}; |
| 153 | + vi.mocked(getConfig).mockImplementationOnce( |
| 154 | + () => |
| 155 | + new Promise((resolve) => { |
| 156 | + resolveSnapshot = resolve; |
| 157 | + }), |
| 158 | + ); |
| 159 | + |
| 160 | + const initPromise = configStore.init(); |
| 161 | + // Yield a few microtasks so init() reaches `await getConfig()`. |
| 162 | + for (let i = 0; i < 5; i++) await Promise.resolve(); |
| 163 | + |
| 164 | + expect(capturedCallback).not.toBeNull(); |
| 165 | + capturedCallback!(eventFirst); |
| 166 | + |
| 167 | + // Now resolve snapshot with a different value: it MUST NOT overwrite the |
| 168 | + // event payload since version has advanced. |
| 169 | + resolveSnapshot({ |
| 170 | + ...SNAPSHOT_CONFIG, |
| 171 | + timer: { ...SNAPSHOT_CONFIG.timer, work_minutes: 1 }, |
| 172 | + }); |
| 173 | + |
| 174 | + await initPromise; |
| 175 | + |
| 176 | + expect(configStore.current.timer.work_minutes).toBe(77); |
| 177 | + }); |
| 178 | +}); |
0 commit comments