import { afterEach, describe, expect, test, vi } from 'vitest'; import { render, screen, fireEvent, waitFor, within } from '@testing-library/svelte'; import type { AcquisitionService } from '#lib/api/admin.js'; vi.mock('#lib/api/admin.js', () => ({ listAcquisitionServices: vi.fn(), addAcquisitionService: vi.fn(), updateAcquisitionService: vi.fn(), removeAcquisitionService: vi.fn(), testAcquisitionService: vi.fn(), setAcquisitionSecret: vi.fn(), clearAcquisitionSecret: vi.fn() })); vi.mock('#lib/stores/toast.svelte.js', () => ({ pushToast: vi.fn() })); import AcquisitionCard from './AcquisitionCard.svelte'; import { addAcquisitionService, listAcquisitionServices, setAcquisitionSecret, testAcquisitionService, updateAcquisitionService } from '#lib/api/admin.js'; function service(over: Partial = {}): AcquisitionService { return { code: 'prowlarr', kind: 'search', protocol: 'prowlarr', label: 'Prowlarr', enabled: false, base_url: '', has_api_key: false, has_password: false, username: '', category: '', timeout_ms: 60000, serves_search: true, serves_downloads: true, serves_enrichment: true, local_path: '/downloads', remote_path: '', searches_per_hour: 120, usable: false, ...over }; } const exchange = (over: Partial = {}) => service({ code: 'exchange', kind: 'download', protocol: 'exchange', label: 'FabledExchange', category: 'minstrel', searches_per_hour: 0, ...over }); afterEach(() => vi.clearAllMocks()); async function renderCard(services: AcquisitionService[]) { vi.mocked(listAcquisitionServices).mockResolvedValue(services); render(AcquisitionCard); await screen.findByTestId('acquisition-health'); } describe('AcquisitionCard', () => { test('starts empty: nothing is seeded', async () => { await renderCard([]); expect(screen.getByText(/no indexer yet/i)).toBeInTheDocument(); expect(screen.getByText(/no download client yet/i)).toBeInTheDocument(); expect(screen.getByTestId('acquisition-health')).toHaveTextContent(/nothing can be downloaded/i); }); test('adding offers the types each list takes', async () => { await renderCard([]); await fireEvent.click(screen.getByRole('button', { name: 'Add indexer' })); const indexerChoices = within(screen.getByTestId('add-search')); expect(indexerChoices.getByRole('button', { name: /prowlarr/i })).toBeInTheDocument(); expect(indexerChoices.getByRole('button', { name: /torznab feed/i })).toBeInTheDocument(); expect(indexerChoices.queryByRole('button', { name: /qbittorrent/i })).toBeNull(); vi.mocked(addAcquisitionService).mockResolvedValue(service()); await fireEvent.click(indexerChoices.getByRole('button', { name: /prowlarr/i })); expect(addAcquisitionService).toHaveBeenCalledWith('prowlarr'); await fireEvent.click(screen.getByRole('button', { name: 'Add download client' })); const clientChoices = within(screen.getByTestId('add-download')); expect(clientChoices.getByRole('button', { name: /qbittorrent/i })).toBeInTheDocument(); expect(clientChoices.getByRole('button', { name: /fabledexchange/i })).toBeInTheDocument(); }); test('an exchange sits under download clients with a switch per job; others have none', async () => { await renderCard([ service({ code: 'qbittorrent', kind: 'download', protocol: 'qbittorrent', label: 'qBittorrent' }), exchange() ]); const clients = within(screen.getByTestId('download-clients')); expect(clients.getAllByTestId('acquisition-service')).toHaveLength(2); // Only the exchange carries the job switches. expect(clients.getAllByTestId('exchange-jobs')).toHaveLength(1); expect(clients.getByRole('checkbox', { name: /downloads what is grabbed/i })).toBeChecked(); expect(clients.getByRole('checkbox', { name: /searches its dht index/i })).toBeChecked(); expect(clients.getByRole('checkbox', { name: /checks a release/i })).toBeChecked(); // qBittorrent asks for a username and a password; the exchange for an optional key. expect(clients.getByText('Username')).toBeInTheDocument(); expect(clients.getByText(/save password/i)).toBeInTheDocument(); expect(clients.getByText(/save key/i)).toBeInTheDocument(); }); test('an exchange searching is named under indexers too', async () => { await renderCard([exchange({ usable: true, enabled: true, base_url: 'http://ex' })]); expect(within(screen.getByTestId('indexers')).getByText(/also searched: fabledexchange/i)).toBeInTheDocument(); expect(screen.getByTestId('acquisition-health')).toHaveTextContent(/searched for and downloaded/i); }); test('switching a job off and saving sends the switches', async () => { const ex = exchange({ base_url: 'http://ex' }); await renderCard([ex]); await fireEvent.click(screen.getByRole('checkbox', { name: /checks a release/i })); vi.mocked(updateAcquisitionService).mockResolvedValue({ ...ex, serves_enrichment: false }); await fireEvent.click(screen.getByRole('button', { name: /^save$/i })); await waitFor(() => expect(updateAcquisitionService).toHaveBeenCalled()); const [code, sent] = vi.mocked(updateAcquisitionService).mock.calls[0]; expect(code).toBe('exchange'); expect(sent).toMatchObject({ serves_enrichment: false, serves_search: true, serves_downloads: true }); }); test('a refused setting is shown beside the form in the server’s words', async () => { await renderCard([service({ base_url: 'http://p' })]); await fireEvent.input(screen.getByRole('textbox', { name: /^name$/i }), { target: { value: 'Mine' } }); vi.mocked(updateAcquisitionService).mockRejectedValue({ code: 'invalid_setting', message: 'Address: start with http:// or https://.', status: 400 }); await fireEvent.click(screen.getByRole('button', { name: /^save$/i })); expect(await screen.findByTestId('acquisition-problem')).toHaveTextContent( 'Address: start with http:// or https://.' ); }); test('Test shows what the server found, and needs an address', async () => { await renderCard([service({ code: 'a' }), service({ code: 'b', label: 'Second', base_url: 'http://p' })]); const [noAddress, addressed] = screen.getAllByRole('button', { name: 'Test' }); expect(noAddress).toBeDisabled(); vi.mocked(testAcquisitionService).mockResolvedValue({ ok: false, detail: 'the API key was refused: HTTP 401. Check it on the service’s own settings page.' }); await fireEvent.click(addressed); expect(testAcquisitionService).toHaveBeenCalledWith('b'); expect(await screen.findByTestId('acquisition-test-result')).toHaveTextContent(/key was refused/i); }); test('a key is sent write-only and never shown back', async () => { await renderCard([service({ base_url: 'http://p', has_api_key: true })]); expect(screen.getByLabelText(/replace the api key/i)).toHaveValue(''); await fireEvent.input(screen.getByLabelText(/replace the api key/i), { target: { value: 'k2' } }); vi.mocked(setAcquisitionSecret).mockResolvedValue(undefined); await fireEvent.click(screen.getByRole('button', { name: /save key/i })); await waitFor(() => expect(setAcquisitionSecret).toHaveBeenCalledWith('prowlarr', 'api-key', 'k2')); }); });