mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 04:40:09 +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:
@@ -9,6 +9,9 @@ import { createPrismaMock } from '../helpers/prisma';
|
||||
const prismaMock = createPrismaMock();
|
||||
const organizerMock = vi.hoisted(() => ({ organize: vi.fn() }));
|
||||
const libraryServiceMock = vi.hoisted(() => ({ triggerLibraryScan: vi.fn() }));
|
||||
const jobQueueMock = vi.hoisted(() => ({
|
||||
addNotificationJob: vi.fn(() => Promise.resolve()),
|
||||
}));
|
||||
const configMock = vi.hoisted(() => ({
|
||||
getBackendMode: vi.fn(),
|
||||
get: vi.fn(),
|
||||
@@ -30,6 +33,10 @@ vi.mock('@/lib/services/config.service', () => ({
|
||||
getConfigService: () => configMock,
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/services/job-queue.service', () => ({
|
||||
getJobQueueService: () => jobQueueMock,
|
||||
}));
|
||||
|
||||
describe('processOrganizeFiles', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
@@ -74,6 +81,121 @@ describe('processOrganizeFiles', () => {
|
||||
expect(libraryServiceMock.triggerLibraryScan).toHaveBeenCalledWith('lib-1');
|
||||
});
|
||||
|
||||
it('skips filesystem scan when disabled', async () => {
|
||||
prismaMock.request.update.mockResolvedValue({});
|
||||
prismaMock.audiobook.findUnique.mockResolvedValue({
|
||||
id: 'a3',
|
||||
title: 'Book',
|
||||
author: 'Author',
|
||||
narrator: null,
|
||||
coverArtUrl: null,
|
||||
audibleAsin: 'ASIN3',
|
||||
year: 2020,
|
||||
});
|
||||
organizerMock.organize.mockResolvedValue({
|
||||
success: true,
|
||||
targetPath: '/media/Author/Book',
|
||||
filesMovedCount: 1,
|
||||
errors: [],
|
||||
audioFiles: ['/media/Author/Book/Book.m4b'],
|
||||
});
|
||||
prismaMock.audiobook.update.mockResolvedValue({});
|
||||
prismaMock.request.update.mockResolvedValue({});
|
||||
configMock.getBackendMode.mockResolvedValue('plex');
|
||||
configMock.get.mockResolvedValue('false');
|
||||
|
||||
const { processOrganizeFiles } = await import('@/lib/processors/organize-files.processor');
|
||||
const result = await processOrganizeFiles({
|
||||
requestId: 'req-3',
|
||||
audiobookId: 'a3',
|
||||
downloadPath: '/downloads/book',
|
||||
jobId: 'job-3',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(libraryServiceMock.triggerLibraryScan).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('continues when scan is enabled but library ID is missing', async () => {
|
||||
prismaMock.request.update.mockResolvedValue({});
|
||||
prismaMock.audiobook.findUnique.mockResolvedValue({
|
||||
id: 'a4',
|
||||
title: 'Book',
|
||||
author: 'Author',
|
||||
narrator: null,
|
||||
coverArtUrl: null,
|
||||
audibleAsin: 'ASIN4',
|
||||
});
|
||||
organizerMock.organize.mockResolvedValue({
|
||||
success: true,
|
||||
targetPath: '/media/Author/Book',
|
||||
filesMovedCount: 1,
|
||||
errors: [],
|
||||
audioFiles: ['/media/Author/Book/Book.m4b'],
|
||||
});
|
||||
prismaMock.audiobook.update.mockResolvedValue({});
|
||||
prismaMock.request.update.mockResolvedValue({});
|
||||
configMock.getBackendMode.mockResolvedValue('plex');
|
||||
configMock.get.mockImplementation(async (key: string) => {
|
||||
if (key === 'plex.trigger_scan_after_import') return 'true';
|
||||
if (key === 'plex_audiobook_library_id') return null;
|
||||
return null;
|
||||
});
|
||||
|
||||
const { processOrganizeFiles } = await import('@/lib/processors/organize-files.processor');
|
||||
const result = await processOrganizeFiles({
|
||||
requestId: 'req-4',
|
||||
audiobookId: 'a4',
|
||||
downloadPath: '/downloads/book',
|
||||
jobId: 'job-4',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(libraryServiceMock.triggerLibraryScan).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('updates year from AudibleCache when missing', async () => {
|
||||
prismaMock.request.update.mockResolvedValue({});
|
||||
prismaMock.audiobook.findUnique.mockResolvedValue({
|
||||
id: 'a5',
|
||||
title: 'Book',
|
||||
author: 'Author',
|
||||
narrator: null,
|
||||
coverArtUrl: null,
|
||||
audibleAsin: 'ASIN5',
|
||||
year: null,
|
||||
});
|
||||
prismaMock.audibleCache.findUnique.mockResolvedValue({
|
||||
releaseDate: '2020-01-01',
|
||||
});
|
||||
organizerMock.organize.mockResolvedValue({
|
||||
success: true,
|
||||
targetPath: '/media/Author/Book',
|
||||
filesMovedCount: 1,
|
||||
errors: [],
|
||||
audioFiles: ['/media/Author/Book/Book.m4b'],
|
||||
});
|
||||
prismaMock.audiobook.update.mockResolvedValue({});
|
||||
prismaMock.request.update.mockResolvedValue({});
|
||||
configMock.getBackendMode.mockResolvedValue('plex');
|
||||
configMock.get.mockResolvedValue('false');
|
||||
|
||||
const { processOrganizeFiles } = await import('@/lib/processors/organize-files.processor');
|
||||
const result = await processOrganizeFiles({
|
||||
requestId: 'req-5',
|
||||
audiobookId: 'a5',
|
||||
downloadPath: '/downloads/book',
|
||||
jobId: 'job-5',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(prismaMock.audiobook.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({ year: 2020 }),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('queues retry when a retryable error occurs', async () => {
|
||||
prismaMock.request.update.mockResolvedValue({});
|
||||
prismaMock.audiobook.findUnique.mockResolvedValue({
|
||||
@@ -116,6 +238,107 @@ describe('processOrganizeFiles', () => {
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('marks request as warn when max retries exceeded and notifies user', async () => {
|
||||
prismaMock.request.update.mockResolvedValue({});
|
||||
prismaMock.audiobook.findUnique.mockResolvedValue({
|
||||
id: 'a6',
|
||||
title: 'Book',
|
||||
author: 'Author',
|
||||
narrator: null,
|
||||
coverArtUrl: null,
|
||||
audibleAsin: 'ASIN6',
|
||||
});
|
||||
organizerMock.organize.mockResolvedValue({
|
||||
success: false,
|
||||
targetPath: '',
|
||||
filesMovedCount: 0,
|
||||
errors: ['No audiobook files found in download'],
|
||||
audioFiles: [],
|
||||
});
|
||||
prismaMock.request.findFirst.mockResolvedValue({
|
||||
importAttempts: 2,
|
||||
maxImportRetries: 3,
|
||||
deletedAt: null,
|
||||
});
|
||||
prismaMock.request.findUnique.mockResolvedValue({
|
||||
id: 'req-6',
|
||||
audiobook: { title: 'Book', author: 'Author' },
|
||||
user: { plexUsername: 'user' },
|
||||
});
|
||||
configMock.get.mockResolvedValue(null);
|
||||
|
||||
const { processOrganizeFiles } = await import('@/lib/processors/organize-files.processor');
|
||||
const result = await processOrganizeFiles({
|
||||
requestId: 'req-6',
|
||||
audiobookId: 'a6',
|
||||
downloadPath: '/downloads/book',
|
||||
jobId: 'job-6',
|
||||
});
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(prismaMock.request.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({ status: 'warn' }),
|
||||
})
|
||||
);
|
||||
expect(jobQueueMock.addNotificationJob).toHaveBeenCalledWith(
|
||||
'request_error',
|
||||
'req-6',
|
||||
'Book',
|
||||
'Author',
|
||||
'user',
|
||||
expect.stringContaining('Max retries')
|
||||
);
|
||||
});
|
||||
|
||||
it('marks request failed for non-retryable errors and notifies user', async () => {
|
||||
prismaMock.request.update.mockResolvedValue({});
|
||||
prismaMock.audiobook.findUnique.mockResolvedValue({
|
||||
id: 'a7',
|
||||
title: 'Book',
|
||||
author: 'Author',
|
||||
narrator: null,
|
||||
coverArtUrl: null,
|
||||
audibleAsin: 'ASIN7',
|
||||
});
|
||||
organizerMock.organize.mockResolvedValue({
|
||||
success: false,
|
||||
targetPath: '',
|
||||
filesMovedCount: 0,
|
||||
errors: ['Unexpected error'],
|
||||
audioFiles: [],
|
||||
});
|
||||
prismaMock.request.findUnique.mockResolvedValue({
|
||||
id: 'req-7',
|
||||
audiobook: { title: 'Book', author: 'Author' },
|
||||
user: { plexUsername: 'user' },
|
||||
});
|
||||
configMock.get.mockResolvedValue(null);
|
||||
|
||||
const { processOrganizeFiles } = await import('@/lib/processors/organize-files.processor');
|
||||
|
||||
await expect(processOrganizeFiles({
|
||||
requestId: 'req-7',
|
||||
audiobookId: 'a7',
|
||||
downloadPath: '/downloads/book',
|
||||
jobId: 'job-7',
|
||||
})).rejects.toThrow(/File organization failed/i);
|
||||
|
||||
expect(prismaMock.request.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({ status: 'failed' }),
|
||||
})
|
||||
);
|
||||
expect(jobQueueMock.addNotificationJob).toHaveBeenCalledWith(
|
||||
'request_error',
|
||||
'req-7',
|
||||
'Book',
|
||||
'Author',
|
||||
'user',
|
||||
expect.stringContaining('File organization failed')
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user