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';
35 let postRes: PostResponse;
37 beforeAll(async () => {
40 await followBeta(alpha);
41 await followBeta(gamma);
42 let betaCommunity = (await resolveBetaCommunity(alpha)).community;
43 postRes = await createPost(
45 betaCommunity.unwrap().community.id
49 afterAll(async () => {
53 function assertCommentFederation(
54 commentOne: CommentView,
55 commentTwo: CommentView
57 expect(commentOne.comment.ap_id).toBe(commentOne.comment.ap_id);
58 expect(commentOne.comment.content).toBe(commentTwo.comment.content);
59 expect(commentOne.creator.name).toBe(commentTwo.creator.name);
60 expect(commentOne.community.actor_id).toBe(commentTwo.community.actor_id);
61 expect(commentOne.comment.published).toBe(commentTwo.comment.published);
62 expect(commentOne.comment.updated).toBe(commentOne.comment.updated);
63 expect(commentOne.comment.deleted).toBe(commentOne.comment.deleted);
64 expect(commentOne.comment.removed).toBe(commentOne.comment.removed);
67 test('Create a comment', async () => {
68 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
69 expect(commentRes.comment_view.comment.content).toBeDefined();
70 expect(commentRes.comment_view.community.local).toBe(false);
71 expect(commentRes.comment_view.creator.local).toBe(true);
72 expect(commentRes.comment_view.counts.score).toBe(1);
74 // Make sure that comment is liked on beta
75 let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
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
91 let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment;
92 assertCommentFederation(betaComment.unwrap(), commentRes.comment_view);
94 let updateCommentRes = await editComment(
96 commentRes.comment_view.comment.id
98 expect(updateCommentRes.comment_view.comment.content).toBe(
99 'A jest test federated comment update'
101 expect(updateCommentRes.comment_view.community.local).toBe(false);
102 expect(updateCommentRes.comment_view.creator.local).toBe(true);
104 // Make sure that post is updated on beta
105 let betaCommentUpdated = (await resolveComment(
107 commentRes.comment_view.comment
109 assertCommentFederation(
111 updateCommentRes.comment_view
115 test('Delete a comment', async () => {
116 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
118 let deleteCommentRes = await deleteComment(
121 commentRes.comment_view.comment.id
123 expect(deleteCommentRes.comment_view.comment.deleted).toBe(true);
124 expect(deleteCommentRes.comment_view.comment.content).toBe("");
126 // Make sure that comment is undefined on beta
127 let betaCommentRes = await resolveComment(beta, commentRes.comment_view.comment) as any;
128 expect(betaCommentRes.error).toBe('couldnt_find_object');
130 let undeleteCommentRes = await deleteComment(
133 commentRes.comment_view.comment.id
135 expect(undeleteCommentRes.comment_view.comment.deleted).toBe(false);
137 // Make sure that comment is undeleted on beta
138 let betaComment2 = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
139 expect(betaComment2.comment.deleted).toBe(false);
140 assertCommentFederation(
142 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(alpha, postRes.post_view.post.id);
161 expect(refetchedPostComments.comments[0].comment.removed).toBe(true);
163 let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
164 expect(unremoveCommentRes.comment_view.comment.removed).toBe(false);
166 // Make sure that comment is unremoved on beta
167 let refetchedPostComments2 = await getComments(alpha, postRes.post_view.post.id);
168 expect(refetchedPostComments2.comments[0].comment.removed).toBe(false);
169 assertCommentFederation(
170 refetchedPostComments2.comments[0],
171 unremoveCommentRes.comment_view
175 test('Remove a comment from admin and community on different instance', async () => {
176 let alpha_user = await registerUser(alpha);
177 let newAlphaApi: API = {
178 client: alpha.client,
179 auth: alpha_user.jwt,
182 // New alpha user creates a community, post, and comment.
183 let newCommunity = await createCommunity(newAlphaApi);
184 let newPost = await createPost(
186 newCommunity.community_view.community.id
188 let commentRes = await createComment(newAlphaApi, newPost.post_view.post.id, None);
189 expect(commentRes.comment_view.comment.content).toBeDefined();
191 // Beta searches that to cache it, then removes it
192 let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
193 let removeCommentRes = await removeComment(
196 betaComment.comment.id
198 expect(removeCommentRes.comment_view.comment.removed).toBe(true);
200 // Make sure its not removed on alpha
201 let refetchedPostComments = await getComments(alpha, newPost.post_view.post.id);
202 expect(refetchedPostComments.comments[0].comment.removed).toBe(false);
203 assertCommentFederation(refetchedPostComments.comments[0], commentRes.comment_view);
206 test('Unlike a comment', async () => {
207 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
208 let unlike = await likeComment(alpha, 0, commentRes.comment_view.comment);
209 expect(unlike.comment_view.counts.score).toBe(0);
211 // Make sure that post is unliked on beta
212 let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
213 expect(betaComment).toBeDefined();
214 expect(betaComment.community.local).toBe(true);
215 expect(betaComment.creator.local).toBe(false);
216 expect(betaComment.counts.score).toBe(0);
219 test('Federated comment like', async () => {
220 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
222 // Find the comment on beta
223 let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
225 let like = await likeComment(beta, 1, betaComment.comment);
226 expect(like.comment_view.counts.score).toBe(2);
228 // Get the post from alpha, check the likes
229 let postComments = await getComments(alpha, postRes.post_view.post.id);
230 expect(postComments.comments[0].counts.score).toBe(2);
233 test('Reply to a comment', async () => {
234 // Create a comment on alpha, find it on beta
235 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
236 let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
238 // find that comment id on beta
241 let replyRes = await createComment(
244 Some(betaComment.comment.id)
246 expect(replyRes.comment_view.comment.content).toBeDefined();
247 expect(replyRes.comment_view.community.local).toBe(true);
248 expect(replyRes.comment_view.creator.local).toBe(true);
249 expect(getCommentParentId(replyRes.comment_view.comment).unwrap()).toBe(betaComment.comment.id);
250 expect(replyRes.comment_view.counts.score).toBe(1);
252 // Make sure that comment is seen on alpha
253 // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
254 // comment, isn't working.
255 // let searchAlpha = await searchComment(alpha, replyRes.comment);
256 let postComments = await getComments(alpha, postRes.post_view.post.id);
257 let alphaComment = postComments.comments[0];
258 expect(alphaComment.comment.content).toBeDefined();
259 expect(getCommentParentId(alphaComment.comment).unwrap()).toBe(postComments.comments[1].comment.id);
260 expect(alphaComment.community.local).toBe(false);
261 expect(alphaComment.creator.local).toBe(false);
262 expect(alphaComment.counts.score).toBe(1);
263 assertCommentFederation(alphaComment, replyRes.comment_view);
266 test('Mention beta', async () => {
267 // Create a mention on alpha
268 let mentionContent = 'A test mention of @lemmy_beta@lemmy-beta:8551';
269 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
270 let mentionRes = await createComment(
272 postRes.post_view.post.id,
273 Some(commentRes.comment_view.comment.id),
276 expect(mentionRes.comment_view.comment.content).toBeDefined();
277 expect(mentionRes.comment_view.community.local).toBe(false);
278 expect(mentionRes.comment_view.creator.local).toBe(true);
279 expect(mentionRes.comment_view.counts.score).toBe(1);
281 let mentionsRes = await getMentions(beta);
282 expect(mentionsRes.mentions[0].comment.content).toBeDefined();
283 expect(mentionsRes.mentions[0].community.local).toBe(true);
284 expect(mentionsRes.mentions[0].creator.local).toBe(false);
285 expect(mentionsRes.mentions[0].counts.score).toBe(1);
288 test('Comment Search', async () => {
289 let commentRes = await createComment(alpha, postRes.post_view.post.id, None);
290 let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment.unwrap();
291 assertCommentFederation(betaComment, commentRes.comment_view);
294 test('A and G subscribe to B (center) A posts, G mentions B, it gets announced to A', async () => {
295 // Create a local post
296 let alphaCommunity = await createCommunity(alpha, "main");
297 let alphaPost = await createPost(alpha, alphaCommunity.community_view.community.id);
298 expect(alphaPost.post_view.community.local).toBe(true);
300 // Make sure gamma sees it
301 let gammaPost = (await resolvePost(gamma, alphaPost.post_view.post)).post.unwrap();
304 'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551';
305 let commentRes = await createComment(
311 expect(commentRes.comment_view.comment.content).toBe(commentContent);
312 expect(commentRes.comment_view.community.local).toBe(false);
313 expect(commentRes.comment_view.creator.local).toBe(true);
314 expect(commentRes.comment_view.counts.score).toBe(1);
316 // Make sure alpha sees it
317 let alphaPostComments2 = await getComments(alpha, alphaPost.post_view.post.id);
318 expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
319 expect(alphaPostComments2.comments[0].community.local).toBe(true);
320 expect(alphaPostComments2.comments[0].creator.local).toBe(false);
321 expect(alphaPostComments2.comments[0].counts.score).toBe(1);
322 assertCommentFederation(alphaPostComments2.comments[0], commentRes.comment_view);
324 // Make sure beta has mentions
325 let mentionsRes = await getMentions(beta);
326 expect(mentionsRes.mentions[0].comment.content).toBe(commentContent);
327 expect(mentionsRes.mentions[0].community.local).toBe(false);
328 expect(mentionsRes.mentions[0].creator.local).toBe(false);
329 // TODO this is failing because fetchInReplyTos aren't getting score
330 // expect(mentionsRes.mentions[0].score).toBe(1);
333 test('Check that activity from another instance is sent to third instance', async () => {
334 // Alpha and gamma users follow beta community
335 let alphaFollow = await followBeta(alpha);
336 expect(alphaFollow.community_view.community.local).toBe(false);
337 expect(alphaFollow.community_view.community.name).toBe('main');
339 let gammaFollow = await followBeta(gamma);
340 expect(gammaFollow.community_view.community.local).toBe(false);
341 expect(gammaFollow.community_view.community.name).toBe('main');
343 // Create a post on beta
344 let betaPost = await createPost(beta, 2);
345 expect(betaPost.post_view.community.local).toBe(true);
347 // Make sure gamma and alpha see it
348 let gammaPost = (await resolvePost(gamma, betaPost.post_view.post)).post.unwrap();
349 expect(gammaPost.post).toBeDefined();
350 let alphaPost = (await resolvePost(alpha, betaPost.post_view.post)).post.unwrap();
351 expect(alphaPost.post).toBeDefined();
353 // The bug: gamma comments, and alpha should see it.
354 let commentContent = 'Comment from gamma';
355 let commentRes = await createComment(
361 expect(commentRes.comment_view.comment.content).toBe(commentContent);
362 expect(commentRes.comment_view.community.local).toBe(false);
363 expect(commentRes.comment_view.creator.local).toBe(true);
364 expect(commentRes.comment_view.counts.score).toBe(1);
366 // Make sure alpha sees it
367 let alphaPostComments2 = await getComments(alpha, alphaPost.post.id);
368 expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
369 expect(alphaPostComments2.comments[0].community.local).toBe(false);
370 expect(alphaPostComments2.comments[0].creator.local).toBe(false);
371 expect(alphaPostComments2.comments[0].counts.score).toBe(1);
372 assertCommentFederation(alphaPostComments2.comments[0], commentRes.comment_view);
374 await unfollowRemotes(alpha);
375 await unfollowRemotes(gamma);
378 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 () => {
379 // Unfollow all remote communities
380 let site = await unfollowRemotes(alpha);
382 site.my_user.unwrap().follows.filter(c => c.community.local == false).length
385 // B creates a post, and two comments, should be invisible to A
386 let postRes = await createPost(beta, 2);
387 expect(postRes.post_view.post.name).toBeDefined();
389 let parentCommentContent = 'An invisible top level comment from beta';
390 let parentCommentRes = await createComment(
392 postRes.post_view.post.id,
396 expect(parentCommentRes.comment_view.comment.content).toBe(
400 // B creates a comment, then a child one of that.
401 let childCommentContent = 'An invisible child comment from beta';
402 let childCommentRes = await createComment(
404 postRes.post_view.post.id,
405 Some(parentCommentRes.comment_view.comment.id),
408 expect(childCommentRes.comment_view.comment.content).toBe(
413 let follow = await followBeta(alpha);
414 expect(follow.community_view.community.local).toBe(false);
415 expect(follow.community_view.community.name).toBe('main');
417 // An update to the child comment on beta, should push the post, parent, and child to alpha now
418 let updatedCommentContent = 'An update child comment from beta';
419 let updateRes = await editComment(
421 childCommentRes.comment_view.comment.id,
422 updatedCommentContent
424 expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent);
426 // Get the post from alpha
427 let alphaPostB = (await resolvePost(alpha, postRes.post_view.post)).post.unwrap();
429 let alphaPost = await getPost(alpha, alphaPostB.post.id);
430 let alphaPostComments = await getComments(alpha, alphaPostB.post.id);
431 expect(alphaPost.post_view.post.name).toBeDefined();
432 assertCommentFederation(alphaPostComments.comments[1], parentCommentRes.comment_view);
433 assertCommentFederation(alphaPostComments.comments[0], updateRes.comment_view);
434 expect(alphaPost.post_view.community.local).toBe(false);
435 expect(alphaPost.post_view.creator.local).toBe(false);
437 await unfollowRemotes(alpha);
441 test('Report a comment', async () => {
442 let betaCommunity = (await resolveBetaCommunity(beta)).community.unwrap();
443 let postRes = (await createPost(beta, betaCommunity.community.id)).post_view.post;
444 expect(postRes).toBeDefined();
445 let commentRes = (await createComment(beta, postRes.id, None)).comment_view.comment;
446 expect(commentRes).toBeDefined();
448 let alphaComment = (await resolveComment(alpha, commentRes)).comment.unwrap().comment;
449 let alphaReport = (await reportComment(alpha, alphaComment.id, randomString(10)))
450 .comment_report_view.comment_report;
452 let betaReport = (await listCommentReports(beta)).comment_reports[0].comment_report;
453 expect(betaReport).toBeDefined();
454 expect(betaReport.resolved).toBe(false);
455 expect(betaReport.original_comment_text).toBe(alphaReport.original_comment_text);
456 expect(betaReport.reason).toBe(alphaReport.reason);
458 function N(gamma: API, id: number, N: any, commentContent: string) {
459 throw new Error('Function not implemented.');