Persist blob sizes (covers + chapter pages) and surface upload storage usage to admins in two places: - Admin System tab: total/covers/chapters totals, ratio of disk, average image sizes, and top-5 largest mangas/chapters leaderboards, behind a new GET /api/v1/admin/storage endpoint (separate from /admin/system so the cheap DB aggregates aren't coupled to its 250ms CPU sample). - Manga detail page: total chapter-content size and per-chapter size, with an em-dash for uncrawled or not-yet-measured chapters. Sizes are captured at write time (crawler put_stream return, uploaded page/cover byte length) into nullable pages.size_bytes / mangas.cover_size_bytes columns; NULL means "not yet measured", distinct from a real 0. A one-shot, idempotent admin "Backfill sizes" action (POST /api/v1/admin/storage/backfill) stats pre-existing blobs in keyset-paginated, capped batches and reports more_remaining so a large legacy library can be drained over multiple runs. Aggregates and leaderboards treat NULL honestly (only fully-measured entities are ranked); a dashboard banner flags unmeasured rows so partial figures aren't read as complete. persist_pages now prunes stale page rows on a shrinking re-crawl so totals and page_count stay consistent. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
275 lines
9.3 KiB
Rust
275 lines
9.3 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>,
|
|
) -> Uuid {
|
|
// 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()
|
|
.id
|
|
}
|
|
|
|
#[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 uncrawled_chapter_reports_zero_storage(pool: PgPool) {
|
|
// A page-less chapter (page_count = 0, no page rows) is "uncrawled"
|
|
// — its size_bytes is 0, which the frontend renders as an em-dash.
|
|
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("Unfetched")).await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!("/api/v1/mangas/{manga_id}/chapters")))
|
|
.await
|
|
.unwrap();
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["items"][0]["page_count"].as_i64().unwrap(), 0);
|
|
assert_eq!(body["items"][0]["size_bytes"].as_i64().unwrap(), 0);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn chapter_with_unmeasured_page_reports_null_storage(pool: PgPool) {
|
|
// A crawled chapter whose pages predate the size backfill (size_bytes
|
|
// IS NULL) must report size_bytes = null — "unknown" — so the frontend
|
|
// shows an em-dash, not a misleading "0 B".
|
|
let h = common::harness(pool.clone());
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
|
|
let chapter_id = seed_chapter(&pool, manga_id, 1, Some("Crawled")).await;
|
|
// One measured page, one unmeasured (NULL) → whole chapter is unknown.
|
|
sqlx::query(
|
|
"INSERT INTO pages (chapter_id, page_number, storage_key, content_type, size_bytes) \
|
|
VALUES ($1, 1, 'k/1.jpg', 'image/jpeg', 100)",
|
|
)
|
|
.bind(chapter_id)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
sqlx::query(
|
|
"INSERT INTO pages (chapter_id, page_number, storage_key, content_type) \
|
|
VALUES ($1, 2, 'k/2.jpg', 'image/jpeg')",
|
|
)
|
|
.bind(chapter_id)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
|
|
let resp = h
|
|
.app
|
|
.clone()
|
|
.oneshot(common::get(&format!("/api/v1/mangas/{manga_id}/chapters")))
|
|
.await
|
|
.unwrap();
|
|
let body = common::body_json(resp).await;
|
|
assert!(
|
|
body["items"][0]["size_bytes"].is_null(),
|
|
"unmeasured page should make the chapter size unknown (null), got {}",
|
|
body["items"][0]["size_bytes"]
|
|
);
|
|
|
|
// The manga detail total is likewise unknown (null), not a silent
|
|
// undercount of just the measured page.
|
|
let detail = h
|
|
.app
|
|
.oneshot(common::get(&format!("/api/v1/mangas/{manga_id}")))
|
|
.await
|
|
.unwrap();
|
|
let detail = common::body_json(detail).await;
|
|
assert!(
|
|
detail["chapter_storage_bytes"].is_null(),
|
|
"manga total should be unknown when a page is unmeasured, got {}",
|
|
detail["chapter_storage_bytes"]
|
|
);
|
|
}
|
|
|
|
#[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_id(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;
|
|
let chapter_id = seed_chapter(&pool, manga_id, 1, Some("The Brand")).await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{manga_id}/chapters/{chapter_id}"
|
|
)))
|
|
.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);
|
|
assert_eq!(body["id"], chapter_id.to_string());
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn get_chapter_unknown_id_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 unknown_chapter = Uuid::new_v4();
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{manga_id}/chapters/{unknown_chapter}"
|
|
)))
|
|
.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_manga = Uuid::nil();
|
|
let unknown_chapter = Uuid::new_v4();
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{unknown_manga}/chapters/{unknown_chapter}"
|
|
)))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
}
|
|
|
|
/// Cross-manga isolation: a chapter id belonging to manga A must not
|
|
/// resolve when accessed via manga B's URL. The (manga_id, id) scoping
|
|
/// in `find_by_id_in_manga` enforces this.
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn get_chapter_from_wrong_manga_is_404(pool: PgPool) {
|
|
let h = common::harness(pool.clone());
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let manga_a = seed_manga(&h, &cookie, "Berserk").await;
|
|
let manga_b = seed_manga(&h, &cookie, "Vagabond").await;
|
|
let chapter_id = seed_chapter(&pool, manga_a, 1, Some("Episode 1")).await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{manga_b}/chapters/{chapter_id}"
|
|
)))
|
|
.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;
|
|
let chapter_id = seed_chapter(&pool, manga_id, 1, None).await;
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{manga_id}/chapters/{chapter_id}/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 unknown_chapter = Uuid::new_v4();
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(&format!(
|
|
"/api/v1/mangas/{manga_id}/chapters/{unknown_chapter}/pages"
|
|
)))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
}
|