Files
ReadMeABook/tests/api/audiobooks-browse.routes.test.ts
T
kikootwo 09cff5b68d Add per-user ignored audiobooks feature
Introduce a per-user "ignored audiobooks" feature to suppress auto-requests. Changes include:

- Database: add Prisma model IgnoredAudiobook and SQL migration to create ignored_audiobooks table with indexes and FK to users.
- Backend: new API routes to list, add, delete, and check ignored audiobooks (/api/user/ignored-audiobooks, /check/:asin, /:id). Add annotateWithIgnoreStatus utility and integrate it into multiple audiobook list endpoints (popular, new-releases, category, search, authors, series).
- Request creator: add ignore-list check (with sibling-ASIN expansion) and a bypassIgnore option for manual requests; return an 'ignored' reason when blocked.
- Frontend: hooks (useIsIgnored, useToggleIgnore, useIgnoredList) and UI updates — AudiobookCard shows an "Ignored" indicator and AudiobookDetailsModal adds an ignore toggle and propagates local state changes.
- Misc: adjust deduplication duration tolerance (to 5% / min 10 minutes), tweak SWR refresh intervals for shelves/syncing, and small logging/info updates.
- Tests: add unit tests for request-creator ignore logic and update existing tests/mocks to account for ignore annotation; extend prisma test helper with ignoredAudiobook mock.

This commit implements the ignore-list end-to-end (DB, server, client, and tests) so users can ignore specific ASINs and have auto-request flows respect that preference.
2026-03-11 11:56:35 -04:00

247 lines
8.9 KiB
TypeScript

/**
* Component: Audiobooks Browse API Route Tests
* Documentation: documentation/testing.md
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { createPrismaMock } from '../helpers/prisma';
const prismaMock = createPrismaMock();
const audibleServiceMock = vi.hoisted(() => ({
search: vi.fn(),
getAudiobookDetails: vi.fn(),
getBaseUrl: vi.fn().mockReturnValue('https://www.audible.com'),
}));
const enrichMock = vi.hoisted(() => vi.fn());
const currentUserMock = vi.hoisted(() => vi.fn());
vi.mock('@/lib/db', () => ({
prisma: prismaMock,
}));
vi.mock('@/lib/integrations/audible.service', () => ({
getAudibleService: () => audibleServiceMock,
}));
vi.mock('@/lib/utils/audiobook-matcher', () => ({
enrichAudiobooksWithMatches: enrichMock,
}));
// Mock ignore status annotation — pass-through that adds isIgnored: false
vi.mock('@/lib/utils/ignored-audiobooks', () => ({
annotateWithIgnoreStatus: vi.fn(async (books: any[]) =>
books.map((b: any) => ({ ...b, isIgnored: false }))
),
}));
vi.mock('@/lib/middleware/auth', () => ({
getCurrentUser: currentUserMock,
}));
describe('Audiobooks browse routes', () => {
beforeEach(() => {
vi.clearAllMocks();
enrichMock.mockResolvedValue([]);
currentUserMock.mockReturnValue(null);
});
it('searches Audible and enriches results', async () => {
audibleServiceMock.search.mockResolvedValue({
query: 'query',
results: [{ asin: 'ASIN', title: 'Title', author: 'Author' }],
totalResults: 1,
page: 1,
hasMore: false,
});
currentUserMock.mockReturnValue({ sub: 'user-1' });
enrichMock.mockResolvedValue([{ asin: 'ASIN', available: false }]);
const { GET } = await import('@/app/api/audiobooks/search/route');
const response = await GET({ nextUrl: new URL('http://app/api/audiobooks/search?q=query') } as any);
const payload = await response.json();
expect(payload.success).toBe(true);
expect(enrichMock).toHaveBeenCalledWith([{ asin: 'ASIN', title: 'Title', author: 'Author' }], 'user-1');
});
it('returns 400 for invalid popular pagination', async () => {
const { GET } = await import('@/app/api/audiobooks/popular/route');
const response = await GET({ nextUrl: new URL('http://app/api/audiobooks/popular?page=0') } as any);
const payload = await response.json();
expect(response.status).toBe(400);
expect(payload.error).toBe('ValidationError');
});
it('returns popular audiobooks with cached cover URLs', async () => {
// Mock AudibleCacheCategory query (popular route now queries category table)
prismaMock.audibleCacheCategory.findMany.mockResolvedValueOnce([
{ asin: 'ASIN', rank: 1 },
]);
prismaMock.audibleCacheCategory.count.mockResolvedValueOnce(1);
// Mock AudibleCache metadata fetch
prismaMock.audibleCache.findMany.mockResolvedValueOnce([
{
asin: 'ASIN',
title: 'Title',
author: 'Author',
narrator: null,
description: null,
coverArtUrl: 'http://image',
cachedCoverPath: '/tmp/cache/asin.jpg',
durationMinutes: 90,
releaseDate: new Date('2024-01-01'),
rating: 4.5,
genres: [],
lastSyncedAt: new Date(),
},
]);
enrichMock.mockResolvedValueOnce([{ asin: 'ASIN', coverArtUrl: '/api/cache/thumbnails/asin.jpg' }]);
const { GET } = await import('@/app/api/audiobooks/popular/route');
const response = await GET({ nextUrl: new URL('http://app/api/audiobooks/popular?page=1&limit=1') } as any);
const payload = await response.json();
expect(payload.success).toBe(true);
expect(payload.audiobooks[0].coverArtUrl).toBe('/api/cache/thumbnails/asin.jpg');
});
it('returns 400 for invalid new releases pagination', async () => {
const { GET } = await import('@/app/api/audiobooks/new-releases/route');
const response = await GET({ nextUrl: new URL('http://app/api/audiobooks/new-releases?page=0') } as any);
const payload = await response.json();
expect(response.status).toBe(400);
expect(payload.error).toBe('ValidationError');
});
it('returns new release audiobooks', async () => {
// Mock AudibleCacheCategory query (new-releases route now queries category table)
prismaMock.audibleCacheCategory.findMany.mockResolvedValueOnce([]);
prismaMock.audibleCacheCategory.count.mockResolvedValueOnce(0);
const { GET } = await import('@/app/api/audiobooks/new-releases/route');
const response = await GET({ nextUrl: new URL('http://app/api/audiobooks/new-releases?page=1&limit=1') } as any);
const payload = await response.json();
expect(payload.success).toBe(true);
expect(payload.count).toBe(0);
});
it('enriches new releases and uses cached cover URLs', async () => {
// Mock AudibleCacheCategory query
prismaMock.audibleCacheCategory.findMany.mockResolvedValueOnce([
{ asin: 'ASIN', rank: 1 },
]);
prismaMock.audibleCacheCategory.count.mockResolvedValueOnce(1);
// Mock AudibleCache metadata fetch
prismaMock.audibleCache.findMany.mockResolvedValueOnce([
{
asin: 'ASIN',
title: 'Title',
author: 'Author',
narrator: null,
description: null,
coverArtUrl: 'http://image',
cachedCoverPath: '/tmp/cache/asin.jpg',
durationMinutes: 90,
releaseDate: new Date('2024-01-01'),
rating: '4.2',
genres: ['Fiction'],
lastSyncedAt: new Date('2024-01-02'),
},
]);
currentUserMock.mockReturnValue({ sub: 'user-1' });
enrichMock.mockResolvedValueOnce([{ asin: 'ASIN', available: true }]);
const { GET } = await import('@/app/api/audiobooks/new-releases/route');
const response = await GET({ nextUrl: new URL('http://app/api/audiobooks/new-releases?page=1&limit=1') } as any);
const payload = await response.json();
expect(payload.success).toBe(true);
expect(enrichMock).toHaveBeenCalledWith(
[
expect.objectContaining({
asin: 'ASIN',
coverArtUrl: '/api/cache/thumbnails/asin.jpg',
}),
],
'user-1'
);
});
it('returns 500 when new releases query fails', async () => {
prismaMock.audibleCacheCategory.findMany.mockRejectedValueOnce(new Error('db down'));
const { GET } = await import('@/app/api/audiobooks/new-releases/route');
const response = await GET({ nextUrl: new URL('http://app/api/audiobooks/new-releases?page=1&limit=1') } as any);
const payload = await response.json();
expect(response.status).toBe(500);
expect(payload.error).toBe('FetchError');
});
it('returns audiobook details when ASIN is valid', async () => {
audibleServiceMock.getAudiobookDetails.mockResolvedValue({ asin: 'ASIN123456', title: 'Title' });
const { GET } = await import('@/app/api/audiobooks/[asin]/route');
const response = await GET({} as any, { params: Promise.resolve({ asin: 'ASIN123456' }) });
const payload = await response.json();
expect(payload.success).toBe(true);
expect(payload.audiobook.asin).toBe('ASIN123456');
});
it('returns 400 when ASIN is invalid', async () => {
const { GET } = await import('@/app/api/audiobooks/[asin]/route');
const response = await GET({} as any, { params: Promise.resolve({ asin: 'BAD' }) });
const payload = await response.json();
expect(response.status).toBe(400);
expect(payload.error).toBe('ValidationError');
});
it('returns 404 when audiobook is not found', async () => {
audibleServiceMock.getAudiobookDetails.mockResolvedValue(null);
const { GET } = await import('@/app/api/audiobooks/[asin]/route');
const response = await GET({} as any, { params: Promise.resolve({ asin: 'ASIN123456' }) });
const payload = await response.json();
expect(response.status).toBe(404);
expect(payload.error).toBe('NotFound');
});
it('returns 500 when audiobook lookup fails', async () => {
audibleServiceMock.getAudiobookDetails.mockRejectedValue(new Error('fail'));
const { GET } = await import('@/app/api/audiobooks/[asin]/route');
const response = await GET({} as any, { params: Promise.resolve({ asin: 'ASIN123456' }) });
const payload = await response.json();
expect(response.status).toBe(500);
expect(payload.error).toBe('FetchError');
});
it('returns cached covers for login', async () => {
// Mock AudibleCacheCategory query (covers route now queries category table)
prismaMock.audibleCacheCategory.findMany.mockResolvedValueOnce([
{ asin: 'ASIN' },
]);
// Mock AudibleCache metadata fetch
prismaMock.audibleCache.findMany.mockResolvedValueOnce([
{ asin: 'ASIN', title: 'Title', author: 'Author', cachedCoverPath: '/tmp/asin.jpg', coverArtUrl: null },
]);
const { GET } = await import('@/app/api/audiobooks/covers/route');
const response = await GET();
const payload = await response.json();
expect(payload.success).toBe(true);
expect(payload.covers[0].coverUrl).toBe('/api/cache/thumbnails/asin.jpg');
});
});