1 jest.setTimeout(180000);
12 searchForBetaCommunity,
24 import { CommentView } from 'lemmy-js-client';
26 import { PostResponse } from 'lemmy-js-client';
28 let postRes: PostResponse;
30 beforeAll(async () => {
32 await followBeta(alpha);
33 await followBeta(gamma);
34 let search = await searchForBetaCommunity(alpha);
35 postRes = await createPost(
37 search.communities.find(c => c.community.local == false).community.id
41 afterAll(async () => {
42 await unfollowRemotes(alpha);
43 await unfollowRemotes(gamma);
46 function assertCommentFederation(
47 commentOne: CommentView,
48 commentTwo: CommentView
50 expect(commentOne.comment.ap_id).toBe(commentOne.comment.ap_id);
51 expect(commentOne.comment.content).toBe(commentTwo.comment.content);
52 expect(commentOne.creator.name).toBe(commentTwo.creator.name);
53 expect(commentOne.community.actor_id).toBe(commentTwo.community.actor_id);
54 expect(commentOne.comment.published).toBe(commentTwo.comment.published);
55 expect(commentOne.comment.updated).toBe(commentOne.comment.updated);
56 expect(commentOne.comment.deleted).toBe(commentOne.comment.deleted);
57 expect(commentOne.comment.removed).toBe(commentOne.comment.removed);
60 test('Create a comment', async () => {
61 let commentRes = await createComment(alpha, postRes.post_view.post.id);
62 expect(commentRes.comment_view.comment.content).toBeDefined();
63 expect(commentRes.comment_view.community.local).toBe(false);
64 expect(commentRes.comment_view.creator.local).toBe(true);
65 expect(commentRes.comment_view.counts.score).toBe(1);
67 // Make sure that comment is liked on beta
68 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
69 let betaComment = searchBeta.comments[0];
70 expect(betaComment).toBeDefined();
71 expect(betaComment.community.local).toBe(true);
72 expect(betaComment.creator.local).toBe(false);
73 expect(betaComment.counts.score).toBe(1);
74 assertCommentFederation(betaComment, commentRes.comment_view);
77 test('Create a comment in a non-existent post', async () => {
78 let commentRes = await createComment(alpha, -1);
79 expect(commentRes).toStrictEqual({ error: 'couldnt_find_post' });
82 test('Update a comment', async () => {
83 let commentRes = await createComment(alpha, postRes.post_view.post.id);
84 // Federate the comment first
85 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
86 assertCommentFederation(searchBeta.comments[0], commentRes.comment_view);
88 let updateCommentRes = await editComment(
90 commentRes.comment_view.comment.id
92 expect(updateCommentRes.comment_view.comment.content).toBe(
93 'A jest test federated comment update'
95 expect(updateCommentRes.comment_view.community.local).toBe(false);
96 expect(updateCommentRes.comment_view.creator.local).toBe(true);
98 // Make sure that post is updated on beta
99 let searchBetaUpdated = await searchComment(
101 commentRes.comment_view.comment
103 assertCommentFederation(
104 searchBetaUpdated.comments[0],
105 updateCommentRes.comment_view
109 test('Delete a comment', async () => {
110 let commentRes = await createComment(alpha, postRes.post_view.post.id);
112 let deleteCommentRes = await deleteComment(
115 commentRes.comment_view.comment.id
117 expect(deleteCommentRes.comment_view.comment.deleted).toBe(true);
119 // Make sure that comment is undefined on beta
120 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
121 let betaComment = searchBeta.comments[0];
122 expect(betaComment).toBeUndefined();
124 let undeleteCommentRes = await deleteComment(
127 commentRes.comment_view.comment.id
129 expect(undeleteCommentRes.comment_view.comment.deleted).toBe(false);
131 // Make sure that comment is undeleted on beta
132 let searchBeta2 = await searchComment(beta, commentRes.comment_view.comment);
133 let betaComment2 = searchBeta2.comments[0];
134 expect(betaComment2.comment.deleted).toBe(false);
135 assertCommentFederation(
136 searchBeta2.comments[0],
137 undeleteCommentRes.comment_view
141 test('Remove a comment from admin and community on the same instance', async () => {
142 let commentRes = await createComment(alpha, postRes.post_view.post.id);
144 // Get the id for beta
145 let betaCommentId = (
146 await searchComment(beta, commentRes.comment_view.comment)
147 ).comments[0].comment.id;
149 // The beta admin removes it (the community lives on beta)
150 let removeCommentRes = await removeComment(beta, true, betaCommentId);
151 expect(removeCommentRes.comment_view.comment.removed).toBe(true);
153 // Make sure that comment is removed on alpha (it gets pushed since an admin from beta removed it)
154 let refetchedPost = await getPost(alpha, postRes.post_view.post.id);
155 expect(refetchedPost.comments[0].comment.removed).toBe(true);
157 let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
158 expect(unremoveCommentRes.comment_view.comment.removed).toBe(false);
160 // Make sure that comment is unremoved on beta
161 let refetchedPost2 = await getPost(alpha, postRes.post_view.post.id);
162 expect(refetchedPost2.comments[0].comment.removed).toBe(false);
163 assertCommentFederation(
164 refetchedPost2.comments[0],
165 unremoveCommentRes.comment_view
169 test('Remove a comment from admin and community on different instance', async () => {
170 let alphaUser = await registerUser(alpha);
171 let newAlphaApi: API = {
172 client: alpha.client,
176 // New alpha user creates a community, post, and comment.
177 let newCommunity = await createCommunity(newAlphaApi);
178 let newPost = await createPost(
180 newCommunity.community_view.community.id
182 let commentRes = await createComment(newAlphaApi, newPost.post_view.post.id);
183 expect(commentRes.comment_view.comment.content).toBeDefined();
185 // Beta searches that to cache it, then removes it
186 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
187 let betaComment = searchBeta.comments[0];
188 let removeCommentRes = await removeComment(
191 betaComment.comment.id
193 expect(removeCommentRes.comment_view.comment.removed).toBe(true);
195 // Make sure its not removed on alpha
196 let refetchedPost = await getPost(newAlphaApi, newPost.post_view.post.id);
197 expect(refetchedPost.comments[0].comment.removed).toBe(false);
198 assertCommentFederation(refetchedPost.comments[0], commentRes.comment_view);
201 test('Unlike a comment', async () => {
202 let commentRes = await createComment(alpha, postRes.post_view.post.id);
203 let unlike = await likeComment(alpha, 0, commentRes.comment_view.comment);
204 expect(unlike.comment_view.counts.score).toBe(0);
206 // Make sure that post is unliked on beta
207 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
208 let betaComment = searchBeta.comments[0];
209 expect(betaComment).toBeDefined();
210 expect(betaComment.community.local).toBe(true);
211 expect(betaComment.creator.local).toBe(false);
212 expect(betaComment.counts.score).toBe(0);
215 test('Federated comment like', async () => {
216 let commentRes = await createComment(alpha, postRes.post_view.post.id);
218 // Find the comment on beta
219 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
220 let betaComment = searchBeta.comments[0];
222 let like = await likeComment(beta, 1, betaComment.comment);
223 expect(like.comment_view.counts.score).toBe(2);
225 // Get the post from alpha, check the likes
226 let post = await getPost(alpha, postRes.post_view.post.id);
227 expect(post.comments[0].counts.score).toBe(2);
230 test('Reply to a comment', async () => {
231 // Create a comment on alpha, find it on beta
232 let commentRes = await createComment(alpha, postRes.post_view.post.id);
233 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
234 let betaComment = searchBeta.comments[0];
236 // find that comment id on beta
239 let replyRes = await createComment(
242 betaComment.comment.id
244 expect(replyRes.comment_view.comment.content).toBeDefined();
245 expect(replyRes.comment_view.community.local).toBe(true);
246 expect(replyRes.comment_view.creator.local).toBe(true);
247 expect(replyRes.comment_view.comment.parent_id).toBe(betaComment.comment.id);
248 expect(replyRes.comment_view.counts.score).toBe(1);
250 // Make sure that comment is seen on alpha
251 // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
252 // comment, isn't working.
253 // let searchAlpha = await searchComment(alpha, replyRes.comment);
254 let post = await getPost(alpha, postRes.post_view.post.id);
255 let alphaComment = post.comments[0];
256 expect(alphaComment.comment.content).toBeDefined();
257 expect(alphaComment.comment.parent_id).toBe(post.comments[1].comment.id);
258 expect(alphaComment.community.local).toBe(false);
259 expect(alphaComment.creator.local).toBe(false);
260 expect(alphaComment.counts.score).toBe(1);
261 assertCommentFederation(alphaComment, replyRes.comment_view);
264 test('Mention beta', async () => {
265 // Create a mention on alpha
266 let mentionContent = 'A test mention of @lemmy_beta@lemmy-beta:8551';
267 let commentRes = await createComment(alpha, postRes.post_view.post.id);
268 let mentionRes = await createComment(
270 postRes.post_view.post.id,
271 commentRes.comment_view.comment.id,
274 expect(mentionRes.comment_view.comment.content).toBeDefined();
275 expect(mentionRes.comment_view.community.local).toBe(false);
276 expect(mentionRes.comment_view.creator.local).toBe(true);
277 expect(mentionRes.comment_view.counts.score).toBe(1);
279 let mentionsRes = await getMentions(beta);
280 expect(mentionsRes.mentions[0].comment.content).toBeDefined();
281 expect(mentionsRes.mentions[0].community.local).toBe(true);
282 expect(mentionsRes.mentions[0].creator.local).toBe(false);
283 expect(mentionsRes.mentions[0].counts.score).toBe(1);
286 test('Comment Search', async () => {
287 let commentRes = await createComment(alpha, postRes.post_view.post.id);
288 let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
289 assertCommentFederation(searchBeta.comments[0], commentRes.comment_view);
292 test('A and G subscribe to B (center) A posts, G mentions B, it gets announced to A', async () => {
293 // Create a local post
294 let alphaPost = await createPost(alpha, 2);
295 expect(alphaPost.post_view.community.local).toBe(true);
297 // Make sure gamma sees it
298 let search = await searchPost(gamma, alphaPost.post_view.post);
299 let gammaPost = search.posts[0];
302 'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551';
303 let commentRes = await createComment(
309 expect(commentRes.comment_view.comment.content).toBe(commentContent);
310 expect(commentRes.comment_view.community.local).toBe(false);
311 expect(commentRes.comment_view.creator.local).toBe(true);
312 expect(commentRes.comment_view.counts.score).toBe(1);
314 // Make sure alpha sees it
315 let alphaPost2 = await getPost(alpha, alphaPost.post_view.post.id);
316 expect(alphaPost2.comments[0].comment.content).toBe(commentContent);
317 expect(alphaPost2.comments[0].community.local).toBe(true);
318 expect(alphaPost2.comments[0].creator.local).toBe(false);
319 expect(alphaPost2.comments[0].counts.score).toBe(1);
320 assertCommentFederation(alphaPost2.comments[0], commentRes.comment_view);
322 // Make sure beta has mentions
323 let mentionsRes = await getMentions(beta);
324 expect(mentionsRes.mentions[0].comment.content).toBe(commentContent);
325 expect(mentionsRes.mentions[0].community.local).toBe(false);
326 expect(mentionsRes.mentions[0].creator.local).toBe(false);
327 // TODO this is failing because fetchInReplyTos aren't getting score
328 // expect(mentionsRes.mentions[0].score).toBe(1);
331 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 () => {
332 // Unfollow all remote communities
333 let followed = await unfollowRemotes(alpha);
335 followed.communities.filter(c => c.community.local == false).length
338 // B creates a post, and two comments, should be invisible to A
339 let postRes = await createPost(beta, 2);
340 expect(postRes.post_view.post.name).toBeDefined();
342 let parentCommentContent = 'An invisible top level comment from beta';
343 let parentCommentRes = await createComment(
345 postRes.post_view.post.id,
349 expect(parentCommentRes.comment_view.comment.content).toBe(
353 // B creates a comment, then a child one of that.
354 let childCommentContent = 'An invisible child comment from beta';
355 let childCommentRes = await createComment(
357 postRes.post_view.post.id,
358 parentCommentRes.comment_view.comment.id,
361 expect(childCommentRes.comment_view.comment.content).toBe(
366 let follow = await followBeta(alpha);
367 expect(follow.community_view.community.local).toBe(false);
368 expect(follow.community_view.community.name).toBe('main');
370 // An update to the child comment on beta, should push the post, parent, and child to alpha now
371 let updatedCommentContent = 'An update child comment from beta';
372 let updateRes = await editComment(
374 childCommentRes.comment_view.comment.id,
375 updatedCommentContent
377 expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent);
379 // Get the post from alpha
380 let search = await searchPost(alpha, postRes.post_view.post);
381 let alphaPostB = search.posts[0];
383 let alphaPost = await getPost(alpha, alphaPostB.post.id);
384 expect(alphaPost.post_view.post.name).toBeDefined();
385 assertCommentFederation(alphaPost.comments[1], parentCommentRes.comment_view);
386 assertCommentFederation(alphaPost.comments[0], updateRes.comment_view);
387 expect(alphaPost.post_view.community.local).toBe(false);
388 expect(alphaPost.post_view.creator.local).toBe(false);
390 await unfollowRemotes(alpha);