From 9dcb7637e7c0e8a10533b45fbf3d76a2ee14e169 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Thu, 12 Nov 2026 14:28:17 +0100 Subject: [PATCH] feat(web): hooks to read and send an artist to Lidarr Reading is a status, never an error. Sending is one POST for one artist; when it worked, or Lidarr did not answer in time, what Lidarr has is read again. --- web/src/api/lidarr.test.tsx | 83 +++++++++++++++++++++++++++++++++++++ web/src/api/lidarr.ts | 40 ++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 web/src/api/lidarr.test.tsx create mode 100644 web/src/api/lidarr.ts diff --git a/web/src/api/lidarr.test.tsx b/web/src/api/lidarr.test.tsx new file mode 100644 index 0000000..afde43b --- /dev/null +++ b/web/src/api/lidarr.test.tsx @@ -0,0 +1,83 @@ +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import { act, renderHook, waitFor } from '@testing-library/react' +import type { ReactNode } from 'react' +import { expect, test } from 'vitest' +import { apiError, mockFetch } from '@/test/mockFetch' +import { useArtistInLidarr, useSendToLidarr } from './lidarr' + +function setup() { + const client = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } }) + const wrapper = ({ children }: { children: ReactNode }) => {children} + return { client, wrapper } +} + +const ok = (in_lidarr: boolean, monitored = false) => ({ provider_status: 'ok', in_lidarr, monitored }) + +test('what Lidarr has is read from GET /artists/{id}/lidarr', async () => { + mockFetch({ 'GET /api/v1/artists/7/lidarr': () => ok(true, true) }) + const { result } = renderHook(() => useArtistInLidarr(7), { wrapper: setup().wrapper }) + await waitFor(() => expect(result.current.data).toEqual(ok(true, true))) +}) + +test('it asks nothing while it is not enabled', async () => { + const { calls } = mockFetch({ 'GET /api/v1/artists/7/lidarr': () => ok(false) }) + const { result } = renderHook(() => useArtistInLidarr(7, { enabled: false }), { wrapper: setup().wrapper }) + await new Promise((r) => setTimeout(r, 30)) + expect(calls).toHaveLength(0) + expect(result.current.fetchStatus).toBe('idle') +}) + +test('sending is a POST to the artist and nothing else', async () => { + const { calls } = mockFetch({ 'POST /api/v1/artists/7/lidarr': () => ({ status: 'added' }) }) + const { result } = renderHook(() => useSendToLidarr(7), { wrapper: setup().wrapper }) + await act(() => result.current.mutateAsync()) + expect(calls.map((c) => c.key)).toEqual(['POST /api/v1/artists/7/lidarr']) + await waitFor(() => expect(result.current.data).toEqual({ status: 'added' })) +}) + +test('after it was sent, what Lidarr has is read again', async () => { + let has = false + const { calls } = mockFetch({ + 'GET /api/v1/artists/7/lidarr': () => ok(has, has), + 'POST /api/v1/artists/7/lidarr': () => { + has = true + return { status: 'added' } + }, + }) + const { wrapper } = setup() + const { result } = renderHook(() => ({ state: useArtistInLidarr(7), send: useSendToLidarr(7) }), { wrapper }) + await waitFor(() => expect(result.current.state.data?.in_lidarr).toBe(false)) + await act(() => result.current.send.mutateAsync()) + await waitFor(() => expect(result.current.state.data?.in_lidarr).toBe(true)) + expect(calls.filter((c) => c.key === 'GET /api/v1/artists/7/lidarr')).toHaveLength(2) +}) + +test('when Lidarr did not answer in time it is read again, because the artist may have been added', async () => { + let has = false + const { calls } = mockFetch({ + 'GET /api/v1/artists/7/lidarr': () => ok(has, has), + 'POST /api/v1/artists/7/lidarr': () => { + has = true + return apiError(502, 'lidarr_unavailable') + }, + }) + const { wrapper } = setup() + const { result } = renderHook(() => ({ state: useArtistInLidarr(7), send: useSendToLidarr(7) }), { wrapper }) + await waitFor(() => expect(result.current.state.data?.in_lidarr).toBe(false)) + await act(() => result.current.send.mutateAsync().catch(() => undefined)) + await waitFor(() => expect(result.current.state.data?.in_lidarr).toBe(true)) + expect(calls.filter((c) => c.key === 'GET /api/v1/artists/7/lidarr')).toHaveLength(2) +}) + +test('a refusal does not read Lidarr again: nothing changed', async () => { + const { calls } = mockFetch({ + 'GET /api/v1/artists/7/lidarr': () => ok(false), + 'POST /api/v1/artists/7/lidarr': () => apiError(422, 'lidarr_rejected', 'Artist exists'), + }) + const { wrapper } = setup() + const { result } = renderHook(() => ({ state: useArtistInLidarr(7), send: useSendToLidarr(7) }), { wrapper }) + await waitFor(() => expect(result.current.state.isSuccess).toBe(true)) + await act(() => result.current.send.mutateAsync().catch(() => undefined)) + await new Promise((r) => setTimeout(r, 30)) + expect(calls.filter((c) => c.key === 'GET /api/v1/artists/7/lidarr')).toHaveLength(1) +}) diff --git a/web/src/api/lidarr.ts b/web/src/api/lidarr.ts new file mode 100644 index 0000000..663681d --- /dev/null +++ b/web/src/api/lidarr.ts @@ -0,0 +1,40 @@ +import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' +import { api, ApiError } from './client' +import type { components } from './schema' + +export type ArtistInLidarr = components['schemas']['ArtistInLidarr'] +export interface SendResult { + status: 'added' | 'already_added' +} + +const lidarrKey = (artistId: number) => ['artist', artistId, 'lidarr'] + +/** + * Whether the user's own Lidarr has the artist. Whatever stops the answer (provider off, no MusicBrainz id, + * no address or key, Lidarr down) is a `provider_status`, not an error. Admins only: others get a 403. + */ +export function useArtistInLidarr(artistId: number, opts: { enabled?: boolean } = {}) { + return useQuery({ + queryKey: lidarrKey(artistId), + enabled: opts.enabled ?? true, + retry: false, + queryFn: () => api.get(`/artists/${artistId}/lidarr`), + }) +} + +/** + * Sends one artist to Lidarr, which then looks for and downloads the music by itself. It is only ever called + * for one artist, from a click the user confirmed. When it worked, or when Lidarr did not answer in time (the + * artist may have been added all the same), what Lidarr has is read again; a refusal changes nothing. + */ +export function useSendToLidarr(artistId: number) { + const client = useQueryClient() + const reread = () => client.invalidateQueries({ queryKey: lidarrKey(artistId) }) + return useMutation({ + mutationFn: () => api.post(`/artists/${artistId}/lidarr`), + onSuccess: reread, + onError: (error) => { + if (error instanceof ApiError && error.code === 'lidarr_unavailable') return reread() + }, + }) +}