feat: list + get chapters under /api/v1/mangas/{id}/chapters

Both endpoints are public reads — anyone browsing can see a manga's
table of contents and chapter metadata. Uploads land in feat/uploads.

- GET /api/v1/mangas/{id}/chapters returns the paged envelope
  ({items, page}) ordered by chapter number ASC. Surfaces 404 if the
  parent manga doesn't exist so an empty result can't be mistaken for
  "no chapters yet" on a real manga.
- GET /api/v1/mangas/{id}/chapters/{number} returns a single chapter,
  404 if either manga or chapter is missing.

repo::chapter exposes list_for_manga, find_by_manga_and_number, and
create. create translates the (manga_id, number) unique violation into
AppError::Conflict so the upload handler can later return a clean 409.

Frontend lib/api/chapters.ts mirrors the shape with listChapters and
getChapter; Vitest asserts the URL shape, paged response handling, and
404 envelope propagation.

Lockstep version bump to 0.4.0.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
MechaCat02
2026-05-16 22:08:20 +02:00
parent 383cfbed3b
commit 2f9912533f
10 changed files with 425 additions and 3 deletions

View File

@@ -0,0 +1,89 @@
import {
describe,
it,
expect,
vi,
beforeEach,
afterEach,
type MockInstance
} from 'vitest';
import { listChapters, getChapter } from './chapters';
function ok(body: unknown): Response {
return new Response(JSON.stringify(body), {
status: 200,
headers: { 'content-type': 'application/json' }
});
}
function envelope(status: number, code: string, message: string): Response {
return new Response(JSON.stringify({ error: { code, message } }), {
status,
headers: { 'content-type': 'application/json' }
});
}
const emptyPage = { items: [], page: { limit: 50, offset: 0, total: null } };
const chapterFixture = {
id: 'c1',
manga_id: 'm1',
number: 1,
title: 'The Brand',
page_count: 0,
created_at: '2026-01-01T00:00:00Z'
};
describe('chapters api client', () => {
let fetchSpy: MockInstance<typeof globalThis.fetch>;
beforeEach(() => {
fetchSpy = vi.spyOn(globalThis, 'fetch');
});
afterEach(() => {
vi.restoreAllMocks();
});
it('listChapters hits /v1/mangas/{id}/chapters with no params', async () => {
fetchSpy.mockResolvedValueOnce(ok(emptyPage));
await listChapters('m1');
const url = fetchSpy.mock.calls[0][0] as string;
expect(url).toMatch(/\/v1\/mangas\/m1\/chapters$/);
});
it('listChapters encodes limit and offset', async () => {
fetchSpy.mockResolvedValueOnce(ok(emptyPage));
await listChapters('m1', { limit: 10, offset: 20 });
const url = fetchSpy.mock.calls[0][0] as string;
expect(url).toContain('limit=10');
expect(url).toContain('offset=20');
});
it('listChapters returns the paged envelope', async () => {
fetchSpy.mockResolvedValueOnce(
ok({
items: [chapterFixture],
page: { limit: 50, offset: 0, total: null }
})
);
const result = await listChapters('m1');
expect(result.items[0]).toEqual(chapterFixture);
expect(result.page.total).toBeNull();
});
it('getChapter hits /v1/mangas/{id}/chapters/{n}', async () => {
fetchSpy.mockResolvedValueOnce(ok(chapterFixture));
const c = await getChapter('m1', 1);
expect(c).toEqual(chapterFixture);
const url = fetchSpy.mock.calls[0][0] as string;
expect(url).toMatch(/\/v1\/mangas\/m1\/chapters\/1$/);
});
it('getChapter surfaces 404 via ApiError.code', async () => {
fetchSpy.mockResolvedValueOnce(envelope(404, 'not_found', 'not found'));
await expect(getChapter('m1', 99)).rejects.toMatchObject({
status: 404,
code: 'not_found'
});
});
});