import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { RadarrClient } from '../../../../src/app/clients/radarr/RadarrClient'; describe('RadarrClient', () => { const fetchMock = vi.fn(); beforeEach(() => { vi.stubGlobal('fetch', fetchMock); fetchMock.mockReset(); }); afterEach(() => { vi.unstubAllGlobals(); }); it('lookup resolves TMDb metadata and fills in client-configured fields', async () => { fetchMock.mockResolvedValue({ ok: true, status: 200, text: async () => JSON.stringify({ title: 'Dune', tmdbId: 438631 }), }); const client = new RadarrClient('http://radarr', 'radarr-key', 4, '/movies'); const movie = await client.lookup(438631); expect(movie).toEqual({ title: 'Dune', tmdbId: 438631, qualityProfileId: 4, rootFolderPath: '/movies', monitored: true, addOptions: { searchForMovie: true }, }); expect(fetchMock.mock.calls[0][1].headers['X-Api-Key']).toBe('radarr-key'); }); it('addMovie treats HTTP 400 (already exists) as success', async () => { fetchMock.mockResolvedValue({ ok: false, status: 400, text: async () => '' }); const client = new RadarrClient('http://radarr', 'radarr-key', 4, '/movies'); await expect( client.addMovie({ title: 'Dune', tmdbId: 438631, qualityProfileId: 4, rootFolderPath: '/movies', monitored: true, addOptions: { searchForMovie: true }, }) ).resolves.toBeUndefined(); }); it('getExisting returns the full Radarr library', async () => { fetchMock.mockResolvedValue({ ok: true, status: 200, text: async () => JSON.stringify([{ title: 'Dune', tmdbId: 438631 }]), }); const client = new RadarrClient('http://radarr', 'radarr-key', 4, '/movies'); const existing = await client.getExisting(); expect(existing[0].tmdbId).toBe(438631); }); });