The admin frame shows its sections only to an admin and asks nothing of the server for anyone else. Libraries can be added, edited (only what changed is sent) and removed after a confirmation that says the music on disk is never touched; a scan can be started for one library or all, optionally reading every file again, and the panel follows it and shows the summary, with the files in unsupported formats standing out.
81 lines
3.1 KiB
TypeScript
81 lines
3.1 KiB
TypeScript
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { render, screen, waitFor } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { beforeEach, expect, test, vi } from 'vitest'
|
|
import { sessionStore } from '@/auth/session'
|
|
import { initI18n } from '@/i18n'
|
|
import { apiError, mockFetch } from '@/test/mockFetch'
|
|
import AddLibraryForm from './AddLibraryForm'
|
|
|
|
const created = () =>
|
|
new Response(JSON.stringify({ id: 9, name: 'Jazz', path: '/srv/jazz', created_at: '2026-11-12T10:00:00Z' }), {
|
|
status: 201,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
|
|
function renderForm() {
|
|
const client = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } })
|
|
return render(
|
|
<QueryClientProvider client={client}>
|
|
<AddLibraryForm />
|
|
</QueryClientProvider>,
|
|
)
|
|
}
|
|
|
|
const name = () => screen.getByLabelText('Name')
|
|
const folder = () => screen.getByLabelText('Folder on the server')
|
|
const add = () => screen.getByRole('button', { name: 'Add library' })
|
|
|
|
beforeEach(async () => {
|
|
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
|
|
await initI18n()
|
|
sessionStore.setState({ status: 'authenticated', user: { id: 1, username: 'tom', is_admin: true } })
|
|
})
|
|
|
|
test('it says the folder is on the machine that runs Sonda, not on this computer', () => {
|
|
mockFetch({})
|
|
renderForm()
|
|
expect(screen.getByText(/on the machine that runs Sonda, not on this computer/)).toBeInTheDocument()
|
|
})
|
|
|
|
test('nothing is sent while the name or the folder is empty', async () => {
|
|
const { calls } = mockFetch({})
|
|
renderForm()
|
|
expect(add()).toBeDisabled()
|
|
await userEvent.type(name(), 'Jazz')
|
|
expect(add()).toBeDisabled()
|
|
await userEvent.type(folder(), ' ')
|
|
expect(add()).toBeDisabled()
|
|
expect(calls).toHaveLength(0)
|
|
})
|
|
|
|
test('adding sends the trimmed name and folder, then empties the form', async () => {
|
|
const { calls } = mockFetch({ 'POST /api/v1/libraries': created })
|
|
renderForm()
|
|
await userEvent.type(name(), ' Jazz ')
|
|
await userEvent.type(folder(), ' /srv/jazz ')
|
|
await userEvent.click(add())
|
|
await waitFor(() => expect(name()).toHaveValue(''))
|
|
expect(folder()).toHaveValue('')
|
|
expect(JSON.parse(String(calls[0].init.body))).toEqual({ name: 'Jazz', path: '/srv/jazz' })
|
|
})
|
|
|
|
test.each([
|
|
[400, 'invalid_input', 'The folder must be an existing directory on the server, not the root, and not inside or around another library.'],
|
|
[409, 'library_exists', 'Another library already has this name or folder.'],
|
|
])('a refusal (%i %s) is explained and what was typed stays', async (status, code, text) => {
|
|
mockFetch({ 'POST /api/v1/libraries': () => apiError(status, code) })
|
|
renderForm()
|
|
await userEvent.type(name(), 'Jazz')
|
|
await userEvent.type(folder(), '/srv/jazz')
|
|
await userEvent.click(add())
|
|
expect(await screen.findByRole('alert')).toHaveTextContent(text)
|
|
expect(name()).toHaveValue('Jazz')
|
|
expect(folder()).toHaveValue('/srv/jazz')
|
|
})
|
|
|
|
test('the name is limited to the 100 characters the server accepts', () => {
|
|
mockFetch({})
|
|
renderForm()
|
|
expect(name()).toHaveAttribute('maxlength', '100')
|
|
})
|