| 1 |
use crate::harness::TestHarness; |
| 2 |
|
| 3 |
#[tokio::test] |
| 4 |
async fn track_thread_happy_path() { |
| 5 |
let mut h = TestHarness::new().await; |
| 6 |
let user_id = h.login_as("tracker").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(user_id, comm_id, "member").await; |
| 10 |
|
| 11 |
let thread_id = h |
| 12 |
.create_thread_with_post(cat_id, user_id, "Track Me", "Content") |
| 13 |
.await; |
| 14 |
|
| 15 |
let thread_url = format!("/p/test/general/{}", thread_id); |
| 16 |
h.client.get(&thread_url).await; |
| 17 |
|
| 18 |
let track_url = format!("/p/test/general/{}/track", thread_id); |
| 19 |
let resp = h.client.post_form(&track_url, "").await; |
| 20 |
assert!(resp.status.is_redirection(), "Expected redirect, got {}", resp.status); |
| 21 |
|
| 22 |
let tracked = mt_db::queries::is_thread_tracked(&h.db, user_id, thread_id) |
| 23 |
.await |
| 24 |
.unwrap(); |
| 25 |
assert!(tracked, "Thread should be tracked"); |
| 26 |
} |
| 27 |
|
| 28 |
#[tokio::test] |
| 29 |
async fn untrack_thread() { |
| 30 |
let mut h = TestHarness::new().await; |
| 31 |
let user_id = h.login_as("untracker").await; |
| 32 |
let comm_id = h.create_community("Test", "test").await; |
| 33 |
let cat_id = h.create_category(comm_id, "General", "general").await; |
| 34 |
h.add_membership(user_id, comm_id, "member").await; |
| 35 |
|
| 36 |
let thread_id = h |
| 37 |
.create_thread_with_post(cat_id, user_id, "Untrack Me", "Content") |
| 38 |
.await; |
| 39 |
|
| 40 |
|
| 41 |
mt_db::mutations::track_thread(&h.db, user_id, thread_id).await.unwrap(); |
| 42 |
|
| 43 |
let thread_url = format!("/p/test/general/{}", thread_id); |
| 44 |
h.client.get(&thread_url).await; |
| 45 |
|
| 46 |
let untrack_url = format!("/p/test/general/{}/untrack", thread_id); |
| 47 |
let resp = h.client.post_form(&untrack_url, "").await; |
| 48 |
assert!(resp.status.is_redirection(), "Expected redirect, got {}", resp.status); |
| 49 |
|
| 50 |
let tracked = mt_db::queries::is_thread_tracked(&h.db, user_id, thread_id) |
| 51 |
.await |
| 52 |
.unwrap(); |
| 53 |
assert!(!tracked, "Thread should not be tracked"); |
| 54 |
} |
| 55 |
|
| 56 |
#[tokio::test] |
| 57 |
async fn read_position_updates_on_view() { |
| 58 |
let mut h = TestHarness::new().await; |
| 59 |
let user_id = h.login_as("readpos").await; |
| 60 |
let comm_id = h.create_community("Test", "test").await; |
| 61 |
let cat_id = h.create_category(comm_id, "General", "general").await; |
| 62 |
h.add_membership(user_id, comm_id, "member").await; |
| 63 |
|
| 64 |
let thread_id = h |
| 65 |
.create_thread_with_post(cat_id, user_id, "Read Pos", "First post") |
| 66 |
.await; |
| 67 |
|
| 68 |
|
| 69 |
mt_db::mutations::track_thread(&h.db, user_id, thread_id).await.unwrap(); |
| 70 |
|
| 71 |
|
| 72 |
let thread_url = format!("/p/test/general/{}", thread_id); |
| 73 |
h.client.get(&thread_url).await; |
| 74 |
|
| 75 |
|
| 76 |
let row: Option<(Option<uuid::Uuid>,)> = sqlx::query_as( |
| 77 |
"SELECT last_read_post_id FROM tracked_threads WHERE user_id = $1 AND thread_id = $2", |
| 78 |
) |
| 79 |
.bind(user_id) |
| 80 |
.bind(thread_id) |
| 81 |
.fetch_optional(&h.db) |
| 82 |
.await |
| 83 |
.unwrap(); |
| 84 |
assert!(row.is_some(), "Tracking row should exist"); |
| 85 |
assert!(row.unwrap().0.is_some(), "last_read_post_id should be set after viewing"); |
| 86 |
} |
| 87 |
|
| 88 |
#[tokio::test] |
| 89 |
async fn unread_count_tracking() { |
| 90 |
let mut h = TestHarness::new().await; |
| 91 |
let user_id = h.login_as("unreadcount").await; |
| 92 |
let comm_id = h.create_community("Test", "test").await; |
| 93 |
let cat_id = h.create_category(comm_id, "General", "general").await; |
| 94 |
h.add_membership(user_id, comm_id, "member").await; |
| 95 |
|
| 96 |
let thread_id = h |
| 97 |
.create_thread_with_post(cat_id, user_id, "Unread Count", "First post") |
| 98 |
.await; |
| 99 |
|
| 100 |
|
| 101 |
mt_db::mutations::track_thread(&h.db, user_id, thread_id).await.unwrap(); |
| 102 |
let thread_url = format!("/p/test/general/{}", thread_id); |
| 103 |
h.client.get(&thread_url).await; |
| 104 |
|
| 105 |
|
| 106 |
let other_id = uuid::Uuid::new_v4(); |
| 107 |
sqlx::query("INSERT INTO users (mnw_account_id, username, display_name) VALUES ($1, $2, $2)") |
| 108 |
.bind(other_id) |
| 109 |
.bind("poster2") |
| 110 |
.execute(&h.db) |
| 111 |
.await |
| 112 |
.unwrap(); |
| 113 |
mt_db::mutations::create_post(&h.db, thread_id, other_id, "New reply", "<p>New reply</p>") |
| 114 |
.await |
| 115 |
.unwrap(); |
| 116 |
|
| 117 |
|
| 118 |
let tracked = mt_db::queries::list_tracked_threads(&h.db, user_id).await.unwrap(); |
| 119 |
assert_eq!(tracked.len(), 1); |
| 120 |
assert!(tracked[0].unread_count > 0, "Should have unread posts"); |
| 121 |
} |
| 122 |
|
| 123 |
#[tokio::test] |
| 124 |
async fn stop_tracking_all() { |
| 125 |
let mut h = TestHarness::new().await; |
| 126 |
let user_id = h.login_as("stopall").await; |
| 127 |
let comm_id = h.create_community("Test", "test").await; |
| 128 |
let cat_id = h.create_category(comm_id, "General", "general").await; |
| 129 |
h.add_membership(user_id, comm_id, "member").await; |
| 130 |
|
| 131 |
let t1 = h.create_thread_with_post(cat_id, user_id, "Thread 1", "content").await; |
| 132 |
let t2 = h.create_thread_with_post(cat_id, user_id, "Thread 2", "content").await; |
| 133 |
|
| 134 |
mt_db::mutations::track_thread(&h.db, user_id, t1).await.unwrap(); |
| 135 |
mt_db::mutations::track_thread(&h.db, user_id, t2).await.unwrap(); |
| 136 |
|
| 137 |
h.client.get("/tracked").await; |
| 138 |
let resp = h.client.post_form("/tracked/stop-all", "").await; |
| 139 |
assert!(resp.status.is_redirection(), "Expected redirect, got {}", resp.status); |
| 140 |
|
| 141 |
let tracked = mt_db::queries::list_tracked_threads(&h.db, user_id).await.unwrap(); |
| 142 |
assert_eq!(tracked.len(), 0, "All tracked threads should be removed"); |
| 143 |
} |
| 144 |
|
| 145 |
#[tokio::test] |
| 146 |
async fn track_requires_login() { |
| 147 |
let mut h = TestHarness::new().await; |
| 148 |
let user_id = h.login_as("trackloginuser").await; |
| 149 |
let comm_id = h.create_community("Test", "test").await; |
| 150 |
let cat_id = h.create_category(comm_id, "General", "general").await; |
| 151 |
h.add_membership(user_id, comm_id, "member").await; |
| 152 |
|
| 153 |
let thread_id = h |
| 154 |
.create_thread_with_post(cat_id, user_id, "Login Track", "content") |
| 155 |
.await; |
| 156 |
|
| 157 |
|
| 158 |
let mut h2 = TestHarness::new().await; |
| 159 |
h2.client.get("/").await; |
| 160 |
|
| 161 |
let track_url = format!("/p/test/general/{}/track", thread_id); |
| 162 |
let resp = h2.client.post_form(&track_url, "").await; |
| 163 |
assert!( |
| 164 |
resp.status.is_redirection(), |
| 165 |
"Expected redirect to login, got {}", |
| 166 |
resp.status |
| 167 |
); |
| 168 |
} |
| 169 |
|