use crate::harness::TestHarness; #[tokio::test] async fn mention_renders_as_profile_link() { let mut h = TestHarness::new().await; let author_id = h.login_as("mentionauth").await; let comm_id = h.create_community("Test", "test").await; let _cat_id = h.create_category(comm_id, "General", "general").await; h.add_membership(author_id, comm_id, "member").await; // Create the mentioned user via direct SQL (so author stays logged in) let mentioned_id = uuid::Uuid::new_v4(); sqlx::query("INSERT INTO users (mnw_account_id, username, display_name) VALUES ($1, $2, $2)") .bind(mentioned_id) .bind("mentionee") .execute(&h.db) .await .unwrap(); h.add_membership(mentioned_id, comm_id, "member").await; // GET page for CSRF h.client.get("/p/test/general/new").await; // Create thread via HTTP with @mention let resp = h.client.post_form( "/p/test/general/new", "title=Mention+Test&body=Hello+%40mentionee+welcome", ).await; assert!(resp.status.is_redirection(), "Expected redirect, got {}", resp.status); // Find the thread and view it let threads = mt_db::queries::list_threads_in_category_paginated( &h.db, "test", "general", 10, 0, ).await.unwrap(); assert!(!threads.is_empty()); let thread_id = threads[0].id; let thread_url = format!("/p/test/general/{}", thread_id); let resp = h.client.get(&thread_url).await; assert!( resp.text.contains("/p/test/u/mentionee"), "Expected mention to render as profile link. Body: {}", &resp.text[resp.text.find("post-body").unwrap_or(0)..].chars().take(500).collect::() ); } #[tokio::test] async fn mention_stored_in_db() { let mut h = TestHarness::new().await; let author_id = h.login_as("mentiondbauth").await; let comm_id = h.create_community("Test", "test").await; let _cat_id = h.create_category(comm_id, "General", "general").await; h.add_membership(author_id, comm_id, "member").await; // Create mentioned user via direct SQL let mentioned_id = uuid::Uuid::new_v4(); sqlx::query("INSERT INTO users (mnw_account_id, username, display_name) VALUES ($1, $2, $2)") .bind(mentioned_id) .bind("mentiondbuser") .execute(&h.db) .await .unwrap(); h.add_membership(mentioned_id, comm_id, "member").await; h.client.get("/p/test/general/new").await; h.client.post_form( "/p/test/general/new", "title=DB+Test&body=Hey+%40mentiondbuser", ).await; // Check DB for mention row let threads = mt_db::queries::list_threads_in_category_paginated( &h.db, "test", "general", 10, 0, ).await.unwrap(); let thread_id = threads[0].id; let posts = mt_db::queries::list_posts_in_thread(&h.db, thread_id).await.unwrap(); let post_id = posts[0].id; let count: i64 = sqlx::query_scalar( "SELECT COUNT(*) FROM post_mentions WHERE post_id = $1 AND mentioned_user_id = $2", ) .bind(post_id) .bind(mentioned_id) .fetch_one(&h.db) .await .unwrap(); assert_eq!(count, 1, "Expected 1 mention row in DB"); } #[tokio::test] async fn self_mention_not_stored() { let mut h = TestHarness::new().await; let author_id = h.login_as("selfmentioner").await; let comm_id = h.create_community("Test", "test").await; let _cat_id = h.create_category(comm_id, "General", "general").await; h.add_membership(author_id, comm_id, "member").await; h.client.get("/p/test/general/new").await; h.client.post_form( "/p/test/general/new", "title=Self+Mention&body=I+am+%40selfmentioner", ).await; let threads = mt_db::queries::list_threads_in_category_paginated( &h.db, "test", "general", 10, 0, ).await.unwrap(); let thread_id = threads[0].id; let posts = mt_db::queries::list_posts_in_thread(&h.db, thread_id).await.unwrap(); let post_id = posts[0].id; let count: i64 = sqlx::query_scalar( "SELECT COUNT(*) FROM post_mentions WHERE post_id = $1", ) .bind(post_id) .fetch_one(&h.db) .await .unwrap(); assert_eq!(count, 0, "Self-mention should not be stored in DB"); } #[tokio::test] async fn unknown_username_left_as_text() { let mut h = TestHarness::new().await; let author_id = h.login_as("unknownmentionauth").await; let comm_id = h.create_community("Test", "test").await; let _cat_id = h.create_category(comm_id, "General", "general").await; h.add_membership(author_id, comm_id, "member").await; h.client.get("/p/test/general/new").await; h.client.post_form( "/p/test/general/new", "title=Unknown+Mention&body=Hello+%40nonexistent", ).await; let threads = mt_db::queries::list_threads_in_category_paginated( &h.db, "test", "general", 10, 0, ).await.unwrap(); let thread_id = threads[0].id; let thread_url = format!("/p/test/general/{}", thread_id); let resp = h.client.get(&thread_url).await; // Should contain @nonexistent as plain text, not as a link assert!( resp.text.contains("@nonexistent"), "Unknown mention should appear as plain text" ); assert!( !resp.text.contains("/p/test/u/nonexistent"), "Unknown mention should NOT be a link" ); // No DB rows let posts = mt_db::queries::list_posts_in_thread(&h.db, thread_id).await.unwrap(); let post_id = posts[0].id; let count: i64 = sqlx::query_scalar( "SELECT COUNT(*) FROM post_mentions WHERE post_id = $1", ) .bind(post_id) .fetch_one(&h.db) .await .unwrap(); assert_eq!(count, 0, "No mention rows for unknown username"); } #[tokio::test] async fn mention_indicator_on_category_listing() { let mut h = TestHarness::new().await; let author_id = h.login_as("catmentionauth").await; let comm_id = h.create_community("Test", "test").await; let _cat_id = h.create_category(comm_id, "General", "general").await; h.add_membership(author_id, comm_id, "member").await; // Create the mentioned user via direct SQL let mentioned_id = uuid::Uuid::new_v4(); sqlx::query("INSERT INTO users (mnw_account_id, username, display_name) VALUES ($1, $2, $2)") .bind(mentioned_id) .bind("catmentionee") .execute(&h.db) .await .unwrap(); h.add_membership(mentioned_id, comm_id, "member").await; // Author creates thread mentioning the other user h.client.get("/p/test/general/new").await; let resp = h.client.post_form( "/p/test/general/new", "title=Category+Mention+Test&body=Hey+%40catmentionee+check+this", ).await; assert!(resp.status.is_redirection(), "Expected redirect, got {}", resp.status); // Re-login as the mentioned user and view the category h.client.get("/").await; let body = serde_json::json!({ "user_id": mentioned_id.to_string(), "username": "catmentionee", }); h.client.post_json("/_test/login", &body.to_string()).await; let resp = h.client.get("/p/test/general").await; assert!( resp.text.contains("badge-mention"), "Expected badge-mention class in category listing. Body snippet: {}", &resp.text[resp.text.find("data-table").unwrap_or(0)..].chars().take(800).collect::() ); assert!( resp.text.contains("mentioned"), "Expected 'mentioned' class on tr element" ); } #[tokio::test] async fn mention_indicator_on_tracked_page() { let mut h = TestHarness::new().await; let author_id = h.login_as("trackmentionauth").await; let comm_id = h.create_community("Test", "test").await; let _cat_id = h.create_category(comm_id, "General", "general").await; h.add_membership(author_id, comm_id, "member").await; // Create the mentioned user let mentioned_id = uuid::Uuid::new_v4(); sqlx::query("INSERT INTO users (mnw_account_id, username, display_name) VALUES ($1, $2, $2)") .bind(mentioned_id) .bind("trackmentionee") .execute(&h.db) .await .unwrap(); h.add_membership(mentioned_id, comm_id, "member").await; // Author creates thread mentioning user B h.client.get("/p/test/general/new").await; let resp = h.client.post_form( "/p/test/general/new", "title=Track+Mention+Test&body=Hello+%40trackmentionee", ).await; assert!(resp.status.is_redirection(), "Expected redirect, got {}", resp.status); // Find thread ID let threads = mt_db::queries::list_threads_in_category_paginated( &h.db, "test", "general", 10, 0, ).await.unwrap(); let thread_id = threads[0].id; // User B tracks the thread mt_db::mutations::track_thread(&h.db, mentioned_id, thread_id).await.unwrap(); // Log in as mentioned user let body = serde_json::json!({ "user_id": mentioned_id.to_string(), "username": "trackmentionee", }); h.client.post_json("/_test/login", &body.to_string()).await; // View tracked page let resp = h.client.get("/tracked").await; assert!( resp.text.contains("badge-mention"), "Expected badge-mention in tracked page. Body snippet: {}", &resp.text[resp.text.find("data-table").unwrap_or(0)..].chars().take(800).collect::() ); assert!( resp.text.contains("mentioned"), "Expected 'mentioned' class on tr element in tracked page" ); }