import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { currentLocale } from '../../src/app/i18n' import { applyProfilePreferences, applyTheme, cachedLocale, cachedTheme, cycleTheme, LOCALE_KEY, localePreference, nextTheme, page, setLocale, setTheme, THEME_KEY, themeChoice, usePreferences, } from '../../src/state/usePreferences' import { toasts } from '../../src/state/useToasts' import { API, mockApi, profile, requestsTo, resetState } from '../helpers' const saved = (theme: string, locale = '') => ({ body: { data: { theme, locale }, meta: {} } }) const failed = { status: 500, body: { error: { code: 'error', message: 'x', details: {} } } } async function bodyOf(request: Request): Promise { return JSON.parse(await request.clone().text()) } function withPreferences(theme: string, locale: string) { return { ...profile.data, preferences: { theme, locale } } } beforeEach(() => { resetState() }) afterEach(() => { vi.restoreAllMocks() applyTheme('system') document.documentElement.classList.remove('dark') }) describe('usePreferences cache', () => { it('uses the documented storage keys', () => { expect(THEME_KEY).toBe('summer-admin.theme') expect(LOCALE_KEY).toBe('summer-admin.locale') }) it('cachedTheme accepts only a valid choice', () => { expect(cachedTheme()).toBe('system') window.localStorage.setItem(THEME_KEY, 'dark') expect(cachedTheme()).toBe('dark') window.localStorage.setItem(THEME_KEY, 'blue') expect(cachedTheme()).toBe('system') window.localStorage.setItem(THEME_KEY, '') expect(cachedTheme()).toBe('system') }) it('cachedLocale accepts only a short locale-shaped code', () => { expect(cachedLocale()).toBe('') for (const code of ['pl', 'en', 'pt-BR', 'zh_Hant']) { window.localStorage.setItem(LOCALE_KEY, code) expect(cachedLocale()).toBe(code) } for (const code of ['p', 'pl;drop', '../en', 'a'.repeat(17), 'en-', '