A listener can now browse the music that a friend's server shares.
- GET /friends lists the friends both admins agreed to, by id and name; an
address or a key is for the admin only. It is empty while federation is off.
- GET /friends/{friend}/artists, albums and tracks (q, limit, offset,
artist_id, album_id) and the one-item routes: this server asks the friend
with a request signed by its own key and passes the page on. The answer is
read into this API's own types first, so what a friend adds is dropped and
the shape is the one in the spec. Nothing is kept, and the ids are the
friend's own.
- Only the three kinds of thing can be asked, and only the parameters their
routes take: a listener cannot make this server ask a friend for anything
else. The friend's address and what the connection said never reach a
listener.
- A friend that cannot be reached, no longer takes this server for a friend,
is busy or is not there is told in words, never as an error page.
- The client of friends gets a generic signed GET with a size limit.
- The web app has a Friends entry in the menu, shown only when there is a
friend to browse, a list of friends and a page for each with artists,
albums and songs, a search and links from an artist to its albums and from
an album to its songs, in English and Italian.
- OpenAPI paths and schemas, contract test with a friend that is a whole
server, and two real servers checked end to end.
184 lines
8.7 KiB
TypeScript
184 lines
8.7 KiB
TypeScript
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(
|
|
<QueryClientProvider client={new QueryClient()}>
|
|
<RouterProvider router={router} />
|
|
</QueryClientProvider>,
|
|
)
|
|
// 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()
|
|
})
|