mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-03 04:40:09 +00:00
Add skip-unreleased auto-search feature
Introduce an indexer-wide option to skip automatic searches for books with future release dates (config key: `indexer.skip_unreleased`, default ON). Adds a GET/PUT admin API for indexer options, a UI toggle on the Indexers settings tab (persisted on save), and persistence of a request-level releaseDate in the Prisma schema. Adds a new request status `awaiting_release` and wires it through constants, UI components (StatusBadge, RequestCard, RecentRequestsTable, Audiobook card/modal, RequestActions), API request flows (bookdate swipe, request creation, manual search, request PATCHs, request listing groups), and services. Implements a pure release-date utility (isUnreleased / shouldSkipAutoSearch) and updates background processors: monitor-rss-feeds (skip matches but do not mutate status), retry-missing-torrents (drives bidirectional transitions between awaiting_search and awaiting_release and queues searches when appropriate), and request-creator/bookdate swipe (gate initial auto-search). Adds tests for the swipe gate and other related test updates. Logs transitions and gate decisions for observability.
This commit is contained in:
@@ -28,15 +28,26 @@ vi.mock('@/lib/integrations/prowlarr.service', () => ({
|
||||
getProwlarrService: () => prowlarrMock,
|
||||
}));
|
||||
|
||||
function futureDate(days = 30): Date {
|
||||
return new Date(Date.now() + days * 24 * 60 * 60 * 1000);
|
||||
}
|
||||
|
||||
describe('processMonitorRssFeeds', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('matches RSS items and queues search jobs', async () => {
|
||||
configMock.get.mockResolvedValue(
|
||||
JSON.stringify([{ id: 1, name: 'Indexer', rssEnabled: true }])
|
||||
);
|
||||
// Indexer config + skip_unreleased setting both read via the same mock — return appropriate value per key.
|
||||
configMock.get.mockImplementation(async (key: string) => {
|
||||
if (key === 'prowlarr_indexers') {
|
||||
return JSON.stringify([{ id: 1, name: 'Indexer', rssEnabled: true }]);
|
||||
}
|
||||
if (key === 'indexer.skip_unreleased') {
|
||||
return null; // default ON
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
prowlarrMock.getAllRssFeeds.mockResolvedValue([
|
||||
{ title: 'Great Book - Author Name' },
|
||||
@@ -45,6 +56,9 @@ describe('processMonitorRssFeeds', () => {
|
||||
prismaMock.request.findMany.mockResolvedValue([
|
||||
{
|
||||
id: 'req-1',
|
||||
type: 'audiobook',
|
||||
status: 'awaiting_search',
|
||||
releaseDate: null,
|
||||
audiobook: { id: 'a1', title: 'Great Book', author: 'Author Name', audibleAsin: 'ASIN1' },
|
||||
},
|
||||
]);
|
||||
@@ -58,6 +72,75 @@ describe('processMonitorRssFeeds', () => {
|
||||
expect.objectContaining({ title: 'Great Book', author: 'Author Name' })
|
||||
);
|
||||
});
|
||||
|
||||
it('skips RSS auto-search when matched book is unreleased and setting ON', async () => {
|
||||
configMock.get.mockImplementation(async (key: string) => {
|
||||
if (key === 'prowlarr_indexers') {
|
||||
return JSON.stringify([{ id: 1, name: 'Indexer', rssEnabled: true }]);
|
||||
}
|
||||
if (key === 'indexer.skip_unreleased') {
|
||||
return 'true';
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
prowlarrMock.getAllRssFeeds.mockResolvedValue([
|
||||
{ title: 'Future Book - Author Name' },
|
||||
]);
|
||||
|
||||
prismaMock.request.findMany.mockResolvedValue([
|
||||
{
|
||||
id: 'req-future',
|
||||
type: 'audiobook',
|
||||
status: 'awaiting_search',
|
||||
releaseDate: futureDate(45),
|
||||
audiobook: { id: 'a-future', title: 'Future Book', author: 'Author Name', audibleAsin: 'ASIN-F' },
|
||||
},
|
||||
]);
|
||||
|
||||
const { processMonitorRssFeeds } = await import('@/lib/processors/monitor-rss-feeds.processor');
|
||||
const result = await processMonitorRssFeeds({ jobId: 'job-2' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(jobQueueMock.addSearchJob).not.toHaveBeenCalled();
|
||||
expect(jobQueueMock.addSearchEbookJob).not.toHaveBeenCalled();
|
||||
// Request status must not be mutated by RSS processor.
|
||||
expect(prismaMock.request.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('runs RSS search when matched book is unreleased but setting is OFF', async () => {
|
||||
configMock.get.mockImplementation(async (key: string) => {
|
||||
if (key === 'prowlarr_indexers') {
|
||||
return JSON.stringify([{ id: 1, name: 'Indexer', rssEnabled: true }]);
|
||||
}
|
||||
if (key === 'indexer.skip_unreleased') {
|
||||
return 'false';
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
prowlarrMock.getAllRssFeeds.mockResolvedValue([
|
||||
{ title: 'Future Book - Author Name' },
|
||||
]);
|
||||
|
||||
prismaMock.request.findMany.mockResolvedValue([
|
||||
{
|
||||
id: 'req-future-off',
|
||||
type: 'audiobook',
|
||||
status: 'awaiting_search',
|
||||
releaseDate: futureDate(45),
|
||||
audiobook: { id: 'a-future', title: 'Future Book', author: 'Author Name', audibleAsin: 'ASIN-F' },
|
||||
},
|
||||
]);
|
||||
|
||||
const { processMonitorRssFeeds } = await import('@/lib/processors/monitor-rss-feeds.processor');
|
||||
const result = await processMonitorRssFeeds({ jobId: 'job-3' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(jobQueueMock.addSearchJob).toHaveBeenCalledWith(
|
||||
'req-future-off',
|
||||
expect.objectContaining({ title: 'Future Book', author: 'Author Name' })
|
||||
);
|
||||
expect(prismaMock.request.update).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import { createJobQueueMock } from '../helpers/job-queue';
|
||||
|
||||
const prismaMock = createPrismaMock();
|
||||
const jobQueueMock = createJobQueueMock();
|
||||
const configMock = vi.hoisted(() => ({ get: vi.fn() }));
|
||||
|
||||
vi.mock('@/lib/db', () => ({
|
||||
prisma: prismaMock,
|
||||
@@ -18,15 +19,32 @@ vi.mock('@/lib/services/job-queue.service', () => ({
|
||||
getJobQueueService: () => jobQueueMock,
|
||||
}));
|
||||
|
||||
vi.mock('@/lib/services/config.service', () => ({
|
||||
getConfigService: () => configMock,
|
||||
}));
|
||||
|
||||
function futureDate(days = 30): Date {
|
||||
return new Date(Date.now() + days * 24 * 60 * 60 * 1000);
|
||||
}
|
||||
|
||||
function pastDate(days = 30): Date {
|
||||
return new Date(Date.now() - days * 24 * 60 * 60 * 1000);
|
||||
}
|
||||
|
||||
describe('processRetryMissingTorrents', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
// Default: setting ON (default when absent)
|
||||
configMock.get.mockResolvedValue(null);
|
||||
});
|
||||
|
||||
it('queues search jobs for awaiting_search requests', async () => {
|
||||
it('queues search jobs for awaiting_search requests with no release date', async () => {
|
||||
prismaMock.request.findMany.mockResolvedValue([
|
||||
{
|
||||
id: 'req-1',
|
||||
type: 'audiobook',
|
||||
status: 'awaiting_search',
|
||||
releaseDate: null,
|
||||
audiobook: { id: 'a1', title: 'Book', author: 'Author', audibleAsin: 'ASIN1' },
|
||||
},
|
||||
]);
|
||||
@@ -39,7 +57,103 @@ describe('processRetryMissingTorrents', () => {
|
||||
'req-1',
|
||||
expect.objectContaining({ id: 'a1', title: 'Book', author: 'Author' })
|
||||
);
|
||||
expect(prismaMock.request.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('transitions awaiting_search → awaiting_release when book is unreleased and setting ON', async () => {
|
||||
configMock.get.mockResolvedValue('true');
|
||||
prismaMock.request.findMany.mockResolvedValue([
|
||||
{
|
||||
id: 'req-2',
|
||||
type: 'audiobook',
|
||||
status: 'awaiting_search',
|
||||
releaseDate: futureDate(30),
|
||||
audiobook: { id: 'a2', title: 'Future Book', author: 'Future Author', audibleAsin: 'ASIN2' },
|
||||
},
|
||||
]);
|
||||
|
||||
const { processRetryMissingTorrents } = await import('@/lib/processors/retry-missing-torrents.processor');
|
||||
const result = await processRetryMissingTorrents({ jobId: 'job-2' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(prismaMock.request.update).toHaveBeenCalledWith({
|
||||
where: { id: 'req-2' },
|
||||
data: { status: 'awaiting_release' },
|
||||
});
|
||||
expect(jobQueueMock.addSearchJob).not.toHaveBeenCalled();
|
||||
expect(jobQueueMock.addSearchEbookJob).not.toHaveBeenCalled();
|
||||
expect(result.transitioned).toBe(1);
|
||||
expect(result.skipped).toBe(1);
|
||||
});
|
||||
|
||||
it('transitions awaiting_release → awaiting_search and runs search when release date passed', async () => {
|
||||
configMock.get.mockResolvedValue('true');
|
||||
prismaMock.request.findMany.mockResolvedValue([
|
||||
{
|
||||
id: 'req-3',
|
||||
type: 'audiobook',
|
||||
status: 'awaiting_release',
|
||||
releaseDate: pastDate(5),
|
||||
audiobook: { id: 'a3', title: 'Released Book', author: 'Some Author', audibleAsin: 'ASIN3' },
|
||||
},
|
||||
]);
|
||||
|
||||
const { processRetryMissingTorrents } = await import('@/lib/processors/retry-missing-torrents.processor');
|
||||
const result = await processRetryMissingTorrents({ jobId: 'job-3' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(prismaMock.request.update).toHaveBeenCalledWith({
|
||||
where: { id: 'req-3' },
|
||||
data: { status: 'awaiting_search' },
|
||||
});
|
||||
expect(jobQueueMock.addSearchJob).toHaveBeenCalledWith(
|
||||
'req-3',
|
||||
expect.objectContaining({ id: 'a3', title: 'Released Book', author: 'Some Author' })
|
||||
);
|
||||
expect(result.transitioned).toBe(1);
|
||||
expect(result.triggered).toBe(1);
|
||||
});
|
||||
|
||||
it('leaves awaiting_release as-is when book is still unreleased', async () => {
|
||||
configMock.get.mockResolvedValue('true');
|
||||
prismaMock.request.findMany.mockResolvedValue([
|
||||
{
|
||||
id: 'req-4',
|
||||
type: 'audiobook',
|
||||
status: 'awaiting_release',
|
||||
releaseDate: futureDate(60),
|
||||
audiobook: { id: 'a4', title: 'Still Future', author: 'Author', audibleAsin: 'ASIN4' },
|
||||
},
|
||||
]);
|
||||
|
||||
const { processRetryMissingTorrents } = await import('@/lib/processors/retry-missing-torrents.processor');
|
||||
const result = await processRetryMissingTorrents({ jobId: 'job-4' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(prismaMock.request.update).not.toHaveBeenCalled();
|
||||
expect(jobQueueMock.addSearchJob).not.toHaveBeenCalled();
|
||||
expect(result.skipped).toBe(1);
|
||||
expect(result.transitioned).toBe(0);
|
||||
});
|
||||
|
||||
it('runs search for awaiting_search with future date when setting is OFF', async () => {
|
||||
configMock.get.mockResolvedValue('false');
|
||||
prismaMock.request.findMany.mockResolvedValue([
|
||||
{
|
||||
id: 'req-5',
|
||||
type: 'audiobook',
|
||||
status: 'awaiting_search',
|
||||
releaseDate: futureDate(10),
|
||||
audiobook: { id: 'a5', title: 'Off Setting Book', author: 'Author', audibleAsin: 'ASIN5' },
|
||||
},
|
||||
]);
|
||||
|
||||
const { processRetryMissingTorrents } = await import('@/lib/processors/retry-missing-torrents.processor');
|
||||
const result = await processRetryMissingTorrents({ jobId: 'job-5' });
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(prismaMock.request.update).not.toHaveBeenCalled();
|
||||
expect(jobQueueMock.addSearchJob).toHaveBeenCalled();
|
||||
expect(result.triggered).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user