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.
This commit is contained in:
parent
bfcf954673
commit
9dcb7637e7
2 changed files with 123 additions and 0 deletions
83
web/src/api/lidarr.test.tsx
Normal file
83
web/src/api/lidarr.test.tsx
Normal file
|
|
@ -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 }) => <QueryClientProvider client={client}>{children}</QueryClientProvider>
|
||||
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)
|
||||
})
|
||||
40
web/src/api/lidarr.ts
Normal file
40
web/src/api/lidarr.ts
Normal file
|
|
@ -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<ArtistInLidarr>(`/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<SendResult>(`/artists/${artistId}/lidarr`),
|
||||
onSuccess: reread,
|
||||
onError: (error) => {
|
||||
if (error instanceof ApiError && error.code === 'lidarr_unavailable') return reread()
|
||||
},
|
||||
})
|
||||
}
|
||||
Loading…
Reference in a new issue