import { afterEach, describe, expect, test, vi } from 'vitest'; import { fireEvent, render, screen, waitFor, within } from '@testing-library/svelte'; import { mockQuery } from '../../../test-utils/query'; import type { AdminMissingResponse, AdminMissingTrack } from '#lib/api/types.js'; vi.mock('#lib/api/admin.js', () => ({ createMissingFilesQuery: vi.fn(), forgetMissingTracks: vi.fn().mockResolvedValue({ forgotten: 1 }), MISSING_FILES_CSV_URL: '/api/admin/library/missing.csv' })); import MissingPage from './+page.svelte'; import { createMissingFilesQuery, forgetMissingTracks } from '#lib/api/admin.js'; const DAY = 24 * 3_600_000; function track( title: string, opts: { lastPlayed?: string | null; removedFor?: AdminMissingTrack['removed_for']; removedBy?: string } = {} ): AdminMissingTrack { return { track_id: `t-${title}`, title, artist_id: 'ar-1', artist_name: 'Lincoln Yard', album_id: 'al-1', album_title: 'Minutes to Noon', file_path: `/music/Lincoln Yard/Minutes to Noon/${title}.flac`, duration_sec: 185, disc_number: 1, track_number: 1, recording_mbid: null, release_group_mbid: null, removed_for: opts.removedFor ?? null, removed_by: opts.removedBy ?? null, missing_since: new Date(Date.now() - 3 * DAY).toISOString(), last_played_at: opts.lastPlayed === undefined ? null : opts.lastPlayed }; } const response: AdminMissingResponse = { total: 3, limit: 50, offset: 0, groups: [ { directory: '/music/Lincoln Yard/Minutes to Noon', missing_since: new Date(Date.now() - 3 * DAY).toISOString(), tracks: [ track('Paper Wings', { lastPlayed: new Date(Date.now() - 2 * DAY).toISOString() }), track('Ring It Out', { removedFor: 'report', removedBy: 'alice' }) ] }, { directory: '/music/Fields of Ontario/Geomantic', missing_since: new Date(Date.now() - 9 * DAY).toISOString(), tracks: [track('1969')] } ] }; afterEach(() => vi.clearAllMocks()); function renderWith(data: AdminMissingResponse | undefined, extra = {}) { vi.mocked(createMissingFilesQuery).mockReturnValue( mockQuery({ data, ...extra }) as ReturnType ); return render(MissingPage); } describe('/collection/missing', () => { test('groups rows under their directory', () => { renderWith(response); expect(screen.getByText('/music/Lincoln Yard/Minutes to Noon')).toBeTruthy(); expect(screen.getByText('/music/Fields of Ontario/Geomantic')).toBeTruthy(); expect(screen.getAllByTestId('missing-track-row')).toHaveLength(3); }); test('the count pill reports the server total, not the page size', () => { renderWith(response); expect(screen.getByTestId('missing-count-pill').textContent?.trim()).toBe('3'); }); test('a group states how many tracks and how long they have waited', () => { renderWith(response); expect(screen.getByText(/2 tracks · 3d ago/)).toBeTruthy(); expect(screen.getByText(/1 track · 9d ago/)).toBeTruthy(); }); // The deciding fact for whether a missing file is worth chasing. // The page's lengths come from the shared formatter: an hour-long track // reads 1:01:05, not 61:05. test('each track shows its length, hours and all', () => { const long = structuredClone(response); long.groups[1].tracks[0].duration_sec = 3665; renderWith(long); expect(screen.getAllByTestId('missing-track-length').map((el) => el.textContent?.trim())).toEqual([ '3:05', '3:05', '1:01:05' ]); }); test('distinguishes a played track from one never played', () => { renderWith(response); expect(screen.getByText(/last played 2d ago/)).toBeTruthy(); expect(screen.getAllByText('never played')).toHaveLength(2); }); // An operator who has never seen this page should not have to guess what // would put a row here. test('the empty state says how a track gets here and how it leaves', () => { renderWith({ total: 0, limit: 50, offset: 0, groups: [] }); expect(screen.getByText(/every track has its file/i)).toBeTruthy(); expect(screen.getByText(/removed from one of the collection's queues/i)).toBeTruthy(); expect(screen.getByText(/when a good copy is in the library/i)).toBeTruthy(); expect(screen.queryByTestId('missing-count-pill')).toBeNull(); }); test('shows a loading line while pending', () => { renderWith(undefined, { isPending: true }); expect(screen.getByText(/reading the list/i)).toBeTruthy(); }); test('shows an error line when the query fails', () => { renderWith(undefined, { isError: true }); expect(screen.getByText(/couldn't load the list/i)).toBeTruthy(); }); // Paging only appears when it can do something: a single page of results // should not render dead Previous/Next buttons. test('no pager when everything fits on one page', () => { renderWith(response); expect(screen.queryByLabelText('Needs a file pages')).toBeNull(); }); test('pager appears when the server reports more than this page holds', () => { renderWith({ ...response, total: 120 }); expect(screen.getByLabelText('Needs a file pages')).toBeTruthy(); expect(screen.getByText('1–3 of 120')).toBeTruthy(); expect(screen.getByRole('button', { name: 'Previous' })).toHaveProperty('disabled', true); expect(screen.getByRole('button', { name: 'Next' })).toHaveProperty('disabled', false); }); test('each track says why it waits', () => { renderWith(response); const why = screen.getAllByTestId('missing-track-why').map((e) => e.textContent ?? ''); expect(why[0]).toMatch(/file went missing from disk/i); expect(why[1]).toMatch(/file removed from a report by alice/i); }); test('Forget asks first, then forgets the one track', async () => { renderWith(response); await fireEvent.click(screen.getByRole('button', { name: 'Forget Paper Wings' })); expect(forgetMissingTracks).not.toHaveBeenCalled(); await fireEvent.click(within(screen.getByRole('dialog')).getByRole('button', { name: /confirm forget/i })); await waitFor(() => expect(forgetMissingTracks).toHaveBeenCalledWith(['t-Paper Wings'])); }); test('Forget all takes the whole folder', async () => { renderWith(response); await fireEvent.click( screen.getByRole('button', { name: 'Forget the 2 tracks in /music/Lincoln Yard/Minutes to Noon' }) ); await fireEvent.click(within(screen.getByRole('dialog')).getByRole('button', { name: /confirm forget/i })); await waitFor(() => expect(forgetMissingTracks).toHaveBeenCalledWith(['t-Paper Wings', 't-Ring It Out'])); }); test('Export CSV links to the whole list', () => { renderWith(response); expect(screen.getByRole('link', { name: /export csv/i })).toHaveAttribute('href', '/api/admin/library/missing.csv'); }); });