Frontend-only branch consuming the multipart endpoints from feat/uploads.
- /upload page with two sections:
- "Create manga": title (required), author, description, optional
cover. Submit posts the FormData to POST /api/v1/mangas via the
existing createManga client.
- "Upload chapter": manga selector (preloaded via listMangas
sort=title, limit=200), chapter number, optional title, and a
drag-drop zone for page images. Pages render in an ordered list
with up/down/remove controls so the user can fix order without
re-uploading. The same hidden file input is used by both the
"browse" link and Playwright's setInputFiles, so the e2e test
exercises the real submission code path even though it doesn't
simulate the drag mechanics.
- Client-side preflight in lib/upload-validation.ts (extracted so
Vitest can target it directly): rejects files over 20 MiB with a
sized message and rejects MIME types outside the
jpeg/png/webp/gif/avif whitelist. Files with an empty file.type fall
through to the backend's magic-byte sniff, which stays the
authoritative check. The submit button is disabled while any pending
page has a client-side error, so an oversized file never reaches the
network.
- API errors are surfaced via the envelope: 401 redirects to /login,
everything else is rendered as the form's role=alert message. The
backend's 415/413/422/409 message strings carry enough context that
the user can act on them without us repeating the field name
client-side (matches what we already surface for /auth errors).
- /upload requires auth: anonymous users see a "Sign in to upload"
prompt linking to /login instead of empty forms.
Vitest coverage (10 cases):
- validateImageFile null on small images and on each of the five
whitelisted MIMEs.
- Oversized files → sized "too large" message that names the file.
- Non-image MIME → "unsupported image type X" naming the type.
- Empty file.type → passes (deferred to backend sniff).
- formatBytes handles B / KiB / MiB.
Playwright coverage (e2e/upload.spec.ts, 4 cases):
- Anonymous user sees the sign-in prompt.
- A "page.png" whose bytes are a PDF (client validator passes because
it trusts the declared MIME for preflight) reaches the mocked
backend, which 415s, and the form renders the backend's message.
- Happy path: create a manga, then upload a 2-page chapter, with both
successes asserted from the mocked 201 responses.
- A 21 MiB file is added to the pages list with a "too large" error,
the submit button stays disabled, and zero POSTs leave the browser.
Lockstep version bump to 0.9.0.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
193 lines
6.3 KiB
TypeScript
193 lines
6.3 KiB
TypeScript
import { test, expect, type Page } from '@playwright/test';
|
|
|
|
const userFixture = {
|
|
id: 'u1',
|
|
username: 'alice',
|
|
created_at: '2026-01-01T00:00:00Z'
|
|
};
|
|
const mangaFixture = {
|
|
id: 'm1',
|
|
title: 'Berserk',
|
|
author: 'Kentaro Miura',
|
|
description: null,
|
|
cover_image_path: null,
|
|
created_at: '2026-01-01T00:00:00Z',
|
|
updated_at: '2026-01-01T00:00:00Z'
|
|
};
|
|
|
|
async function mockBaseUploadApis(page: Page) {
|
|
await page.route('**/api/v1/auth/me', (route) =>
|
|
route.fulfill({
|
|
status: 200,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({ user: userFixture })
|
|
})
|
|
);
|
|
await page.route('**/api/v1/mangas?*', (route) =>
|
|
route.fulfill({
|
|
status: 200,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({
|
|
items: [mangaFixture],
|
|
page: { limit: 200, offset: 0, total: 1 }
|
|
})
|
|
})
|
|
);
|
|
}
|
|
|
|
test('anonymous user sees sign-in prompt on /upload', async ({ page }) => {
|
|
await page.route('**/api/v1/auth/me', (route) =>
|
|
route.fulfill({
|
|
status: 401,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({
|
|
error: { code: 'unauthenticated', message: 'unauthenticated' }
|
|
})
|
|
})
|
|
);
|
|
await page.route('**/api/v1/mangas?*', (route) =>
|
|
route.fulfill({
|
|
status: 200,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({ items: [], page: { limit: 200, offset: 0, total: 0 } })
|
|
})
|
|
);
|
|
|
|
await page.goto('/upload');
|
|
await expect(page.getByTestId('upload-signin')).toBeVisible();
|
|
});
|
|
|
|
test('uploading a non-image page surfaces the backend 415 message', async ({ page }) => {
|
|
await mockBaseUploadApis(page);
|
|
|
|
// Backend rejects with 415 unsupported_media_type — we want to see
|
|
// the human message rendered as the chapter error.
|
|
await page.route('**/api/v1/mangas/m1/chapters', (route) =>
|
|
route.fulfill({
|
|
status: 415,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({
|
|
error: {
|
|
code: 'unsupported_media_type',
|
|
message: 'page[0]: unsupported image type application/pdf'
|
|
}
|
|
})
|
|
})
|
|
);
|
|
|
|
await page.goto('/upload');
|
|
await page.getByTestId('chapter-manga').selectOption('m1');
|
|
await page.getByTestId('chapter-number').fill('1');
|
|
|
|
// Client validator allows image/png; we lie about the file type so
|
|
// the request actually reaches the (mocked) backend, exercising the
|
|
// 415 envelope path.
|
|
await page.getByTestId('chapter-pages-input').setInputFiles({
|
|
name: 'fake.png',
|
|
mimeType: 'image/png',
|
|
buffer: Buffer.from('%PDF-1.4', 'utf-8')
|
|
});
|
|
|
|
await page.getByTestId('chapter-submit').click();
|
|
await expect(page.getByTestId('chapter-error')).toContainText(
|
|
'unsupported image type'
|
|
);
|
|
});
|
|
|
|
test('happy path: create manga + upload chapter (mocked)', async ({ page }) => {
|
|
await mockBaseUploadApis(page);
|
|
|
|
let createdManga: typeof mangaFixture | null = null;
|
|
let createdChapter: { id: string; number: number } | null = null;
|
|
|
|
await page.route('**/api/v1/mangas', (route) => {
|
|
if (route.request().method() === 'POST') {
|
|
createdManga = { ...mangaFixture, id: 'm2', title: 'Naruto' };
|
|
route.fulfill({
|
|
status: 201,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify(createdManga)
|
|
});
|
|
} else {
|
|
route.fallback();
|
|
}
|
|
});
|
|
await page.route('**/api/v1/mangas/m1/chapters', (route) => {
|
|
if (route.request().method() === 'POST') {
|
|
createdChapter = { id: 'c1', number: 1 };
|
|
route.fulfill({
|
|
status: 201,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({
|
|
id: 'c1',
|
|
manga_id: 'm1',
|
|
number: 1,
|
|
title: null,
|
|
page_count: 2,
|
|
created_at: '2026-01-01T00:00:00Z'
|
|
})
|
|
});
|
|
} else {
|
|
route.fallback();
|
|
}
|
|
});
|
|
|
|
await page.goto('/upload');
|
|
|
|
// Create manga.
|
|
await page.getByTestId('manga-title').fill('Naruto');
|
|
await page.getByTestId('manga-submit').click();
|
|
await expect(page.getByTestId('manga-success')).toContainText('Created');
|
|
expect(createdManga).not.toBeNull();
|
|
|
|
// Upload chapter with two pages.
|
|
await page.getByTestId('chapter-manga').selectOption('m1');
|
|
await page.getByTestId('chapter-number').fill('1');
|
|
await page.getByTestId('chapter-pages-input').setInputFiles([
|
|
{
|
|
name: '1.png',
|
|
mimeType: 'image/png',
|
|
buffer: Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
|
|
},
|
|
{
|
|
name: '2.png',
|
|
mimeType: 'image/png',
|
|
buffer: Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
|
|
}
|
|
]);
|
|
await expect(page.getByTestId('chapter-pages-list')).toContainText('1.png');
|
|
await expect(page.getByTestId('chapter-pages-list')).toContainText('2.png');
|
|
|
|
await page.getByTestId('chapter-submit').click();
|
|
await expect(page.getByTestId('chapter-success')).toContainText(
|
|
'2 pages'
|
|
);
|
|
expect(createdChapter).not.toBeNull();
|
|
});
|
|
|
|
test('client preflight blocks oversized files without hitting the network', async ({ page }) => {
|
|
await mockBaseUploadApis(page);
|
|
|
|
let chapterPostCalls = 0;
|
|
await page.route('**/api/v1/mangas/m1/chapters', (route) => {
|
|
if (route.request().method() === 'POST') chapterPostCalls += 1;
|
|
route.fallback();
|
|
});
|
|
|
|
await page.goto('/upload');
|
|
await page.getByTestId('chapter-manga').selectOption('m1');
|
|
await page.getByTestId('chapter-number').fill('1');
|
|
|
|
// A ~21 MiB buffer — exceeds the 20 MiB client cap.
|
|
const big = Buffer.alloc(21 * 1024 * 1024, 0xff);
|
|
await page.getByTestId('chapter-pages-input').setInputFiles({
|
|
name: 'huge.png',
|
|
mimeType: 'image/png',
|
|
buffer: big
|
|
});
|
|
|
|
await expect(page.getByTestId('chapter-pages-list')).toContainText('too large');
|
|
await expect(page.getByTestId('chapter-submit')).toBeDisabled();
|
|
expect(chapterPostCalls).toBe(0);
|
|
});
|