relay filter/appview bootstrap
1mod common;
2
3use chrono::Utc;
4use serde_json::json;
5use prism::records::{
6 NewAccount, NewRecord, CHANNEL_COLLECTION, INVITE_COLLECTION, LATTICE_COLLECTION,
7 MEMBERSHIP_COLLECTION, SHARD_COLLECTION,
8};
9
10async fn insert_test_account(pool: &sqlx::PgPool, did: &str) {
11 prism::db::upsert_account(
12 pool,
13 &NewAccount {
14 did: did.to_string(),
15 handle: did.to_string(),
16 created_at: Utc::now(),
17 },
18 )
19 .await
20 .unwrap();
21}
22
23#[tokio::test]
24async fn test_lattice_insert() {
25 let _ = common::base_url().await;
26 let pool = common::get_db_pool().await;
27
28 let test_did = "did:plc:tap_lattice_test";
29 insert_test_account(&pool, test_did).await;
30
31 let uri = format!("at://{}/systems.gmstn.development.lattice/abc123", test_did);
32 let now = Utc::now();
33
34 prism::db::insert_record(
35 &pool,
36 &NewRecord {
37 uri: uri.clone(),
38 cid: "bafylatticetest001".to_string(),
39 collection: LATTICE_COLLECTION.to_string(),
40 creator_did: test_did.to_string(),
41 created_at: now,
42 indexed_at: now,
43 data: json!({"description": "Test lattice"}),
44 target_did: None,
45 ref_cids: vec![],
46 },
47 )
48 .await
49 .unwrap();
50
51 let records = prism::db::list_records_by_creator(&pool, LATTICE_COLLECTION, test_did, None, 10)
52 .await
53 .unwrap();
54
55 assert!(!records.is_empty());
56 assert_eq!(records[0].uri, uri);
57 assert_eq!(records[0].cid, "bafylatticetest001");
58 assert_eq!(records[0].creator_did, test_did);
59}
60
61#[tokio::test]
62async fn test_shard_insert() {
63 let _ = common::base_url().await;
64 let pool = common::get_db_pool().await;
65
66 let test_did = "did:plc:tap_shard_test";
67 insert_test_account(&pool, test_did).await;
68
69 let uri = format!("at://{}/systems.gmstn.development.shard/xyz789", test_did);
70 let now = Utc::now();
71
72 prism::db::insert_record(
73 &pool,
74 &NewRecord {
75 uri: uri.clone(),
76 cid: "bafyshardtest001".to_string(),
77 collection: SHARD_COLLECTION.to_string(),
78 creator_did: test_did.to_string(),
79 created_at: now,
80 indexed_at: now,
81 data: json!({"description": "Test shard"}),
82 target_did: None,
83 ref_cids: vec![],
84 },
85 )
86 .await
87 .unwrap();
88
89 let records = prism::db::list_records_by_creator(&pool, SHARD_COLLECTION, test_did, None, 10)
90 .await
91 .unwrap();
92
93 assert!(!records.is_empty());
94 assert_eq!(records[0].uri, uri);
95 assert_eq!(records[0].cid, "bafyshardtest001");
96 assert_eq!(records[0].creator_did, test_did);
97}
98
99#[tokio::test]
100async fn test_channel_insert() {
101 let _ = common::base_url().await;
102 let pool = common::get_db_pool().await;
103
104 let test_did = "did:plc:tap_channel_insert";
105 insert_test_account(&pool, test_did).await;
106
107 let uri = format!("at://{}/systems.gmstn.development.channel/chan001", test_did);
108 let now = Utc::now();
109
110 prism::db::insert_record(
111 &pool,
112 &NewRecord {
113 uri: uri.clone(),
114 cid: "bafychanneltest001".to_string(),
115 collection: CHANNEL_COLLECTION.to_string(),
116 creator_did: test_did.to_string(),
117 created_at: now,
118 indexed_at: now,
119 data: json!({"name": "Test Channel", "topic": "Test Topic"}),
120 target_did: None,
121 ref_cids: vec![],
122 },
123 )
124 .await
125 .unwrap();
126
127 let records = prism::db::list_records_by_creator(&pool, CHANNEL_COLLECTION, test_did, None, 10)
128 .await
129 .unwrap();
130
131 assert_eq!(records.len(), 1);
132 assert_eq!(records[0].cid, "bafychanneltest001");
133 assert_eq!(records[0].uri, uri);
134}
135
136#[tokio::test]
137async fn test_invite_insert() {
138 let _ = common::base_url().await;
139 let pool = common::get_db_pool().await;
140
141 let creator_did = "did:plc:tap_invite_creator";
142 let recipient_did = "did:plc:tap_invite_recipient";
143 insert_test_account(&pool, creator_did).await;
144 insert_test_account(&pool, recipient_did).await;
145
146 let uri = format!("at://{}/systems.gmstn.development.channel.invite/inv001", creator_did);
147 let now = Utc::now();
148
149 prism::db::insert_record(
150 &pool,
151 &NewRecord {
152 uri: uri.clone(),
153 cid: "bafyinvitetest001".to_string(),
154 collection: INVITE_COLLECTION.to_string(),
155 creator_did: creator_did.to_string(),
156 created_at: now,
157 indexed_at: now,
158 data: json!({"recipient": recipient_did}),
159 target_did: Some(recipient_did.to_string()),
160 ref_cids: vec!["bafychannel001".to_string()],
161 },
162 )
163 .await
164 .unwrap();
165
166 let records = prism::db::list_records_by_target(&pool, INVITE_COLLECTION, recipient_did, None, 10)
167 .await
168 .unwrap();
169
170 assert_eq!(records.len(), 1);
171 assert_eq!(records[0].cid, "bafyinvitetest001");
172 assert_eq!(records[0].ref_cids, vec!["bafychannel001".to_string()]);
173}
174
175#[tokio::test]
176async fn test_membership_insert() {
177 let _ = common::base_url().await;
178 let pool = common::get_db_pool().await;
179
180 let recipient_did = "did:plc:tap_membership_recipient";
181 insert_test_account(&pool, recipient_did).await;
182
183 let uri = format!(
184 "at://{}/systems.gmstn.development.channel.membership/mem001",
185 recipient_did
186 );
187 let now = Utc::now();
188
189 prism::db::insert_record(
190 &pool,
191 &NewRecord {
192 uri: uri.clone(),
193 cid: "bafymembertest001".to_string(),
194 collection: MEMBERSHIP_COLLECTION.to_string(),
195 creator_did: recipient_did.to_string(),
196 created_at: now,
197 indexed_at: now,
198 data: json!({"state": "accepted"}),
199 target_did: Some(recipient_did.to_string()),
200 ref_cids: vec!["bafychannel001".to_string(), "bafyinvite001".to_string()],
201 },
202 )
203 .await
204 .unwrap();
205
206 let records = prism::db::list_records_by_target(&pool, MEMBERSHIP_COLLECTION, recipient_did, None, 10)
207 .await
208 .unwrap();
209
210 assert_eq!(records.len(), 1);
211 assert_eq!(records[0].cid, "bafymembertest001");
212 assert_eq!(records[0].data["state"], "accepted");
213}
214
215#[tokio::test]
216async fn test_account_upsert_is_idempotent() {
217 let _ = common::base_url().await;
218 let pool = common::get_db_pool().await;
219
220 let test_did = "did:plc:tap_account_idempotent";
221 let now = Utc::now();
222
223 prism::db::upsert_account(
224 &pool,
225 &prism::records::NewAccount {
226 did: test_did.to_string(),
227 handle: "first_handle".to_string(),
228 created_at: now,
229 },
230 )
231 .await
232 .unwrap();
233
234 prism::db::upsert_account(
235 &pool,
236 &prism::records::NewAccount {
237 did: test_did.to_string(),
238 handle: "second_handle".to_string(),
239 created_at: now,
240 },
241 )
242 .await
243 .unwrap();
244
245 let account: Option<(String,)> =
246 sqlx::query_as("SELECT handle FROM account WHERE did = $1")
247 .bind(test_did)
248 .fetch_optional(&pool)
249 .await
250 .unwrap();
251
252 assert!(account.is_some());
253 assert_eq!(account.unwrap().0, "first_handle");
254}
255
256#[tokio::test]
257async fn test_record_upsert_updates_data() {
258 let _ = common::base_url().await;
259 let pool = common::get_db_pool().await;
260
261 let test_did = "did:plc:tap_record_upsert";
262 insert_test_account(&pool, test_did).await;
263
264 let uri = format!("at://{}/systems.gmstn.development.channel/upsert001", test_did);
265 let now = Utc::now();
266
267 prism::db::insert_record(
268 &pool,
269 &NewRecord {
270 uri: uri.clone(),
271 cid: "bafyupsert001".to_string(),
272 collection: CHANNEL_COLLECTION.to_string(),
273 creator_did: test_did.to_string(),
274 created_at: now,
275 indexed_at: now,
276 data: json!({"name": "Original Name"}),
277 target_did: None,
278 ref_cids: vec![],
279 },
280 )
281 .await
282 .unwrap();
283
284 prism::db::insert_record(
285 &pool,
286 &NewRecord {
287 uri: uri.clone(),
288 cid: "bafyupsert002".to_string(),
289 collection: CHANNEL_COLLECTION.to_string(),
290 creator_did: test_did.to_string(),
291 created_at: now,
292 indexed_at: now,
293 data: json!({"name": "Updated Name"}),
294 target_did: None,
295 ref_cids: vec![],
296 },
297 )
298 .await
299 .unwrap();
300
301 let records = prism::db::list_records_by_creator(&pool, CHANNEL_COLLECTION, test_did, None, 10)
302 .await
303 .unwrap();
304
305 assert_eq!(records.len(), 1);
306 assert_eq!(records[0].cid, "bafyupsert002");
307 assert_eq!(records[0].data["name"], "Updated Name");
308}