import { afterEach, describe, expect, test, vi } from 'vitest'; import { fireEvent, render, screen } from '@testing-library/svelte'; import { readable } from 'svelte/store'; import type { AdminSuspectResponse, AdminSuspectTrack } from '#lib/api/types.js'; vi.mock('#lib/api/admin.js', () => ({ createSuspectSourcesQuery: vi.fn(), setSuspectVerdict: vi.fn(async () => {}), removeSuspectFile: vi.fn(async () => {}) })); import SuspectSourcesPage from './+page.svelte'; import { createSuspectSourcesQuery, removeSuspectFile, setSuspectVerdict } from '#lib/api/admin.js'; const mocked = createSuspectSourcesQuery as ReturnType; const HUMANZ = '/music/Gorillaz/Humanz (2017)'; function track(title: string, file: string, markers: string[], disc: number | null = 15): AdminSuspectTrack { return { track_id: `t-${title}`, title, artist_id: 'ar-1', artist_name: 'Gorillaz', album_id: 'al-1', album_title: 'Humanz', file_path: `${HUMANZ}/${file}`, duration_sec: 180, disc_number: disc, track_number: 4, markers, verdict: 'suspect', verdict_at: new Date(Date.now() - 3_600_000).toISOString(), }; } function page(offset: number, total: number, groups: AdminSuspectResponse['groups']): AdminSuspectResponse { return { total, limit: 50, offset, groups }; } // The page reads only these fields of the infinite query. function infinite(pages: AdminSuspectResponse[], opts: { hasNextPage?: boolean; isPending?: boolean } = {}) { return readable({ data: { pages }, isPending: opts.isPending ?? false, isError: false, hasNextPage: opts.hasNextPage ?? false, isFetchingNextPage: false, fetchNextPage: () => {}, refetch: () => {} }); } afterEach(() => vi.clearAllMocks()); describe('Suspect sources page', () => { test('shows each flagged track with its file name and markers', () => { mocked.mockReturnValue( infinite([ page(0, 2, [ { directory: HUMANZ, tracks: [ track('Strobelite', 'Gorillaz - Strobelite (Official Video).mp3', ['music video']), track('Andromeda', 'Gorillaz - Andromeda [Audio] [HQ].mp3', ['[Audio]', '[HD]']) ] } ]) ]) ); render(SuspectSourcesPage); expect(screen.getByTestId('suspect-count-pill')).toHaveTextContent('2'); expect(screen.getByText(HUMANZ)).toBeInTheDocument(); expect(screen.getByText('Gorillaz - Strobelite (Official Video).mp3')).toBeInTheDocument(); expect(screen.getAllByTestId('suspect-marker').map((m) => m.textContent)).toEqual([ 'music video', '[Audio]', '[HD]' ]); expect(screen.getAllByText(/disc 15, track 4/)).toHaveLength(2); }); // A folder split across two pages is still one folder on screen. test('joins a folder that straddles a page boundary', () => { mocked.mockReturnValue( infinite([ page(0, 3, [{ directory: HUMANZ, tracks: [track('Strobelite', 'a (Official Video).mp3', ['music video'])] }]), page(1, 3, [ { directory: HUMANZ, tracks: [track('Momentz', 'b (Visualizer).mp3', ['visualiser'])] }, { directory: '/music/Daft Punk/RAM', tracks: [track('Doin It Right', 'c (Music Video).mp3', ['music video'])] } ]) ]) ); render(SuspectSourcesPage); expect(screen.getAllByText(HUMANZ)).toHaveLength(1); expect(screen.getAllByText('2 tracks')).toHaveLength(1); expect(screen.getAllByTestId('suspect-track-row')).toHaveLength(3); }); test('empty library explains what would land here', () => { mocked.mockReturnValue(infinite([page(0, 0, [])])); render(SuspectSourcesPage); expect(screen.getByText(/no file names look like video rips/i)).toBeInTheDocument(); }); test('loads more as you scroll, with no button', () => { mocked.mockReturnValue( infinite([page(0, 80, [{ directory: HUMANZ, tracks: [track('Strobelite', 'a (Official Video).mp3', ['music video'])] }])], { hasNextPage: true }) ); const { container } = render(SuspectSourcesPage); expect(container.querySelector('div[aria-hidden="true"].h-px')).not.toBeNull(); expect(screen.queryByRole('button', { name: /more/i })).not.toBeInTheDocument(); }); // A flagged track is held back from radio and the mixes, and the // operator can let it back. test('This one is fine lets a held-back track back in', async () => { mocked.mockReturnValue( infinite([ page(0, 1, [ { directory: HUMANZ, tracks: [track('Strobelite', 'Gorillaz - Strobelite (Official Video).mp3', ['music video'])] } ]) ]) ); render(SuspectSourcesPage); expect(screen.getByTestId('suspect-verdict')).toHaveTextContent(/Held back/); await fireEvent.click(screen.getByRole('button', { name: 'This one is fine' })); expect(setSuspectVerdict).toHaveBeenCalledWith('t-Strobelite', 'fine'); expect(await screen.findByText('Marked fine')).toBeInTheDocument(); expect(screen.getByRole('button', { name: 'Hold back again' })).toBeInTheDocument(); }); // A rip that is not fine has its file removed, on a second click; the track // waits for a clean copy. test('Remove this file asks once more, then removes the rip', async () => { mocked.mockReturnValue( infinite([ page(0, 1, [ { directory: HUMANZ, tracks: [track('Strobelite', 'Gorillaz - Strobelite (Official Video).mp3', ['music video'])] } ]) ]) ); render(SuspectSourcesPage); await fireEvent.click(screen.getByRole('button', { name: 'Remove the file of Strobelite' })); expect(removeSuspectFile).not.toHaveBeenCalled(); await fireEvent.click(screen.getByRole('button', { name: 'Confirm remove the file of Strobelite' })); expect(removeSuspectFile).toHaveBeenCalledWith('t-Strobelite'); }); });