use crate::harness::TestHarness; #[tokio::test] async fn search_by_thread_title() { let mut h = TestHarness::new().await; let user_id = h.login_as("searchtitle").await; let comm_id = h.create_community("Test", "test").await; let cat_id = h.create_category(comm_id, "General", "general").await; h.add_membership(user_id, comm_id, "member").await; h.create_thread_with_post(cat_id, user_id, "Unique Flamingo Discussion", "body text").await; h.create_thread_with_post(cat_id, user_id, "Other Thread", "nothing here").await; let resp = h.client.get("/search?q=flamingo").await; assert_eq!(resp.status, 200); assert!( resp.text.contains("Unique Flamingo Discussion"), "Expected thread title in search results. Body: {}", &resp.text ); assert!( !resp.text.contains("Other Thread"), "Non-matching thread should not appear" ); } #[tokio::test] async fn search_body_content_match() { let mut h = TestHarness::new().await; let user_id = h.login_as("searchbody").await; let comm_id = h.create_community("Test", "test").await; let cat_id = h.create_category(comm_id, "General", "general").await; h.add_membership(user_id, comm_id, "member").await; h.create_thread_with_post(cat_id, user_id, "Generic Title", "zygomorphic flower patterns").await; let resp = h.client.get("/search?q=zygomorphic").await; assert_eq!(resp.status, 200); assert!( resp.text.contains("Generic Title"), "Thread with matching body should appear. Body: {}", &resp.text ); } #[tokio::test] async fn search_scoped_to_community() { let mut h = TestHarness::new().await; let user_id = h.login_as("searchscoped").await; let comm1 = h.create_community("Alpha", "alpha").await; let cat1 = h.create_category(comm1, "General", "general").await; h.add_membership(user_id, comm1, "member").await; let comm2 = h.create_community("Beta", "beta").await; let cat2 = h.create_category(comm2, "General", "general").await; h.add_membership(user_id, comm2, "member").await; h.create_thread_with_post(cat1, user_id, "Shared Keyword Xylophone", "content").await; h.create_thread_with_post(cat2, user_id, "Shared Keyword Xylophone Two", "content").await; // Scoped to alpha let resp = h.client.get("/search?q=xylophone&scope=alpha").await; assert_eq!(resp.status, 200); assert!(resp.text.contains("Shared Keyword Xylophone"), "Alpha result should appear"); assert!(!resp.text.contains("Xylophone Two"), "Beta result should NOT appear in scoped search"); // Global let resp = h.client.get("/search?q=xylophone").await; assert!(resp.text.contains("Xylophone Two"), "Beta result should appear in global search"); } #[tokio::test] async fn search_empty_query_returns_nothing() { let mut h = TestHarness::new().await; let user_id = h.login_as("searchempty").await; let comm_id = h.create_community("Test", "test").await; let cat_id = h.create_category(comm_id, "General", "general").await; h.add_membership(user_id, comm_id, "member").await; h.create_thread_with_post(cat_id, user_id, "Should Not Appear", "content").await; let resp = h.client.get("/search?q=").await; assert_eq!(resp.status, 200); assert!( !resp.text.contains("Should Not Appear"), "Empty query should return no results" ); } #[tokio::test] async fn search_deleted_thread_excluded() { let mut h = TestHarness::new().await; let user_id = h.login_as("searchdeleted").await; let comm_id = h.create_community("Test", "test").await; let cat_id = h.create_category(comm_id, "General", "general").await; h.add_membership(user_id, comm_id, "member").await; let thread_id = h.create_thread_with_post(cat_id, user_id, "Ephemeral Jellyfish", "content").await; // Soft-delete the thread mt_db::mutations::soft_delete_thread(&h.db, thread_id).await.unwrap(); let resp = h.client.get("/search?q=jellyfish").await; assert_eq!(resp.status, 200); assert!( !resp.text.contains("Ephemeral Jellyfish"), "Deleted thread should not appear in search" ); }