use dialtone_common::containers::credentialed_actor::CredentialedActor; use dialtone_common::rest::actors::actor_reference::ActorReferenceType; use dialtone_common::utils::make_acct::make_acct; use dialtone_sqlx::db::actor::page_by_reference::page_actors_by_reference; use dialtone_sqlx::db::actor_reference::insert::insert_actor_reference; use dialtone_sqlx::db::user_principal::create_user; use dialtone_test_util::create_actor::create_actor_for_user_tst_utl; use dialtone_test_util::{test_action, test_pg}; use sqlx::{Pool, Postgres}; use std::{thread, time::Duration}; #[tokio::test] async fn page_actors_by_reference_test() { test_pg::test_pg(move |pool| async move { let user_name = "testuser"; let host_name = "example.net"; let acct = make_acct(user_name, host_name); create_user(&pool, &acct, "supersecret").await.unwrap(); let subject_actor = create_actor_for_user_tst_utl(&pool, "subject", host_name, &acct, false).await; let subject_actor_id = subject_actor.owned_actor.ap.id; let likes_actors = create_many_actors(&pool, host_name, "liked", 10).await; for n in 0..likes_actors.len() { insert_actor_reference( &pool, &subject_actor_id, &likes_actors[n].owned_actor.ap.id, &ActorReferenceType::Likes, ) .await .unwrap(); } create_many_actors(&pool, host_name, "indifferent", 10).await; let liked_page1_result = page_actors_by_reference( &pool, None, None, 5, &subject_actor_id, &ActorReferenceType::Likes, ) .await; test_action!(liked_page1_result); let liked_page1 = liked_page1_result.unwrap().unwrap(); assert_eq!(liked_page1.page_values.len(), 5); let liked_page2_result = page_actors_by_reference( &pool, None, Some(&liked_page1.max_modified_at), 5, &subject_actor_id, &ActorReferenceType::Likes, ) .await; test_action!(liked_page2_result); let liked_page2 = liked_page2_result.unwrap().unwrap(); assert_eq!(liked_page2.page_values.len(), 5); let liked_page3_result = page_actors_by_reference( &pool, None, Some(&liked_page2.max_modified_at), 5, &subject_actor_id, &ActorReferenceType::Likes, ) .await; test_action!(liked_page3_result); let liked_page3 = liked_page3_result.unwrap(); assert!(liked_page3.is_none()); }) .await; } async fn create_many_actors( pool: &Pool, host_name: &str, user_name: &str, num_actors: i32, ) -> Vec { let acct = make_acct(user_name, host_name); let action = create_user(pool, &acct, "supersecret").await; assert!(action.is_ok()); let mut actors = Vec::new(); for n in 1..=num_actors { let pun = format!("testactor_{}_of_{}", n, user_name); let actor = create_actor_for_user_tst_utl(pool, &pun, host_name, &acct, false).await; actors.push(actor); // insure 1 millisecond separation thread::sleep(Duration::from_millis(1)); } actors }