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:
@@ -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);
|
||||
|
||||
@@ -1175,7 +1175,7 @@ async fn list_for_manga_returns_source_order_reversed(pool: PgPool) {
|
||||
}
|
||||
|
||||
#[sqlx::test(migrations = "./migrations")]
|
||||
async fn list_for_manga_places_null_source_index_last(pool: PgPool) {
|
||||
async fn list_for_manga_interleaves_null_source_index_by_number(pool: PgPool) {
|
||||
crawler::ensure_source(&pool, "target", "T", "https://x.example")
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -1184,23 +1184,24 @@ async fn list_for_manga_places_null_source_index_last(pool: PgPool) {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// Crawled chapters get source_index 0 and 1; the upload path leaves
|
||||
// it NULL. NULLS LAST plus the (number, created_at) tail means the
|
||||
// upload sits after both crawled rows even though its number is in
|
||||
// the middle.
|
||||
// Crawled chapters, newest-first in the source DOM (so chapter 3 is at
|
||||
// source_index 0 and chapter 1 at source_index 1). Reversed for display that
|
||||
// is [Ch.1, Ch.3]. The uploaded chapter 2 must slot BETWEEN them by number,
|
||||
// not fall to the end — that was the bug (NULLS LAST dumped it last
|
||||
// regardless of its number).
|
||||
let crawled = vec![
|
||||
SourceChapterRef {
|
||||
source_chapter_key: "a".into(),
|
||||
number: 1,
|
||||
title: Some("Ch.1".into()),
|
||||
url: "https://x.example/foo/a".into(),
|
||||
},
|
||||
SourceChapterRef {
|
||||
source_chapter_key: "b".into(),
|
||||
number: 3,
|
||||
title: Some("Ch.3".into()),
|
||||
url: "https://x.example/foo/b".into(),
|
||||
},
|
||||
SourceChapterRef {
|
||||
source_chapter_key: "a".into(),
|
||||
number: 1,
|
||||
title: Some("Ch.1".into()),
|
||||
url: "https://x.example/foo/a".into(),
|
||||
},
|
||||
];
|
||||
crawler::sync_manga_chapters(&pool, "target", up.manga_id, &crawled)
|
||||
.await
|
||||
@@ -1220,11 +1221,11 @@ async fn list_for_manga_places_null_source_index_last(pool: PgPool) {
|
||||
assert_eq!(
|
||||
titles,
|
||||
vec![
|
||||
"Ch.3".to_string(),
|
||||
"Ch.1".to_string(),
|
||||
"User upload Ch.2".to_string(),
|
||||
"Ch.3".to_string(),
|
||||
],
|
||||
"crawled rows ordered by reversed source_index; user upload \
|
||||
(NULL source_index) falls through to the end",
|
||||
"uploaded chapter (NULL source_index) interleaves by number between the \
|
||||
crawled rows instead of falling to the end",
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user