- 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
145 lines
4.0 KiB
TypeScript
145 lines
4.0 KiB
TypeScript
/**
|
|
* In-memory stand-in for the parts of the Supabase client the identity step
|
|
* uses: auth events, profile select/update, profile_roles select. Tests mock
|
|
* `@/lib/supabase` to hand this object out.
|
|
*/
|
|
import { vi } from 'vitest'
|
|
import type {
|
|
AuthChangeEvent,
|
|
FaunosteoClient,
|
|
Profile,
|
|
Session,
|
|
User,
|
|
} from '@faunosteo/supabase'
|
|
|
|
type Listener = (event: AuthChangeEvent, session: Session | null) => void
|
|
type Row = Record<string, unknown>
|
|
|
|
export function fakeUser(patch: Partial<User> = {}): User {
|
|
return {
|
|
id: 'user-1',
|
|
aud: 'authenticated',
|
|
email: 'alice@example.test',
|
|
email_confirmed_at: '2026-10-11T10:00:00Z',
|
|
created_at: '2026-10-01T10:00:00Z',
|
|
app_metadata: {},
|
|
user_metadata: {},
|
|
...patch,
|
|
} as User
|
|
}
|
|
|
|
export function fakeSession(user: User = fakeUser()): Session {
|
|
return {
|
|
access_token: 'at',
|
|
refresh_token: 'rt',
|
|
token_type: 'bearer',
|
|
expires_in: 3600,
|
|
user,
|
|
} as Session
|
|
}
|
|
|
|
export function fakeProfile(patch: Partial<Profile> = {}): Profile {
|
|
return {
|
|
id: 'user-1',
|
|
display_name: 'Alice Local',
|
|
first_name: null,
|
|
last_name: null,
|
|
phone: null,
|
|
phone_verified_at: null,
|
|
locale: 'fr',
|
|
timezone: 'Europe/Paris',
|
|
active: true,
|
|
practice_name: null,
|
|
professional_title: null,
|
|
siret: null,
|
|
created_at: '2026-10-01T10:00:00Z',
|
|
updated_at: '2026-10-01T10:00:00Z',
|
|
...patch,
|
|
}
|
|
}
|
|
|
|
/** Minimal PostgREST builder: filters, then select / update, awaitable. */
|
|
function builder(tables: Record<string, Row[]>, table: string) {
|
|
const filters: Array<(row: Row) => boolean> = []
|
|
let patch: Row | null = null
|
|
const rows = () => (tables[table] ?? []).filter((r) => filters.every((f) => f(r)))
|
|
const apply = () => {
|
|
if (!patch) return rows()
|
|
const hit = rows()
|
|
hit.forEach((r) => Object.assign(r, patch))
|
|
return hit
|
|
}
|
|
const api = {
|
|
select: () => api,
|
|
update: (values: Row) => {
|
|
patch = values
|
|
return api
|
|
},
|
|
eq: (col: string, value: unknown) => {
|
|
filters.push((r) => r[col] === value)
|
|
return api
|
|
},
|
|
is: (col: string, value: unknown) => {
|
|
filters.push((r) => r[col] === value)
|
|
return api
|
|
},
|
|
maybeSingle: async () => ({ data: apply()[0] ?? null, error: null }),
|
|
single: async () => {
|
|
const [first] = apply()
|
|
return first
|
|
? { data: first, error: null }
|
|
: { data: null, error: { message: 'no row', code: 'PGRST116' } }
|
|
},
|
|
then: (resolve: (v: { data: Row[]; error: null }) => void) =>
|
|
resolve({ data: apply(), error: null }),
|
|
}
|
|
return api
|
|
}
|
|
|
|
export interface FakeSupabase {
|
|
client: FaunosteoClient
|
|
tables: Record<string, Row[]>
|
|
emit: (event: AuthChangeEvent, session: Session | null) => void
|
|
auth: {
|
|
initialize: ReturnType<typeof vi.fn>
|
|
signInWithPassword: ReturnType<typeof vi.fn>
|
|
signUp: ReturnType<typeof vi.fn>
|
|
signOut: ReturnType<typeof vi.fn>
|
|
updateUser: ReturnType<typeof vi.fn>
|
|
resetPasswordForEmail: ReturnType<typeof vi.fn>
|
|
signInWithOAuth: ReturnType<typeof vi.fn>
|
|
}
|
|
}
|
|
|
|
export function createFakeSupabase(
|
|
tables: Record<string, Row[]> = { profiles: [fakeProfile()], profile_roles: [] },
|
|
): FakeSupabase {
|
|
const listeners: Listener[] = []
|
|
const auth = {
|
|
initialize: vi.fn(async () => ({ error: null })),
|
|
onAuthStateChange: vi.fn((cb: Listener) => {
|
|
listeners.push(cb)
|
|
return { data: { subscription: { unsubscribe: vi.fn() } } }
|
|
}),
|
|
signInWithPassword: vi.fn(),
|
|
signUp: vi.fn(),
|
|
signOut: vi.fn(async () => ({ error: null })),
|
|
updateUser: vi.fn(),
|
|
resetPasswordForEmail: vi.fn(),
|
|
signInWithOAuth: vi.fn(),
|
|
}
|
|
const client = {
|
|
auth,
|
|
from: (table: string) => builder(tables, table),
|
|
} as unknown as FaunosteoClient
|
|
return {
|
|
client,
|
|
tables,
|
|
auth,
|
|
emit: (event, session) => listeners.forEach((cb) => cb(event, session)),
|
|
}
|
|
}
|
|
|
|
/** Resolves once the store has run its deferred account load. */
|
|
export const flush = () => new Promise((r) => setTimeout(r, 0))
|