import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { createFakeSupabase, fakeProfile, fakeSession, flush, type FakeSupabase, } from '../helpers/fakeSupabase' const mocks = vi.hoisted(() => ({ fake: null as unknown })) vi.mock('@/lib/supabase', () => ({ getSupabaseClient: () => (mocks.fake as FakeSupabase | null)?.client ?? null, isSupabaseConfigured: () => mocks.fake !== null, })) import { useAuthStore } from '@/state/authStore' import { useLocaleStore } from '@/state/localeStore' import { startLocaleSync } from '@/services/localeSync' let fake: FakeSupabase let stops: Array<() => void> = [] beforeEach(() => { localStorage.clear() useLocaleStore.setState({ locale: 'fr' }) useAuthStore.setState({ status: 'loading', session: null, user: null, profile: null, roles: [], recovery: false, urlFailure: null, }) fake = createFakeSupabase({ profiles: [fakeProfile({ locale: 'en' })], profile_roles: [], }) mocks.fake = fake stops = [useAuthStore.getState().start(), startLocaleSync()] }) afterEach(() => stops.forEach((s) => s())) describe('locale sync', () => { it('applies the account locale once when the profile loads', async () => { fake.emit('SIGNED_IN', fakeSession()) await flush() expect(useLocaleStore.getState().locale).toBe('en') expect(localStorage.getItem('osteo:locale')).toBe('en') }) it('saves a language switch to profiles.locale while signed in', async () => { fake.emit('SIGNED_IN', fakeSession()) await flush() useLocaleStore.getState().setLocale('fr') await flush() expect(fake.tables.profiles[0].locale).toBe('fr') expect(useAuthStore.getState().profile?.locale).toBe('fr') }) it('leaves the profile alone when signed out', async () => { fake.emit('INITIAL_SESSION', null) useLocaleStore.getState().setLocale('fr') await flush() expect(fake.tables.profiles[0].locale).toBe('en') }) })