Skip to main content

max / makenotwork

3.5 KB · 116 lines History Blame Raw
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 // Insert a preview directly via DB mutation
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!(resp.text.contains("Example Site"), "Expected preview title");
39 assert!(
40 resp.text.contains("An example website"),
41 "Expected preview description"
42 );
43 assert!(
44 resp.text.contains("https://example.com"),
45 "Expected preview URL"
46 );
47 }
48
49 #[tokio::test]
50 async fn post_without_links_has_no_previews() {
51 let mut h = TestHarness::new().await;
52 let author_id = h.login_as("nolinkauthor").await;
53 let comm_id = h.create_community("Test", "test").await;
54 let cat_id = h.create_category(comm_id, "General", "general").await;
55 h.add_membership(author_id, comm_id, "member").await;
56
57 let thread_id = h
58 .create_thread_with_post(cat_id, author_id, "No Links", "Just plain text")
59 .await;
60
61 let posts = mt_db::queries::list_posts_in_thread(&h.db, thread_id)
62 .await
63 .unwrap();
64 let post_id = posts[0].id;
65
66 let previews = mt_db::queries::list_link_previews_for_posts(&h.db, &[post_id])
67 .await
68 .unwrap();
69 assert!(previews.is_empty(), "No link previews for plain text post");
70 }
71
72 #[tokio::test]
73 async fn multiple_previews_for_post() {
74 let mut h = TestHarness::new().await;
75 let author_id = h.login_as("multilpauthor").await;
76 let comm_id = h.create_community("Test", "test").await;
77 let cat_id = h.create_category(comm_id, "General", "general").await;
78 h.add_membership(author_id, comm_id, "member").await;
79
80 let thread_id = h
81 .create_thread_with_post(cat_id, author_id, "Multi Link", "Content")
82 .await;
83
84 let posts = mt_db::queries::list_posts_in_thread(&h.db, thread_id)
85 .await
86 .unwrap();
87 let post_id = posts[0].id;
88
89 // Insert 3 previews
90 for i in 1..=3 {
91 mt_db::mutations::insert_link_preview(
92 &h.db,
93 post_id,
94 &format!("https://example{i}.com"),
95 Some(&format!("Site {i}")),
96 None,
97 )
98 .await
99 .unwrap();
100 }
101
102 let thread_url = format!("/p/test/general/{thread_id}");
103 let resp = h.client.get(&thread_url).await;
104
105 for i in 1..=3 {
106 assert!(
107 resp.text.contains(&format!("Site {i}")),
108 "Expected preview title 'Site {i}' in HTML"
109 );
110 assert!(
111 resp.text.contains(&format!("https://example{i}.com")),
112 "Expected preview URL 'https://example{i}.com' in HTML"
113 );
114 }
115 }
116