Per-user reading progress and uploader attribution. Schema (migration 0011): `read_progress` table (one row per (user, manga); chapter_id nullable on chapter delete) and nullable `uploaded_by` columns on mangas + chapters with partial indexes scoped to non-null rows. Endpoints (all `/me/*`, auth-scoped): - PUT `/v1/me/read-progress` upserts. FK violations + cross-manga chapter ids both surface as 4xx (404 / 422) so the API can't be used to write logically invalid rows. - GET `/v1/me/read-progress` paged newest-first list. - GET `/v1/me/read-progress/:manga_id` enriched with chapter_number for the manga page's Continue CTA. - DELETE `/v1/me/read-progress/:manga_id` idempotent. - GET `/v1/me/uploads` interleaved manga + chapter uploads as a tagged union; limit-only pagination. Existing manga + chapter upload handlers stamp `uploaded_by`. Frontend: - Reader emits progress on mount + page change (debounce) and via IntersectionObserver in continuous mode. High-water mark is seeded from the persisted server value so re-opening a chapter doesn't regress to page 1. Tab close survives via `sendBeacon` (fallback `keepalive` fetch); SPA navigation flushes via regular fetch. - Manga detail page shows "Continue reading Chapter N — page M" above the chapters list, working even for mangas with >50 chapters. - New `/profile/history` tab with reading history (clear-per-row, inline error on failure) and uploads (mangas + chapters mixed chronologically with type-aware rendering). 171 backend tests (incl. 16 history tests covering ownership, FK race, cross-link guard, chapter SET NULL behaviour) and 97 frontend tests + svelte-check clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
168 lines
5.4 KiB
Rust
168 lines
5.4 KiB
Rust
mod common;
|
|
|
|
use axum::http::StatusCode;
|
|
use serde_json::json;
|
|
use sqlx::PgPool;
|
|
use tower::ServiceExt;
|
|
use uuid::Uuid;
|
|
#[allow(unused_imports)]
|
|
use serde_json as _;
|
|
|
|
async fn seed_manga(h: &common::Harness, cookie: &str, title: &str) -> Uuid {
|
|
common::seed_manga_via_api(&h.app, cookie, title).await
|
|
}
|
|
|
|
async fn seed_chapter(pool: &PgPool, manga_id: Uuid, number: i32, title: Option<&str>) {
|
|
// Historical seed — uploaded_by remains NULL, mirroring the
|
|
// pre-Phase-5 rows in the production DB.
|
|
mangalord::repo::chapter::create(pool, manga_id, number, title, None)
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn list_chapters_is_empty_initially(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!("/api/v1/mangas/{manga_id}/chapters")))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["items"], json!([]));
|
|
assert_eq!(body["page"]["limit"], 50);
|
|
assert_eq!(body["page"]["offset"], 0);
|
|
assert!(body["page"]["total"].is_null());
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn list_chapters_returned_in_number_order(pool: PgPool) {
|
|
let h = common::harness(pool.clone());
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
|
|
seed_chapter(&pool, manga_id, 3, Some("The Black Swordsman")).await;
|
|
seed_chapter(&pool, manga_id, 1, Some("The Brand")).await;
|
|
seed_chapter(&pool, manga_id, 2, None).await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!("/api/v1/mangas/{manga_id}/chapters")))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
let body = common::body_json(resp).await;
|
|
let numbers: Vec<i64> = body["items"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|c| c["number"].as_i64().unwrap())
|
|
.collect();
|
|
assert_eq!(numbers, vec![1, 2, 3]);
|
|
assert_eq!(body["items"][1]["title"], serde_json::Value::Null);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn list_chapters_returns_404_for_unknown_manga(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let unknown = Uuid::nil();
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!("/api/v1/mangas/{unknown}/chapters")))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["error"]["code"], "not_found");
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn get_chapter_by_number(pool: PgPool) {
|
|
let h = common::harness(pool.clone());
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
seed_chapter(&pool, manga_id, 1, Some("The Brand")).await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{manga_id}/chapters/1"
|
|
)))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["number"], 1);
|
|
assert_eq!(body["title"], "The Brand");
|
|
assert_eq!(body["page_count"], 0);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn get_chapter_unknown_number_is_404(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{manga_id}/chapters/99"
|
|
)))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["error"]["code"], "not_found");
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn get_chapter_unknown_manga_is_404(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let unknown = Uuid::nil();
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!("/api/v1/mangas/{unknown}/chapters/1")))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn list_pages_empty_for_chapter_without_upload(pool: PgPool) {
|
|
let h = common::harness(pool.clone());
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
seed_chapter(&pool, manga_id, 1, None).await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{manga_id}/chapters/1/pages"
|
|
)))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["pages"], json!([]));
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn list_pages_returns_404_for_unknown_chapter(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{manga_id}/chapters/99/pages"
|
|
)))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
}
|