1 jest.setTimeout(180000);
12 searchForBetaCommunity,
26 import { CommentView } from 'lemmy-js-client';
28 import { PostResponse } from 'lemmy-js-client';
30 let postRes: PostResponse;
32 beforeAll(async () => {
34 await followBeta(alpha);
35 await followBeta(gamma);
36 let search = await searchForBetaCommunity(alpha);
38 postRes = await createPost(
40 search.communities.find(c => c.community.local == false).community.id
44 afterAll(async () => {
45 await unfollowRemotes(alpha);
46 await unfollowRemotes(gamma);
49 function assertCommentFederation(
50 commentOne: CommentView,
51 commentTwo: CommentView
53 expect(commentOne.comment.ap_id).toBe(commentOne.comment.ap_id);
54 expect(commentOne.comment.content).toBe(commentTwo.comment.content);
55 expect(commentOne.creator.name).toBe(commentTwo.creator.name);
56 expect(commentOne.community.actor_id).toBe(commentTwo.community.actor_id);
57 expect(commentOne.comment.published).toBe(commentTwo.comment.published);
58 expect(commentOne.comment.updated).toBe(commentOne.comment.updated);
59 expect(commentOne.comment.deleted).toBe(commentOne.comment.deleted);
60 expect(commentOne.comment.removed).toBe(commentOne.comment.removed);
63 test('Create a comment', async () => {
64 let commentRes = await createComment(alpha, postRes.post_view.post.id);
65 expect(commentRes.comment_view.comment.content).toBeDefined();
66 expect(commentRes.comment_view.community.local).toBe(false);
67 expect(commentRes.comment_view.creator.local).toBe(true);
68 expect(commentRes.comment_view.counts.score).toBe(1);
71 // Make sure that comment is liked on beta
72 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
73 let betaComment = searchBeta.comments[0];
74 expect(betaComment).toBeDefined();
75 expect(betaComment.community.local).toBe(true);
76 expect(betaComment.creator.local).toBe(false);
77 expect(betaComment.counts.score).toBe(1);
78 assertCommentFederation(betaComment, commentRes.comment_view);
81 test('Create a comment in a non-existent post', async () => {
82 let commentRes = await createComment(alpha, -1);
83 expect(commentRes).toStrictEqual({ error: 'couldnt_find_post' });
86 test('Update a comment', async () => {
87 let commentRes = await createComment(alpha, postRes.post_view.post.id);
88 // Federate the comment first
89 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
90 assertCommentFederation(searchBeta.comments[0], commentRes.comment_view);
93 let updateCommentRes = await editComment(
95 commentRes.comment_view.comment.id
97 expect(updateCommentRes.comment_view.comment.content).toBe(
98 'A jest test federated comment update'
100 expect(updateCommentRes.comment_view.community.local).toBe(false);
101 expect(updateCommentRes.comment_view.creator.local).toBe(true);
104 // Make sure that post is updated on beta
105 let searchBetaUpdated = await searchComment(
107 commentRes.comment_view.comment
109 assertCommentFederation(
110 searchBetaUpdated.comments[0],
111 updateCommentRes.comment_view
115 test('Delete a comment', async () => {
116 let commentRes = await createComment(alpha, postRes.post_view.post.id);
119 let deleteCommentRes = await deleteComment(
122 commentRes.comment_view.comment.id
124 expect(deleteCommentRes.comment_view.comment.deleted).toBe(true);
127 // Make sure that comment is undefined on beta
128 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
129 let betaComment = searchBeta.comments[0];
130 expect(betaComment).toBeUndefined();
133 let undeleteCommentRes = await deleteComment(
136 commentRes.comment_view.comment.id
138 expect(undeleteCommentRes.comment_view.comment.deleted).toBe(false);
141 // Make sure that comment is undeleted on beta
142 let searchBeta2 = await searchComment(beta, commentRes.comment_view.comment);
143 let betaComment2 = searchBeta2.comments[0];
144 expect(betaComment2.comment.deleted).toBe(false);
145 assertCommentFederation(
146 searchBeta2.comments[0],
147 undeleteCommentRes.comment_view
151 test('Remove a comment from admin and community on the same instance', async () => {
152 let commentRes = await createComment(alpha, postRes.post_view.post.id);
155 // Get the id for beta
156 let betaCommentId = (
157 await searchComment(beta, commentRes.comment_view.comment)
158 ).comments[0].comment.id;
160 // The beta admin removes it (the community lives on beta)
161 let removeCommentRes = await removeComment(beta, true, betaCommentId);
162 expect(removeCommentRes.comment_view.comment.removed).toBe(true);
165 // Make sure that comment is removed on alpha (it gets pushed since an admin from beta removed it)
166 let refetchedPost = await getPost(alpha, postRes.post_view.post.id);
167 expect(refetchedPost.comments[0].comment.removed).toBe(true);
169 let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
170 expect(unremoveCommentRes.comment_view.comment.removed).toBe(false);
173 // Make sure that comment is unremoved on beta
174 let refetchedPost2 = await getPost(alpha, postRes.post_view.post.id);
175 expect(refetchedPost2.comments[0].comment.removed).toBe(false);
176 assertCommentFederation(
177 refetchedPost2.comments[0],
178 unremoveCommentRes.comment_view
182 test('Remove a comment from admin and community on different instance', async () => {
183 let alphaUser = await registerUser(alpha);
184 let newAlphaApi: API = {
185 client: alpha.client,
189 // New alpha user creates a community, post, and comment.
190 let newCommunity = await createCommunity(newAlphaApi);
192 let newPost = await createPost(
194 newCommunity.community_view.community.id
197 let commentRes = await createComment(newAlphaApi, newPost.post_view.post.id);
198 expect(commentRes.comment_view.comment.content).toBeDefined();
201 // Beta searches that to cache it, then removes it
202 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
203 let betaComment = searchBeta.comments[0];
204 let removeCommentRes = await removeComment(
207 betaComment.comment.id
209 expect(removeCommentRes.comment_view.comment.removed).toBe(true);
212 // Make sure its not removed on alpha
213 let refetchedPost = await getPost(newAlphaApi, newPost.post_view.post.id);
214 expect(refetchedPost.comments[0].comment.removed).toBe(false);
215 assertCommentFederation(refetchedPost.comments[0], commentRes.comment_view);
218 test('Unlike a comment', async () => {
219 let commentRes = await createComment(alpha, postRes.post_view.post.id);
221 let unlike = await likeComment(alpha, 0, commentRes.comment_view.comment);
222 expect(unlike.comment_view.counts.score).toBe(0);
225 // Make sure that post is unliked on beta
226 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
227 let betaComment = searchBeta.comments[0];
228 expect(betaComment).toBeDefined();
229 expect(betaComment.community.local).toBe(true);
230 expect(betaComment.creator.local).toBe(false);
231 expect(betaComment.counts.score).toBe(0);
234 test('Federated comment like', async () => {
235 let commentRes = await createComment(alpha, postRes.post_view.post.id);
238 // Find the comment on beta
239 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
240 let betaComment = searchBeta.comments[0];
242 let like = await likeComment(beta, 1, betaComment.comment);
243 expect(like.comment_view.counts.score).toBe(2);
246 // Get the post from alpha, check the likes
247 let post = await getPost(alpha, postRes.post_view.post.id);
248 expect(post.comments[0].counts.score).toBe(2);
251 test('Reply to a comment', async () => {
252 // Create a comment on alpha, find it on beta
253 let commentRes = await createComment(alpha, postRes.post_view.post.id);
255 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
256 let betaComment = searchBeta.comments[0];
258 // find that comment id on beta
261 let replyRes = await createComment(
264 betaComment.comment.id
266 expect(replyRes.comment_view.comment.content).toBeDefined();
267 expect(replyRes.comment_view.community.local).toBe(true);
268 expect(replyRes.comment_view.creator.local).toBe(true);
269 expect(replyRes.comment_view.comment.parent_id).toBe(betaComment.comment.id);
270 expect(replyRes.comment_view.counts.score).toBe(1);
273 // Make sure that comment is seen on alpha
274 // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
275 // comment, isn't working.
276 // let searchAlpha = await searchComment(alpha, replyRes.comment);
277 let post = await getPost(alpha, postRes.post_view.post.id);
278 let alphaComment = post.comments[0];
279 expect(alphaComment.comment.content).toBeDefined();
280 expect(alphaComment.comment.parent_id).toBe(post.comments[1].comment.id);
281 expect(alphaComment.community.local).toBe(false);
282 expect(alphaComment.creator.local).toBe(false);
283 expect(alphaComment.counts.score).toBe(1);
284 assertCommentFederation(alphaComment, replyRes.comment_view);
287 test('Mention beta', async () => {
288 // Create a mention on alpha
289 let mentionContent = 'A test mention of @lemmy_beta@lemmy-beta:8551';
290 let commentRes = await createComment(alpha, postRes.post_view.post.id);
292 let mentionRes = await createComment(
294 postRes.post_view.post.id,
295 commentRes.comment_view.comment.id,
298 expect(mentionRes.comment_view.comment.content).toBeDefined();
299 expect(mentionRes.comment_view.community.local).toBe(false);
300 expect(mentionRes.comment_view.creator.local).toBe(true);
301 expect(mentionRes.comment_view.counts.score).toBe(1);
304 let mentionsRes = await getMentions(beta);
305 expect(mentionsRes.mentions[0].comment.content).toBeDefined();
306 expect(mentionsRes.mentions[0].community.local).toBe(true);
307 expect(mentionsRes.mentions[0].creator.local).toBe(false);
308 expect(mentionsRes.mentions[0].counts.score).toBe(1);
311 test('Comment Search', async () => {
312 let commentRes = await createComment(alpha, postRes.post_view.post.id);
314 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
315 assertCommentFederation(searchBeta.comments[0], commentRes.comment_view);
318 test('A and G subscribe to B (center) A posts, G mentions B, it gets announced to A', async () => {
319 // Create a local post
320 let alphaPost = await createPost(alpha, 2);
321 expect(alphaPost.post_view.community.local).toBe(true);
324 // Make sure gamma sees it
325 let search = await searchPost(gamma, alphaPost.post_view.post);
326 let gammaPost = search.posts[0];
329 'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551';
330 let commentRes = await createComment(
336 expect(commentRes.comment_view.comment.content).toBe(commentContent);
337 expect(commentRes.comment_view.community.local).toBe(false);
338 expect(commentRes.comment_view.creator.local).toBe(true);
339 expect(commentRes.comment_view.counts.score).toBe(1);
342 // Make sure alpha sees it
343 let alphaPost2 = await getPost(alpha, alphaPost.post_view.post.id);
344 expect(alphaPost2.comments[0].comment.content).toBe(commentContent);
345 expect(alphaPost2.comments[0].community.local).toBe(true);
346 expect(alphaPost2.comments[0].creator.local).toBe(false);
347 expect(alphaPost2.comments[0].counts.score).toBe(1);
348 assertCommentFederation(alphaPost2.comments[0], commentRes.comment_view);
351 // Make sure beta has mentions
352 let mentionsRes = await getMentions(beta);
353 expect(mentionsRes.mentions[0].comment.content).toBe(commentContent);
354 expect(mentionsRes.mentions[0].community.local).toBe(false);
355 expect(mentionsRes.mentions[0].creator.local).toBe(false);
356 // TODO this is failing because fetchInReplyTos aren't getting score
357 // expect(mentionsRes.mentions[0].score).toBe(1);
360 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 () => {
361 // Unfollow all remote communities
362 let followed = await unfollowRemotes(alpha);
364 followed.communities.filter(c => c.community.local == false).length
367 // B creates a post, and two comments, should be invisible to A
368 let postRes = await createPost(beta, 2);
369 expect(postRes.post_view.post.name).toBeDefined();
372 let parentCommentContent = 'An invisible top level comment from beta';
373 let parentCommentRes = await createComment(
375 postRes.post_view.post.id,
379 expect(parentCommentRes.comment_view.comment.content).toBe(
384 // B creates a comment, then a child one of that.
385 let childCommentContent = 'An invisible child comment from beta';
386 let childCommentRes = await createComment(
388 postRes.post_view.post.id,
389 parentCommentRes.comment_view.comment.id,
392 expect(childCommentRes.comment_view.comment.content).toBe(
398 let follow = await followBeta(alpha);
399 expect(follow.community_view.community.local).toBe(false);
400 expect(follow.community_view.community.name).toBe('main');
403 // An update to the child comment on beta, should push the post, parent, and child to alpha now
404 let updatedCommentContent = 'An update child comment from beta';
405 let updateRes = await editComment(
407 childCommentRes.comment_view.comment.id,
408 updatedCommentContent
410 expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent);
413 // Get the post from alpha
414 let search = await searchPost(alpha, postRes.post_view.post);
415 let alphaPostB = search.posts[0];
418 let alphaPost = await getPost(alpha, alphaPostB.post.id);
419 expect(alphaPost.post_view.post.name).toBeDefined();
420 assertCommentFederation(alphaPost.comments[1], parentCommentRes.comment_view);
421 assertCommentFederation(alphaPost.comments[0], updateRes.comment_view);
422 expect(alphaPost.post_view.community.local).toBe(false);
423 expect(alphaPost.post_view.creator.local).toBe(false);