fix: interleave uploaded chapters by number in the chapter list

Uploaded chapters have no source_index, and ORDER BY source_index DESC NULLS
LAST sorted every one of them after all crawled chapters regardless of number
— an uploaded chapter 5 landed after crawled chapter 100. Slot each uploaded
chapter by number, just before the crawled chapter with the smallest greater
number, so it interleaves. Crawled rows keep their reversed source-DOM order,
preserving the deliberate placement of non-numeric entries (migration 0021).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
MechaCat02
2026-07-11 14:17:18 +02:00
parent 9508fb8e86
commit c24e296f07
6 changed files with 90 additions and 26 deletions

View File

@@ -148,6 +148,49 @@ async fn list_chapters_returned_in_number_order(pool: PgPool) {
assert_eq!(body["items"][1]["title"], serde_json::Value::Null);
}
#[sqlx::test(migrations = "./migrations")]
async fn list_interleaves_uploaded_and_crawled_chapters_by_number(pool: PgPool) {
// Mixed source: crawled chapters carry a `source_index`; an uploaded
// chapter has none. The uploaded chapter must interleave by NUMBER, not sort
// after every crawled chapter (the old `source_index DESC NULLS LAST` bug
// dumped uploaded chapter 2 to the end, giving [1, 3, 2]).
let h = common::harness(pool.clone());
let (_, cookie) = common::register_user(&h.app).await;
let manga_id = seed_manga(&h, &cookie, "Berserk").await;
// Two crawled chapters (1 and 3) with DOM positions — newest-first, so
// chapter 3 is at source_index 0 and chapter 1 at source_index 1.
let c1 = seed_chapter(&pool, manga_id, 1, Some("Crawled One")).await;
let c3 = seed_chapter(&pool, manga_id, 3, Some("Crawled Three")).await;
sqlx::query("UPDATE chapters SET source_index = 1 WHERE id = $1")
.bind(c1)
.execute(&pool)
.await
.unwrap();
sqlx::query("UPDATE chapters SET source_index = 0 WHERE id = $1")
.bind(c3)
.execute(&pool)
.await
.unwrap();
// Uploaded chapter 2 — no source_index.
seed_chapter(&pool, manga_id, 2, Some("Uploaded Two")).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], "uploaded chapter 2 must slot between 1 and 3");
}
#[sqlx::test(migrations = "./migrations")]
async fn list_chapters_returns_404_for_unknown_manga(pool: PgPool) {
let h = common::harness(pool);