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.
97 lines
4.4 KiB
TypeScript
97 lines
4.4 KiB
TypeScript
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { render, screen, within } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { MemoryRouter } from 'react-router-dom'
|
|
import { beforeEach, expect, test, vi } from 'vitest'
|
|
import { sessionStore } from '@/auth/session'
|
|
import { initI18n } from '@/i18n'
|
|
import { mockFetch } from '@/test/mockFetch'
|
|
import Sidebar from './Sidebar'
|
|
|
|
const KEY = 'sonda.sidebar'
|
|
|
|
beforeEach(async () => {
|
|
localStorage.clear()
|
|
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
|
|
await initI18n()
|
|
sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: true } })
|
|
})
|
|
|
|
function renderSidebar(friends: { id: number; name: string }[] = []) {
|
|
mockFetch({ 'GET /api/v1/friends': () => ({ friends }) })
|
|
render(
|
|
<QueryClientProvider client={new QueryClient({ defaultOptions: { queries: { retry: false } } })}>
|
|
<MemoryRouter initialEntries={['/albums']}>
|
|
<Sidebar />
|
|
</MemoryRouter>
|
|
</QueryClientProvider>,
|
|
)
|
|
return screen.getByRole('navigation', { name: 'Main' })
|
|
}
|
|
|
|
test('the sidebar starts open, with a button that folds it', () => {
|
|
const nav = renderSidebar()
|
|
expect(nav).toHaveAttribute('data-collapsed', 'false')
|
|
expect(within(nav).getByRole('button', { name: 'Collapse the sidebar' })).toBeInTheDocument()
|
|
expect(within(nav).getByText('Albums')).toBeVisible()
|
|
})
|
|
|
|
test('folded, it keeps every link with its name and a tooltip, and the same button opens it again', async () => {
|
|
const nav = renderSidebar()
|
|
await userEvent.click(within(nav).getByRole('button', { name: 'Collapse the sidebar' }))
|
|
expect(nav).toHaveAttribute('data-collapsed', 'true')
|
|
const albums = within(nav).getByRole('link', { name: 'Albums' })
|
|
expect(albums).toHaveAttribute('aria-current', 'page')
|
|
expect(albums).toHaveAttribute('title', 'Albums')
|
|
for (const name of ['Home', 'Search', 'Mixes', 'Playlists', 'Smart playlists', 'Admin', 'Settings']) {
|
|
expect(within(nav).getByRole('link', { name })).toBeInTheDocument()
|
|
}
|
|
await userEvent.click(within(nav).getByRole('button', { name: 'Expand the sidebar' }))
|
|
expect(nav).toHaveAttribute('data-collapsed', 'false')
|
|
expect(within(nav).getByRole('link', { name: 'Albums' })).not.toHaveAttribute('title')
|
|
})
|
|
|
|
test('folded, the logo shrinks to its wave and the word is still the name of the app', async () => {
|
|
const nav = renderSidebar()
|
|
expect(within(nav).getAllByRole('img', { name: 'Sonda' })).toHaveLength(1)
|
|
await userEvent.click(within(nav).getByRole('button', { name: 'Collapse the sidebar' }))
|
|
const logos = within(nav).getAllByRole('img', { name: 'Sonda' })
|
|
expect(logos).toHaveLength(2) // the word (hidden from the wide bar) and the wave
|
|
expect(logos.filter((l) => l.classList.contains('sm:hidden'))).toHaveLength(1)
|
|
expect(logos.filter((l) => l.classList.contains('sm:block'))).toHaveLength(1)
|
|
})
|
|
|
|
test('the choice is remembered in the browser', async () => {
|
|
const first = renderSidebar()
|
|
await userEvent.click(within(first).getByRole('button', { name: 'Collapse the sidebar' }))
|
|
expect(JSON.parse(localStorage.getItem(KEY) ?? 'null')).toBe('collapsed')
|
|
document.body.innerHTML = ''
|
|
const second = renderSidebar()
|
|
expect(second).toHaveAttribute('data-collapsed', 'true')
|
|
await userEvent.click(within(second).getByRole('button', { name: 'Expand the sidebar' }))
|
|
expect(JSON.parse(localStorage.getItem(KEY) ?? 'null')).toBe('open')
|
|
})
|
|
|
|
test('a browser that will not keep it still folds and opens the sidebar', async () => {
|
|
vi.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
|
|
throw new Error('blocked')
|
|
})
|
|
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
|
|
throw new Error('blocked')
|
|
})
|
|
const nav = renderSidebar()
|
|
expect(nav).toHaveAttribute('data-collapsed', 'false')
|
|
await userEvent.click(within(nav).getByRole('button', { name: 'Collapse the sidebar' }))
|
|
expect(nav).toHaveAttribute('data-collapsed', 'true')
|
|
})
|
|
|
|
test('Friends is in the menu only when there is a friend to browse', async () => {
|
|
const none = renderSidebar()
|
|
await screen.findByRole('link', { name: 'Albums' })
|
|
expect(within(none).queryByRole('link', { name: 'Friends' })).not.toBeInTheDocument()
|
|
})
|
|
|
|
test('with a friend the menu has a Friends link', async () => {
|
|
const nav = renderSidebar([{ id: 3, name: 'Anna' }])
|
|
expect(await within(nav).findByRole('link', { name: 'Friends' })).toHaveAttribute('href', '/friends')
|
|
})
|