Backend:
- Migration 0005_search.sql enables pg_trgm and adds GIN indexes
(gin_trgm_ops) on mangas.title and on mangas.author (partial, WHERE
author IS NOT NULL).
- repo::manga::list keeps the existing substring (ILIKE) clause and
adds the `%` operator on title + author so the search tolerates typos
('narto' → 'Naruto'). Both branches share the trgm index. A second
count(*) query (same WHERE clause, indexed) yields the total without
scanning twice in any meaningful sense.
- New ListSort enum (Recent / Title) interpolated into ORDER BY from a
hard-coded match — never from request input, so the format!() is not
a SQL-injection seam. Default stays Recent (created_at DESC).
- api::mangas accepts `?sort=recent|title` (snake_case) via serde and
returns `page.total` as a number instead of null.
- api::pagination::PagedResponse gains a `with_total` constructor.
Backend coverage in tests/api_mangas.rs (4 new cases plus the existing
list_is_empty_initially updated to assert total: 0):
- list_returns_total_count_independent_of_pagination — limit=2 with 3
rows returns 2 items and total=3.
- search_via_trigram_tolerates_typos — `?search=narto` finds Naruto.
- list_sort_title_orders_alphabetically — three out-of-order inserts
come back A→Z.
- search_reflects_filtered_total — search narrows total to 1.
Frontend:
- lib/api/mangas.ts gains a `MangaSort` type and threads `sort` through
listMangas's query-string builder.
- Home page renders a "Sort" select (Recent / Title A→Z) that re-runs
the list query, and shows "Showing N of M" when total is present.
Lockstep version bump to 0.8.0.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
303 lines
9.3 KiB
Rust
303 lines
9.3 KiB
Rust
mod common;
|
|
|
|
use axum::http::StatusCode;
|
|
use serde_json::json;
|
|
use sqlx::PgPool;
|
|
use tower::ServiceExt;
|
|
|
|
use common::MultipartBuilder;
|
|
|
|
fn metadata(title: &str) -> serde_json::Value {
|
|
json!({ "title": title })
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn list_is_empty_initially(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let resp = h.app.oneshot(common::get("/api/v1/mangas")).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_eq!(body["page"]["total"], 0);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn list_returns_total_count_independent_of_pagination(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
|
|
for title in ["One Piece", "Berserk", "Vinland Saga"] {
|
|
let _ = h
|
|
.app
|
|
.clone()
|
|
.oneshot(common::post_multipart_with_cookie(
|
|
"/api/v1/mangas",
|
|
MultipartBuilder::new().add_json("metadata", json!({ "title": title })),
|
|
&cookie,
|
|
))
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get("/api/v1/mangas?limit=2"))
|
|
.await
|
|
.unwrap();
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["items"].as_array().unwrap().len(), 2);
|
|
// Total reflects the unfiltered population, not the page size.
|
|
assert_eq!(body["page"]["total"], 3);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn search_via_trigram_tolerates_typos(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let _ = h
|
|
.app
|
|
.clone()
|
|
.oneshot(common::post_multipart_with_cookie(
|
|
"/api/v1/mangas",
|
|
MultipartBuilder::new().add_json("metadata", json!({ "title": "Naruto" })),
|
|
&cookie,
|
|
))
|
|
.await
|
|
.unwrap();
|
|
|
|
// 'narto' is one letter off — the % operator on the GIN trgm index
|
|
// should still match it.
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get("/api/v1/mangas?search=narto"))
|
|
.await
|
|
.unwrap();
|
|
let body = common::body_json(resp).await;
|
|
let titles: Vec<&str> = body["items"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|m| m["title"].as_str().unwrap())
|
|
.collect();
|
|
assert_eq!(titles, vec!["Naruto"]);
|
|
assert_eq!(body["page"]["total"], 1);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn list_sort_title_orders_alphabetically(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
|
|
for title in ["Vinland Saga", "Berserk", "One Piece"] {
|
|
let _ = h
|
|
.app
|
|
.clone()
|
|
.oneshot(common::post_multipart_with_cookie(
|
|
"/api/v1/mangas",
|
|
MultipartBuilder::new().add_json("metadata", json!({ "title": title })),
|
|
&cookie,
|
|
))
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get("/api/v1/mangas?sort=title"))
|
|
.await
|
|
.unwrap();
|
|
let body = common::body_json(resp).await;
|
|
let titles: Vec<&str> = body["items"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|m| m["title"].as_str().unwrap())
|
|
.collect();
|
|
assert_eq!(titles, vec!["Berserk", "One Piece", "Vinland Saga"]);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn search_reflects_filtered_total(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
|
|
for title in ["One Piece", "Berserk", "Vinland Saga"] {
|
|
let _ = h
|
|
.app
|
|
.clone()
|
|
.oneshot(common::post_multipart_with_cookie(
|
|
"/api/v1/mangas",
|
|
MultipartBuilder::new().add_json("metadata", json!({ "title": title })),
|
|
&cookie,
|
|
))
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get("/api/v1/mangas?search=berserk"))
|
|
.await
|
|
.unwrap();
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["items"].as_array().unwrap().len(), 1);
|
|
assert_eq!(body["page"]["total"], 1);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn create_then_list_roundtrip(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
|
|
let created = h
|
|
.app
|
|
.clone()
|
|
.oneshot(common::post_multipart_with_cookie(
|
|
"/api/v1/mangas",
|
|
MultipartBuilder::new().add_json(
|
|
"metadata",
|
|
json!({ "title": "Berserk", "author": "Kentaro Miura", "description": null }),
|
|
),
|
|
&cookie,
|
|
))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(created.status(), StatusCode::CREATED);
|
|
let body = common::body_json(created).await;
|
|
assert_eq!(body["title"], "Berserk");
|
|
assert_eq!(body["author"], "Kentaro Miura");
|
|
assert!(body["id"].as_str().is_some());
|
|
|
|
let listed = h.app.oneshot(common::get("/api/v1/mangas")).await.unwrap();
|
|
let listed_body = common::body_json(listed).await;
|
|
let items = listed_body["items"].as_array().unwrap();
|
|
assert_eq!(items.len(), 1);
|
|
assert_eq!(items[0]["title"], "Berserk");
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn search_filters_by_title_and_author(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
|
|
for (title, author) in [
|
|
("One Piece", "Eiichiro Oda"),
|
|
("Berserk", "Kentaro Miura"),
|
|
("Vinland Saga", "Makoto Yukimura"),
|
|
] {
|
|
let _ = h
|
|
.app
|
|
.clone()
|
|
.oneshot(common::post_multipart_with_cookie(
|
|
"/api/v1/mangas",
|
|
MultipartBuilder::new()
|
|
.add_json("metadata", json!({ "title": title, "author": author })),
|
|
&cookie,
|
|
))
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
let resp = h
|
|
.app
|
|
.clone()
|
|
.oneshot(common::get("/api/v1/mangas?search=miura"))
|
|
.await
|
|
.unwrap();
|
|
let body = common::body_json(resp).await;
|
|
let titles: Vec<&str> = body["items"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|m| m["title"].as_str().unwrap())
|
|
.collect();
|
|
assert_eq!(titles, vec!["Berserk"]);
|
|
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get("/api/v1/mangas?search=saga"))
|
|
.await
|
|
.unwrap();
|
|
let body = common::body_json(resp).await;
|
|
let titles: Vec<&str> = body["items"]
|
|
.as_array()
|
|
.unwrap()
|
|
.iter()
|
|
.map(|m| m["title"].as_str().unwrap())
|
|
.collect();
|
|
assert_eq!(titles, vec!["Vinland Saga"]);
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn create_rejects_empty_title_with_validation_failed(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::post_multipart_with_cookie(
|
|
"/api/v1/mangas",
|
|
MultipartBuilder::new().add_json("metadata", metadata(" ")),
|
|
&cookie,
|
|
))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["error"]["code"], "validation_failed");
|
|
assert!(body["error"]["details"]["title"].is_string());
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn create_rejects_missing_metadata_part(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let (_, cookie) = common::register_user(&h.app).await;
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::post_multipart_with_cookie(
|
|
"/api/v1/mangas",
|
|
MultipartBuilder::new(), // no metadata part
|
|
&cookie,
|
|
))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::UNPROCESSABLE_ENTITY);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["error"]["code"], "validation_failed");
|
|
assert_eq!(body["error"]["details"]["metadata"], "required");
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn create_requires_authentication(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::post_multipart(
|
|
"/api/v1/mangas",
|
|
MultipartBuilder::new().add_json("metadata", metadata("Berserk")),
|
|
))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["error"]["code"], "unauthenticated");
|
|
}
|
|
|
|
#[sqlx::test(migrations = "./migrations")]
|
|
async fn get_unknown_id_is_404_with_envelope(pool: PgPool) {
|
|
let h = common::harness(pool);
|
|
let resp = h
|
|
.app
|
|
.oneshot(common::get(
|
|
"/api/v1/mangas/00000000-0000-0000-0000-000000000000",
|
|
))
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
let body = common::body_json(resp).await;
|
|
assert_eq!(body["error"]["code"], "not_found");
|
|
let msg = body["error"]["message"].as_str().expect("message is string");
|
|
assert!(!msg.is_empty(), "message should be non-empty");
|
|
}
|