diff --git a/README.md b/README.md index a96751f..7079808 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ It is a single executable, one database (SQLite by default, PostgreSQL if you pr ## What is not there yet -Radio queues, OpenSubsonic compatibility, transcoding, and the ListenBrainz integration. The web client exists (see [Web app](#web-app)) but `sondad` does not serve it yet, and its admin area covers libraries, scanning, users and the external providers so far (the bulk jobs are still API-only). +Radio queues, OpenSubsonic compatibility, transcoding, and the ListenBrainz integration. The web client exists (see [Web app](#web-app)) but `sondad` does not serve it yet, and its admin area covers everything the API can do for an admin: libraries and scanning, users, the external providers and the bulk jobs. ## Requirements @@ -328,9 +328,9 @@ npm install SONDA_URL=http://127.0.0.1:8080 npm run dev # open http://localhost:5173, /api is proxied to Sonda ``` -What it does today: sign in (and create the first account of a new server), browse artists, albums and songs, search, and play with a queue, shuffle, repeat, media keys, a volume slider with mute (remembered in the browser) and a full-screen player. Artist pages show the Wikipedia text, similar artists and tags when those providers are on, each with its credit, and the photo credit next to the photo. It reports one listen per play. Discover suggests artists from the ones you pick, or from your listening only after you allow it (a dialog says that their names go to Last.fm), explains each score, and keeps your wishlist and the artists you are not interested in. An admin who set up the `lidarr` provider gets a "Request on Lidarr" button on an artist's page and on the Discover cards: it asks first, then sends that one artist to their own Lidarr, which does the downloading (the web app downloads nothing; the page of an artist reads what Lidarr has, the cards ask nothing until you send). An admin also gets an Admin area to add, rename, move and remove libraries and to start a scan (of one library or all, optionally reading every file again) and follow it, and to manage accounts: add one by email, give or take away the admin role, reset a password (which ends that person's sessions) and delete an account. The last admin cannot be changed, and your own account has no buttons there. Providers have a card each that says what the provider is sent: turning one on asks first, the API key is write-only (never shown, and gone from the page once saved), and there are a test of the key, the contact or address, removing what a provider gave, and, for Lidarr, the profiles and root folder to use when an artist is added. There are six themes (Sonda, Tokyo Night, Gruvbox dark, Gruvbox light, Dracula and Neon) and two languages (English and Italian), both chosen in Settings. +What it does today: sign in (and create the first account of a new server), browse artists, albums and songs, search, and play with a queue, shuffle, repeat, media keys, a volume slider with mute (remembered in the browser) and a full-screen player. Artist pages show the Wikipedia text, similar artists and tags when those providers are on, each with its credit, and the photo credit next to the photo. It reports one listen per play. Discover suggests artists from the ones you pick, or from your listening only after you allow it (a dialog says that their names go to Last.fm), explains each score, and keeps your wishlist and the artists you are not interested in. An admin who set up the `lidarr` provider gets a "Request on Lidarr" button on an artist's page and on the Discover cards: it asks first, then sends that one artist to their own Lidarr, which does the downloading (the web app downloads nothing; the page of an artist reads what Lidarr has, the cards ask nothing until you send). An admin also gets an Admin area to add, rename, move and remove libraries and to start a scan (of one library or all, optionally reading every file again) and follow it, and to manage accounts: add one by email, give or take away the admin role, reset a password (which ends that person's sessions) and delete an account. The last admin cannot be changed, and your own account has no buttons there. Providers have a card each that says what the provider is sent: turning one on asks first, the API key is write-only (never shown, and gone from the page once saved), and there are a test of the key, the contact or address, removing what a provider gave, and, for Lidarr, the profiles and root folder to use when an artist is added. The bulk jobs (album covers, artist pictures, artist descriptions) each have a card that says what the job sends and to whom: starting one asks first and says it goes through the whole library, reading everything again is a separate choice, a job whose providers are off cannot be started, and the page follows its progress and can stop it. There are six themes (Sonda, Tokyo Night, Gruvbox dark, Gruvbox light, Dracula and Neon) and two languages (English and Italian), both chosen in Settings. -More commands, all run in `web/`: `npm test`, `npm run build`, and `npm run gen:api`, which rebuilds `src/api/schema.d.ts` from the OpenAPI spec (it fetches the generator with `npx`, so it needs the network once). The bulk jobs screen and serving the app from the binary come later. +More commands, all run in `web/`: `npm test`, `npm run build`, and `npm run gen:api`, which rebuilds `src/api/schema.d.ts` from the OpenAPI spec (it fetches the generator with `npx`, so it needs the network once). Serving the app from the binary comes later. ## Development diff --git a/web/src/components/layout/Shell.test.tsx b/web/src/components/layout/Shell.test.tsx index 6fe51f3..75c95f6 100644 --- a/web/src/components/layout/Shell.test.tsx +++ b/web/src/components/layout/Shell.test.tsx @@ -71,6 +71,29 @@ test('the admin area has a Providers section too', async () => { 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 }), + }) + 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 }) }) + renderAt('/admin/jobs') + expect(screen.getByText('Only admins can see this page.')).toBeInTheDocument() + await new Promise((r) => setTimeout(r, 30)) + expect(calls).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: [] }) }) diff --git a/web/src/pages/admin/AdminLayout.tsx b/web/src/pages/admin/AdminLayout.tsx index f37873c..0b87de9 100644 --- a/web/src/pages/admin/AdminLayout.tsx +++ b/web/src/pages/admin/AdminLayout.tsx @@ -9,6 +9,7 @@ const SECTIONS = [ { to: '/admin/libraries', key: 'admin.sectionLibraries' }, { to: '/admin/users', key: 'admin.sectionUsers' }, { to: '/admin/providers', key: 'admin.sectionProviders' }, + { to: '/admin/jobs', key: 'admin.sectionJobs' }, ] /** diff --git a/web/src/pages/admin/JobsPage.test.tsx b/web/src/pages/admin/JobsPage.test.tsx new file mode 100644 index 0000000..d2e53d8 --- /dev/null +++ b/web/src/pages/admin/JobsPage.test.tsx @@ -0,0 +1,70 @@ +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import { render, screen, waitFor, within } from '@testing-library/react' +import { beforeEach, expect, test, vi } from 'vitest' +import { sessionStore } from '@/auth/session' +import { initI18n } from '@/i18n' +import { apiError, mockFetch, type MockRoutes } from '@/test/mockFetch' +import JobsPage from './JobsPage' + +const provider = (name: string, enabled: boolean) => ({ + name, + enabled, + needs_api_key: false, + needs_url: false, + has_api_key: false, + contact: '', + url: '', + sends: '', + notes: '', + updated_at: null, +}) + +function routes(enabled: Record, over: MockRoutes = {}): MockRoutes { + return { + 'GET /api/v1/providers': () => ({ providers: Object.entries(enabled).map(([name, on]) => provider(name, on)) }), + 'GET /api/v1/library/covers': () => ({ running: false }), + 'GET /api/v1/library/artist-images': () => ({ running: false }), + 'GET /api/v1/library/descriptions': () => ({ running: false }), + ...over, + } +} + +function renderPage() { + const client = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } }) + return render( + + + , + ) +} + +beforeEach(async () => { + vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US') + await initI18n() + sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: true } }) +}) + +test('it says what the jobs are and shows the three of them', async () => { + mockFetch(routes({ musicbrainz: true, commons: true, wikipedia: true, coverartarchive: false })) + renderPage() + expect(await screen.findByText(/These jobs go through the whole library in the background/)).toBeInTheDocument() + const names = (await screen.findAllByRole('heading', { level: 3 })).map((h) => h.textContent) + expect(names).toEqual(['Album covers', 'Artist pictures', 'Artist descriptions']) +}) + +test('a provider that is off blocks the jobs that need it, read from the server', async () => { + mockFetch(routes({ musicbrainz: true, commons: false, wikipedia: true, coverartarchive: false })) + renderPage() + const pictures = await screen.findByRole('region', { name: 'Artist pictures' }) + await waitFor(() => expect(within(pictures).getByRole('button', { name: 'Start Artist pictures' })).toBeDisabled()) + expect(pictures).toHaveTextContent('Needs Wikimedia Commons turned on') + const descriptions = screen.getByRole('region', { name: 'Artist descriptions' }) + expect(within(descriptions).getByRole('button', { name: 'Start Artist descriptions' })).toBeEnabled() +}) + +test('the jobs still show, and are not blocked, when the providers cannot be read', async () => { + mockFetch(routes({}, { 'GET /api/v1/providers': () => apiError(500, 'internal') })) + renderPage() + const pictures = await screen.findByRole('region', { name: 'Artist pictures' }) + expect(within(pictures).getByRole('button', { name: 'Start Artist pictures' })).toBeEnabled() +}) diff --git a/web/src/pages/admin/JobsPage.tsx b/web/src/pages/admin/JobsPage.tsx new file mode 100644 index 0000000..b5754dd --- /dev/null +++ b/web/src/pages/admin/JobsPage.tsx @@ -0,0 +1,28 @@ +import { useTranslation } from 'react-i18next' +import { useProviders } from '@/api/adminProviders' +import type { JobKind } from '@/api/jobs' +import Section from '@/components/Section' +import JobCard from './JobCard' + +const JOBS: JobKind[] = ['covers', 'artist-images', 'descriptions'] + +/** + * The bulk jobs, one card each. It reads which providers are on, to block a job whose provider is off; when + * they cannot be read nothing is blocked (the server would end such a job at once and say why). + */ +export default function JobsPage() { + const { t } = useTranslation() + const providers = useProviders() + const enabled = Object.fromEntries((providers.data ?? []).map((p) => [p.name, p.enabled])) + + return ( +
+

{t('admin.jobs.intro')}

+
+ {JOBS.map((kind) => ( + + ))} +
+
+ ) +} diff --git a/web/src/router.tsx b/web/src/router.tsx index efec78a..d5b7b8c 100644 --- a/web/src/router.tsx +++ b/web/src/router.tsx @@ -7,6 +7,7 @@ import ArtistsPage from '@/pages/ArtistsPage' import DiscoverPage from '@/pages/DiscoverPage' import HomePage from '@/pages/HomePage' import AdminLayout from '@/pages/admin/AdminLayout' +import JobsPage from '@/pages/admin/JobsPage' import LibrariesPage from '@/pages/admin/LibrariesPage' import ProvidersPage from '@/pages/admin/ProvidersPage' import UsersPage from '@/pages/admin/UsersPage' @@ -36,6 +37,7 @@ export const routes: RouteObject[] = [ { path: 'libraries', element: }, { path: 'users', element: }, { path: 'providers', element: }, + { path: 'jobs', element: }, ], }, { path: '*', element: },