1 jest.setTimeout(180000);
12 searchForBetaCommunity,
26 } 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);
37 postRes = await createPost(
39 search.communities.filter(c => c.local == false)[0].id
43 afterAll(async () => {
44 await unfollowRemotes(alpha);
45 await unfollowRemotes(gamma);
48 function assertCommentFederation(
50 commentTwo: Comment) {
51 expect(commentOne.ap_id).toBe(commentOne.ap_id);
52 expect(commentOne.content).toBe(commentTwo.content);
53 expect(commentOne.creator_name).toBe(commentTwo.creator_name);
54 expect(commentOne.community_actor_id).toBe(commentTwo.community_actor_id);
55 expect(commentOne.published).toBe(commentTwo.published);
56 expect(commentOne.updated).toBe(commentOne.updated);
57 expect(commentOne.deleted).toBe(commentOne.deleted);
58 expect(commentOne.removed).toBe(commentOne.removed);
61 test('Create a comment', async () => {
62 let commentRes = await createComment(alpha, postRes.post.id);
63 expect(commentRes.comment.content).toBeDefined();
64 expect(commentRes.comment.community_local).toBe(false);
65 expect(commentRes.comment.creator_local).toBe(true);
66 expect(commentRes.comment.score).toBe(1);
68 // Make sure that comment is liked on beta
69 let searchBeta = await searchComment(beta, commentRes.comment);
70 let betaComment = searchBeta.comments[0];
71 expect(betaComment).toBeDefined();
72 expect(betaComment.community_local).toBe(true);
73 expect(betaComment.creator_local).toBe(false);
74 expect(betaComment.score).toBe(1);
75 assertCommentFederation(betaComment, commentRes.comment);
78 test('Create a comment in a non-existent post', async () => {
79 let commentRes = await createComment(alpha, -1);
80 expect(commentRes).toStrictEqual({ error: 'couldnt_find_post' });
83 test('Update a comment', async () => {
84 let commentRes = await createComment(alpha, postRes.post.id);
85 // Federate the comment first
86 let searchBeta = await searchComment(beta, commentRes.comment);
87 assertCommentFederation(searchBeta.comments[0], commentRes.comment);
89 let updateCommentRes = await updateComment(alpha, commentRes.comment.id);
90 expect(updateCommentRes.comment.content).toBe(
91 'A jest test federated comment update'
93 expect(updateCommentRes.comment.community_local).toBe(false);
94 expect(updateCommentRes.comment.creator_local).toBe(true);
96 // Make sure that post is updated on beta
97 let searchBetaUpdated = await searchComment(beta, commentRes.comment);
98 assertCommentFederation(searchBetaUpdated.comments[0], updateCommentRes.comment);
101 test('Delete a comment', async () => {
102 let commentRes = await createComment(alpha, postRes.post.id);
104 let deleteCommentRes = await deleteComment(
107 commentRes.comment.id
109 expect(deleteCommentRes.comment.deleted).toBe(true);
111 // Make sure that comment is undefined on beta
112 let searchBeta = await searchComment(beta, commentRes.comment);
113 let betaComment = searchBeta.comments[0];
114 expect(betaComment).toBeUndefined();
116 let undeleteCommentRes = await deleteComment(
119 commentRes.comment.id
121 expect(undeleteCommentRes.comment.deleted).toBe(false);
123 // Make sure that comment is undeleted on beta
124 let searchBeta2 = await searchComment(beta, commentRes.comment);
125 let betaComment2 = searchBeta2.comments[0];
126 expect(betaComment2.deleted).toBe(false);
127 assertCommentFederation(searchBeta2.comments[0], undeleteCommentRes.comment);
130 test('Remove a comment from admin and community on the same instance', async () => {
131 let commentRes = await createComment(alpha, postRes.post.id);
133 // Get the id for beta
134 let betaCommentId = (await searchComment(beta, commentRes.comment))
137 // The beta admin removes it (the community lives on beta)
138 let removeCommentRes = await removeComment(beta, true, betaCommentId);
139 expect(removeCommentRes.comment.removed).toBe(true);
141 // Make sure that comment is removed on alpha (it gets pushed since an admin from beta removed it)
142 let refetchedPost = await getPost(alpha, postRes.post.id);
143 expect(refetchedPost.comments[0].removed).toBe(true);
145 let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
146 expect(unremoveCommentRes.comment.removed).toBe(false);
148 // Make sure that comment is unremoved on beta
149 let refetchedPost2 = await getPost(alpha, postRes.post.id);
150 expect(refetchedPost2.comments[0].removed).toBe(false);
151 assertCommentFederation(refetchedPost2.comments[0], unremoveCommentRes.comment);
154 test('Remove a comment from admin and community on different instance', async () => {
155 let alphaUser = await registerUser(alpha);
156 let newAlphaApi: API = {
157 client: alpha.client,
161 // New alpha user creates a community, post, and comment.
162 let newCommunity = await createCommunity(newAlphaApi);
163 let newPost = await createPost(newAlphaApi, newCommunity.community.id);
164 let commentRes = await createComment(newAlphaApi, newPost.post.id);
165 expect(commentRes.comment.content).toBeDefined();
167 // Beta searches that to cache it, then removes it
168 let searchBeta = await searchComment(beta, commentRes.comment);
169 let betaComment = searchBeta.comments[0];
170 let removeCommentRes = await removeComment(beta, true, betaComment.id);
171 expect(removeCommentRes.comment.removed).toBe(true);
173 // Make sure its not removed on alpha
174 let refetchedPost = await getPost(newAlphaApi, newPost.post.id);
175 expect(refetchedPost.comments[0].removed).toBe(false);
176 assertCommentFederation(refetchedPost.comments[0], commentRes.comment);
179 test('Unlike a comment', async () => {
180 let commentRes = await createComment(alpha, postRes.post.id);
181 let unlike = await likeComment(alpha, 0, commentRes.comment);
182 expect(unlike.comment.score).toBe(0);
184 // Make sure that post is unliked on beta
185 let searchBeta = await searchComment(beta, commentRes.comment);
186 let betaComment = searchBeta.comments[0];
187 expect(betaComment).toBeDefined();
188 expect(betaComment.community_local).toBe(true);
189 expect(betaComment.creator_local).toBe(false);
190 expect(betaComment.score).toBe(0);
193 test('Federated comment like', async () => {
194 let commentRes = await createComment(alpha, postRes.post.id);
196 // Find the comment on beta
197 let searchBeta = await searchComment(beta, commentRes.comment);
198 let betaComment = searchBeta.comments[0];
200 let like = await likeComment(beta, 1, betaComment);
201 expect(like.comment.score).toBe(2);
203 // Get the post from alpha, check the likes
204 let post = await getPost(alpha, postRes.post.id);
205 expect(post.comments[0].score).toBe(2);
208 test('Reply to a comment', async () => {
209 // Create a comment on alpha, find it on beta
210 let commentRes = await createComment(alpha, postRes.post.id);
211 let searchBeta = await searchComment(beta, commentRes.comment);
212 let betaComment = searchBeta.comments[0];
214 // find that comment id on beta
217 let replyRes = await createComment(beta, betaComment.post_id, betaComment.id);
218 expect(replyRes.comment.content).toBeDefined();
219 expect(replyRes.comment.community_local).toBe(true);
220 expect(replyRes.comment.creator_local).toBe(true);
221 expect(replyRes.comment.parent_id).toBe(betaComment.id);
222 expect(replyRes.comment.score).toBe(1);
224 // Make sure that comment is seen on alpha
225 // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
226 // comment, isn't working.
227 // let searchAlpha = await searchComment(alpha, replyRes.comment);
228 let post = await getPost(alpha, postRes.post.id);
229 let alphaComment = post.comments[0];
230 expect(alphaComment.content).toBeDefined();
231 expect(alphaComment.parent_id).toBe(post.comments[1].id);
232 expect(alphaComment.community_local).toBe(false);
233 expect(alphaComment.creator_local).toBe(false);
234 expect(alphaComment.score).toBe(1);
235 assertCommentFederation(alphaComment, replyRes.comment);
238 test('Mention beta', async () => {
239 // Create a mention on alpha
240 let mentionContent = 'A test mention of @lemmy_beta@lemmy-beta:8551';
241 let commentRes = await createComment(alpha, postRes.post.id);
242 let mentionRes = await createComment(
245 commentRes.comment.id,
248 expect(mentionRes.comment.content).toBeDefined();
249 expect(mentionRes.comment.community_local).toBe(false);
250 expect(mentionRes.comment.creator_local).toBe(true);
251 expect(mentionRes.comment.score).toBe(1);
253 let mentionsRes = await getMentions(beta);
254 expect(mentionsRes.mentions[0].content).toBeDefined();
255 expect(mentionsRes.mentions[0].community_local).toBe(true);
256 expect(mentionsRes.mentions[0].creator_local).toBe(false);
257 expect(mentionsRes.mentions[0].score).toBe(1);
260 test('Comment Search', async () => {
261 let commentRes = await createComment(alpha, postRes.post.id);
262 let searchBeta = await searchComment(beta, commentRes.comment);
263 assertCommentFederation(searchBeta.comments[0], commentRes.comment);
266 test('A and G subscribe to B (center) A posts, G mentions B, it gets announced to A', async () => {
267 // Create a local post
268 let alphaPost = await createPost(alpha, 2);
269 expect(alphaPost.post.community_local).toBe(true);
271 // Make sure gamma sees it
272 let search = await searchPost(gamma, alphaPost.post);
273 let gammaPost = search.posts[0];
276 'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551';
277 let commentRes = await createComment(
283 expect(commentRes.comment.content).toBe(commentContent);
284 expect(commentRes.comment.community_local).toBe(false);
285 expect(commentRes.comment.creator_local).toBe(true);
286 expect(commentRes.comment.score).toBe(1);
288 // Make sure alpha sees it
289 let alphaPost2 = await getPost(alpha, alphaPost.post.id);
290 expect(alphaPost2.comments[0].content).toBe(commentContent);
291 expect(alphaPost2.comments[0].community_local).toBe(true);
292 expect(alphaPost2.comments[0].creator_local).toBe(false);
293 expect(alphaPost2.comments[0].score).toBe(1);
294 assertCommentFederation(alphaPost2.comments[0], commentRes.comment);
296 // Make sure beta has mentions
297 let mentionsRes = await getMentions(beta);
298 expect(mentionsRes.mentions[0].content).toBe(commentContent);
299 expect(mentionsRes.mentions[0].community_local).toBe(false);
300 expect(mentionsRes.mentions[0].creator_local).toBe(false);
301 // TODO this is failing because fetchInReplyTos aren't getting score
302 // expect(mentionsRes.mentions[0].score).toBe(1);
305 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 () => {
306 // Unfollow all remote communities
307 let followed = await unfollowRemotes(alpha);
309 followed.communities.filter(c => c.community_local == false).length
312 // B creates a post, and two comments, should be invisible to A
313 let postRes = await createPost(beta, 2);
314 expect(postRes.post.name).toBeDefined();
316 let parentCommentContent = 'An invisible top level comment from beta';
317 let parentCommentRes = await createComment(
323 expect(parentCommentRes.comment.content).toBe(parentCommentContent);
325 // B creates a comment, then a child one of that.
326 let childCommentContent = 'An invisible child comment from beta';
327 let childCommentRes = await createComment(
330 parentCommentRes.comment.id,
333 expect(childCommentRes.comment.content).toBe(childCommentContent);
336 let follow = await followBeta(alpha);
337 expect(follow.community.local).toBe(false);
338 expect(follow.community.name).toBe('main');
340 // An update to the child comment on beta, should push the post, parent, and child to alpha now
341 let updatedCommentContent = 'An update child comment from beta';
342 let updateRes = await updateComment(
344 childCommentRes.comment.id,
345 updatedCommentContent
347 expect(updateRes.comment.content).toBe(updatedCommentContent);
349 // Get the post from alpha
350 let search = await searchPost(alpha, postRes.post);
351 let alphaPostB = search.posts[0];
353 let alphaPost = await getPost(alpha, alphaPostB.id);
354 expect(alphaPost.post.name).toBeDefined();
355 assertCommentFederation(alphaPost.comments[1], parentCommentRes.comment);
356 assertCommentFederation(alphaPost.comments[0], updateRes.comment);
357 expect(alphaPost.post.community_local).toBe(false);
358 expect(alphaPost.post.creator_local).toBe(false);