import { afterEach, describe, expect, test, vi } from 'vitest'; import { render, screen, fireEvent, waitFor } from '@testing-library/svelte'; import { mockQuery } from '../../../test-utils/query'; import type { MusicRequest } from '#lib/api/types.js'; // Wrap useQueryClient so the page can call invalidateQueries() without a // real QueryClient context. Everything else from svelte-query passes through. vi.mock('#lib/api/admin.js', () => ({ createAdminRequestsQuery: vi.fn(), approveRequest: vi.fn(), rejectRequest: vi.fn() })); vi.mock('#lib/api/wanted.js', () => ({ createReleaseProfilesQuery: vi.fn() })); import AdminRequestsPage from './+page.svelte'; import { createAdminRequestsQuery, approveRequest, rejectRequest } from '#lib/api/admin.js'; import { createReleaseProfilesQuery } from '#lib/api/wanted.js'; const baseRow: MusicRequest = { id: 'r1', user_id: 'u1234567-aaaa-bbbb-cccc-deadbeef0001', status: 'pending', kind: 'album', artist_mbid: 'a-mbid', album_mbid: 'al-mbid', artist_name: 'Boards of Canada', album_title: 'Geogaddi', requested_at: '2026-04-29T10:00:00Z', updated_at: '2026-04-29T10:00:00Z', imported_album_count: 0, imported_track_count: 0 }; afterEach(() => vi.clearAllMocks()); function setup(rows: MusicRequest[] = [baseRow]) { (createAdminRequestsQuery as ReturnType).mockReturnValue( mockQuery({ data: rows }) ); (createReleaseProfilesQuery as ReturnType).mockReturnValue( mockQuery({ data: { profiles: [ { id: 'p-std', name: 'Standard', primary_types: ['Album'], secondary_types: [], is_default: true, follow_count: 0 }, { id: 'p-live', name: 'With live', primary_types: ['Album'], secondary_types: ['Live'], is_default: false, follow_count: 0 } ], primary_types: ['Album'], secondary_types: ['Live'] } }) ); return render(AdminRequestsPage); } describe('/admin/requests', () => { test('Tab switch refetches with new status filter', async () => { setup(); await fireEvent.click(screen.getByRole('tab', { name: /approved/i })); expect(createAdminRequestsQuery).toHaveBeenCalledWith('approved'); }); test('Approve sends no profile', async () => { setup(); (approveRequest as ReturnType).mockResolvedValueOnce(baseRow); await fireEvent.click(screen.getByRole('button', { name: /approve geogaddi/i })); expect(approveRequest).toHaveBeenCalledWith('r1', undefined); }); test('An album request has no profile choice', () => { setup(); expect(screen.queryByRole('button', { name: /on a profile/i })).toBeNull(); }); test('An artist request is approved on the chosen profile', async () => { setup([{ ...baseRow, kind: 'artist', album_mbid: undefined, album_title: undefined }]); await fireEvent.click( screen.getByRole('button', { name: /approve boards of canada on a profile/i }) ); const select = screen.getByLabelText(/release-type profile/i) as HTMLSelectElement; expect(select.value).toBe('p-std'); await fireEvent.change(select, { target: { value: 'p-live' } }); (approveRequest as ReturnType).mockResolvedValueOnce(baseRow); await fireEvent.click(screen.getByRole('button', { name: /approve and follow/i })); expect(approveRequest).toHaveBeenCalledWith('r1', 'p-live'); }); test('Reject opens notes textarea; confirm sends notes', async () => { setup(); await fireEvent.click(screen.getByRole('button', { name: /reject geogaddi/i })); const textarea = screen.getByLabelText(/notes/i) as HTMLTextAreaElement; await fireEvent.input(textarea, { target: { value: 'duplicate' } }); (rejectRequest as ReturnType).mockResolvedValueOnce({ ...baseRow, status: 'rejected' }); await fireEvent.click( screen.getByRole('button', { name: /confirm reject/i }) ); expect(rejectRequest).toHaveBeenCalledWith('r1', 'duplicate'); }); test('An approve error shows a toast', async () => { setup(); (approveRequest as ReturnType).mockRejectedValueOnce({ code: 'request_not_pending', message: 'not pending', status: 409 }); await fireEvent.click(screen.getByRole('button', { name: /approve geogaddi/i })); await waitFor(() => expect(screen.getByText(/no longer pending/i)).toBeInTheDocument() ); }); test('Track-kind row renders disclosure copy about adding the album', () => { setup([ { ...baseRow, kind: 'track', track_title: 'Roygbiv', album_title: 'Geogaddi' } ]); expect(screen.getByText(/wants the album/i)).toBeInTheDocument(); }); test('A row names who asked for it', () => { setup([{ ...baseRow, username: 'alice' }]); expect(screen.getByText(/^by alice · /)).toBeInTheDocument(); expect(screen.queryByText(/u1234567/)).not.toBeInTheDocument(); }); test('A row without a name falls back to the user id', () => { setup(); expect(screen.getByText(/^by user u1234567 · /)).toBeInTheDocument(); }); });