| 1 |
use crate::harness::TestHarness; |
| 2 |
|
| 3 |
#[tokio::test] |
| 4 |
async fn link_preview_renders_in_thread() { |
| 5 |
let mut h = TestHarness::new().await; |
| 6 |
let author_id = h.login_as("lpauthor").await; |
| 7 |
let comm_id = h.create_community("Test", "test").await; |
| 8 |
let cat_id = h.create_category(comm_id, "General", "general").await; |
| 9 |
h.add_membership(author_id, comm_id, "member").await; |
| 10 |
|
| 11 |
let thread_id = h |
| 12 |
.create_thread_with_post(cat_id, author_id, "Link Test", "Content") |
| 13 |
.await; |
| 14 |
|
| 15 |
let posts = mt_db::queries::list_posts_in_thread(&h.db, thread_id) |
| 16 |
.await |
| 17 |
.unwrap(); |
| 18 |
let post_id = posts[0].id; |
| 19 |
|
| 20 |
|
| 21 |
mt_db::mutations::insert_link_preview( |
| 22 |
&h.db, |
| 23 |
post_id, |
| 24 |
"https://example.com", |
| 25 |
Some("Example Site"), |
| 26 |
Some("An example website"), |
| 27 |
) |
| 28 |
.await |
| 29 |
.unwrap(); |
| 30 |
|
| 31 |
let thread_url = format!("/p/test/general/{}", thread_id); |
| 32 |
let resp = h.client.get(&thread_url).await; |
| 33 |
|
| 34 |
assert!( |
| 35 |
resp.text.contains("link-preview-card"), |
| 36 |
"Expected link preview card in thread HTML" |
| 37 |
); |
| 38 |
assert!( |
| 39 |
resp.text.contains("Example Site"), |
| 40 |
"Expected preview title" |
| 41 |
); |
| 42 |
assert!( |
| 43 |
resp.text.contains("An example website"), |
| 44 |
"Expected preview description" |
| 45 |
); |
| 46 |
assert!( |
| 47 |
resp.text.contains("https://example.com"), |
| 48 |
"Expected preview URL" |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
#[tokio::test] |
| 53 |
async fn post_without_links_has_no_previews() { |
| 54 |
let mut h = TestHarness::new().await; |
| 55 |
let author_id = h.login_as("nolinkauthor").await; |
| 56 |
let comm_id = h.create_community("Test", "test").await; |
| 57 |
let cat_id = h.create_category(comm_id, "General", "general").await; |
| 58 |
h.add_membership(author_id, comm_id, "member").await; |
| 59 |
|
| 60 |
let thread_id = h |
| 61 |
.create_thread_with_post(cat_id, author_id, "No Links", "Just plain text") |
| 62 |
.await; |
| 63 |
|
| 64 |
let posts = mt_db::queries::list_posts_in_thread(&h.db, thread_id) |
| 65 |
.await |
| 66 |
.unwrap(); |
| 67 |
let post_id = posts[0].id; |
| 68 |
|
| 69 |
let previews = mt_db::queries::list_link_previews_for_posts(&h.db, &[post_id]) |
| 70 |
.await |
| 71 |
.unwrap(); |
| 72 |
assert!(previews.is_empty(), "No link previews for plain text post"); |
| 73 |
} |
| 74 |
|
| 75 |
#[tokio::test] |
| 76 |
async fn multiple_previews_for_post() { |
| 77 |
let mut h = TestHarness::new().await; |
| 78 |
let author_id = h.login_as("multilpauthor").await; |
| 79 |
let comm_id = h.create_community("Test", "test").await; |
| 80 |
let cat_id = h.create_category(comm_id, "General", "general").await; |
| 81 |
h.add_membership(author_id, comm_id, "member").await; |
| 82 |
|
| 83 |
let thread_id = h |
| 84 |
.create_thread_with_post(cat_id, author_id, "Multi Link", "Content") |
| 85 |
.await; |
| 86 |
|
| 87 |
let posts = mt_db::queries::list_posts_in_thread(&h.db, thread_id) |
| 88 |
.await |
| 89 |
.unwrap(); |
| 90 |
let post_id = posts[0].id; |
| 91 |
|
| 92 |
|
| 93 |
for i in 1..=3 { |
| 94 |
mt_db::mutations::insert_link_preview( |
| 95 |
&h.db, |
| 96 |
post_id, |
| 97 |
&format!("https://example{i}.com"), |
| 98 |
Some(&format!("Site {i}")), |
| 99 |
None, |
| 100 |
) |
| 101 |
.await |
| 102 |
.unwrap(); |
| 103 |
} |
| 104 |
|
| 105 |
let thread_url = format!("/p/test/general/{}", thread_id); |
| 106 |
let resp = h.client.get(&thread_url).await; |
| 107 |
|
| 108 |
for i in 1..=3 { |
| 109 |
assert!( |
| 110 |
resp.text.contains(&format!("Site {i}")), |
| 111 |
"Expected preview title 'Site {i}' in HTML" |
| 112 |
); |
| 113 |
assert!( |
| 114 |
resp.text.contains(&format!("https://example{i}.com")), |
| 115 |
"Expected preview URL 'https://example{i}.com' in HTML" |
| 116 |
); |
| 117 |
} |
| 118 |
} |
| 119 |
|