1 jest.setTimeout(120000);
12 searchForBetaCommunity,
27 } from 'lemmy-js-client';
29 import { PostResponse } from 'lemmy-js-client';
31 let postRes: PostResponse;
33 beforeAll(async () => {
35 await followBeta(alpha);
36 await followBeta(gamma);
37 let search = await searchForBetaCommunity(alpha);
39 postRes = await createPost(
41 search.communities.filter(c => c.local == false)[0].id
45 afterAll(async () => {
46 await unfollowRemotes(alpha);
47 await unfollowRemotes(gamma);
50 function assertCommentFederation(
52 commentTwo: Comment) {
53 expect(commentOne.ap_id).toBe(commentOne.ap_id);
54 expect(commentOne.content).toBe(commentTwo.content);
55 expect(commentOne.creator_name).toBe(commentTwo.creator_name);
56 expect(commentOne.community_actor_id).toBe(commentTwo.community_actor_id);
57 expect(commentOne.published).toBe(commentTwo.published);
58 expect(commentOne.updated).toBe(commentOne.updated);
59 expect(commentOne.deleted).toBe(commentOne.deleted);
60 expect(commentOne.removed).toBe(commentOne.removed);
63 test('Create a comment', async () => {
64 let commentRes = await createComment(alpha, postRes.post.id);
65 expect(commentRes.comment.content).toBeDefined();
66 expect(commentRes.comment.community_local).toBe(false);
67 expect(commentRes.comment.creator_local).toBe(true);
68 expect(commentRes.comment.score).toBe(1);
71 // Make sure that comment is liked on beta
72 let searchBeta = await searchComment(beta, commentRes.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.score).toBe(1);
78 assertCommentFederation(betaComment, commentRes.comment);
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.id);
88 // Federate the comment first
89 let searchBeta = await searchComment(beta, commentRes.comment);
90 assertCommentFederation(searchBeta.comments[0], commentRes.comment);
93 let updateCommentRes = await updateComment(alpha, commentRes.comment.id);
94 expect(updateCommentRes.comment.content).toBe(
95 'A jest test federated comment update'
97 expect(updateCommentRes.comment.community_local).toBe(false);
98 expect(updateCommentRes.comment.creator_local).toBe(true);
101 // Make sure that post is updated on beta
102 let searchBetaUpdated = await searchComment(beta, commentRes.comment);
103 assertCommentFederation(searchBetaUpdated.comments[0], updateCommentRes.comment);
106 test('Delete a comment', async () => {
107 let commentRes = await createComment(alpha, postRes.post.id);
110 let deleteCommentRes = await deleteComment(
113 commentRes.comment.id
115 expect(deleteCommentRes.comment.deleted).toBe(true);
118 // Make sure that comment is undefined on beta
119 let searchBeta = await searchComment(beta, commentRes.comment);
120 let betaComment = searchBeta.comments[0];
121 expect(betaComment).toBeUndefined();
124 let undeleteCommentRes = await deleteComment(
127 commentRes.comment.id
129 expect(undeleteCommentRes.comment.deleted).toBe(false);
132 // Make sure that comment is undeleted on beta
133 let searchBeta2 = await searchComment(beta, commentRes.comment);
134 let betaComment2 = searchBeta2.comments[0];
135 expect(betaComment2.deleted).toBe(false);
136 assertCommentFederation(searchBeta2.comments[0], undeleteCommentRes.comment);
139 test('Remove a comment from admin and community on the same instance', async () => {
140 let commentRes = await createComment(alpha, postRes.post.id);
143 // Get the id for beta
144 let betaCommentId = (await searchComment(beta, commentRes.comment))
147 // The beta admin removes it (the community lives on beta)
148 let removeCommentRes = await removeComment(beta, true, betaCommentId);
149 expect(removeCommentRes.comment.removed).toBe(true);
152 // Make sure that comment is removed on alpha (it gets pushed since an admin from beta removed it)
153 let refetchedPost = await getPost(alpha, postRes.post.id);
154 expect(refetchedPost.comments[0].removed).toBe(true);
156 let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
157 expect(unremoveCommentRes.comment.removed).toBe(false);
160 // Make sure that comment is unremoved on beta
161 let refetchedPost2 = await getPost(alpha, postRes.post.id);
162 expect(refetchedPost2.comments[0].removed).toBe(false);
163 assertCommentFederation(refetchedPost2.comments[0], unremoveCommentRes.comment);
166 test('Remove a comment from admin and community on different instance', async () => {
167 let alphaUser = await registerUser(alpha);
168 let newAlphaApi: API = {
169 client: alpha.client,
173 // New alpha user creates a community, post, and comment.
174 let newCommunity = await createCommunity(newAlphaApi);
176 let newPost = await createPost(newAlphaApi, newCommunity.community.id);
178 let commentRes = await createComment(newAlphaApi, newPost.post.id);
179 expect(commentRes.comment.content).toBeDefined();
182 // Beta searches that to cache it, then removes it
183 let searchBeta = await searchComment(beta, commentRes.comment);
184 let betaComment = searchBeta.comments[0];
185 let removeCommentRes = await removeComment(beta, true, betaComment.id);
186 expect(removeCommentRes.comment.removed).toBe(true);
189 // Make sure its not removed on alpha
190 let refetchedPost = await getPost(newAlphaApi, newPost.post.id);
191 expect(refetchedPost.comments[0].removed).toBe(false);
192 assertCommentFederation(refetchedPost.comments[0], commentRes.comment);
195 test('Unlike a comment', async () => {
196 let commentRes = await createComment(alpha, postRes.post.id);
198 let unlike = await likeComment(alpha, 0, commentRes.comment);
199 expect(unlike.comment.score).toBe(0);
202 // Make sure that post is unliked on beta
203 let searchBeta = await searchComment(beta, commentRes.comment);
204 let betaComment = searchBeta.comments[0];
205 expect(betaComment).toBeDefined();
206 expect(betaComment.community_local).toBe(true);
207 expect(betaComment.creator_local).toBe(false);
208 expect(betaComment.score).toBe(0);
211 test('Federated comment like', async () => {
212 let commentRes = await createComment(alpha, postRes.post.id);
215 // Find the comment on beta
216 let searchBeta = await searchComment(beta, commentRes.comment);
217 let betaComment = searchBeta.comments[0];
219 let like = await likeComment(beta, 1, betaComment);
220 expect(like.comment.score).toBe(2);
223 // Get the post from alpha, check the likes
224 let post = await getPost(alpha, postRes.post.id);
225 expect(post.comments[0].score).toBe(2);
228 test('Reply to a comment', async () => {
229 // Create a comment on alpha, find it on beta
230 let commentRes = await createComment(alpha, postRes.post.id);
232 let searchBeta = await searchComment(beta, commentRes.comment);
233 let betaComment = searchBeta.comments[0];
235 // find that comment id on beta
238 let replyRes = await createComment(beta, betaComment.post_id, betaComment.id);
239 expect(replyRes.comment.content).toBeDefined();
240 expect(replyRes.comment.community_local).toBe(true);
241 expect(replyRes.comment.creator_local).toBe(true);
242 expect(replyRes.comment.parent_id).toBe(betaComment.id);
243 expect(replyRes.comment.score).toBe(1);
246 // Make sure that comment is seen on alpha
247 // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
248 // comment, isn't working.
249 // let searchAlpha = await searchComment(alpha, replyRes.comment);
250 let post = await getPost(alpha, postRes.post.id);
251 let alphaComment = post.comments[0];
252 expect(alphaComment.content).toBeDefined();
253 expect(alphaComment.parent_id).toBe(post.comments[1].id);
254 expect(alphaComment.community_local).toBe(false);
255 expect(alphaComment.creator_local).toBe(false);
256 expect(alphaComment.score).toBe(1);
257 assertCommentFederation(alphaComment, replyRes.comment);
260 test('Mention beta', async () => {
261 // Create a mention on alpha
262 let mentionContent = 'A test mention of @lemmy_beta@lemmy-beta:8551';
263 let commentRes = await createComment(alpha, postRes.post.id);
265 let mentionRes = await createComment(
268 commentRes.comment.id,
271 expect(mentionRes.comment.content).toBeDefined();
272 expect(mentionRes.comment.community_local).toBe(false);
273 expect(mentionRes.comment.creator_local).toBe(true);
274 expect(mentionRes.comment.score).toBe(1);
277 let mentionsRes = await getMentions(beta);
278 expect(mentionsRes.mentions[0].content).toBeDefined();
279 expect(mentionsRes.mentions[0].community_local).toBe(true);
280 expect(mentionsRes.mentions[0].creator_local).toBe(false);
281 expect(mentionsRes.mentions[0].score).toBe(1);
284 test('Comment Search', async () => {
285 let commentRes = await createComment(alpha, postRes.post.id);
287 let searchBeta = await searchComment(beta, commentRes.comment);
288 assertCommentFederation(searchBeta.comments[0], commentRes.comment);
291 test('A and G subscribe to B (center) A posts, G mentions B, it gets announced to A', async () => {
292 // Create a local post
293 let alphaPost = await createPost(alpha, 2);
294 expect(alphaPost.post.community_local).toBe(true);
297 // Make sure gamma sees it
298 let search = await searchPost(gamma, alphaPost.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.content).toBe(commentContent);
310 expect(commentRes.comment.community_local).toBe(false);
311 expect(commentRes.comment.creator_local).toBe(true);
312 expect(commentRes.comment.score).toBe(1);
315 // Make sure alpha sees it
316 let alphaPost2 = await getPost(alpha, alphaPost.post.id);
317 expect(alphaPost2.comments[0].content).toBe(commentContent);
318 expect(alphaPost2.comments[0].community_local).toBe(true);
319 expect(alphaPost2.comments[0].creator_local).toBe(false);
320 expect(alphaPost2.comments[0].score).toBe(1);
321 assertCommentFederation(alphaPost2.comments[0], commentRes.comment);
323 // Make sure beta has mentions
324 let mentionsRes = await getMentions(beta);
325 expect(mentionsRes.mentions[0].content).toBe(commentContent);
326 expect(mentionsRes.mentions[0].community_local).toBe(false);
327 expect(mentionsRes.mentions[0].creator_local).toBe(false);
328 // TODO this is failing because fetchInReplyTos aren't getting score
329 // expect(mentionsRes.mentions[0].score).toBe(1);
332 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 () => {
333 // Unfollow all remote communities
334 let followed = await unfollowRemotes(alpha);
336 followed.communities.filter(c => c.community_local == false).length
339 // B creates a post, and two comments, should be invisible to A
340 let postRes = await createPost(beta, 2);
341 expect(postRes.post.name).toBeDefined();
344 let parentCommentContent = 'An invisible top level comment from beta';
345 let parentCommentRes = await createComment(
351 expect(parentCommentRes.comment.content).toBe(parentCommentContent);
354 // B creates a comment, then a child one of that.
355 let childCommentContent = 'An invisible child comment from beta';
356 let childCommentRes = await createComment(
359 parentCommentRes.comment.id,
362 expect(childCommentRes.comment.content).toBe(childCommentContent);
366 let follow = await followBeta(alpha);
367 expect(follow.community.local).toBe(false);
368 expect(follow.community.name).toBe('main');
371 // An update to the child comment on beta, should push the post, parent, and child to alpha now
372 let updatedCommentContent = 'An update child comment from beta';
373 let updateRes = await updateComment(
375 childCommentRes.comment.id,
376 updatedCommentContent
378 expect(updateRes.comment.content).toBe(updatedCommentContent);
381 // Get the post from alpha
382 let search = await searchPost(alpha, postRes.post);
383 let alphaPostB = search.posts[0];
386 let alphaPost = await getPost(alpha, alphaPostB.id);
387 expect(alphaPost.post.name).toBeDefined();
388 assertCommentFederation(alphaPost.comments[1], parentCommentRes.comment);
389 assertCommentFederation(alphaPost.comments[0], updateRes.comment);
390 expect(alphaPost.post.community_local).toBe(false);
391 expect(alphaPost.post.creator_local).toBe(false);