mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-02 20:30:10 +00:00
Add series fields to audiobooks and update related logic
Introduces 'series' and 'seriesPart' fields to the Audiobook model and database schema. Updates API routes, file organization, and path template utilities to support series metadata. Enhances chapter merging logic, improves notification backend testing, and expands test coverage for admin and API routes.
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* Component: Interactive Torrent Search Modal Tests
|
||||
* Documentation: documentation/frontend/components.md
|
||||
*/
|
||||
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import React from 'react';
|
||||
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const searchByRequestMock = vi.hoisted(() => vi.fn());
|
||||
const selectTorrentMock = vi.hoisted(() => vi.fn());
|
||||
const searchByAudiobookMock = vi.hoisted(() => vi.fn());
|
||||
const requestWithTorrentMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock('@/lib/hooks/useRequests', () => ({
|
||||
useInteractiveSearch: () => ({
|
||||
searchTorrents: searchByRequestMock,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
}),
|
||||
useSelectTorrent: () => ({
|
||||
selectTorrent: selectTorrentMock,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
}),
|
||||
useSearchTorrents: () => ({
|
||||
searchTorrents: searchByAudiobookMock,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
}),
|
||||
useRequestWithTorrent: () => ({
|
||||
requestWithTorrent: requestWithTorrentMock,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
}),
|
||||
}));
|
||||
|
||||
const baseResult = {
|
||||
guid: 'torrent-1',
|
||||
rank: 1,
|
||||
title: 'Test Torrent',
|
||||
size: 2.4 * 1024 ** 3,
|
||||
score: 88,
|
||||
bonusPoints: 5,
|
||||
seeders: 42,
|
||||
indexer: 'ProIndexer',
|
||||
format: 'M4B',
|
||||
infoUrl: 'https://example.com/torrent',
|
||||
};
|
||||
|
||||
describe('InteractiveTorrentSearchModal', () => {
|
||||
it('searches by request id on open and confirms download', async () => {
|
||||
searchByRequestMock.mockResolvedValueOnce([baseResult]);
|
||||
selectTorrentMock.mockResolvedValueOnce(undefined);
|
||||
const onClose = vi.fn();
|
||||
const onSuccess = vi.fn();
|
||||
const { InteractiveTorrentSearchModal } = await import('@/components/requests/InteractiveTorrentSearchModal');
|
||||
|
||||
render(
|
||||
<InteractiveTorrentSearchModal
|
||||
isOpen={true}
|
||||
onClose={onClose}
|
||||
onSuccess={onSuccess}
|
||||
requestId="req-123"
|
||||
audiobook={{ title: 'Test Book', author: 'Test Author' }}
|
||||
/>
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(searchByRequestMock).toHaveBeenCalledWith('req-123', undefined);
|
||||
});
|
||||
|
||||
expect(await screen.findByText('Test Torrent')).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Download' }));
|
||||
const downloadButtons = screen.getAllByRole('button', { name: 'Download' });
|
||||
fireEvent.click(downloadButtons[downloadButtons.length - 1]);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(selectTorrentMock).toHaveBeenCalledWith('req-123', baseResult);
|
||||
});
|
||||
expect(onSuccess).toHaveBeenCalled();
|
||||
expect(onClose).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('searches by audiobook data and requests with torrent', async () => {
|
||||
searchByAudiobookMock.mockResolvedValueOnce([baseResult]);
|
||||
requestWithTorrentMock.mockResolvedValueOnce(undefined);
|
||||
const onClose = vi.fn();
|
||||
const fullAudiobook = { asin: 'ASIN-1', title: 'Test Book', author: 'Test Author' };
|
||||
const { InteractiveTorrentSearchModal } = await import('@/components/requests/InteractiveTorrentSearchModal');
|
||||
|
||||
render(
|
||||
<InteractiveTorrentSearchModal
|
||||
isOpen={true}
|
||||
onClose={onClose}
|
||||
audiobook={{ title: 'Test Book', author: 'Test Author' }}
|
||||
fullAudiobook={fullAudiobook as any}
|
||||
/>
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(searchByAudiobookMock).toHaveBeenCalledWith('Test Book', 'Test Author', 'ASIN-1');
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Download' }));
|
||||
const downloadButtons = screen.getAllByRole('button', { name: 'Download' });
|
||||
fireEvent.click(downloadButtons[downloadButtons.length - 1]);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(requestWithTorrentMock).toHaveBeenCalledWith(fullAudiobook, baseResult);
|
||||
});
|
||||
expect(onClose).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('uses a custom title when pressing Enter', async () => {
|
||||
searchByRequestMock.mockResolvedValueOnce([]);
|
||||
searchByRequestMock.mockResolvedValueOnce([]);
|
||||
const { InteractiveTorrentSearchModal } = await import('@/components/requests/InteractiveTorrentSearchModal');
|
||||
|
||||
render(
|
||||
<InteractiveTorrentSearchModal
|
||||
isOpen={true}
|
||||
onClose={vi.fn()}
|
||||
requestId="req-456"
|
||||
audiobook={{ title: 'Original Title', author: 'Author' }}
|
||||
/>
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(searchByRequestMock).toHaveBeenCalledWith('req-456', undefined);
|
||||
});
|
||||
|
||||
const input = screen.getByPlaceholderText('Enter book title to search...');
|
||||
fireEvent.change(input, { target: { value: 'Custom Title' } });
|
||||
fireEvent.keyPress(input, { key: 'Enter', code: 'Enter', charCode: 13 });
|
||||
|
||||
await waitFor(() => {
|
||||
expect(searchByRequestMock).toHaveBeenNthCalledWith(2, 'req-456', 'Custom Title');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,187 @@
|
||||
/**
|
||||
* Component: Request Card Tests
|
||||
* Documentation: documentation/frontend/components.md
|
||||
*/
|
||||
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import React from 'react';
|
||||
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const cancelRequestMock = vi.hoisted(() => vi.fn());
|
||||
const manualSearchMock = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock('@/lib/hooks/useRequests', () => ({
|
||||
useCancelRequest: () => ({ cancelRequest: cancelRequestMock, isLoading: false }),
|
||||
useManualSearch: () => ({ triggerManualSearch: manualSearchMock, isLoading: false }),
|
||||
}));
|
||||
|
||||
vi.mock('@/components/requests/InteractiveTorrentSearchModal', () => ({
|
||||
InteractiveTorrentSearchModal: ({
|
||||
isOpen,
|
||||
requestId,
|
||||
}: {
|
||||
isOpen: boolean;
|
||||
requestId?: string;
|
||||
}) => (
|
||||
<div data-testid="interactive-modal" data-open={String(isOpen)} data-request-id={requestId} />
|
||||
),
|
||||
}));
|
||||
|
||||
vi.mock('next/image', () => ({
|
||||
__esModule: true,
|
||||
default: (props: any) => <img {...props} />,
|
||||
}));
|
||||
|
||||
const baseRequest = {
|
||||
id: 'req-1',
|
||||
status: 'pending',
|
||||
progress: 0,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
audiobook: {
|
||||
id: 'book-1',
|
||||
title: 'Test Book',
|
||||
author: 'Test Author',
|
||||
},
|
||||
};
|
||||
|
||||
describe('RequestCard', () => {
|
||||
beforeEach(() => {
|
||||
cancelRequestMock.mockReset();
|
||||
manualSearchMock.mockReset();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it('shows progress and active indicator for downloads', async () => {
|
||||
const { RequestCard } = await import('@/components/requests/RequestCard');
|
||||
|
||||
render(
|
||||
<RequestCard
|
||||
request={{
|
||||
...baseRequest,
|
||||
status: 'downloading',
|
||||
progress: 45,
|
||||
}}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(screen.getByText('Downloading')).toBeInTheDocument();
|
||||
expect(screen.getByText('Active')).toBeInTheDocument();
|
||||
expect(screen.getByText('45%')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('toggles the error message for failed requests', async () => {
|
||||
const { RequestCard } = await import('@/components/requests/RequestCard');
|
||||
|
||||
render(
|
||||
<RequestCard
|
||||
request={{
|
||||
...baseRequest,
|
||||
status: 'failed',
|
||||
errorMessage: 'Failure details',
|
||||
}}
|
||||
/>
|
||||
);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Show error' }));
|
||||
expect(await screen.findByText('Failure details')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('triggers manual search, interactive search, and cancel actions', async () => {
|
||||
const { RequestCard } = await import('@/components/requests/RequestCard');
|
||||
|
||||
manualSearchMock.mockResolvedValueOnce(undefined);
|
||||
cancelRequestMock.mockResolvedValueOnce(undefined);
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(true);
|
||||
|
||||
render(<RequestCard request={baseRequest} />);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Manual Search' }));
|
||||
await waitFor(() => {
|
||||
expect(manualSearchMock).toHaveBeenCalledWith('req-1');
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Interactive Search' }));
|
||||
expect(screen.getByTestId('interactive-modal')).toHaveAttribute('data-open', 'true');
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Cancel' }));
|
||||
await waitFor(() => {
|
||||
expect(cancelRequestMock).toHaveBeenCalledWith('req-1');
|
||||
});
|
||||
});
|
||||
|
||||
it('shows setup indicator when progress is zero', async () => {
|
||||
const { RequestCard } = await import('@/components/requests/RequestCard');
|
||||
|
||||
render(
|
||||
<RequestCard
|
||||
request={{
|
||||
...baseRequest,
|
||||
status: 'processing',
|
||||
progress: 0,
|
||||
}}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(screen.getByText('Setting up...')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('hides action buttons when showActions is false', async () => {
|
||||
const { RequestCard } = await import('@/components/requests/RequestCard');
|
||||
|
||||
render(<RequestCard request={baseRequest} showActions={false} />);
|
||||
|
||||
expect(screen.queryByRole('button', { name: 'Manual Search' })).toBeNull();
|
||||
expect(screen.queryByRole('button', { name: 'Cancel' })).toBeNull();
|
||||
});
|
||||
|
||||
it('alerts when manual search fails', async () => {
|
||||
const { RequestCard } = await import('@/components/requests/RequestCard');
|
||||
|
||||
manualSearchMock.mockRejectedValueOnce(new Error('Search failed'));
|
||||
const alertSpy = vi.spyOn(window, 'alert').mockImplementation(() => {});
|
||||
|
||||
render(<RequestCard request={baseRequest} />);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Manual Search' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(alertSpy).toHaveBeenCalledWith('Search failed');
|
||||
});
|
||||
});
|
||||
|
||||
it('does not cancel when confirmation is declined', async () => {
|
||||
const { RequestCard } = await import('@/components/requests/RequestCard');
|
||||
|
||||
vi.spyOn(window, 'confirm').mockReturnValue(false);
|
||||
|
||||
render(<RequestCard request={baseRequest} />);
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Cancel' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(cancelRequestMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('shows completed timestamp when available', async () => {
|
||||
const { RequestCard } = await import('@/components/requests/RequestCard');
|
||||
|
||||
render(
|
||||
<RequestCard
|
||||
request={{
|
||||
...baseRequest,
|
||||
completedAt: new Date('2024-01-01T00:00:00Z').toISOString(),
|
||||
}}
|
||||
/>
|
||||
);
|
||||
|
||||
expect(screen.getByText(/Completed/)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Component: Status Badge Tests
|
||||
* Documentation: documentation/frontend/components.md
|
||||
*/
|
||||
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import React from 'react';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { StatusBadge } from '@/components/requests/StatusBadge';
|
||||
|
||||
describe('StatusBadge', () => {
|
||||
it('uses the initializing label for zero-progress downloads', () => {
|
||||
render(<StatusBadge status="downloading" progress={0} />);
|
||||
expect(screen.getByText('Initializing...')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('falls back to the raw status when unknown', () => {
|
||||
render(<StatusBadge status="custom_status" />);
|
||||
expect(screen.getByText('custom_status')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user