]> Untitled Git - lemmy.git/blob - api_tests/src/comment.spec.ts
Merge branch 'add-view-helper-functions' of https://github.com/eiknat/lemmy into...
[lemmy.git] / api_tests / src / comment.spec.ts
1 jest.setTimeout(120000);
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 } from './shared';
25 import {
26   Comment,
27 } from 'lemmy-js-client';
28
29 import { PostResponse } from 'lemmy-js-client';
30
31 let postRes: PostResponse;
32
33 beforeAll(async () => {
34   await setupLogins();
35   await followBeta(alpha);
36   await followBeta(gamma);
37   let search = await searchForBetaCommunity(alpha);
38   await delay(10000);
39   postRes = await createPost(
40     alpha,
41     search.communities.filter(c => c.local == false)[0].id
42   );
43 });
44
45 afterAll(async () => {
46   await unfollowRemotes(alpha);
47   await unfollowRemotes(gamma);
48 });
49
50 function assertCommentFederation(
51   commentOne: Comment,
52   commentTwo: Comment) {
53   expect(commentOne.ap_id).toBe(commentOne.ap_id);
54   expect(commentOne.content).toBe(commentTwo.content);
55   expect(commentOne.creator_name).toBe(commentTwo.creator_name);
56   expect(commentOne.community_actor_id).toBe(commentTwo.community_actor_id);
57   expect(commentOne.published).toBe(commentTwo.published);
58   expect(commentOne.updated).toBe(commentOne.updated);
59   expect(commentOne.deleted).toBe(commentOne.deleted);
60   expect(commentOne.removed).toBe(commentOne.removed);
61 }
62
63 test('Create a comment', async () => {
64   let commentRes = await createComment(alpha, postRes.post.id);
65   expect(commentRes.comment.content).toBeDefined();
66   expect(commentRes.comment.community_local).toBe(false);
67   expect(commentRes.comment.creator_local).toBe(true);
68   expect(commentRes.comment.score).toBe(1);
69   await delay();
70
71   // Make sure that comment is liked on beta
72   let searchBeta = await searchComment(beta, commentRes.comment);
73   let betaComment = searchBeta.comments[0];
74   expect(betaComment).toBeDefined();
75   expect(betaComment.community_local).toBe(true);
76   expect(betaComment.creator_local).toBe(false);
77   expect(betaComment.score).toBe(1);
78   assertCommentFederation(betaComment, commentRes.comment);
79 });
80
81 test('Create a comment in a non-existent post', async () => {
82   let commentRes = await createComment(alpha, -1);
83   expect(commentRes).toStrictEqual({ error: 'couldnt_find_post' });
84 });
85
86 test('Update a comment', async () => {
87   let commentRes = await createComment(alpha, postRes.post.id);
88   // Federate the comment first
89   let searchBeta = await searchComment(beta, commentRes.comment);
90   assertCommentFederation(searchBeta.comments[0], commentRes.comment);
91
92   await delay();
93   let updateCommentRes = await updateComment(alpha, commentRes.comment.id);
94   expect(updateCommentRes.comment.content).toBe(
95     'A jest test federated comment update'
96   );
97   expect(updateCommentRes.comment.community_local).toBe(false);
98   expect(updateCommentRes.comment.creator_local).toBe(true);
99   await delay();
100
101   // Make sure that post is updated on beta
102   let searchBetaUpdated = await searchComment(beta, commentRes.comment);
103   assertCommentFederation(searchBetaUpdated.comments[0], updateCommentRes.comment);
104 });
105
106 test('Delete a comment', async () => {
107   let commentRes = await createComment(alpha, postRes.post.id);
108   await delay();
109
110   let deleteCommentRes = await deleteComment(
111     alpha,
112     true,
113     commentRes.comment.id
114   );
115   expect(deleteCommentRes.comment.deleted).toBe(true);
116   await delay();
117
118   // Make sure that comment is undefined on beta
119   let searchBeta = await searchComment(beta, commentRes.comment);
120   let betaComment = searchBeta.comments[0];
121   expect(betaComment).toBeUndefined();
122   await delay();
123
124   let undeleteCommentRes = await deleteComment(
125     alpha,
126     false,
127     commentRes.comment.id
128   );
129   expect(undeleteCommentRes.comment.deleted).toBe(false);
130   await delay();
131
132   // Make sure that comment is undeleted on beta
133   let searchBeta2 = await searchComment(beta, commentRes.comment);
134   let betaComment2 = searchBeta2.comments[0];
135   expect(betaComment2.deleted).toBe(false);
136   assertCommentFederation(searchBeta2.comments[0], undeleteCommentRes.comment);
137 });
138
139 test('Remove a comment from admin and community on the same instance', async () => {
140   let commentRes = await createComment(alpha, postRes.post.id);
141   await delay();
142
143   // Get the id for beta
144   let betaCommentId = (await searchComment(beta, commentRes.comment))
145     .comments[0].id;
146
147   // The beta admin removes it (the community lives on beta)
148   let removeCommentRes = await removeComment(beta, true, betaCommentId);
149   expect(removeCommentRes.comment.removed).toBe(true);
150   await delay();
151
152   // Make sure that comment is removed on alpha (it gets pushed since an admin from beta removed it)
153   let refetchedPost = await getPost(alpha, postRes.post.id);
154   expect(refetchedPost.comments[0].removed).toBe(true);
155
156   let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
157   expect(unremoveCommentRes.comment.removed).toBe(false);
158   await delay();
159
160   // Make sure that comment is unremoved on beta
161   let refetchedPost2 = await getPost(alpha, postRes.post.id);
162   expect(refetchedPost2.comments[0].removed).toBe(false);
163   assertCommentFederation(refetchedPost2.comments[0], unremoveCommentRes.comment);
164 });
165
166 test('Remove a comment from admin and community on different instance', async () => {
167   let alphaUser = await registerUser(alpha);
168   let newAlphaApi: API = {
169     client: alpha.client,
170     auth: alphaUser.jwt,
171   };
172
173   // New alpha user creates a community, post, and comment.
174   let newCommunity = await createCommunity(newAlphaApi);
175   await delay();
176   let newPost = await createPost(newAlphaApi, newCommunity.community.id);
177   await delay();
178   let commentRes = await createComment(newAlphaApi, newPost.post.id);
179   expect(commentRes.comment.content).toBeDefined();
180   await delay();
181
182   // Beta searches that to cache it, then removes it
183   let searchBeta = await searchComment(beta, commentRes.comment);
184   let betaComment = searchBeta.comments[0];
185   let removeCommentRes = await removeComment(beta, true, betaComment.id);
186   expect(removeCommentRes.comment.removed).toBe(true);
187   await delay();
188
189   // Make sure its not removed on alpha
190   let refetchedPost = await getPost(newAlphaApi, newPost.post.id);
191   expect(refetchedPost.comments[0].removed).toBe(false);
192   assertCommentFederation(refetchedPost.comments[0], commentRes.comment);
193 });
194
195 test('Unlike a comment', async () => {
196   let commentRes = await createComment(alpha, postRes.post.id);
197   await delay();
198   let unlike = await likeComment(alpha, 0, commentRes.comment);
199   expect(unlike.comment.score).toBe(0);
200   await delay();
201
202   // Make sure that post is unliked on beta
203   let searchBeta = await searchComment(beta, commentRes.comment);
204   let betaComment = searchBeta.comments[0];
205   expect(betaComment).toBeDefined();
206   expect(betaComment.community_local).toBe(true);
207   expect(betaComment.creator_local).toBe(false);
208   expect(betaComment.score).toBe(0);
209 });
210
211 test('Federated comment like', async () => {
212   let commentRes = await createComment(alpha, postRes.post.id);
213   await delay();
214
215   // Find the comment on beta
216   let searchBeta = await searchComment(beta, commentRes.comment);
217   let betaComment = searchBeta.comments[0];
218
219   let like = await likeComment(beta, 1, betaComment);
220   expect(like.comment.score).toBe(2);
221   await delay();
222
223   // Get the post from alpha, check the likes
224   let post = await getPost(alpha, postRes.post.id);
225   expect(post.comments[0].score).toBe(2);
226 });
227
228 test('Reply to a comment', async () => {
229   // Create a comment on alpha, find it on beta
230   let commentRes = await createComment(alpha, postRes.post.id);
231   await delay();
232   let searchBeta = await searchComment(beta, commentRes.comment);
233   let betaComment = searchBeta.comments[0];
234
235   // find that comment id on beta
236
237   // Reply from beta
238   let replyRes = await createComment(beta, betaComment.post_id, betaComment.id);
239   expect(replyRes.comment.content).toBeDefined();
240   expect(replyRes.comment.community_local).toBe(true);
241   expect(replyRes.comment.creator_local).toBe(true);
242   expect(replyRes.comment.parent_id).toBe(betaComment.id);
243   expect(replyRes.comment.score).toBe(1);
244   await delay();
245
246   // Make sure that comment is seen on alpha
247   // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
248   // comment, isn't working.
249   // let searchAlpha = await searchComment(alpha, replyRes.comment);
250   let post = await getPost(alpha, postRes.post.id);
251   let alphaComment = post.comments[0];
252   expect(alphaComment.content).toBeDefined();
253   expect(alphaComment.parent_id).toBe(post.comments[1].id);
254   expect(alphaComment.community_local).toBe(false);
255   expect(alphaComment.creator_local).toBe(false);
256   expect(alphaComment.score).toBe(1);
257   assertCommentFederation(alphaComment, replyRes.comment);
258 });
259
260 test('Mention beta', async () => {
261   // Create a mention on alpha
262   let mentionContent = 'A test mention of @lemmy_beta@lemmy-beta:8551';
263   let commentRes = await createComment(alpha, postRes.post.id);
264   await delay();
265   let mentionRes = await createComment(
266     alpha,
267     postRes.post.id,
268     commentRes.comment.id,
269     mentionContent
270   );
271   expect(mentionRes.comment.content).toBeDefined();
272   expect(mentionRes.comment.community_local).toBe(false);
273   expect(mentionRes.comment.creator_local).toBe(true);
274   expect(mentionRes.comment.score).toBe(1);
275   await delay();
276
277   let mentionsRes = await getMentions(beta);
278   expect(mentionsRes.mentions[0].content).toBeDefined();
279   expect(mentionsRes.mentions[0].community_local).toBe(true);
280   expect(mentionsRes.mentions[0].creator_local).toBe(false);
281   expect(mentionsRes.mentions[0].score).toBe(1);
282 });
283
284 test('Comment Search', async () => {
285   let commentRes = await createComment(alpha, postRes.post.id);
286   await delay();
287   let searchBeta = await searchComment(beta, commentRes.comment);
288   assertCommentFederation(searchBeta.comments[0], commentRes.comment);
289 });
290
291 test('A and G subscribe to B (center) A posts, G mentions B, it gets announced to A', async () => {
292   // Create a local post
293   let alphaPost = await createPost(alpha, 2);
294   expect(alphaPost.post.community_local).toBe(true);
295   await delay();
296
297   // Make sure gamma sees it
298   let search = await searchPost(gamma, alphaPost.post);
299   let gammaPost = search.posts[0];
300
301   let commentContent =
302     'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551';
303   let commentRes = await createComment(
304     gamma,
305     gammaPost.id,
306     undefined,
307     commentContent
308   );
309   expect(commentRes.comment.content).toBe(commentContent);
310   expect(commentRes.comment.community_local).toBe(false);
311   expect(commentRes.comment.creator_local).toBe(true);
312   expect(commentRes.comment.score).toBe(1);
313   await delay();
314
315   // Make sure alpha sees it
316   let alphaPost2 = await getPost(alpha, alphaPost.post.id);
317   expect(alphaPost2.comments[0].content).toBe(commentContent);
318   expect(alphaPost2.comments[0].community_local).toBe(true);
319   expect(alphaPost2.comments[0].creator_local).toBe(false);
320   expect(alphaPost2.comments[0].score).toBe(1);
321   assertCommentFederation(alphaPost2.comments[0], commentRes.comment);
322
323   // Make sure beta has mentions
324   let mentionsRes = await getMentions(beta);
325   expect(mentionsRes.mentions[0].content).toBe(commentContent);
326   expect(mentionsRes.mentions[0].community_local).toBe(false);
327   expect(mentionsRes.mentions[0].creator_local).toBe(false);
328   // TODO this is failing because fetchInReplyTos aren't getting score
329   // expect(mentionsRes.mentions[0].score).toBe(1);
330 });
331
332 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 () => {
333   // Unfollow all remote communities
334   let followed = await unfollowRemotes(alpha);
335   expect(
336     followed.communities.filter(c => c.community_local == false).length
337   ).toBe(0);
338
339   // B creates a post, and two comments, should be invisible to A
340   let postRes = await createPost(beta, 2);
341   expect(postRes.post.name).toBeDefined();
342   await delay();
343
344   let parentCommentContent = 'An invisible top level comment from beta';
345   let parentCommentRes = await createComment(
346     beta,
347     postRes.post.id,
348     undefined,
349     parentCommentContent
350   );
351   expect(parentCommentRes.comment.content).toBe(parentCommentContent);
352   await delay();
353
354   // B creates a comment, then a child one of that.
355   let childCommentContent = 'An invisible child comment from beta';
356   let childCommentRes = await createComment(
357     beta,
358     postRes.post.id,
359     parentCommentRes.comment.id,
360     childCommentContent
361   );
362   expect(childCommentRes.comment.content).toBe(childCommentContent);
363   await delay();
364
365   // Follow beta again
366   let follow = await followBeta(alpha);
367   expect(follow.community.local).toBe(false);
368   expect(follow.community.name).toBe('main');
369   await delay();
370
371   // An update to the child comment on beta, should push the post, parent, and child to alpha now
372   let updatedCommentContent = 'An update child comment from beta';
373   let updateRes = await updateComment(
374     beta,
375     childCommentRes.comment.id,
376     updatedCommentContent
377   );
378   expect(updateRes.comment.content).toBe(updatedCommentContent);
379   await delay();
380
381   // Get the post from alpha
382   let search = await searchPost(alpha, postRes.post);
383   let alphaPostB = search.posts[0];
384   await delay();
385
386   let alphaPost = await getPost(alpha, alphaPostB.id);
387   expect(alphaPost.post.name).toBeDefined();
388   assertCommentFederation(alphaPost.comments[1], parentCommentRes.comment);
389   assertCommentFederation(alphaPost.comments[0], updateRes.comment);
390   expect(alphaPost.post.community_local).toBe(false);
391   expect(alphaPost.post.creator_local).toBe(false);
392 });