packages/supabase assets module: closed AssetFailure kinds, begin / quarantine upload (upsert off) / confirm / remove, own-row state read with an explicit column list, download under the SELECT policy. App: client transport (magic byte sniff, never SVG, createImageBitmap, canvas 1024 px, JPEG 0.9, 5 MiB cap), AvatarSection (preview, save, 1 s polling up to 30 s, rejection and failure messages per closed code, removal with confirmation, in-flight row resumed at mount), auth store blob URL revoked on change and sign-out, AvatarCircle in UserBadge and the top bar link to Mon compte (D3), FR/EN strings, fake client extended, 27 new tests (app 300).
143 lines
4.9 KiB
TypeScript
143 lines
4.9 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import {
|
|
createFakeSupabase,
|
|
fakeAssetRow,
|
|
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,
|
|
avatar: null,
|
|
avatarUrl: 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('loads the avatar state and downloads the clean photo once', async () => {
|
|
fake.tables.assets.push(fakeAssetRow())
|
|
stop = useAuthStore.getState().start()
|
|
fake.emit('SIGNED_IN', fakeSession())
|
|
await flush()
|
|
await flush()
|
|
const s = useAuthStore.getState()
|
|
expect(s.avatar?.clean?.id).toBe('asset-1')
|
|
expect(s.avatarUrl).toMatch(/^blob:/)
|
|
expect(fake.storage.download).toHaveBeenCalledTimes(1)
|
|
await useAuthStore.getState().refreshAvatar()
|
|
expect(fake.storage.download).toHaveBeenCalledTimes(1)
|
|
fake.tables.assets.push(fakeAssetRow({ id: 'asset-2', object_key: 'profiles/user-1/asset-2/avatar.webp', created_at: '2026-10-13T12:00:00Z' }))
|
|
fake.tables.assets[0].status = 'deleted'
|
|
await useAuthStore.getState().refreshAvatar()
|
|
expect(fake.storage.download).toHaveBeenLastCalledWith('profiles/user-1/asset-2/avatar.webp')
|
|
})
|
|
|
|
it('clearAvatar drops the URL and the clean row; sign-out clears it too', async () => {
|
|
fake.tables.assets.push(fakeAssetRow())
|
|
stop = useAuthStore.getState().start()
|
|
fake.emit('SIGNED_IN', fakeSession())
|
|
await flush()
|
|
await flush()
|
|
useAuthStore.getState().clearAvatar()
|
|
expect(useAuthStore.getState().avatarUrl).toBeNull()
|
|
expect(useAuthStore.getState().avatar?.clean).toBeNull()
|
|
await useAuthStore.getState().refreshAvatar()
|
|
fake.emit('SIGNED_OUT', null)
|
|
expect(useAuthStore.getState()).toMatchObject({ avatar: null, avatarUrl: null })
|
|
})
|
|
|
|
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')
|
|
})
|
|
})
|