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:
kikootwo
2026-01-22 15:56:55 -05:00
parent dc7e557694
commit 31bca0052f
105 changed files with 10384 additions and 75 deletions
@@ -0,0 +1,41 @@
/**
* Component: Flag Config Row Tests
* Documentation: documentation/phase3/ranking-algorithm.md
*/
// @vitest-environment jsdom
import { describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react';
import { FlagConfigRow } from '@/components/admin/FlagConfigRow';
import type { IndexerFlagConfig } from '@/lib/utils/ranking-algorithm';
describe('FlagConfigRow', () => {
it('updates name and modifier values and allows removal', () => {
const onChange = vi.fn();
const onRemove = vi.fn();
const config: IndexerFlagConfig = { name: 'Freeleech', modifier: 20 };
render(<FlagConfigRow config={config} onChange={onChange} onRemove={onRemove} />);
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'Internal' } });
fireEvent.change(screen.getByRole('slider'), { target: { value: '-15' } });
fireEvent.click(screen.getByTitle('Remove flag rule'));
expect(onChange).toHaveBeenCalledWith({ name: 'Internal', modifier: 20 });
expect(onChange).toHaveBeenCalledWith({ name: 'Freeleech', modifier: -15 });
expect(onRemove).toHaveBeenCalledTimes(1);
});
it('shows disqualification warning for large negative modifiers', () => {
render(
<FlagConfigRow
config={{ name: 'Bad', modifier: -60 }}
onChange={vi.fn()}
onRemove={vi.fn()}
/>
);
expect(screen.getByText(/Would disqualify/i)).toBeInTheDocument();
});
});
@@ -0,0 +1,38 @@
/**
* Component: Available Indexer Row Tests
* Documentation: documentation/frontend/components.md
*/
// @vitest-environment jsdom
import { describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react';
import { AvailableIndexerRow } from '@/components/admin/indexers/AvailableIndexerRow';
describe('AvailableIndexerRow', () => {
const indexer = {
id: 1,
name: 'Test Indexer',
protocol: 'torrent',
supportsRss: true,
};
it('renders an add button when the indexer is not added', () => {
const onAdd = vi.fn();
render(<AvailableIndexerRow indexer={indexer} isAdded={false} onAdd={onAdd} />);
fireEvent.click(screen.getByRole('button', { name: 'Add' }));
expect(onAdd).toHaveBeenCalledTimes(1);
expect(screen.getByText('Test Indexer')).toBeInTheDocument();
expect(screen.getByText('torrent')).toBeInTheDocument();
});
it('renders the added state when already configured', () => {
render(<AvailableIndexerRow indexer={indexer} isAdded onAdd={vi.fn()} />);
expect(screen.getByText('Added')).toBeInTheDocument();
expect(screen.queryByRole('button', { name: 'Add' })).toBeNull();
});
});
@@ -0,0 +1,67 @@
/**
* Component: Category Tree View Tests
* Documentation: documentation/frontend/components.md
*/
// @vitest-environment jsdom
import { describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen, within } from '@testing-library/react';
import { CategoryTreeView } from '@/components/admin/indexers/CategoryTreeView';
import { getChildIds } from '@/lib/utils/torrent-categories';
describe('CategoryTreeView', () => {
it('selects parent and children when the parent is toggled', () => {
const onChange = vi.fn();
render(<CategoryTreeView selectedCategories={[]} onChange={onChange} />);
const audioLabel = screen.getByText('Audio');
const audioRow = audioLabel.closest('div')?.parentElement;
if (!audioRow) {
throw new Error('Audio parent row not found');
}
fireEvent.click(within(audioRow).getByRole('switch'));
const audioChildren = getChildIds(3000);
expect(onChange).toHaveBeenCalledWith(
expect.arrayContaining([3000, ...audioChildren])
);
});
it('toggles a child category on and off', () => {
const onChange = vi.fn();
render(<CategoryTreeView selectedCategories={[]} onChange={onChange} />);
const audiobookLabel = screen.getByText('Audiobook');
const audiobookRow = audiobookLabel.closest('div')?.parentElement;
if (!audiobookRow) {
throw new Error('Audiobook row not found');
}
fireEvent.click(within(audiobookRow).getByRole('switch'));
expect(onChange).toHaveBeenCalledWith(expect.arrayContaining([3030]));
});
it('disables child toggles when all children are selected', () => {
const audioChildren = getChildIds(3000);
render(
<CategoryTreeView
selectedCategories={[3000, ...audioChildren]}
onChange={vi.fn()}
/>
);
const audiobookLabel = screen.getByText('Audiobook');
const audiobookRow = audiobookLabel.closest('div')?.parentElement;
if (!audiobookRow) {
throw new Error('Audiobook row not found');
}
expect(within(audiobookRow).getByRole('switch')).toBeDisabled();
});
});
@@ -0,0 +1,48 @@
/**
* Component: Delete Confirm Modal Tests
* Documentation: documentation/frontend/components.md
*/
// @vitest-environment jsdom
import { describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react';
import { DeleteConfirmModal } from '@/components/admin/indexers/DeleteConfirmModal';
describe('DeleteConfirmModal', () => {
it('confirms removal and closes the modal', () => {
const onClose = vi.fn();
const onConfirm = vi.fn();
render(
<DeleteConfirmModal
isOpen
onClose={onClose}
onConfirm={onConfirm}
indexerName="TrackerOne"
/>
);
fireEvent.click(screen.getByRole('button', { name: 'Remove Indexer' }));
expect(onConfirm).toHaveBeenCalledTimes(1);
expect(onClose).toHaveBeenCalledTimes(1);
});
it('closes without confirming when canceled', () => {
const onClose = vi.fn();
render(
<DeleteConfirmModal
isOpen
onClose={onClose}
onConfirm={vi.fn()}
indexerName="TrackerTwo"
/>
);
fireEvent.click(screen.getByRole('button', { name: 'Cancel' }));
expect(onClose).toHaveBeenCalledTimes(1);
});
});
@@ -0,0 +1,34 @@
/**
* Component: Indexer Card Tests
* Documentation: documentation/frontend/components.md
*/
// @vitest-environment jsdom
import { describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react';
import { IndexerCard } from '@/components/admin/indexers/IndexerCard';
describe('IndexerCard', () => {
it('renders indexer info and triggers edit/delete actions', () => {
const onEdit = vi.fn();
const onDelete = vi.fn();
render(
<IndexerCard
indexer={{ id: 2, name: 'IndexerTwo', protocol: 'usenet' }}
onEdit={onEdit}
onDelete={onDelete}
/>
);
expect(screen.getByText('IndexerTwo')).toBeInTheDocument();
expect(screen.getByText('usenet')).toBeInTheDocument();
fireEvent.click(screen.getByTitle('Edit indexer'));
fireEvent.click(screen.getByTitle('Delete indexer'));
expect(onEdit).toHaveBeenCalledTimes(1);
expect(onDelete).toHaveBeenCalledTimes(1);
});
});
@@ -0,0 +1,107 @@
/**
* Component: Indexer Config Modal Tests
* Documentation: documentation/frontend/components.md
*/
// @vitest-environment jsdom
import { describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen, within } from '@testing-library/react';
import { IndexerConfigModal } from '@/components/admin/indexers/IndexerConfigModal';
describe('IndexerConfigModal', () => {
it('clamps numeric inputs and saves configuration', () => {
const onSave = vi.fn();
const onClose = vi.fn();
render(
<IndexerConfigModal
isOpen
onClose={onClose}
mode="add"
indexer={{ id: 1, name: 'Prowlarr', protocol: 'torrent', supportsRss: true }}
onSave={onSave}
/>
);
const [priorityInput, seedingInput] = screen.getAllByRole('spinbutton');
fireEvent.change(priorityInput, { target: { value: '99' } });
expect(priorityInput).toHaveValue(25);
fireEvent.change(seedingInput, { target: { value: '-5' } });
expect(seedingInput).toHaveValue(0);
const rssToggle = screen.getByRole('checkbox');
fireEvent.click(rssToggle);
fireEvent.click(screen.getByRole('button', { name: 'Add Indexer' }));
expect(onSave).toHaveBeenCalledWith(
expect.objectContaining({
id: 1,
name: 'Prowlarr',
priority: 25,
seedingTimeMinutes: 0,
rssEnabled: false,
categories: expect.arrayContaining([3030]),
})
);
expect(onClose).toHaveBeenCalledTimes(1);
});
it('validates that at least one category is selected', () => {
const onSave = vi.fn();
render(
<IndexerConfigModal
isOpen
onClose={vi.fn()}
mode="add"
indexer={{ id: 2, name: 'NoCats', protocol: 'torrent', supportsRss: true }}
onSave={onSave}
/>
);
const audiobookLabel = screen.getByText('Audiobook');
const audiobookRow = audiobookLabel.closest('div')?.parentElement;
if (!audiobookRow) {
throw new Error('Audiobook row not found');
}
fireEvent.click(within(audiobookRow).getByRole('switch'));
fireEvent.click(screen.getByRole('button', { name: 'Add Indexer' }));
expect(screen.getByText('At least one category must be selected')).toBeInTheDocument();
expect(onSave).not.toHaveBeenCalled();
});
it('forces RSS to false when the indexer does not support RSS', () => {
const onSave = vi.fn();
const onClose = vi.fn();
render(
<IndexerConfigModal
isOpen
onClose={onClose}
mode="add"
indexer={{ id: 3, name: 'NoRSS', protocol: 'torrent', supportsRss: false }}
onSave={onSave}
/>
);
const rssToggle = screen.getByRole('checkbox');
expect(rssToggle).toBeDisabled();
fireEvent.click(screen.getByRole('button', { name: 'Add Indexer' }));
expect(onSave).toHaveBeenCalledWith(
expect.objectContaining({
id: 3,
name: 'NoRSS',
rssEnabled: false,
})
);
expect(onClose).toHaveBeenCalledTimes(1);
});
});
@@ -0,0 +1,166 @@
/**
* Component: Indexer Management Tests
* Documentation: documentation/frontend/components.md
*/
// @vitest-environment jsdom
import { afterEach, describe, expect, it, vi } from 'vitest';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import { IndexerManagement } from '@/components/admin/indexers/IndexerManagement';
const fetchWithAuthMock = vi.hoisted(() => vi.fn());
vi.mock('@/lib/utils/api', () => ({
fetchWithAuth: fetchWithAuthMock,
}));
vi.mock('@/components/admin/indexers/IndexerConfigModal', () => ({
IndexerConfigModal: ({ isOpen, mode, indexer, initialConfig, onSave, onClose }: any) => {
if (!isOpen) return null;
const priority = initialConfig?.priority ? initialConfig.priority + 1 : 10;
return (
<div data-testid="indexer-config-modal">
<div data-testid="modal-mode">{mode}</div>
<button
onClick={() =>
onSave({
id: indexer.id,
name: indexer.name,
priority,
seedingTimeMinutes: 0,
rssEnabled: true,
categories: [3030],
})
}
>
Save
</button>
<button onClick={onClose}>Close</button>
</div>
);
},
}));
describe('IndexerManagement', () => {
const emptyIndexers: any[] = [];
afterEach(() => {
vi.unstubAllGlobals();
fetchWithAuthMock.mockReset();
});
it('fetches indexers in wizard mode and adds a configuration', async () => {
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
success: true,
indexers: [
{ id: 1, name: 'IndexerA', protocol: 'torrent', supportsRss: true },
],
}),
});
vi.stubGlobal('fetch', fetchMock);
const onIndexersChange = vi.fn();
render(
<IndexerManagement
prowlarrUrl="http://prowlarr.local"
prowlarrApiKey="apikey"
mode="wizard"
initialIndexers={emptyIndexers}
onIndexersChange={onIndexersChange}
/>
);
fireEvent.click(screen.getByRole('button', { name: 'Fetch Indexers' }));
await waitFor(() => {
expect(fetchMock).toHaveBeenCalledWith('/api/setup/test-prowlarr', expect.any(Object));
});
expect(fetchWithAuthMock).not.toHaveBeenCalled();
expect(screen.getByText('IndexerA')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: 'Add' }));
fireEvent.click(screen.getByRole('button', { name: 'Save' }));
await waitFor(() => {
const lastCall = onIndexersChange.mock.calls.at(-1)?.[0] as any[] | undefined;
expect(lastCall).toHaveLength(1);
expect(lastCall?.[0]).toMatchObject({ id: 1, name: 'IndexerA' });
});
expect(screen.getByText('Configured Indexers (1)')).toBeInTheDocument();
});
it('uses authenticated fetch in settings mode', async () => {
const fetchMock = vi.fn();
vi.stubGlobal('fetch', fetchMock);
fetchWithAuthMock.mockResolvedValue({
ok: true,
json: async () => ({
success: true,
indexers: [
{ id: 2, name: 'IndexerB', protocol: 'torrent', supportsRss: false },
],
}),
});
render(
<IndexerManagement
prowlarrUrl="http://prowlarr.local"
prowlarrApiKey="apikey"
mode="settings"
initialIndexers={emptyIndexers}
/>
);
fireEvent.click(screen.getByRole('button', { name: 'Fetch Indexers' }));
await waitFor(() => {
expect(fetchWithAuthMock).toHaveBeenCalledWith(
'/api/admin/settings/test-prowlarr',
expect.objectContaining({ method: 'POST' })
);
});
expect(fetchMock).not.toHaveBeenCalled();
expect(screen.getByText('IndexerB')).toBeInTheDocument();
});
it('removes a configured indexer after confirmation', async () => {
const onIndexersChange = vi.fn();
render(
<IndexerManagement
prowlarrUrl="http://prowlarr.local"
prowlarrApiKey="apikey"
mode="settings"
initialIndexers={[
{
id: 5,
name: 'ConfiguredIndexer',
priority: 10,
seedingTimeMinutes: 0,
rssEnabled: true,
categories: [3030],
},
]}
onIndexersChange={onIndexersChange}
/>
);
fireEvent.click(screen.getByTitle('Delete indexer'));
fireEvent.click(screen.getByRole('button', { name: 'Remove Indexer' }));
await waitFor(() => {
const lastCall = onIndexersChange.mock.calls.at(-1)?.[0] as any[] | undefined;
expect(lastCall).toHaveLength(0);
});
expect(screen.getByText('Configured Indexers (0)')).toBeInTheDocument();
});
});