import { beforeEach, describe, expect, it, vi } from 'vitest'; import { useSettingsStore } from '@/stores/settings'; const hostApiFetchMock = vi.hoisted(() => vi.fn()); const invokeIpcMock = vi.hoisted(() => vi.fn()); vi.mock('@/lib/host-api', () => ({ hostApiFetch: (...args: unknown[]) => hostApiFetchMock(...args), })); vi.mock('@/lib/api-client', () => ({ invokeIpc: (...args: unknown[]) => invokeIpcMock(...args), })); describe('settings administrator session', () => { beforeEach(() => { hostApiFetchMock.mockReset(); invokeIpcMock.mockReset(); window.localStorage.clear(); useSettingsStore.setState({ devModeUnlocked: false }); hostApiFetchMock.mockResolvedValue({ language: 'en', devModeUnlocked: true }); invokeIpcMock.mockImplementation(async (channel: string, password?: unknown) => { if (channel === 'admin:isUnlocked') return false; if (channel === 'admin:verifyPassword') return { success: password === 'zhiniankeji666' }; if (channel === 'admin:lock') return { success: true }; return undefined; }); }); it('ignores persisted and synced developer unlock flags', async () => { await useSettingsStore.getState().init(); expect(useSettingsStore.getState().devModeUnlocked).toBe(false); useSettingsStore.getState().applySyncedPreferences({ devModeUnlocked: true }); expect(useSettingsStore.getState().devModeUnlocked).toBe(false); }); it('does not persist the session unlock after verifying the password', async () => { const unlocked = await useSettingsStore.getState().unlockDevMode('zhiniankeji666'); expect(unlocked).toBe(true); expect(useSettingsStore.getState().devModeUnlocked).toBe(true); expect(window.localStorage.getItem('niancode-settings')).not.toContain('devModeUnlocked'); await useSettingsStore.getState().lockDevMode(); expect(useSettingsStore.getState().devModeUnlocked).toBe(false); }); });