]> Untitled Git - lemmy.git/blob - api_tests/src/comment.spec.ts
Removing sniptt/monads for lemmy-js-client. (#2644)
[lemmy.git] / api_tests / src / comment.spec.ts
1 jest.setTimeout(180000);
2 import { CommentView } from "lemmy-js-client";
3 import { PostResponse } from "lemmy-js-client";
4
5 import {
6   alpha,
7   beta,
8   gamma,
9   setupLogins,
10   createPost,
11   getPost,
12   resolveComment,
13   likeComment,
14   followBeta,
15   resolveBetaCommunity,
16   createComment,
17   editComment,
18   deleteComment,
19   removeComment,
20   getMentions,
21   resolvePost,
22   unfollowRemotes,
23   createCommunity,
24   registerUser,
25   reportComment,
26   listCommentReports,
27   randomString,
28   API,
29   unfollows,
30   getComments,
31   getCommentParentId,
32   resolveCommunity,
33 } from "./shared";
34
35 let postRes: PostResponse;
36
37 beforeAll(async () => {
38   await setupLogins();
39   await unfollows();
40   await followBeta(alpha);
41   await followBeta(gamma);
42   let betaCommunity = (await resolveBetaCommunity(alpha)).community;
43   if (betaCommunity) {
44     postRes = await createPost(alpha, betaCommunity.community.id);
45   }
46 });
47
48 afterAll(async () => {
49   await unfollows();
50 });
51
52 function assertCommentFederation(
53   commentOne?: CommentView,
54   commentTwo?: CommentView
55 ) {
56   expect(commentOne?.comment.ap_id).toBe(commentTwo?.comment.ap_id);
57   expect(commentOne?.comment.content).toBe(commentTwo?.comment.content);
58   expect(commentOne?.creator.name).toBe(commentTwo?.creator.name);
59   expect(commentOne?.community.actor_id).toBe(commentTwo?.community.actor_id);
60   expect(commentOne?.comment.published).toBe(commentTwo?.comment.published);
61   expect(commentOne?.comment.updated).toBe(commentOne?.comment.updated);
62   expect(commentOne?.comment.deleted).toBe(commentOne?.comment.deleted);
63   expect(commentOne?.comment.removed).toBe(commentOne?.comment.removed);
64 }
65
66 test("Create a comment", async () => {
67   let commentRes = await createComment(alpha, postRes.post_view.post.id);
68   expect(commentRes.comment_view.comment.content).toBeDefined();
69   expect(commentRes.comment_view.community.local).toBe(false);
70   expect(commentRes.comment_view.creator.local).toBe(true);
71   expect(commentRes.comment_view.counts.score).toBe(1);
72
73   // Make sure that comment is liked on beta
74   let betaComment = (
75     await resolveComment(beta, commentRes.comment_view.comment)
76   ).comment;
77   expect(betaComment).toBeDefined();
78   expect(betaComment?.community.local).toBe(true);
79   expect(betaComment?.creator.local).toBe(false);
80   expect(betaComment?.counts.score).toBe(1);
81   assertCommentFederation(betaComment, commentRes.comment_view);
82 });
83
84 test("Create a comment in a non-existent post", async () => {
85   let commentRes = (await createComment(alpha, -1)) as any;
86   expect(commentRes.error).toBe("couldnt_find_post");
87 });
88
89 test("Update a comment", async () => {
90   let commentRes = await createComment(alpha, postRes.post_view.post.id);
91   // Federate the comment first
92   let betaComment = (
93     await resolveComment(beta, commentRes.comment_view.comment)
94   ).comment;
95   assertCommentFederation(betaComment, commentRes.comment_view);
96
97   let updateCommentRes = await editComment(
98     alpha,
99     commentRes.comment_view.comment.id
100   );
101   expect(updateCommentRes.comment_view.comment.content).toBe(
102     "A jest test federated comment update"
103   );
104   expect(updateCommentRes.comment_view.community.local).toBe(false);
105   expect(updateCommentRes.comment_view.creator.local).toBe(true);
106
107   // Make sure that post is updated on beta
108   let betaCommentUpdated = (
109     await resolveComment(beta, commentRes.comment_view.comment)
110   ).comment;
111   assertCommentFederation(betaCommentUpdated, updateCommentRes.comment_view);
112 });
113
114 test("Delete a comment", async () => {
115   let commentRes = await createComment(alpha, postRes.post_view.post.id);
116
117   let deleteCommentRes = await deleteComment(
118     alpha,
119     true,
120     commentRes.comment_view.comment.id
121   );
122   expect(deleteCommentRes.comment_view.comment.deleted).toBe(true);
123   expect(deleteCommentRes.comment_view.comment.content).toBe("");
124
125   // Make sure that comment is undefined on beta
126   let betaCommentRes = (await resolveComment(
127     beta,
128     commentRes.comment_view.comment
129   )) as any;
130   expect(betaCommentRes.error).toBe("couldnt_find_object");
131
132   let undeleteCommentRes = await deleteComment(
133     alpha,
134     false,
135     commentRes.comment_view.comment.id
136   );
137   expect(undeleteCommentRes.comment_view.comment.deleted).toBe(false);
138
139   // Make sure that comment is undeleted on beta
140   let betaComment2 = (
141     await resolveComment(beta, commentRes.comment_view.comment)
142   ).comment;
143   expect(betaComment2?.comment.deleted).toBe(false);
144   assertCommentFederation(betaComment2, undeleteCommentRes.comment_view);
145 });
146
147 test("Remove a comment from admin and community on the same instance", async () => {
148   let commentRes = await createComment(alpha, postRes.post_view.post.id);
149
150   // Get the id for beta
151   let betaCommentId = (
152     await resolveComment(beta, commentRes.comment_view.comment)
153   ).comment?.comment.id;
154
155   if (!betaCommentId) {
156     throw "beta comment id is missing";
157   }
158
159   // The beta admin removes it (the community lives on beta)
160   let removeCommentRes = await removeComment(beta, true, betaCommentId);
161   expect(removeCommentRes.comment_view.comment.removed).toBe(true);
162   expect(removeCommentRes.comment_view.comment.content).toBe("");
163
164   // Make sure that comment is removed on alpha (it gets pushed since an admin from beta removed it)
165   let refetchedPostComments = await getComments(
166     alpha,
167     postRes.post_view.post.id
168   );
169   expect(refetchedPostComments.comments[0].comment.removed).toBe(true);
170
171   let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
172   expect(unremoveCommentRes.comment_view.comment.removed).toBe(false);
173
174   // Make sure that comment is unremoved on beta
175   let refetchedPostComments2 = await getComments(
176     alpha,
177     postRes.post_view.post.id
178   );
179   expect(refetchedPostComments2.comments[0].comment.removed).toBe(false);
180   assertCommentFederation(
181     refetchedPostComments2.comments[0],
182     unremoveCommentRes.comment_view
183   );
184 });
185
186 test("Remove a comment from admin and community on different instance", async () => {
187   let alpha_user = await registerUser(alpha);
188   let newAlphaApi: API = {
189     client: alpha.client,
190     auth: alpha_user.jwt ?? "",
191   };
192
193   // New alpha user creates a community, post, and comment.
194   let newCommunity = await createCommunity(newAlphaApi);
195   let newPost = await createPost(
196     newAlphaApi,
197     newCommunity.community_view.community.id
198   );
199   let commentRes = await createComment(newAlphaApi, newPost.post_view.post.id);
200   expect(commentRes.comment_view.comment.content).toBeDefined();
201
202   // Beta searches that to cache it, then removes it
203   let betaComment = (
204     await resolveComment(beta, commentRes.comment_view.comment)
205   ).comment;
206
207   if (!betaComment) {
208     throw "beta comment missing";
209   }
210
211   let removeCommentRes = await removeComment(
212     beta,
213     true,
214     betaComment.comment.id
215   );
216   expect(removeCommentRes.comment_view.comment.removed).toBe(true);
217
218   // Make sure its not removed on alpha
219   let refetchedPostComments = await getComments(
220     alpha,
221     newPost.post_view.post.id
222   );
223   expect(refetchedPostComments.comments[0].comment.removed).toBe(false);
224   assertCommentFederation(
225     refetchedPostComments.comments[0],
226     commentRes.comment_view
227   );
228 });
229
230 test("Unlike a comment", async () => {
231   let commentRes = await createComment(alpha, postRes.post_view.post.id);
232   let unlike = await likeComment(alpha, 0, commentRes.comment_view.comment);
233   expect(unlike.comment_view.counts.score).toBe(0);
234
235   // Make sure that post is unliked on beta
236   let betaComment = (
237     await resolveComment(beta, commentRes.comment_view.comment)
238   ).comment;
239   expect(betaComment).toBeDefined();
240   expect(betaComment?.community.local).toBe(true);
241   expect(betaComment?.creator.local).toBe(false);
242   expect(betaComment?.counts.score).toBe(0);
243 });
244
245 test("Federated comment like", async () => {
246   let commentRes = await createComment(alpha, postRes.post_view.post.id);
247
248   // Find the comment on beta
249   let betaComment = (
250     await resolveComment(beta, commentRes.comment_view.comment)
251   ).comment;
252
253   if (!betaComment) {
254     throw "Missing beta comment";
255   }
256
257   let like = await likeComment(beta, 1, betaComment.comment);
258   expect(like.comment_view.counts.score).toBe(2);
259
260   // Get the post from alpha, check the likes
261   let postComments = await getComments(alpha, postRes.post_view.post.id);
262   expect(postComments.comments[0].counts.score).toBe(2);
263 });
264
265 test("Reply to a comment", async () => {
266   // Create a comment on alpha, find it on beta
267   let commentRes = await createComment(alpha, postRes.post_view.post.id);
268   let betaComment = (
269     await resolveComment(beta, commentRes.comment_view.comment)
270   ).comment;
271
272   if (!betaComment) {
273     throw "Missing beta comment";
274   }
275
276   // find that comment id on beta
277
278   // Reply from beta
279   let replyRes = await createComment(
280     beta,
281     betaComment.post.id,
282     betaComment.comment.id
283   );
284   expect(replyRes.comment_view.comment.content).toBeDefined();
285   expect(replyRes.comment_view.community.local).toBe(true);
286   expect(replyRes.comment_view.creator.local).toBe(true);
287   expect(getCommentParentId(replyRes.comment_view.comment)).toBe(
288     betaComment.comment.id
289   );
290   expect(replyRes.comment_view.counts.score).toBe(1);
291
292   // Make sure that comment is seen on alpha
293   // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
294   // comment, isn't working.
295   // let searchAlpha = await searchComment(alpha, replyRes.comment);
296   let postComments = await getComments(alpha, postRes.post_view.post.id);
297   let alphaComment = postComments.comments[0];
298   expect(alphaComment.comment.content).toBeDefined();
299   expect(getCommentParentId(alphaComment.comment)).toBe(
300     postComments.comments[1].comment.id
301   );
302   expect(alphaComment.community.local).toBe(false);
303   expect(alphaComment.creator.local).toBe(false);
304   expect(alphaComment.counts.score).toBe(1);
305   assertCommentFederation(alphaComment, replyRes.comment_view);
306 });
307
308 test("Mention beta", async () => {
309   // Create a mention on alpha
310   let mentionContent = "A test mention of @lemmy_beta@lemmy-beta:8551";
311   let commentRes = await createComment(alpha, postRes.post_view.post.id);
312   let mentionRes = await createComment(
313     alpha,
314     postRes.post_view.post.id,
315     commentRes.comment_view.comment.id,
316     mentionContent
317   );
318   expect(mentionRes.comment_view.comment.content).toBeDefined();
319   expect(mentionRes.comment_view.community.local).toBe(false);
320   expect(mentionRes.comment_view.creator.local).toBe(true);
321   expect(mentionRes.comment_view.counts.score).toBe(1);
322
323   let mentionsRes = await getMentions(beta);
324   expect(mentionsRes.mentions[0].comment.content).toBeDefined();
325   expect(mentionsRes.mentions[0].community.local).toBe(true);
326   expect(mentionsRes.mentions[0].creator.local).toBe(false);
327   expect(mentionsRes.mentions[0].counts.score).toBe(1);
328 });
329
330 test("Comment Search", async () => {
331   let commentRes = await createComment(alpha, postRes.post_view.post.id);
332   let betaComment = (
333     await resolveComment(beta, commentRes.comment_view.comment)
334   ).comment;
335   assertCommentFederation(betaComment, commentRes.comment_view);
336 });
337
338 test("A and G subscribe to B (center) A posts, G mentions B, it gets announced to A", async () => {
339   // Create a local post
340   let alphaCommunity = (await resolveCommunity(alpha, "!main@lemmy-alpha:8541"))
341     .community;
342
343   if (!alphaCommunity) {
344     throw "Missing alpha community";
345   }
346
347   let alphaPost = await createPost(alpha, alphaCommunity.community.id);
348   expect(alphaPost.post_view.community.local).toBe(true);
349
350   // Make sure gamma sees it
351   let gammaPost = (await resolvePost(gamma, alphaPost.post_view.post)).post;
352
353   if (!gammaPost) {
354     throw "Missing gamma post";
355   }
356
357   let commentContent =
358     "A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551";
359   let commentRes = await createComment(
360     gamma,
361     gammaPost.post.id,
362     undefined,
363     commentContent
364   );
365   expect(commentRes.comment_view.comment.content).toBe(commentContent);
366   expect(commentRes.comment_view.community.local).toBe(false);
367   expect(commentRes.comment_view.creator.local).toBe(true);
368   expect(commentRes.comment_view.counts.score).toBe(1);
369
370   // Make sure alpha sees it
371   let alphaPostComments2 = await getComments(
372     alpha,
373     alphaPost.post_view.post.id
374   );
375   expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
376   expect(alphaPostComments2.comments[0].community.local).toBe(true);
377   expect(alphaPostComments2.comments[0].creator.local).toBe(false);
378   expect(alphaPostComments2.comments[0].counts.score).toBe(1);
379   assertCommentFederation(
380     alphaPostComments2.comments[0],
381     commentRes.comment_view
382   );
383
384   // Make sure beta has mentions
385   let mentionsRes = await getMentions(beta);
386   expect(mentionsRes.mentions[0].comment.content).toBe(commentContent);
387   expect(mentionsRes.mentions[0].community.local).toBe(false);
388   expect(mentionsRes.mentions[0].creator.local).toBe(false);
389   // TODO this is failing because fetchInReplyTos aren't getting score
390   // expect(mentionsRes.mentions[0].score).toBe(1);
391 });
392
393 test("Check that activity from another instance is sent to third instance", async () => {
394   // Alpha and gamma users follow beta community
395   let alphaFollow = await followBeta(alpha);
396   expect(alphaFollow.community_view.community.local).toBe(false);
397   expect(alphaFollow.community_view.community.name).toBe("main");
398
399   let gammaFollow = await followBeta(gamma);
400   expect(gammaFollow.community_view.community.local).toBe(false);
401   expect(gammaFollow.community_view.community.name).toBe("main");
402
403   // Create a post on beta
404   let betaPost = await createPost(beta, 2);
405   expect(betaPost.post_view.community.local).toBe(true);
406
407   // Make sure gamma and alpha see it
408   let gammaPost = (await resolvePost(gamma, betaPost.post_view.post)).post;
409   if (!gammaPost) {
410     throw "Missing gamma post";
411   }
412   expect(gammaPost.post).toBeDefined();
413
414   let alphaPost = (await resolvePost(alpha, betaPost.post_view.post)).post;
415   if (!alphaPost) {
416     throw "Missing alpha post";
417   }
418   expect(alphaPost.post).toBeDefined();
419
420   // The bug: gamma comments, and alpha should see it.
421   let commentContent = "Comment from gamma";
422   let commentRes = await createComment(
423     gamma,
424     gammaPost.post.id,
425     undefined,
426     commentContent
427   );
428   expect(commentRes.comment_view.comment.content).toBe(commentContent);
429   expect(commentRes.comment_view.community.local).toBe(false);
430   expect(commentRes.comment_view.creator.local).toBe(true);
431   expect(commentRes.comment_view.counts.score).toBe(1);
432
433   // Make sure alpha sees it
434   let alphaPostComments2 = await getComments(alpha, alphaPost.post.id);
435   expect(alphaPostComments2.comments[0].comment.content).toBe(commentContent);
436   expect(alphaPostComments2.comments[0].community.local).toBe(false);
437   expect(alphaPostComments2.comments[0].creator.local).toBe(false);
438   expect(alphaPostComments2.comments[0].counts.score).toBe(1);
439   assertCommentFederation(
440     alphaPostComments2.comments[0],
441     commentRes.comment_view
442   );
443
444   await unfollowRemotes(alpha);
445   await unfollowRemotes(gamma);
446 });
447
448 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 () => {
449   // Unfollow all remote communities
450   let site = await unfollowRemotes(alpha);
451   expect(
452     site.my_user?.follows.filter(c => c.community.local == false).length
453   ).toBe(0);
454
455   // B creates a post, and two comments, should be invisible to A
456   let postRes = await createPost(beta, 2);
457   expect(postRes.post_view.post.name).toBeDefined();
458
459   let parentCommentContent = "An invisible top level comment from beta";
460   let parentCommentRes = await createComment(
461     beta,
462     postRes.post_view.post.id,
463     undefined,
464     parentCommentContent
465   );
466   expect(parentCommentRes.comment_view.comment.content).toBe(
467     parentCommentContent
468   );
469
470   // B creates a comment, then a child one of that.
471   let childCommentContent = "An invisible child comment from beta";
472   let childCommentRes = await createComment(
473     beta,
474     postRes.post_view.post.id,
475     parentCommentRes.comment_view.comment.id,
476     childCommentContent
477   );
478   expect(childCommentRes.comment_view.comment.content).toBe(
479     childCommentContent
480   );
481
482   // Follow beta again
483   let follow = await followBeta(alpha);
484   expect(follow.community_view.community.local).toBe(false);
485   expect(follow.community_view.community.name).toBe("main");
486
487   // An update to the child comment on beta, should push the post, parent, and child to alpha now
488   let updatedCommentContent = "An update child comment from beta";
489   let updateRes = await editComment(
490     beta,
491     childCommentRes.comment_view.comment.id,
492     updatedCommentContent
493   );
494   expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent);
495
496   // Get the post from alpha
497   let alphaPostB = (await resolvePost(alpha, postRes.post_view.post)).post;
498   if (!alphaPostB) {
499     throw "Missing alpha post B";
500   }
501
502   let alphaPost = await getPost(alpha, alphaPostB.post.id);
503   let alphaPostComments = await getComments(alpha, alphaPostB.post.id);
504   expect(alphaPost.post_view.post.name).toBeDefined();
505   assertCommentFederation(
506     alphaPostComments.comments[1],
507     parentCommentRes.comment_view
508   );
509   assertCommentFederation(
510     alphaPostComments.comments[0],
511     updateRes.comment_view
512   );
513   expect(alphaPost.post_view.community.local).toBe(false);
514   expect(alphaPost.post_view.creator.local).toBe(false);
515
516   await unfollowRemotes(alpha);
517 });
518
519 test("Report a comment", async () => {
520   let betaCommunity = (await resolveBetaCommunity(beta)).community;
521   if (!betaCommunity) {
522     throw "Missing beta community";
523   }
524   let postRes = (await createPost(beta, betaCommunity.community.id)).post_view
525     .post;
526   expect(postRes).toBeDefined();
527   let commentRes = (await createComment(beta, postRes.id)).comment_view.comment;
528   expect(commentRes).toBeDefined();
529
530   let alphaComment = (await resolveComment(alpha, commentRes)).comment?.comment;
531   if (!alphaComment) {
532     throw "Missing alpha comment";
533   }
534
535   let alphaReport = (
536     await reportComment(alpha, alphaComment.id, randomString(10))
537   ).comment_report_view.comment_report;
538
539   let betaReport = (await listCommentReports(beta)).comment_reports[0]
540     .comment_report;
541   expect(betaReport).toBeDefined();
542   expect(betaReport.resolved).toBe(false);
543   expect(betaReport.original_comment_text).toBe(
544     alphaReport.original_comment_text
545   );
546   expect(betaReport.reason).toBe(alphaReport.reason);
547 });