diff --git a/crates/twigpine-node/src/db/mod.rs b/crates/twigpine-node/src/db/mod.rs index 72f26380..79c9e218 100644 --- a/crates/twigpine-node/src/db/mod.rs +++ b/crates/twigpine-node/src/db/mod.rs @@ -4122,10 +4122,10 @@ impl Db { } /// All repos with star counts, mirror-deduplicated via `DEDUP_CTE` and - /// ordered newest-first, optionally filtered to one owner. Returns the full - /// set (no SQL pagination): the listing surface filters by per-caller `"/"` - /// visibility in Rust and paginates after, so neither the page nor the count - /// leaks a repo the caller may not read (#97). + /// ordered newest-first with ties broken by repo ID, optionally filtered to + /// one owner. Returns the full set (no SQL pagination): the listing surface + /// filters by per-caller `"/"` visibility in Rust and paginates after, so + /// neither the page nor the count leaks a repo the caller may not read (#97). /// /// The owner filter is normalized to its did:key short form before binding so /// the SQL predicate matches `crate::api::did_matches`: a full `did:key:z...` @@ -4151,7 +4151,7 @@ impl Db { LEFT JOIN ( SELECT repo_id, COUNT(*) AS cnt FROM repo_stars GROUP BY repo_id ) s ON s.repo_id = d.id - ORDER BY d.updated_at DESC", + ORDER BY d.updated_at DESC, d.id ASC", Self::dedup_cte() ); let rows = sqlx::query(&sql) diff --git a/crates/twigpine-node/src/test_support.rs b/crates/twigpine-node/src/test_support.rs index 107ea5b0..a2b5fae8 100644 --- a/crates/twigpine-node/src/test_support.rs +++ b/crates/twigpine-node/src/test_support.rs @@ -7211,6 +7211,131 @@ mod tests { .with_state(state) } + #[sqlx::test] + async fn list_repos_pages_tied_timestamps_in_id_order(pool: PgPool) { + let state = test_state(pool).await; + let tied_at = chrono::DateTime::from_timestamp(1_700_000_000, 0).unwrap(); + for (id, name) in [ + ("a", "zulu"), + ("b", "yankee"), + ("c", "xray"), + ("d", "whiskey"), + ] { + let mut repo = seed_repo("did:key:zListingTieOwner", name); + repo.id = id.to_string(); + repo.updated_at = tied_at; + state.db.create_repo(&repo).await.unwrap(); + } + + let app = list_repos_router(state); + let mut ids = Vec::new(); + for offset in [0, 2, 4] { + let response = app + .clone() + .oneshot(anon_get(&format!("/api/v1/repos?limit=2&offset={offset}"))) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!(response.headers()["X-Total-Count"], "4"); + ids.extend( + json_body(response) + .await + .as_array() + .unwrap() + .iter() + .map(|repo| repo["id"].as_str().unwrap().to_string()), + ); + } + assert_eq!(ids, ["a", "b", "c", "d"], "tied repos across all pages"); + } + + #[sqlx::test] + async fn list_repos_pages_distinct_timestamps_by_visibility(pool: PgPool) { + let state = test_state(pool).await; + let owner = "did:key:zListingOrderOwner"; + let reader = "did:key:zListingOrderReader"; + for (i, name) in ["public-old", "private", "shared", "public-new"] + .into_iter() + .enumerate() + { + let mut repo = seed_repo(owner, name); + repo.is_public = name.starts_with("public"); + repo.updated_at = + chrono::DateTime::from_timestamp(1_700_000_000 + i as i64, 0).unwrap(); + state.db.create_repo(&repo).await.unwrap(); + for star in 0..i { + state + .db + .star_repo(&repo.id, &format!("did:key:zListingStar{star}")) + .await + .unwrap(); + } + if name == "shared" { + state + .db + .set_visibility_rule( + &repo.id, + "/", + crate::db::VisibilityMode::A, + &[reader.to_string()], + owner, + ) + .await + .unwrap(); + } + } + let app = list_repos_router(state); + for (caller, expected) in [ + (None, vec![("public-new", 3), ("public-old", 0)]), + ( + Some(owner), + vec![ + ("public-new", 3), + ("shared", 2), + ("private", 1), + ("public-old", 0), + ], + ), + ( + Some(reader), + vec![("public-new", 3), ("shared", 2), ("public-old", 0)], + ), + ] { + let mut rows = Vec::new(); + for offset in (0..expected.len()).step_by(2) { + let uri = format!("/api/v1/repos?owner={owner}&limit=2&offset={offset}"); + let request = match caller { + Some(did) => signed_request_as(did, Method::GET, &uri, Body::empty()), + None => anon_get(&uri), + }; + let response = app.clone().oneshot(request).await.unwrap(); + assert_eq!(response.status(), StatusCode::OK); + assert_eq!( + response.headers()["X-Total-Count"], + expected.len().to_string() + ); + rows.extend( + json_body(response) + .await + .as_array() + .unwrap() + .iter() + .cloned(), + ); + } + let actual: Vec<_> = rows + .iter() + .map(|repo| { + ( + repo["name"].as_str().unwrap(), + repo["star_count"].as_i64().unwrap(), + ) + }) + .collect(); + assert_eq!(actual, expected, "caller: {caller:?}"); + } + } + #[sqlx::test] async fn list_repos_hides_private_repo_and_count_from_anonymous(pool: PgPool) { let owner = "did:key:zLISTOWNERAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";