import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { render, screen, waitFor, within } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { createMemoryRouter, RouterProvider } from 'react-router-dom' import { beforeEach, expect, test, vi } from 'vitest' import { sessionStore } from '@/auth/session' import { initI18n } from '@/i18n' import { routes } from '@/router' import { mockFetch } from '@/test/mockFetch' beforeEach(async () => { vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US') await initI18n() }) async function renderAt(path: string) { const router = createMemoryRouter(routes, { initialEntries: [path] }) render( , ) // The pages are fetched when first opened: wait until the one asked for is in. await waitFor(() => expect(router.state.initialized).toBe(true)) await screen.findByRole('main') } /** The menu asks which friends there are; the tests below are about what an admin page asks. */ const notFriends = (c: { url: URL }) => c.url.pathname !== '/api/v1/friends' function mainNav() { return screen.getByRole('navigation', { name: 'Main' }) } test('at /artists the Artists link is the current page and the others are not', async () => { await renderAt('/artists') const nav = within(mainNav()) expect(nav.getByRole('link', { name: 'Artists' })).toHaveAttribute('aria-current', 'page') for (const name of ['Home', 'Search', 'Discover', 'Albums', 'Songs', 'Settings']) { expect(nav.getByRole('link', { name })).not.toHaveAttribute('aria-current') } }) const adminRoutes = { 'GET /api/v1/libraries': () => ({ libraries: [] }), 'GET /api/v1/library/scan': () => ({ running: false }), } test('an admin has an Admin link, and a user who is not one has none', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: false } }) await renderAt('/artists') expect(within(mainNav()).queryByRole('link', { name: 'Admin' })).not.toBeInTheDocument() }) test('the Admin link opens the libraries and stays current inside the admin area', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: true } }) mockFetch(adminRoutes) await renderAt('/admin') expect(await screen.findByRole('region', { name: 'Libraries' })).toBeInTheDocument() expect(within(mainNav()).getByRole('link', { name: 'Admin' })).toHaveAttribute('aria-current', 'page') }) test('the admin area has a Users section next to Libraries', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: true } }) mockFetch({ ...adminRoutes, 'GET /api/v1/users': () => ({ users: [{ id: 1, email: 'tom@example.org', is_admin: true }] }) }) await renderAt('/admin/users') expect(await screen.findByRole('region', { name: 'Users' })).toBeInTheDocument() const sections = within(screen.getByRole('navigation', { name: 'Admin sections' })) expect(sections.getByRole('link', { name: 'Users' })).toHaveAttribute('aria-current', 'page') expect(sections.getByRole('link', { name: 'Libraries' })).not.toHaveAttribute('aria-current') }) test('the admin area has a Providers section too', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: true } }) mockFetch({ ...adminRoutes, 'GET /api/v1/providers': () => ({ providers: [] }) }) await renderAt('/admin/providers') expect(await screen.findByRole('region', { name: 'Providers' })).toBeInTheDocument() expect(within(screen.getByRole('navigation', { name: 'Admin sections' })).getByRole('link', { name: 'Providers' })).toHaveAttribute('aria-current', 'page') }) test('the admin area has a Jobs section too', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: true } }) mockFetch({ ...adminRoutes, 'GET /api/v1/providers': () => ({ providers: [] }), 'GET /api/v1/library/covers': () => ({ running: false }), 'GET /api/v1/library/artist-images': () => ({ running: false }), 'GET /api/v1/library/descriptions': () => ({ running: false }), }) await renderAt('/admin/jobs') expect(await screen.findByRole('region', { name: 'Jobs' })).toBeInTheDocument() expect(within(screen.getByRole('navigation', { name: 'Admin sections' })).getByRole('link', { name: 'Jobs' })).toHaveAttribute('aria-current', 'page') }) test('someone who is not an admin who opens the jobs address gets a notice and no request', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 2, email: 'ann@example.org', is_admin: false } }) const { calls } = mockFetch({ 'GET /api/v1/library/covers': () => ({ running: false }) }) await renderAt('/admin/jobs') expect(await screen.findByText('Only admins can see this page.')).toBeInTheDocument() await new Promise((r) => setTimeout(r, 30)) expect(calls.filter(notFriends)).toHaveLength(0) }) test('someone who is not an admin who opens the providers address gets a notice and no request', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 2, email: 'ann@example.org', is_admin: false } }) const { calls } = mockFetch({ 'GET /api/v1/providers': () => ({ providers: [] }) }) await renderAt('/admin/providers') expect(await screen.findByText('Only admins can see this page.')).toBeInTheDocument() await new Promise((r) => setTimeout(r, 30)) expect(calls.filter(notFriends)).toHaveLength(0) }) test('someone who is not an admin who opens the users address gets a notice and no request', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 2, email: 'ann@example.org', is_admin: false } }) const { calls } = mockFetch({ 'GET /api/v1/users': () => ({ users: [] }) }) await renderAt('/admin/users') expect(await screen.findByText('Only admins can see this page.')).toBeInTheDocument() await new Promise((r) => setTimeout(r, 30)) expect(calls.filter(notFriends)).toHaveLength(0) }) test('someone who is not an admin who opens the admin address gets a notice and no request', async () => { sessionStore.setState({ status: 'authenticated', user: { id: 2, email: 'ann@example.org', is_admin: false } }) const { calls } = mockFetch(adminRoutes) await renderAt('/admin/libraries') expect(await screen.findByText('Only admins can see this page.')).toBeInTheDocument() await new Promise((r) => setTimeout(r, 30)) expect(calls.filter((c) => c.url.pathname.includes('librar'))).toHaveLength(0) }) test('Home is the current page only on /', async () => { await renderAt('/') expect(within(mainNav()).getByRole('link', { name: 'Home' })).toHaveAttribute('aria-current', 'page') }) test('an artist page keeps the Artists link current', async () => { await renderAt('/artists/5') expect(within(mainNav()).getByRole('link', { name: 'Artists' })).toHaveAttribute('aria-current', 'page') }) test('the page has one main landmark and says what the server does with data', async () => { await renderAt('/songs') expect(screen.getAllByRole('main')).toHaveLength(1) expect(screen.getByText('Nothing leaves this server unless an admin turns a provider on.')).toBeInTheDocument() }) test('the home page opens with a greeting as its heading', async () => { await renderAt('/') expect(screen.getByRole('heading', { level: 1 })).toHaveTextContent(/^Good (morning|afternoon|evening)$/) }) test.each([ ['/search', 'Search'], ['/artists', 'Artists'], ['/albums', 'Albums'], ['/songs', 'Songs'], ['/discover', 'Discover'], ['/settings', 'Settings'], ])('the route %s opens a page headed %s', async (path, heading) => { await renderAt(path) expect(screen.getByRole('heading', { level: 1, name: heading })).toBeInTheDocument() }) test.each(['/artists/5', '/albums/5'])('the detail route %s opens inside the shell and starts loading', async (path) => { mockFetch({}) await renderAt(path) expect(within(screen.getByRole('main')).getByRole('status')).toHaveTextContent('Loading') }) test('an unknown path shows the not-found page with a way back', async () => { await renderAt('/no/such/page') expect(screen.getByRole('heading', { name: 'This page does not exist' })).toBeInTheDocument() expect(screen.getByRole('link', { name: 'Back to Home' })).toHaveAttribute('href', '/') }) test('a skip link comes first and moves the focus to the page', async () => { const user = userEvent.setup() await renderAt('/artists') await user.tab() const skip = screen.getByRole('link', { name: 'Skip to content' }) expect(skip).toHaveFocus() await user.keyboard('{Enter}') expect(screen.getByRole('main')).toHaveFocus() })