sonda/web/src/pages/RadioPage.test.tsx
tgiachi 0b3bf8d49f feat: a Radio page, endless play, and the radio of an artist
The Radio page shows the stations Sonda makes by itself, in groups (made for you,
by genre, by decade). A click starts one and the player knows it is a radio: before
the queue runs out it asks the server for other songs of the same station and puts
them at the end (not the ones already queued; a station so small that all its songs
are queued starts them over), one question at a time, never twice within two seconds,
and again after ten when the server cannot answer. Every artist has a Start radio
button. The full-screen player says which station plays.

- Radio item in the menu (Library group), on the Library page and in the phone tab
- player store: radio and appendTracks; useRadioRefill
2026-11-16 12:18:15 +01:00

97 lines
5.2 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 { initI18n } from '@/i18n'
import { usePlayer } from '@/player/store'
import { apiError, mockFetch, type MockRoutes } from '@/test/mockFetch'
import RadioPage from './RadioPage'
beforeEach(async () => {
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
await initI18n()
usePlayer.setState({ queue: [], original: [], index: 0, playing: false, positionMs: 0, radio: null })
})
const station = (id: string, kind: string, title: string, over: object = {}) => ({ id, kind, title, genre: null, decade: null, artists: [], album_ids: [], ...over })
const stations = [
station('favourites', 'favourites', 'Your favourites'),
station('unheard', 'unheard', 'New to you'),
station('genre:hard rock', 'genre', 'Hard Rock Mix', { genre: 'hard rock' }),
station('genre:jazz', 'genre', 'Jazz Mix', { genre: 'jazz' }),
station('all', 'all', 'Everything'),
station('decade:1990', 'decade', '1990s', { decade: 1990 }),
station('decade:2000', 'decade', '2000s', { decade: 2000 }),
]
const song = (id: number) => ({ id, title: `Song ${id}`, duration_ms: 1000, track_no: 1, disc_no: 1, album: null, artists: [{ id: 1, name: 'Björk' }], format: 'mp3', replay_gain: null })
const played = (id: string, title: string, kind = 'all', over: object = {}) => ({ ...station(id, kind, title, over), tracks: [song(1), song(2), song(3)] })
function setup(over: MockRoutes = {}) {
const mocked = mockFetch({ 'GET /api/v1/radio': () => ({ items: stations }), ...over })
render(
<QueryClientProvider client={new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } })}>
<MemoryRouter>
<RadioPage />
</MemoryRouter>
</QueryClientProvider>,
)
return mocked
}
test('the page has the stations that Sonda made, in groups, with names in the language of the page', async () => {
setup()
expect(screen.getByRole('heading', { level: 1, name: 'Radio' })).toBeInTheDocument()
const forYou = await screen.findByRole('region', { name: 'Made for you' })
expect(within(forYou).getAllByRole('button').map((b) => b.textContent)).toEqual([
expect.stringContaining('Your favourites'),
expect.stringContaining('New to you'),
expect.stringContaining('Everything'),
])
const genres = screen.getByRole('region', { name: 'By genre' })
expect(within(genres).getByRole('button', { name: /Hard Rock Radio/ })).toBeInTheDocument()
expect(within(genres).getByRole('button', { name: /Jazz Radio/ })).toBeInTheDocument()
const decades = screen.getByRole('region', { name: 'By decade' })
expect(within(decades).getAllByRole('button').map((b) => b.textContent)).toEqual([expect.stringContaining('1990s'), expect.stringContaining('2000s')])
})
test('a click starts the station: its songs play, and the player knows it is a radio', async () => {
const { calls } = setup({ 'GET /api/v1/radio/genre%3Ajazz': () => played('genre:jazz', 'Jazz Mix', 'genre', { genre: 'jazz' }) })
await userEvent.click(await screen.findByRole('button', { name: /Jazz Radio/ }))
await vi.waitFor(() => expect(usePlayer.getState().queue).toHaveLength(3))
expect(usePlayer.getState().radio).toEqual({ id: 'genre:jazz', title: 'Jazz Radio' })
expect(usePlayer.getState().playing).toBe(true)
expect(calls.filter((c) => c.url.pathname.startsWith('/api/v1/radio/'))).toHaveLength(1)
})
test('the station that plays is marked', async () => {
setup({ 'GET /api/v1/radio/all': () => played('all', 'Everything') })
await userEvent.click(await screen.findByRole('button', { name: /Everything/ }))
await vi.waitFor(() => expect(screen.getByRole('button', { name: /Everything/ })).toHaveAttribute('aria-pressed', 'true'))
expect(screen.getByRole('button', { name: /Jazz Radio/ })).toHaveAttribute('aria-pressed', 'false')
expect(within(screen.getByRole('button', { name: /Everything/ })).getByText('Playing')).toBeInTheDocument()
})
test('a station that cannot start says so and can be tried again', async () => {
let fail = true
setup({ 'GET /api/v1/radio/all': () => (fail ? apiError(500, 'internal', 'x') : played('all', 'Everything')) })
await userEvent.click(await screen.findByRole('button', { name: /Everything/ }))
expect(await screen.findByRole('alert')).toBeInTheDocument()
fail = false
await userEvent.click(screen.getByRole('button', { name: /Everything/ }))
await vi.waitFor(() => expect(usePlayer.getState().queue).toHaveLength(3))
})
test('a list that cannot be read has a way to try again', async () => {
let fail = true
setup({ 'GET /api/v1/radio': () => (fail ? apiError(500, 'internal', 'x') : { items: stations }) })
const retry = await screen.findByRole('button', { name: 'Try again' })
fail = false
await userEvent.click(retry)
expect(await screen.findByRole('region', { name: 'Made for you' })).toBeInTheDocument()
})
test('with no station at all it says why', async () => {
setup({ 'GET /api/v1/radio': () => ({ items: [] }) })
expect(await screen.findByText(/no station/i)).toBeInTheDocument()
})