import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { cleanup, render, screen, within } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { MemoryRouter, Route, Routes } from 'react-router-dom' import type { OwnCatalogGrant, PlatformRole } from '@faunosteo/supabase' import { createFakeSupabase, fakeProfile, fakeUser, 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 AppLayout from '@/components/shell/AppLayout' import { navigationHint } from '@/auth/navigationHint' import { useAuthStore } from '@/state/authStore' import { useLocaleStore } from '@/state/localeStore' import { fr } from '@/i18n/fr' const NOW = Date.parse('2026-11-02T12:00:00Z') function grant(kind: OwnCatalogGrant['kind'], patch: Partial = {}): OwnCatalogGrant { return { kind, expires_at: null, revoked_at: null, ...patch } } describe('navigationHint', () => { it('shows nothing extra without a grant or the owner role', () => { expect(navigationHint([], [], NOW)).toEqual({ catalog: false, admin: false }) expect(navigationHint([], ['admin', 'support'], NOW)).toEqual({ catalog: false, admin: false }) }) it('shows the catalogue to a catalogue reader only', () => { expect(navigationHint([grant('catalog_reader')], [], NOW)).toEqual({ catalog: true, admin: false }) }) it('shows both entries to any capability holder', () => { for (const kind of ['sheet_editor', 'sheet_reviewer', 'sheet_publisher'] as const) { expect(navigationHint([grant(kind)], [], NOW)).toEqual({ catalog: true, admin: true }) } }) it('shows the administration to the owner role, the catalogue only with a grant', () => { expect(navigationHint([], ['owner'], NOW)).toEqual({ catalog: false, admin: true }) }) it('ignores revoked and expired grants, keeps one expiring later', () => { const revoked = grant('sheet_editor', { revoked_at: '2026-10-01T00:00:00Z' }) const expired = grant('catalog_reader', { expires_at: '2026-11-01T00:00:00Z' }) expect(navigationHint([revoked, expired], [], NOW)).toEqual({ catalog: false, admin: false }) const later = grant('catalog_reader', { expires_at: '2026-12-01T00:00:00Z' }) expect(navigationHint([later], [], NOW).catalog).toBe(true) }) }) function mount(grants: OwnCatalogGrant[], roles: PlatformRole[] = []) { useAuthStore.setState({ catalogGrants: grants, roles }) return render( }> dashboard page

} />
, ) } function sidebar() { return screen.getByRole('navigation', { name: fr['shell.nav_label'] }) } beforeEach(() => { useLocaleStore.setState({ locale: 'fr' }) mocks.fake = createFakeSupabase() satisfies FakeSupabase useAuthStore.setState({ status: 'signed_in', user: fakeUser(), profile: fakeProfile(), avatar: null, avatarUrl: null, }) }) afterEach(() => { cleanup() vi.unstubAllGlobals() }) describe('SidebarNav entries', () => { it('hides both V1-2 entries from an account without a right', () => { mount([]) expect(within(sidebar()).queryByRole('link', { name: fr['shell.nav.anatomy'] })).toBeNull() expect(within(sidebar()).queryByRole('link', { name: fr['shell.nav.admin'] })).toBeNull() expect(within(sidebar()).queryByRole('separator')).toBeNull() }) it('shows « Fiches anatomiques » to a catalogue reader, without the administration', () => { mount([grant('catalog_reader')]) expect(within(sidebar()).getByRole('link', { name: fr['shell.nav.anatomy'] })).toHaveAttribute( 'href', '/app/anatomy', ) expect(within(sidebar()).queryByRole('link', { name: fr['shell.nav.admin'] })).toBeNull() }) it('shows both to a capability holder, « Administration » set apart before « Mon compte »', () => { mount([grant('sheet_reviewer')]) const links = within(sidebar()).getAllByRole('link').map((link) => link.textContent) expect(links).toEqual([ fr['shell.nav.dashboard'], fr['shell.nav.clients'], fr['shell.nav.animals'], fr['shell.nav.anatomy'], fr['shell.nav.admin'], fr['shell.nav.account'], fr['shell.nav.local_tool'] + fr['shell.nav.local_tool_hint'], ]) expect(within(sidebar()).getByRole('link', { name: fr['shell.nav.admin'] })).toHaveAttribute( 'href', '/app/admin', ) expect(within(sidebar()).getAllByRole('separator')).toHaveLength(2) }) it('shows « Administration » to the owner role without a grant', () => { mount([], ['owner']) expect(within(sidebar()).getByRole('link', { name: fr['shell.nav.admin'] })).toBeInTheDocument() expect(within(sidebar()).queryByRole('link', { name: fr['shell.nav.anatomy'] })).toBeNull() }) it('carries the same entries into the mobile drawer', async () => { const user = userEvent.setup() // A narrow screen: the drawer closes by itself once the sidebar breakpoint matches. vi.stubGlobal('matchMedia', () => ({ matches: false, addEventListener: () => {}, removeEventListener: () => {}, })) mount([grant('sheet_editor')]) await user.click(screen.getByRole('button', { name: fr['shell.open_menu'] })) const drawer = await screen.findByRole('dialog', { name: fr['shell.nav_label'] }) expect(within(drawer).getByRole('link', { name: fr['shell.nav.anatomy'] })).toBeInTheDocument() expect(within(drawer).getByRole('link', { name: fr['shell.nav.admin'] })).toBeInTheDocument() }) })