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(
46 betaCommunity.unwrap().community.id
50 afterAll(async () => {
54 function assertCommentFederation(
55 commentOne: CommentView,
56 commentTwo: CommentView
58 expect(commentOne.comment.ap_id).toBe(commentOne.comment.ap_id);
59 expect(commentOne.comment.content).toBe(commentTwo.comment.content);
60 expect(commentOne.creator.name).toBe(commentTwo.creator.name);
61 expect(commentOne.community.actor_id).toBe(commentTwo.community.actor_id);
62 expect(commentOne.comment.published).toBe(commentTwo.comment.published);
63 expect(commentOne.comment.updated).toBe(commentOne.comment.updated);
64 expect(commentOne.comment.deleted).toBe(commentOne.comment.deleted);
65 expect(commentOne.comment.removed).toBe(commentOne.comment.removed);
68 test('Create a comment', async () => {
69 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
70 expect(commentRes.comment_view.comment.content).toBeDefined();
71 expect(commentRes.comment_view.community.local).toBe(false);
72 expect(commentRes.comment_view.creator.local).toBe(true);
73 expect(commentRes.comment_view.counts.score).toBe(1);
75 // Make sure that comment is liked on beta
76 let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
77 expect(betaComment).toBeDefined();
78 expect(betaComment.community.local).toBe(true);
79 expect(betaComment.creator.local).toBe(false);
80 expect(betaComment.counts.score).toBe(1);
81 assertCommentFederation(betaComment, commentRes.comment_view);
84 test('Create a comment in a non-existent post', async () => {
85 let commentRes = await createComment(alpha, -1, None) as any;
86 expect(commentRes.error).toBe('couldnt_find_post');
89 test('Update a comment', async () => {
90 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
91 // Federate the comment first
92 let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment;
93 assertCommentFederation(betaComment.unwrap(), commentRes.comment_view);
95 let updateCommentRes = await editComment(
97 commentRes.comment_view.comment.id
99 expect(updateCommentRes.comment_view.comment.content).toBe(
100 'A jest test federated comment update'
102 expect(updateCommentRes.comment_view.community.local).toBe(false);
103 expect(updateCommentRes.comment_view.creator.local).toBe(true);
105 // Make sure that post is updated on beta
106 let betaCommentUpdated = (await resolveComment(
108 commentRes.comment_view.comment
110 assertCommentFederation(
112 updateCommentRes.comment_view
116 test('Delete a comment', async () => {
117 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
119 let deleteCommentRes = await deleteComment(
122 commentRes.comment_view.comment.id
124 expect(deleteCommentRes.comment_view.comment.deleted).toBe(true);
125 expect(deleteCommentRes.comment_view.comment.content).toBe("");
127 // Make sure that comment is undefined on beta
128 let betaCommentRes = await resolveComment(beta, commentRes.comment_view.comment) as any;
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
139 let betaComment2 = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
140 expect(betaComment2.comment.deleted).toBe(false);
141 assertCommentFederation(
143 undeleteCommentRes.comment_view
147 test('Remove a comment from admin and community on the same instance', async () => {
148 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
150 // Get the id for beta
151 let betaCommentId = (
152 await resolveComment(beta, commentRes.comment_view.comment)
153 ).comment.unwrap().comment.id;
155 // The beta admin removes it (the community lives on beta)
156 let removeCommentRes = await removeComment(beta, true, betaCommentId);
157 expect(removeCommentRes.comment_view.comment.removed).toBe(true);
158 expect(removeCommentRes.comment_view.comment.content).toBe("");
160 // Make sure that comment is removed on alpha (it gets pushed since an admin from beta removed it)
161 let refetchedPostComments = await getComments(alpha, postRes.post_view.post.id);
162 expect(refetchedPostComments.comments[0].comment.removed).toBe(true);
164 let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
165 expect(unremoveCommentRes.comment_view.comment.removed).toBe(false);
167 // Make sure that comment is unremoved on beta
168 let refetchedPostComments2 = await getComments(alpha, postRes.post_view.post.id);
169 expect(refetchedPostComments2.comments[0].comment.removed).toBe(false);
170 assertCommentFederation(
171 refetchedPostComments2.comments[0],
172 unremoveCommentRes.comment_view
176 test('Remove a comment from admin and community on different instance', async () => {
177 let alpha_user = await registerUser(alpha);
178 let newAlphaApi: API = {
179 client: alpha.client,
180 auth: alpha_user.jwt,
183 // New alpha user creates a community, post, and comment.
184 let newCommunity = await createCommunity(newAlphaApi);
185 let newPost = await createPost(
187 newCommunity.community_view.community.id
189 let commentRes = await createComment(newAlphaApi, newPost.post_view.post.id, None);
190 expect(commentRes.comment_view.comment.content).toBeDefined();
192 // Beta searches that to cache it, then removes it
193 let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
194 let removeCommentRes = await removeComment(
197 betaComment.comment.id
199 expect(removeCommentRes.comment_view.comment.removed).toBe(true);
201 // Make sure its not removed on alpha
202 let refetchedPostComments = await getComments(alpha, newPost.post_view.post.id);
203 expect(refetchedPostComments.comments[0].comment.removed).toBe(false);
204 assertCommentFederation(refetchedPostComments.comments[0], commentRes.comment_view);
207 test('Unlike a comment', async () => {
208 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
209 let unlike = await likeComment(alpha, 0, commentRes.comment_view.comment);
210 expect(unlike.comment_view.counts.score).toBe(0);
212 // Make sure that post is unliked on beta
213 let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
214 expect(betaComment).toBeDefined();
215 expect(betaComment.community.local).toBe(true);
216 expect(betaComment.creator.local).toBe(false);
217 expect(betaComment.counts.score).toBe(0);
220 test('Federated comment like', async () => {
221 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
223 // Find the comment on beta
224 let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
226 let like = await likeComment(beta, 1, betaComment.comment);
227 expect(like.comment_view.counts.score).toBe(2);
229 // Get the post from alpha, check the likes
230 let postComments = await getComments(alpha, postRes.post_view.post.id);
231 expect(postComments.comments[0].counts.score).toBe(2);
234 test('Reply to a comment', async () => {
235 // Create a comment on alpha, find it on beta
236 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
237 let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
239 // find that comment id on beta
242 let replyRes = await createComment(
245 Some(betaComment.comment.id)
247 expect(replyRes.comment_view.comment.content).toBeDefined();
248 expect(replyRes.comment_view.community.local).toBe(true);
249 expect(replyRes.comment_view.creator.local).toBe(true);
250 expect(getCommentParentId(replyRes.comment_view.comment).unwrap()).toBe(betaComment.comment.id);
251 expect(replyRes.comment_view.counts.score).toBe(1);
253 // Make sure that comment is seen on alpha
254 // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
255 // comment, isn't working.
256 // let searchAlpha = await searchComment(alpha, replyRes.comment);
257 let postComments = await getComments(alpha, postRes.post_view.post.id);
258 let alphaComment = postComments.comments[0];
259 expect(alphaComment.comment.content).toBeDefined();
260 expect(getCommentParentId(alphaComment.comment).unwrap()).toBe(postComments.comments[1].comment.id);
261 expect(alphaComment.community.local).toBe(false);
262 expect(alphaComment.creator.local).toBe(false);
263 expect(alphaComment.counts.score).toBe(1);
264 assertCommentFederation(alphaComment, replyRes.comment_view);
267 test('Mention beta', async () => {
268 // Create a mention on alpha
269 let mentionContent = 'A test mention of @lemmy_beta@lemmy-beta:8551';
270 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
271 let mentionRes = await createComment(
273 postRes.post_view.post.id,
274 Some(commentRes.comment_view.comment.id),
277 expect(mentionRes.comment_view.comment.content).toBeDefined();
278 expect(mentionRes.comment_view.community.local).toBe(false);
279 expect(mentionRes.comment_view.creator.local).toBe(true);
280 expect(mentionRes.comment_view.counts.score).toBe(1);
282 let mentionsRes = await getMentions(beta);
283 expect(mentionsRes.mentions[0].comment.content).toBeDefined();
284 expect(mentionsRes.mentions[0].community.local).toBe(true);
285 expect(mentionsRes.mentions[0].creator.local).toBe(false);
286 expect(mentionsRes.mentions[0].counts.score).toBe(1);
289 test('Comment Search', async () => {
290 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
291 let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
292 assertCommentFederation(betaComment, commentRes.comment_view);
295 test('A and G subscribe to B (center) A posts, G mentions B, it gets announced to A', async () => {
296 // Create a local post
297 let alphaCommunity = (await resolveCommunity(alpha, "!main@lemmy-alpha:8541")).community.unwrap();
298 let alphaPost = await createPost(alpha, alphaCommunity.community.id);
299 expect(alphaPost.post_view.community.local).toBe(true);
301 // Make sure gamma sees it
302 let gammaPost = (await resolvePost(gamma, alphaPost.post_view.post)).post.unwrap();
305 'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551';
306 let commentRes = await createComment(
312 expect(commentRes.comment_view.comment.content).toBe(commentContent);
313 expect(commentRes.comment_view.community.local).toBe(false);
314 expect(commentRes.comment_view.creator.local).toBe(true);
315 expect(commentRes.comment_view.counts.score).toBe(1);
317 // Make sure alpha sees it
318 let alphaPostComments2 = await getComments(alpha, alphaPost.post_view.post.id);
319 expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
320 expect(alphaPostComments2.comments[0].community.local).toBe(true);
321 expect(alphaPostComments2.comments[0].creator.local).toBe(false);
322 expect(alphaPostComments2.comments[0].counts.score).toBe(1);
323 assertCommentFederation(alphaPostComments2.comments[0], commentRes.comment_view);
325 // Make sure beta has mentions
326 let mentionsRes = await getMentions(beta);
327 expect(mentionsRes.mentions[0].comment.content).toBe(commentContent);
328 expect(mentionsRes.mentions[0].community.local).toBe(false);
329 expect(mentionsRes.mentions[0].creator.local).toBe(false);
330 // TODO this is failing because fetchInReplyTos aren't getting score
331 // expect(mentionsRes.mentions[0].score).toBe(1);
334 test('Check that activity from another instance is sent to third instance', async () => {
335 // Alpha and gamma users follow beta community
336 let alphaFollow = await followBeta(alpha);
337 expect(alphaFollow.community_view.community.local).toBe(false);
338 expect(alphaFollow.community_view.community.name).toBe('main');
340 let gammaFollow = await followBeta(gamma);
341 expect(gammaFollow.community_view.community.local).toBe(false);
342 expect(gammaFollow.community_view.community.name).toBe('main');
344 // Create a post on beta
345 let betaPost = await createPost(beta, 2);
346 expect(betaPost.post_view.community.local).toBe(true);
348 // Make sure gamma and alpha see it
349 let gammaPost = (await resolvePost(gamma, betaPost.post_view.post)).post.unwrap();
350 expect(gammaPost.post).toBeDefined();
351 let alphaPost = (await resolvePost(alpha, betaPost.post_view.post)).post.unwrap();
352 expect(alphaPost.post).toBeDefined();
354 // The bug: gamma comments, and alpha should see it.
355 let commentContent = 'Comment from gamma';
356 let commentRes = await createComment(
362 expect(commentRes.comment_view.comment.content).toBe(commentContent);
363 expect(commentRes.comment_view.community.local).toBe(false);
364 expect(commentRes.comment_view.creator.local).toBe(true);
365 expect(commentRes.comment_view.counts.score).toBe(1);
367 // Make sure alpha sees it
368 let alphaPostComments2 = await getComments(alpha, alphaPost.post.id);
369 expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
370 expect(alphaPostComments2.comments[0].community.local).toBe(false);
371 expect(alphaPostComments2.comments[0].creator.local).toBe(false);
372 expect(alphaPostComments2.comments[0].counts.score).toBe(1);
373 assertCommentFederation(alphaPostComments2.comments[0], commentRes.comment_view);
375 await unfollowRemotes(alpha);
376 await unfollowRemotes(gamma);
379 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 () => {
380 // Unfollow all remote communities
381 let site = await unfollowRemotes(alpha);
383 site.my_user.unwrap().follows.filter(c => c.community.local == false).length
386 // B creates a post, and two comments, should be invisible to A
387 let postRes = await createPost(beta, 2);
388 expect(postRes.post_view.post.name).toBeDefined();
390 let parentCommentContent = 'An invisible top level comment from beta';
391 let parentCommentRes = await createComment(
393 postRes.post_view.post.id,
397 expect(parentCommentRes.comment_view.comment.content).toBe(
401 // B creates a comment, then a child one of that.
402 let childCommentContent = 'An invisible child comment from beta';
403 let childCommentRes = await createComment(
405 postRes.post_view.post.id,
406 Some(parentCommentRes.comment_view.comment.id),
409 expect(childCommentRes.comment_view.comment.content).toBe(
414 let follow = await followBeta(alpha);
415 expect(follow.community_view.community.local).toBe(false);
416 expect(follow.community_view.community.name).toBe('main');
418 // An update to the child comment on beta, should push the post, parent, and child to alpha now
419 let updatedCommentContent = 'An update child comment from beta';
420 let updateRes = await editComment(
422 childCommentRes.comment_view.comment.id,
423 updatedCommentContent
425 expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent);
427 // Get the post from alpha
428 let alphaPostB = (await resolvePost(alpha, postRes.post_view.post)).post.unwrap();
430 let alphaPost = await getPost(alpha, alphaPostB.post.id);
431 let alphaPostComments = await getComments(alpha, alphaPostB.post.id);
432 expect(alphaPost.post_view.post.name).toBeDefined();
433 assertCommentFederation(alphaPostComments.comments[1], parentCommentRes.comment_view);
434 assertCommentFederation(alphaPostComments.comments[0], updateRes.comment_view);
435 expect(alphaPost.post_view.community.local).toBe(false);
436 expect(alphaPost.post_view.creator.local).toBe(false);
438 await unfollowRemotes(alpha);
442 test('Report a comment', async () => {
443 let betaCommunity = (await resolveBetaCommunity(beta)).community.unwrap();
444 let postRes = (await createPost(beta, betaCommunity.community.id)).post_view.post;
445 expect(postRes).toBeDefined();
446 let commentRes = (await createComment(beta, postRes.id, None)).comment_view.comment;
447 expect(commentRes).toBeDefined();
449 let alphaComment = (await resolveComment(alpha, commentRes)).comment.unwrap().comment;
450 let alphaReport = (await reportComment(alpha, alphaComment.id, randomString(10)))
451 .comment_report_view.comment_report;
453 let betaReport = (await listCommentReports(beta)).comment_reports[0].comment_report;
454 expect(betaReport).toBeDefined();
455 expect(betaReport.resolved).toBe(false);
456 expect(betaReport.original_comment_text).toBe(alphaReport.original_comment_text);
457 expect(betaReport.reason).toBe(alphaReport.reason);
459 function N(gamma: API, id: number, N: any, commentContent: string) {
460 throw new Error('Function not implemented.');