use crate::harness::TestHarness; #[tokio::test] async fn link_preview_renders_in_thread() { let mut h = TestHarness::new().await; let author_id = h.login_as("lpauthor").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; let thread_id = h .create_thread_with_post(cat_id, author_id, "Link Test", "Content") .await; let posts = mt_db::queries::list_posts_in_thread(&h.db, thread_id) .await .unwrap(); let post_id = posts[0].id; // Insert a preview directly via DB mutation mt_db::mutations::insert_link_preview( &h.db, post_id, "https://example.com", Some("Example Site"), Some("An example website"), ) .await .unwrap(); let thread_url = format!("/p/test/general/{}", thread_id); let resp = h.client.get(&thread_url).await; assert!( resp.text.contains("link-preview-card"), "Expected link preview card in thread HTML" ); assert!( resp.text.contains("Example Site"), "Expected preview title" ); assert!( resp.text.contains("An example website"), "Expected preview description" ); assert!( resp.text.contains("https://example.com"), "Expected preview URL" ); } #[tokio::test] async fn post_without_links_has_no_previews() { let mut h = TestHarness::new().await; let author_id = h.login_as("nolinkauthor").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; let thread_id = h .create_thread_with_post(cat_id, author_id, "No Links", "Just plain text") .await; let posts = mt_db::queries::list_posts_in_thread(&h.db, thread_id) .await .unwrap(); let post_id = posts[0].id; let previews = mt_db::queries::list_link_previews_for_posts(&h.db, &[post_id]) .await .unwrap(); assert!(previews.is_empty(), "No link previews for plain text post"); } #[tokio::test] async fn multiple_previews_for_post() { let mut h = TestHarness::new().await; let author_id = h.login_as("multilpauthor").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; let thread_id = h .create_thread_with_post(cat_id, author_id, "Multi Link", "Content") .await; let posts = mt_db::queries::list_posts_in_thread(&h.db, thread_id) .await .unwrap(); let post_id = posts[0].id; // Insert 3 previews for i in 1..=3 { mt_db::mutations::insert_link_preview( &h.db, post_id, &format!("https://example{i}.com"), Some(&format!("Site {i}")), None, ) .await .unwrap(); } let thread_url = format!("/p/test/general/{}", thread_id); let resp = h.client.get(&thread_url).await; for i in 1..=3 { assert!( resp.text.contains(&format!("Site {i}")), "Expected preview title 'Site {i}' in HTML" ); assert!( resp.text.contains(&format!("https://example{i}.com")), "Expected preview URL 'https://example{i}.com' in HTML" ); } }