]> Untitled Git - lemmy.git/blob - api_tests/src/comment.spec.ts
Trying to add some long delays.
[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   delay,
24   longDelay,
25 } from './shared';
26 import {
27   Comment,
28 } from 'lemmy-js-client';
29
30 import { PostResponse } from 'lemmy-js-client';
31
32 let postRes: PostResponse;
33
34 beforeAll(async () => {
35   await setupLogins();
36   await followBeta(alpha);
37   await followBeta(gamma);
38   let search = await searchForBetaCommunity(alpha);
39   await longDelay();
40   postRes = await createPost(
41     alpha,
42     search.communities.filter(c => c.local == false)[0].id
43   );
44 });
45
46 afterAll(async () => {
47   await unfollowRemotes(alpha);
48   await unfollowRemotes(gamma);
49 });
50
51 function assertCommentFederation(
52   commentOne: Comment,
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);
62 }
63
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);
70   await longDelay();
71
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);
80 });
81
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' });
85 });
86
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);
92
93   await delay();
94   let updateCommentRes = await updateComment(alpha, commentRes.comment.id);
95   expect(updateCommentRes.comment.content).toBe(
96     'A jest test federated comment update'
97   );
98   expect(updateCommentRes.comment.community_local).toBe(false);
99   expect(updateCommentRes.comment.creator_local).toBe(true);
100   await delay();
101
102   // Make sure that post is updated on beta
103   let searchBetaUpdated = await searchComment(beta, commentRes.comment);
104   assertCommentFederation(searchBetaUpdated.comments[0], updateCommentRes.comment);
105 });
106
107 test('Delete a comment', async () => {
108   let commentRes = await createComment(alpha, postRes.post.id);
109   await delay();
110
111   let deleteCommentRes = await deleteComment(
112     alpha,
113     true,
114     commentRes.comment.id
115   );
116   expect(deleteCommentRes.comment.deleted).toBe(true);
117   await delay();
118
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();
123   await delay();
124
125   let undeleteCommentRes = await deleteComment(
126     alpha,
127     false,
128     commentRes.comment.id
129   );
130   expect(undeleteCommentRes.comment.deleted).toBe(false);
131   await delay();
132
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);
138 });
139
140 test('Remove a comment from admin and community on the same instance', async () => {
141   let commentRes = await createComment(alpha, postRes.post.id);
142   await delay();
143
144   // Get the id for beta
145   let betaCommentId = (await searchComment(beta, commentRes.comment))
146     .comments[0].id;
147
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);
151   await longDelay();
152
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);
156
157   let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
158   expect(unremoveCommentRes.comment.removed).toBe(false);
159   await longDelay();
160
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);
165 });
166
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,
171     auth: alphaUser.jwt,
172   };
173
174   // New alpha user creates a community, post, and comment.
175   let newCommunity = await createCommunity(newAlphaApi);
176   await delay();
177   let newPost = await createPost(newAlphaApi, newCommunity.community.id);
178   await delay();
179   let commentRes = await createComment(newAlphaApi, newPost.post.id);
180   expect(commentRes.comment.content).toBeDefined();
181   await delay();
182
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);
188   await delay();
189
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);
194 });
195
196 test('Unlike a comment', async () => {
197   let commentRes = await createComment(alpha, postRes.post.id);
198   await delay();
199   let unlike = await likeComment(alpha, 0, commentRes.comment);
200   expect(unlike.comment.score).toBe(0);
201   await delay();
202
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);
210 });
211
212 test('Federated comment like', async () => {
213   let commentRes = await createComment(alpha, postRes.post.id);
214   await longDelay();
215
216   // Find the comment on beta
217   let searchBeta = await searchComment(beta, commentRes.comment);
218   let betaComment = searchBeta.comments[0];
219
220   let like = await likeComment(beta, 1, betaComment);
221   expect(like.comment.score).toBe(2);
222   await longDelay();
223
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);
227 });
228
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);
232   await delay();
233   let searchBeta = await searchComment(beta, commentRes.comment);
234   let betaComment = searchBeta.comments[0];
235
236   // find that comment id on beta
237
238   // Reply from 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);
245   await longDelay();
246
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);
259 });
260
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);
265   await delay();
266   let mentionRes = await createComment(
267     alpha,
268     postRes.post.id,
269     commentRes.comment.id,
270     mentionContent
271   );
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);
276   await delay();
277
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);
283 });
284
285 test('Comment Search', async () => {
286   let commentRes = await createComment(alpha, postRes.post.id);
287   await delay();
288   let searchBeta = await searchComment(beta, commentRes.comment);
289   assertCommentFederation(searchBeta.comments[0], commentRes.comment);
290 });
291
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);
296   await delay();
297
298   // Make sure gamma sees it
299   let search = await searchPost(gamma, alphaPost.post);
300   let gammaPost = search.posts[0];
301
302   let commentContent =
303     'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551';
304   let commentRes = await createComment(
305     gamma,
306     gammaPost.id,
307     undefined,
308     commentContent
309   );
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);
314   await longDelay();
315
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);
323   await delay();
324
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);
332 });
333
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);
337   expect(
338     followed.communities.filter(c => c.community_local == false).length
339   ).toBe(0);
340
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();
344   await delay();
345
346   let parentCommentContent = 'An invisible top level comment from beta';
347   let parentCommentRes = await createComment(
348     beta,
349     postRes.post.id,
350     undefined,
351     parentCommentContent
352   );
353   expect(parentCommentRes.comment.content).toBe(parentCommentContent);
354   await delay();
355
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(
359     beta,
360     postRes.post.id,
361     parentCommentRes.comment.id,
362     childCommentContent
363   );
364   expect(childCommentRes.comment.content).toBe(childCommentContent);
365   await delay();
366
367   // Follow beta again
368   let follow = await followBeta(alpha);
369   expect(follow.community.local).toBe(false);
370   expect(follow.community.name).toBe('main');
371   await delay();
372
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(
376     beta,
377     childCommentRes.comment.id,
378     updatedCommentContent
379   );
380   expect(updateRes.comment.content).toBe(updatedCommentContent);
381   await delay();
382
383   // Get the post from alpha
384   let search = await searchPost(alpha, postRes.post);
385   let alphaPostB = search.posts[0];
386   await longDelay();
387
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);
394 });