import { beforeEach, describe, expect, it, vi } from 'vitest' import { createPinia, setActivePinia } from 'pinia' import { useLayoutStore } from '@KTXC/stores/layoutStore' import { useTenantStore } from '@KTXC/stores/tenantStore' import { useUserStore } from '@KTXC/stores/userStore' const userServiceMock = vi.hoisted(() => ({ updateProfile: vi.fn(() => Promise.resolve()), updateSettings: vi.fn(() => Promise.resolve()), flushAll: vi.fn(() => Promise.resolve()), })) vi.mock('@KTXC/services/user/userService', () => ({ userService: userServiceMock, })) describe('layoutStore', () => { beforeEach(() => { const storage = new Map() vi.stubGlobal('localStorage', { getItem: vi.fn((key: string) => storage.get(key) ?? null), setItem: vi.fn((key: string, value: string) => storage.set(key, value)), removeItem: vi.fn((key: string) => storage.delete(key)), clear: vi.fn(() => storage.clear()), }) setActivePinia(createPinia()) vi.clearAllMocks() }) it('hydrates initial layout preferences after settings are loaded', () => { const userStore = useUserStore() const tenantStore = useTenantStore() const layoutStore = useLayoutStore() expect(layoutStore.sidebarDrawer).toBe(true) expect(layoutStore.miniSidebar).toBe(false) expect(layoutStore.theme).toBe('light') tenantStore.init({ id: 'tenant', domain: 'example.test', label: 'Example', settings: { theme_default_mode: 'dark', }, }) userStore.init({ settings: { sidebar_drawer: false, mini_sidebar: true, theme: 'system', }, }) layoutStore.hydrateFromSettings() expect(layoutStore.sidebarDrawer).toBe(false) expect(layoutStore.miniSidebar).toBe(true) expect(layoutStore.theme).toBe('system') expect(userServiceMock.updateSettings).not.toHaveBeenCalled() }) it('continues to persist user-driven layout changes', () => { const layoutStore = useLayoutStore() layoutStore.toggleSidebarDrawer() layoutStore.setMiniSidebar(true) layoutStore.setTheme('dark') expect(userServiceMock.updateSettings).toHaveBeenCalledWith({ sidebar_drawer: false }) expect(userServiceMock.updateSettings).toHaveBeenCalledWith({ mini_sidebar: true }) expect(userServiceMock.updateSettings).toHaveBeenCalledWith({ theme: 'dark' }) }) })