]> Untitled Git - lemmy.git/blob - api_tests/src/comment.spec.ts
Trying again.
[lemmy.git] / api_tests / src / comment.spec.ts
1 jest.setTimeout(180000);
2 import {
3   alpha,
4   beta,
5   gamma,
6   setupLogins,
7   createPost,
8   getPost,
9   searchComment,
10   likeComment,
11   followBeta,
12   searchForBetaCommunity,
13   createComment,
14   updateComment,
15   deleteComment,
16   removeComment,
17   getMentions,
18   searchPost,
19   unfollowRemotes,
20   createCommunity,
21   registerUser,
22   API,
23 } from './shared';
24 import {
25   Comment,
26 } from 'lemmy-js-client';
27
28 import { PostResponse } from 'lemmy-js-client';
29
30 let postRes: PostResponse;
31
32 beforeAll(async () => {
33   await setupLogins();
34   await followBeta(alpha);
35   await followBeta(gamma);
36   let search = await searchForBetaCommunity(alpha);
37   postRes = await createPost(
38     alpha,
39     search.communities.filter(c => c.local == false)[0].id
40   );
41 });
42
43 afterAll(async () => {
44   await unfollowRemotes(alpha);
45   await unfollowRemotes(gamma);
46 });
47
48 function assertCommentFederation(
49   commentOne: Comment,
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);
59 }
60
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);
67
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);
76 });
77
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' });
81 });
82
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);
88
89   let updateCommentRes = await updateComment(alpha, commentRes.comment.id);
90   expect(updateCommentRes.comment.content).toBe(
91     'A jest test federated comment update'
92   );
93   expect(updateCommentRes.comment.community_local).toBe(false);
94   expect(updateCommentRes.comment.creator_local).toBe(true);
95
96   // Make sure that post is updated on beta
97   let searchBetaUpdated = await searchComment(beta, commentRes.comment);
98   assertCommentFederation(searchBetaUpdated.comments[0], updateCommentRes.comment);
99 });
100
101 test('Delete a comment', async () => {
102   let commentRes = await createComment(alpha, postRes.post.id);
103
104   let deleteCommentRes = await deleteComment(
105     alpha,
106     true,
107     commentRes.comment.id
108   );
109   expect(deleteCommentRes.comment.deleted).toBe(true);
110
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();
115
116   let undeleteCommentRes = await deleteComment(
117     alpha,
118     false,
119     commentRes.comment.id
120   );
121   expect(undeleteCommentRes.comment.deleted).toBe(false);
122
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);
128 });
129
130 test('Remove a comment from admin and community on the same instance', async () => {
131   let commentRes = await createComment(alpha, postRes.post.id);
132
133   // Get the id for beta
134   let betaCommentId = (await searchComment(beta, commentRes.comment))
135     .comments[0].id;
136
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);
140
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);
144
145   let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
146   expect(unremoveCommentRes.comment.removed).toBe(false);
147
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);
152 });
153
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,
158     auth: alphaUser.jwt,
159   };
160
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();
166
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);
172
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);
177 });
178
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);
183
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);
191 });
192
193 test('Federated comment like', async () => {
194   let commentRes = await createComment(alpha, postRes.post.id);
195
196   // Find the comment on beta
197   let searchBeta = await searchComment(beta, commentRes.comment);
198   let betaComment = searchBeta.comments[0];
199
200   let like = await likeComment(beta, 1, betaComment);
201   expect(like.comment.score).toBe(2);
202
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);
206 });
207
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];
213
214   // find that comment id on beta
215
216   // Reply from 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);
223
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);
236 });
237
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(
243     alpha,
244     postRes.post.id,
245     commentRes.comment.id,
246     mentionContent
247   );
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);
252
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);
258 });
259
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);
264 });
265
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);
270
271   // Make sure gamma sees it
272   let search = await searchPost(gamma, alphaPost.post);
273   let gammaPost = search.posts[0];
274
275   let commentContent =
276     'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551';
277   let commentRes = await createComment(
278     gamma,
279     gammaPost.id,
280     undefined,
281     commentContent
282   );
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);
287
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);
295
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);
303 });
304
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);
308   expect(
309     followed.communities.filter(c => c.community_local == false).length
310   ).toBe(0);
311
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();
315
316   let parentCommentContent = 'An invisible top level comment from beta';
317   let parentCommentRes = await createComment(
318     beta,
319     postRes.post.id,
320     undefined,
321     parentCommentContent
322   );
323   expect(parentCommentRes.comment.content).toBe(parentCommentContent);
324
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(
328     beta,
329     postRes.post.id,
330     parentCommentRes.comment.id,
331     childCommentContent
332   );
333   expect(childCommentRes.comment.content).toBe(childCommentContent);
334
335   // Follow beta again
336   let follow = await followBeta(alpha);
337   expect(follow.community.local).toBe(false);
338   expect(follow.community.name).toBe('main');
339
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(
343     beta,
344     childCommentRes.comment.id,
345     updatedCommentContent
346   );
347   expect(updateRes.comment.content).toBe(updatedCommentContent);
348
349   // Get the post from alpha
350   let search = await searchPost(alpha, postRes.post);
351   let alphaPostB = search.posts[0];
352
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);
359
360   await unfollowRemotes(alpha);
361 });