1 jest.setTimeout(180000);
12 searchForBetaCommunity,
28 } from 'lemmy-js-client';
30 import { PostResponse } from 'lemmy-js-client';
32 let postRes: PostResponse;
34 beforeAll(async () => {
36 await followBeta(alpha);
37 await followBeta(gamma);
38 let search = await searchForBetaCommunity(alpha);
40 postRes = await createPost(
42 search.communities.filter(c => c.local == false)[0].id
46 afterAll(async () => {
47 await unfollowRemotes(alpha);
48 await unfollowRemotes(gamma);
51 function assertCommentFederation(
53 commentTwo: Comment) {
54 expect(commentOne.ap_id).toBe(commentOne.ap_id);
55 expect(commentOne.content).toBe(commentTwo.content);
56 expect(commentOne.creator_name).toBe(commentTwo.creator_name);
57 expect(commentOne.community_actor_id).toBe(commentTwo.community_actor_id);
58 expect(commentOne.published).toBe(commentTwo.published);
59 expect(commentOne.updated).toBe(commentOne.updated);
60 expect(commentOne.deleted).toBe(commentOne.deleted);
61 expect(commentOne.removed).toBe(commentOne.removed);
64 test('Create a comment', async () => {
65 let commentRes = await createComment(alpha, postRes.post.id);
66 expect(commentRes.comment.content).toBeDefined();
67 expect(commentRes.comment.community_local).toBe(false);
68 expect(commentRes.comment.creator_local).toBe(true);
69 expect(commentRes.comment.score).toBe(1);
72 // Make sure that comment is liked on beta
73 let searchBeta = await searchComment(beta, commentRes.comment);
74 let betaComment = searchBeta.comments[0];
75 expect(betaComment).toBeDefined();
76 expect(betaComment.community_local).toBe(true);
77 expect(betaComment.creator_local).toBe(false);
78 expect(betaComment.score).toBe(1);
79 assertCommentFederation(betaComment, commentRes.comment);
82 test('Create a comment in a non-existent post', async () => {
83 let commentRes = await createComment(alpha, -1);
84 expect(commentRes).toStrictEqual({ error: 'couldnt_find_post' });
87 test('Update a comment', async () => {
88 let commentRes = await createComment(alpha, postRes.post.id);
89 // Federate the comment first
90 let searchBeta = await searchComment(beta, commentRes.comment);
91 assertCommentFederation(searchBeta.comments[0], commentRes.comment);
94 let updateCommentRes = await updateComment(alpha, commentRes.comment.id);
95 expect(updateCommentRes.comment.content).toBe(
96 'A jest test federated comment update'
98 expect(updateCommentRes.comment.community_local).toBe(false);
99 expect(updateCommentRes.comment.creator_local).toBe(true);
102 // Make sure that post is updated on beta
103 let searchBetaUpdated = await searchComment(beta, commentRes.comment);
104 assertCommentFederation(searchBetaUpdated.comments[0], updateCommentRes.comment);
107 test('Delete a comment', async () => {
108 let commentRes = await createComment(alpha, postRes.post.id);
111 let deleteCommentRes = await deleteComment(
114 commentRes.comment.id
116 expect(deleteCommentRes.comment.deleted).toBe(true);
119 // Make sure that comment is undefined on beta
120 let searchBeta = await searchComment(beta, commentRes.comment);
121 let betaComment = searchBeta.comments[0];
122 expect(betaComment).toBeUndefined();
125 let undeleteCommentRes = await deleteComment(
128 commentRes.comment.id
130 expect(undeleteCommentRes.comment.deleted).toBe(false);
133 // Make sure that comment is undeleted on beta
134 let searchBeta2 = await searchComment(beta, commentRes.comment);
135 let betaComment2 = searchBeta2.comments[0];
136 expect(betaComment2.deleted).toBe(false);
137 assertCommentFederation(searchBeta2.comments[0], undeleteCommentRes.comment);
140 test('Remove a comment from admin and community on the same instance', async () => {
141 let commentRes = await createComment(alpha, postRes.post.id);
144 // Get the id for beta
145 let betaCommentId = (await searchComment(beta, commentRes.comment))
148 // The beta admin removes it (the community lives on beta)
149 let removeCommentRes = await removeComment(beta, true, betaCommentId);
150 expect(removeCommentRes.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.id);
155 expect(refetchedPost.comments[0].removed).toBe(true);
157 let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
158 expect(unremoveCommentRes.comment.removed).toBe(false);
161 // Make sure that comment is unremoved on beta
162 let refetchedPost2 = await getPost(alpha, postRes.post.id);
163 expect(refetchedPost2.comments[0].removed).toBe(false);
164 assertCommentFederation(refetchedPost2.comments[0], unremoveCommentRes.comment);
167 test('Remove a comment from admin and community on different instance', async () => {
168 let alphaUser = await registerUser(alpha);
169 let newAlphaApi: API = {
170 client: alpha.client,
174 // New alpha user creates a community, post, and comment.
175 let newCommunity = await createCommunity(newAlphaApi);
177 let newPost = await createPost(newAlphaApi, newCommunity.community.id);
179 let commentRes = await createComment(newAlphaApi, newPost.post.id);
180 expect(commentRes.comment.content).toBeDefined();
183 // Beta searches that to cache it, then removes it
184 let searchBeta = await searchComment(beta, commentRes.comment);
185 let betaComment = searchBeta.comments[0];
186 let removeCommentRes = await removeComment(beta, true, betaComment.id);
187 expect(removeCommentRes.comment.removed).toBe(true);
190 // Make sure its not removed on alpha
191 let refetchedPost = await getPost(newAlphaApi, newPost.post.id);
192 expect(refetchedPost.comments[0].removed).toBe(false);
193 assertCommentFederation(refetchedPost.comments[0], commentRes.comment);
196 test('Unlike a comment', async () => {
197 let commentRes = await createComment(alpha, postRes.post.id);
199 let unlike = await likeComment(alpha, 0, commentRes.comment);
200 expect(unlike.comment.score).toBe(0);
203 // Make sure that post is unliked on beta
204 let searchBeta = await searchComment(beta, commentRes.comment);
205 let betaComment = searchBeta.comments[0];
206 expect(betaComment).toBeDefined();
207 expect(betaComment.community_local).toBe(true);
208 expect(betaComment.creator_local).toBe(false);
209 expect(betaComment.score).toBe(0);
212 test('Federated comment like', async () => {
213 let commentRes = await createComment(alpha, postRes.post.id);
216 // Find the comment on beta
217 let searchBeta = await searchComment(beta, commentRes.comment);
218 let betaComment = searchBeta.comments[0];
220 let like = await likeComment(beta, 1, betaComment);
221 expect(like.comment.score).toBe(2);
224 // Get the post from alpha, check the likes
225 let post = await getPost(alpha, postRes.post.id);
226 expect(post.comments[0].score).toBe(2);
229 test('Reply to a comment', async () => {
230 // Create a comment on alpha, find it on beta
231 let commentRes = await createComment(alpha, postRes.post.id);
233 let searchBeta = await searchComment(beta, commentRes.comment);
234 let betaComment = searchBeta.comments[0];
236 // find that comment id on beta
239 let replyRes = await createComment(beta, betaComment.post_id, betaComment.id);
240 expect(replyRes.comment.content).toBeDefined();
241 expect(replyRes.comment.community_local).toBe(true);
242 expect(replyRes.comment.creator_local).toBe(true);
243 expect(replyRes.comment.parent_id).toBe(betaComment.id);
244 expect(replyRes.comment.score).toBe(1);
247 // Make sure that comment is seen on alpha
248 // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
249 // comment, isn't working.
250 // let searchAlpha = await searchComment(alpha, replyRes.comment);
251 let post = await getPost(alpha, postRes.post.id);
252 let alphaComment = post.comments[0];
253 expect(alphaComment.content).toBeDefined();
254 expect(alphaComment.parent_id).toBe(post.comments[1].id);
255 expect(alphaComment.community_local).toBe(false);
256 expect(alphaComment.creator_local).toBe(false);
257 expect(alphaComment.score).toBe(1);
258 assertCommentFederation(alphaComment, replyRes.comment);
261 test('Mention beta', async () => {
262 // Create a mention on alpha
263 let mentionContent = 'A test mention of @lemmy_beta@lemmy-beta:8551';
264 let commentRes = await createComment(alpha, postRes.post.id);
266 let mentionRes = await createComment(
269 commentRes.comment.id,
272 expect(mentionRes.comment.content).toBeDefined();
273 expect(mentionRes.comment.community_local).toBe(false);
274 expect(mentionRes.comment.creator_local).toBe(true);
275 expect(mentionRes.comment.score).toBe(1);
278 let mentionsRes = await getMentions(beta);
279 expect(mentionsRes.mentions[0].content).toBeDefined();
280 expect(mentionsRes.mentions[0].community_local).toBe(true);
281 expect(mentionsRes.mentions[0].creator_local).toBe(false);
282 expect(mentionsRes.mentions[0].score).toBe(1);
285 test('Comment Search', async () => {
286 let commentRes = await createComment(alpha, postRes.post.id);
288 let searchBeta = await searchComment(beta, commentRes.comment);
289 assertCommentFederation(searchBeta.comments[0], commentRes.comment);
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.community_local).toBe(true);
298 // Make sure gamma sees it
299 let search = await searchPost(gamma, alphaPost.post);
300 let gammaPost = search.posts[0];
303 'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551';
304 let commentRes = await createComment(
310 expect(commentRes.comment.content).toBe(commentContent);
311 expect(commentRes.comment.community_local).toBe(false);
312 expect(commentRes.comment.creator_local).toBe(true);
313 expect(commentRes.comment.score).toBe(1);
316 // Make sure alpha sees it
317 let alphaPost2 = await getPost(alpha, alphaPost.post.id);
318 expect(alphaPost2.comments[0].content).toBe(commentContent);
319 expect(alphaPost2.comments[0].community_local).toBe(true);
320 expect(alphaPost2.comments[0].creator_local).toBe(false);
321 expect(alphaPost2.comments[0].score).toBe(1);
322 assertCommentFederation(alphaPost2.comments[0], commentRes.comment);
325 // Make sure beta has mentions
326 let mentionsRes = await getMentions(beta);
327 expect(mentionsRes.mentions[0].content).toBe(commentContent);
328 expect(mentionsRes.mentions[0].community_local).toBe(false);
329 expect(mentionsRes.mentions[0].creator_local).toBe(false);
330 // TODO this is failing because fetchInReplyTos aren't getting score
331 // expect(mentionsRes.mentions[0].score).toBe(1);
334 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 () => {
335 // Unfollow all remote communities
336 let followed = await unfollowRemotes(alpha);
338 followed.communities.filter(c => c.community_local == false).length
341 // B creates a post, and two comments, should be invisible to A
342 let postRes = await createPost(beta, 2);
343 expect(postRes.post.name).toBeDefined();
346 let parentCommentContent = 'An invisible top level comment from beta';
347 let parentCommentRes = await createComment(
353 expect(parentCommentRes.comment.content).toBe(parentCommentContent);
356 // B creates a comment, then a child one of that.
357 let childCommentContent = 'An invisible child comment from beta';
358 let childCommentRes = await createComment(
361 parentCommentRes.comment.id,
364 expect(childCommentRes.comment.content).toBe(childCommentContent);
368 let follow = await followBeta(alpha);
369 expect(follow.community.local).toBe(false);
370 expect(follow.community.name).toBe('main');
373 // An update to the child comment on beta, should push the post, parent, and child to alpha now
374 let updatedCommentContent = 'An update child comment from beta';
375 let updateRes = await updateComment(
377 childCommentRes.comment.id,
378 updatedCommentContent
380 expect(updateRes.comment.content).toBe(updatedCommentContent);
383 // Get the post from alpha
384 let search = await searchPost(alpha, postRes.post);
385 let alphaPostB = search.posts[0];
388 let alphaPost = await getPost(alpha, alphaPostB.id);
389 expect(alphaPost.post.name).toBeDefined();
390 assertCommentFederation(alphaPost.comments[1], parentCommentRes.comment);
391 assertCommentFederation(alphaPost.comments[0], updateRes.comment);
392 expect(alphaPost.post.community_local).toBe(false);
393 expect(alphaPost.post.creator_local).toBe(false);