- tests/helpers/fakeSupabase: in-memory auth events + profiles/profile_roles builder - validation schemas and safeNextPath; classifyAuthError mapping (packages/supabase) - LoginRoute (unavailable, validation, next path, phrased failure, OAuth buttons, show password) - SignupRoute (policy, mismatch, metadata + redirect, user exists) - RequireAuth / RedirectIfSignedIn, AppLayout (nav, active, drawer, sign out), Dashboard, Account forms
108 lines
3.4 KiB
TypeScript
108 lines
3.4 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import {
|
|
createFakeSupabase,
|
|
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'
|
|
|
|
const INITIAL = {
|
|
status: 'loading' as const,
|
|
session: null,
|
|
user: null,
|
|
profile: null,
|
|
roles: [],
|
|
recovery: false,
|
|
urlFailure: null,
|
|
}
|
|
|
|
let fake: FakeSupabase
|
|
let stop: () => void = () => undefined
|
|
|
|
beforeEach(() => {
|
|
useAuthStore.setState(INITIAL)
|
|
fake = createFakeSupabase()
|
|
fake.tables.profile_roles = [
|
|
{ user_id: 'user-1', role: 'admin', revoked_at: null },
|
|
{ user_id: 'user-1', role: 'support', revoked_at: '2026-01-01' },
|
|
]
|
|
mocks.fake = fake
|
|
})
|
|
afterEach(() => stop())
|
|
|
|
describe('authStore', () => {
|
|
it('is unavailable when no backend is configured', () => {
|
|
mocks.fake = null
|
|
stop = useAuthStore.getState().start()
|
|
expect(useAuthStore.getState().status).toBe('unavailable')
|
|
})
|
|
|
|
it('loads the profile and active roles after SIGNED_IN', async () => {
|
|
stop = useAuthStore.getState().start()
|
|
expect(fake.auth.initialize).toHaveBeenCalled()
|
|
fake.emit('SIGNED_IN', fakeSession())
|
|
expect(useAuthStore.getState().status).toBe('signed_in')
|
|
await flush()
|
|
const s = useAuthStore.getState()
|
|
expect(s.profile?.display_name).toBe('Alice Local')
|
|
expect(s.roles).toEqual(['admin'])
|
|
})
|
|
|
|
it('clears everything on SIGNED_OUT and on an INITIAL_SESSION without session', async () => {
|
|
stop = useAuthStore.getState().start()
|
|
fake.emit('INITIAL_SESSION', null)
|
|
expect(useAuthStore.getState().status).toBe('signed_out')
|
|
fake.emit('SIGNED_IN', fakeSession())
|
|
await flush()
|
|
fake.emit('SIGNED_OUT', null)
|
|
expect(useAuthStore.getState()).toMatchObject({
|
|
status: 'signed_out',
|
|
user: null,
|
|
profile: null,
|
|
roles: [],
|
|
})
|
|
})
|
|
|
|
it('flags a recovery link until the password is saved', () => {
|
|
stop = useAuthStore.getState().start()
|
|
fake.emit('PASSWORD_RECOVERY', fakeSession())
|
|
expect(useAuthStore.getState().recovery).toBe(true)
|
|
useAuthStore.getState().clearRecovery()
|
|
expect(useAuthStore.getState().recovery).toBe(false)
|
|
})
|
|
|
|
it('records a URL exchange failure for the landing routes', async () => {
|
|
fake.auth.initialize.mockResolvedValueOnce({
|
|
error: { code: 'flow_state_expired', message: 'x', status: 400 },
|
|
})
|
|
stop = useAuthStore.getState().start()
|
|
await flush()
|
|
expect(useAuthStore.getState().urlFailure).toBe('link_expired')
|
|
})
|
|
|
|
it('updateProfile writes the row and keeps the store in sync', async () => {
|
|
stop = useAuthStore.getState().start()
|
|
fake.emit('SIGNED_IN', fakeSession())
|
|
await flush()
|
|
await useAuthStore.getState().updateProfile({ first_name: 'Alice' })
|
|
expect(useAuthStore.getState().profile?.first_name).toBe('Alice')
|
|
expect(fake.tables.profiles[0].first_name).toBe('Alice')
|
|
})
|
|
|
|
it('signOut calls the client and resets the state', async () => {
|
|
stop = useAuthStore.getState().start()
|
|
fake.emit('SIGNED_IN', fakeSession())
|
|
await useAuthStore.getState().signOut()
|
|
expect(fake.auth.signOut).toHaveBeenCalled()
|
|
expect(useAuthStore.getState().status).toBe('signed_out')
|
|
})
|
|
})
|