import { describe, expect, it } from 'vitest' import type { AuthError } from '@supabase/supabase-js' import { classifyAuthError } from '../src/auth' function err(patch: Partial & { code?: string }): AuthError { return { name: 'AuthApiError', message: 'm', ...patch } as AuthError } describe('classifyAuthError', () => { it('maps GoTrue codes to the closed kinds the UI phrases', () => { expect(classifyAuthError(err({ code: 'invalid_credentials', status: 400 }))).toBe('invalid_credentials') expect(classifyAuthError(err({ code: 'email_not_confirmed', status: 400 }))).toBe('email_not_confirmed') expect(classifyAuthError(err({ code: 'user_already_exists', status: 422 }))).toBe('user_exists') expect(classifyAuthError(err({ code: 'weak_password', status: 422 }))).toBe('weak_password') expect(classifyAuthError(err({ code: 'otp_expired', status: 403 }))).toBe('link_expired') expect(classifyAuthError(err({ code: 'bad_code_verifier', status: 400 }))).toBe('link_expired') expect(classifyAuthError(err({ code: 'insufficient_aal', status: 401 }))).toBe('aal2_required') }) it('falls back on the HTTP status, then network, then unknown', () => { expect(classifyAuthError(err({ status: 429 }))).toBe('rate_limited') expect(classifyAuthError(err({ status: 400, message: 'Invalid login credentials' }))).toBe('invalid_credentials') expect(classifyAuthError(err({ status: 0 }))).toBe('network') expect(classifyAuthError(err({ status: 500, code: 'unexpected_failure' }))).toBe('unknown') }) })