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

@@ -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",
);
}