1 jest.setTimeout(180000);
2 import { None, Some } from "@sniptt/monads";
3 import { CommentView } from "lemmy-js-client";
4 import { PostResponse } from "lemmy-js-client";
36 let postRes: PostResponse;
38 beforeAll(async () => {
41 await followBeta(alpha);
42 await followBeta(gamma);
43 let betaCommunity = (await resolveBetaCommunity(alpha)).community;
44 postRes = await createPost(alpha, betaCommunity.unwrap().community.id);
47 afterAll(async () => {
51 function assertCommentFederation(
52 commentOne: CommentView,
53 commentTwo: CommentView
55 expect(commentOne.comment.ap_id).toBe(commentOne.comment.ap_id);
56 expect(commentOne.comment.content).toBe(commentTwo.comment.content);
57 expect(commentOne.creator.name).toBe(commentTwo.creator.name);
58 expect(commentOne.community.actor_id).toBe(commentTwo.community.actor_id);
59 expect(commentOne.comment.published).toBe(commentTwo.comment.published);
60 expect(commentOne.comment.updated).toBe(commentOne.comment.updated);
61 expect(commentOne.comment.deleted).toBe(commentOne.comment.deleted);
62 expect(commentOne.comment.removed).toBe(commentOne.comment.removed);
65 test("Create a comment", async () => {
66 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
67 expect(commentRes.comment_view.comment.content).toBeDefined();
68 expect(commentRes.comment_view.community.local).toBe(false);
69 expect(commentRes.comment_view.creator.local).toBe(true);
70 expect(commentRes.comment_view.counts.score).toBe(1);
72 // Make sure that comment is liked on beta
74 await resolveComment(beta, commentRes.comment_view.comment)
76 expect(betaComment).toBeDefined();
77 expect(betaComment.community.local).toBe(true);
78 expect(betaComment.creator.local).toBe(false);
79 expect(betaComment.counts.score).toBe(1);
80 assertCommentFederation(betaComment, commentRes.comment_view);
83 test("Create a comment in a non-existent post", async () => {
84 let commentRes = (await createComment(alpha, -1, None)) as any;
85 expect(commentRes.error).toBe("couldnt_find_post");
88 test("Update a comment", async () => {
89 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
90 // Federate the comment first
92 await resolveComment(beta, commentRes.comment_view.comment)
94 assertCommentFederation(betaComment.unwrap(), commentRes.comment_view);
96 let updateCommentRes = await editComment(
98 commentRes.comment_view.comment.id
100 expect(updateCommentRes.comment_view.comment.content).toBe(
101 "A jest test federated comment update"
103 expect(updateCommentRes.comment_view.community.local).toBe(false);
104 expect(updateCommentRes.comment_view.creator.local).toBe(true);
106 // Make sure that post is updated on beta
107 let betaCommentUpdated = (
108 await resolveComment(beta, commentRes.comment_view.comment)
110 assertCommentFederation(betaCommentUpdated, updateCommentRes.comment_view);
113 test("Delete a comment", async () => {
114 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
116 let deleteCommentRes = await deleteComment(
119 commentRes.comment_view.comment.id
121 expect(deleteCommentRes.comment_view.comment.deleted).toBe(true);
122 expect(deleteCommentRes.comment_view.comment.content).toBe("");
124 // Make sure that comment is undefined on beta
125 let betaCommentRes = (await resolveComment(
127 commentRes.comment_view.comment
129 expect(betaCommentRes.error).toBe("couldnt_find_object");
131 let undeleteCommentRes = await deleteComment(
134 commentRes.comment_view.comment.id
136 expect(undeleteCommentRes.comment_view.comment.deleted).toBe(false);
138 // Make sure that comment is undeleted on beta
140 await resolveComment(beta, commentRes.comment_view.comment)
142 expect(betaComment2.comment.deleted).toBe(false);
143 assertCommentFederation(betaComment2, undeleteCommentRes.comment_view);
146 test("Remove a comment from admin and community on the same instance", async () => {
147 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
149 // Get the id for beta
150 let betaCommentId = (
151 await resolveComment(beta, commentRes.comment_view.comment)
152 ).comment.unwrap().comment.id;
154 // The beta admin removes it (the community lives on beta)
155 let removeCommentRes = await removeComment(beta, true, betaCommentId);
156 expect(removeCommentRes.comment_view.comment.removed).toBe(true);
157 expect(removeCommentRes.comment_view.comment.content).toBe("");
159 // Make sure that comment is removed on alpha (it gets pushed since an admin from beta removed it)
160 let refetchedPostComments = await getComments(
162 postRes.post_view.post.id
164 expect(refetchedPostComments.comments[0].comment.removed).toBe(true);
166 let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
167 expect(unremoveCommentRes.comment_view.comment.removed).toBe(false);
169 // Make sure that comment is unremoved on beta
170 let refetchedPostComments2 = await getComments(
172 postRes.post_view.post.id
174 expect(refetchedPostComments2.comments[0].comment.removed).toBe(false);
175 assertCommentFederation(
176 refetchedPostComments2.comments[0],
177 unremoveCommentRes.comment_view
181 test("Remove a comment from admin and community on different instance", async () => {
182 let alpha_user = await registerUser(alpha);
183 let newAlphaApi: API = {
184 client: alpha.client,
185 auth: alpha_user.jwt,
188 // New alpha user creates a community, post, and comment.
189 let newCommunity = await createCommunity(newAlphaApi);
190 let newPost = await createPost(
192 newCommunity.community_view.community.id
194 let commentRes = await createComment(
196 newPost.post_view.post.id,
199 expect(commentRes.comment_view.comment.content).toBeDefined();
201 // Beta searches that to cache it, then removes it
203 await resolveComment(beta, commentRes.comment_view.comment)
205 let removeCommentRes = await removeComment(
208 betaComment.comment.id
210 expect(removeCommentRes.comment_view.comment.removed).toBe(true);
212 // Make sure its not removed on alpha
213 let refetchedPostComments = await getComments(
215 newPost.post_view.post.id
217 expect(refetchedPostComments.comments[0].comment.removed).toBe(false);
218 assertCommentFederation(
219 refetchedPostComments.comments[0],
220 commentRes.comment_view
224 test("Unlike a comment", async () => {
225 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
226 let unlike = await likeComment(alpha, 0, commentRes.comment_view.comment);
227 expect(unlike.comment_view.counts.score).toBe(0);
229 // Make sure that post is unliked on beta
231 await resolveComment(beta, commentRes.comment_view.comment)
233 expect(betaComment).toBeDefined();
234 expect(betaComment.community.local).toBe(true);
235 expect(betaComment.creator.local).toBe(false);
236 expect(betaComment.counts.score).toBe(0);
239 test("Federated comment like", async () => {
240 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
242 // Find the comment on beta
244 await resolveComment(beta, commentRes.comment_view.comment)
247 let like = await likeComment(beta, 1, betaComment.comment);
248 expect(like.comment_view.counts.score).toBe(2);
250 // Get the post from alpha, check the likes
251 let postComments = await getComments(alpha, postRes.post_view.post.id);
252 expect(postComments.comments[0].counts.score).toBe(2);
255 test("Reply to a comment", async () => {
256 // Create a comment on alpha, find it on beta
257 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
259 await resolveComment(beta, commentRes.comment_view.comment)
262 // find that comment id on beta
265 let replyRes = await createComment(
268 Some(betaComment.comment.id)
270 expect(replyRes.comment_view.comment.content).toBeDefined();
271 expect(replyRes.comment_view.community.local).toBe(true);
272 expect(replyRes.comment_view.creator.local).toBe(true);
273 expect(getCommentParentId(replyRes.comment_view.comment).unwrap()).toBe(
274 betaComment.comment.id
276 expect(replyRes.comment_view.counts.score).toBe(1);
278 // Make sure that comment is seen on alpha
279 // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
280 // comment, isn't working.
281 // let searchAlpha = await searchComment(alpha, replyRes.comment);
282 let postComments = await getComments(alpha, postRes.post_view.post.id);
283 let alphaComment = postComments.comments[0];
284 expect(alphaComment.comment.content).toBeDefined();
285 expect(getCommentParentId(alphaComment.comment).unwrap()).toBe(
286 postComments.comments[1].comment.id
288 expect(alphaComment.community.local).toBe(false);
289 expect(alphaComment.creator.local).toBe(false);
290 expect(alphaComment.counts.score).toBe(1);
291 assertCommentFederation(alphaComment, replyRes.comment_view);
294 test("Mention beta", async () => {
295 // Create a mention on alpha
296 let mentionContent = "A test mention of @lemmy_beta@lemmy-beta:8551";
297 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
298 let mentionRes = await createComment(
300 postRes.post_view.post.id,
301 Some(commentRes.comment_view.comment.id),
304 expect(mentionRes.comment_view.comment.content).toBeDefined();
305 expect(mentionRes.comment_view.community.local).toBe(false);
306 expect(mentionRes.comment_view.creator.local).toBe(true);
307 expect(mentionRes.comment_view.counts.score).toBe(1);
309 let mentionsRes = await getMentions(beta);
310 expect(mentionsRes.mentions[0].comment.content).toBeDefined();
311 expect(mentionsRes.mentions[0].community.local).toBe(true);
312 expect(mentionsRes.mentions[0].creator.local).toBe(false);
313 expect(mentionsRes.mentions[0].counts.score).toBe(1);
316 test("Comment Search", async () => {
317 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
319 await resolveComment(beta, commentRes.comment_view.comment)
321 assertCommentFederation(betaComment, commentRes.comment_view);
324 test("A and G subscribe to B (center) A posts, G mentions B, it gets announced to A", async () => {
325 // Create a local post
326 let alphaCommunity = (
327 await resolveCommunity(alpha, "!main@lemmy-alpha:8541")
328 ).community.unwrap();
329 let alphaPost = await createPost(alpha, alphaCommunity.community.id);
330 expect(alphaPost.post_view.community.local).toBe(true);
332 // Make sure gamma sees it
334 await resolvePost(gamma, alphaPost.post_view.post)
338 "A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551";
339 let commentRes = await createComment(
345 expect(commentRes.comment_view.comment.content).toBe(commentContent);
346 expect(commentRes.comment_view.community.local).toBe(false);
347 expect(commentRes.comment_view.creator.local).toBe(true);
348 expect(commentRes.comment_view.counts.score).toBe(1);
350 // Make sure alpha sees it
351 let alphaPostComments2 = await getComments(
353 alphaPost.post_view.post.id
355 expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
356 expect(alphaPostComments2.comments[0].community.local).toBe(true);
357 expect(alphaPostComments2.comments[0].creator.local).toBe(false);
358 expect(alphaPostComments2.comments[0].counts.score).toBe(1);
359 assertCommentFederation(
360 alphaPostComments2.comments[0],
361 commentRes.comment_view
364 // Make sure beta has mentions
365 let mentionsRes = await getMentions(beta);
366 expect(mentionsRes.mentions[0].comment.content).toBe(commentContent);
367 expect(mentionsRes.mentions[0].community.local).toBe(false);
368 expect(mentionsRes.mentions[0].creator.local).toBe(false);
369 // TODO this is failing because fetchInReplyTos aren't getting score
370 // expect(mentionsRes.mentions[0].score).toBe(1);
373 test("Check that activity from another instance is sent to third instance", async () => {
374 // Alpha and gamma users follow beta community
375 let alphaFollow = await followBeta(alpha);
376 expect(alphaFollow.community_view.community.local).toBe(false);
377 expect(alphaFollow.community_view.community.name).toBe("main");
379 let gammaFollow = await followBeta(gamma);
380 expect(gammaFollow.community_view.community.local).toBe(false);
381 expect(gammaFollow.community_view.community.name).toBe("main");
383 // Create a post on beta
384 let betaPost = await createPost(beta, 2);
385 expect(betaPost.post_view.community.local).toBe(true);
387 // Make sure gamma and alpha see it
389 await resolvePost(gamma, betaPost.post_view.post)
391 expect(gammaPost.post).toBeDefined();
393 await resolvePost(alpha, betaPost.post_view.post)
395 expect(alphaPost.post).toBeDefined();
397 // The bug: gamma comments, and alpha should see it.
398 let commentContent = "Comment from gamma";
399 let commentRes = await createComment(
405 expect(commentRes.comment_view.comment.content).toBe(commentContent);
406 expect(commentRes.comment_view.community.local).toBe(false);
407 expect(commentRes.comment_view.creator.local).toBe(true);
408 expect(commentRes.comment_view.counts.score).toBe(1);
410 // Make sure alpha sees it
411 let alphaPostComments2 = await getComments(alpha, alphaPost.post.id);
412 expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
413 expect(alphaPostComments2.comments[0].community.local).toBe(false);
414 expect(alphaPostComments2.comments[0].creator.local).toBe(false);
415 expect(alphaPostComments2.comments[0].counts.score).toBe(1);
416 assertCommentFederation(
417 alphaPostComments2.comments[0],
418 commentRes.comment_view
421 await unfollowRemotes(alpha);
422 await unfollowRemotes(gamma);
425 test("Fetch in_reply_tos: A is unsubbed from B, B makes a post, and some embedded comments, A subs to B, B updates the lowest level comment, A fetches both the post and all the inreplyto comments for that post.", async () => {
426 // Unfollow all remote communities
427 let site = await unfollowRemotes(alpha);
429 site.my_user.unwrap().follows.filter(c => c.community.local == false).length
432 // B creates a post, and two comments, should be invisible to A
433 let postRes = await createPost(beta, 2);
434 expect(postRes.post_view.post.name).toBeDefined();
436 let parentCommentContent = "An invisible top level comment from beta";
437 let parentCommentRes = await createComment(
439 postRes.post_view.post.id,
443 expect(parentCommentRes.comment_view.comment.content).toBe(
447 // B creates a comment, then a child one of that.
448 let childCommentContent = "An invisible child comment from beta";
449 let childCommentRes = await createComment(
451 postRes.post_view.post.id,
452 Some(parentCommentRes.comment_view.comment.id),
455 expect(childCommentRes.comment_view.comment.content).toBe(
460 let follow = await followBeta(alpha);
461 expect(follow.community_view.community.local).toBe(false);
462 expect(follow.community_view.community.name).toBe("main");
464 // An update to the child comment on beta, should push the post, parent, and child to alpha now
465 let updatedCommentContent = Some("An update child comment from beta");
466 let updateRes = await editComment(
468 childCommentRes.comment_view.comment.id,
469 updatedCommentContent
471 expect(updateRes.comment_view.comment.content).toBe(
472 updatedCommentContent.unwrap()
475 // Get the post from alpha
477 await resolvePost(alpha, postRes.post_view.post)
480 let alphaPost = await getPost(alpha, alphaPostB.post.id);
481 let alphaPostComments = await getComments(alpha, alphaPostB.post.id);
482 expect(alphaPost.post_view.post.name).toBeDefined();
483 assertCommentFederation(
484 alphaPostComments.comments[1],
485 parentCommentRes.comment_view
487 assertCommentFederation(
488 alphaPostComments.comments[0],
489 updateRes.comment_view
491 expect(alphaPost.post_view.community.local).toBe(false);
492 expect(alphaPost.post_view.creator.local).toBe(false);
494 await unfollowRemotes(alpha);
497 test("Report a comment", async () => {
498 let betaCommunity = (await resolveBetaCommunity(beta)).community.unwrap();
499 let postRes = (await createPost(beta, betaCommunity.community.id)).post_view
501 expect(postRes).toBeDefined();
502 let commentRes = (await createComment(beta, postRes.id, None)).comment_view
504 expect(commentRes).toBeDefined();
506 let alphaComment = (await resolveComment(alpha, commentRes)).comment.unwrap()
509 await reportComment(alpha, alphaComment.id, randomString(10))
510 ).comment_report_view.comment_report;
512 let betaReport = (await listCommentReports(beta)).comment_reports[0]
514 expect(betaReport).toBeDefined();
515 expect(betaReport.resolved).toBe(false);
516 expect(betaReport.original_comment_text).toBe(
517 alphaReport.original_comment_text
519 expect(betaReport.reason).toBe(alphaReport.reason);