]> Untitled Git - lemmy.git/blob - ui/src/api_tests/comment.spec.ts
Merge branch 'main' into federation-authorisation
[lemmy.git] / ui / src / api_tests / comment.spec.ts
1 import {
2   alpha,
3   beta,
4   gamma,
5   setupLogins,
6   createPost,
7   getPost,
8   searchComment,
9   likeComment,
10   followBeta,
11   searchForBetaCommunity,
12   createComment,
13   updateComment,
14   deleteComment,
15   removeComment,
16   getMentions,
17   searchPost,
18   unfollowRemotes,
19 } from './shared';
20
21 import { PostResponse } from '../interfaces';
22
23 let postRes: PostResponse;
24
25 beforeAll(async () => {
26   await setupLogins();
27   await followBeta(alpha);
28   await followBeta(gamma);
29   let search = await searchForBetaCommunity(alpha);
30   postRes = await createPost(
31     alpha,
32     search.communities.filter(c => c.local == false)[0].id
33   );
34 });
35
36 afterAll(async () => {
37   await unfollowRemotes(alpha);
38   await unfollowRemotes(gamma);
39 });
40
41 test('Create a comment', async () => {
42   let commentRes = await createComment(alpha, postRes.post.id);
43   expect(commentRes.comment.content).toBeDefined();
44   expect(commentRes.comment.community_local).toBe(false);
45   expect(commentRes.comment.creator_local).toBe(true);
46   expect(commentRes.comment.score).toBe(1);
47
48   // Make sure that comment is liked on beta
49   let searchBeta = await searchComment(beta, commentRes.comment);
50   let betaComment = searchBeta.comments[0];
51   expect(betaComment).toBeDefined();
52   expect(betaComment.community_local).toBe(true);
53   expect(betaComment.creator_local).toBe(false);
54   expect(betaComment.score).toBe(1);
55 });
56
57 test('Update a comment', async () => {
58   let commentRes = await createComment(alpha, postRes.post.id);
59   let updateCommentRes = await updateComment(alpha, commentRes.comment.id);
60   expect(updateCommentRes.comment.content).toBe(
61     'A jest test federated comment update'
62   );
63   expect(updateCommentRes.comment.community_local).toBe(false);
64   expect(updateCommentRes.comment.creator_local).toBe(true);
65
66   // Make sure that post is updated on beta
67   let searchBeta = await searchComment(beta, commentRes.comment);
68   let betaComment = searchBeta.comments[0];
69   expect(betaComment.content).toBe('A jest test federated comment update');
70 });
71
72 test('Delete a comment', async () => {
73   let commentRes = await createComment(alpha, postRes.post.id);
74   let deleteCommentRes = await deleteComment(
75     alpha,
76     true,
77     commentRes.comment.id
78   );
79   expect(deleteCommentRes.comment.deleted).toBe(true);
80
81   // Make sure that comment is deleted on beta
82   // The search doesnt work below, because it returns a tombstone / http::gone
83   // let searchBeta = await searchComment(beta, commentRes.comment);
84   // console.log(searchBeta);
85   // let betaComment = searchBeta.comments[0];
86   // Create a fake post, just to get the previous new post id
87   let createdBetaPostJustToGetId = await createPost(beta, 2);
88   let betaPost = await getPost(beta, createdBetaPostJustToGetId.post.id - 1);
89   let betaComment = betaPost.comments[0];
90   expect(betaComment.deleted).toBe(true);
91
92   let undeleteCommentRes = await deleteComment(
93     alpha,
94     false,
95     commentRes.comment.id
96   );
97   expect(undeleteCommentRes.comment.deleted).toBe(false);
98
99   // Make sure that comment is undeleted on beta
100   let searchBeta2 = await searchComment(beta, commentRes.comment);
101   let betaComment2 = searchBeta2.comments[0];
102   expect(betaComment2.deleted).toBe(false);
103 });
104
105 test('Remove a comment', async () => {
106   let commentRes = await createComment(alpha, postRes.post.id);
107   let removeCommentRes = await removeComment(
108     alpha,
109     true,
110     commentRes.comment.id
111   );
112   expect(removeCommentRes.comment.removed).toBe(true);
113
114   // Make sure that comment is removed on beta
115   let searchBeta = await searchComment(beta, commentRes.comment);
116   let betaComment = searchBeta.comments[0];
117   expect(betaComment.removed).toBe(true);
118
119   let unremoveCommentRes = await removeComment(
120     alpha,
121     false,
122     commentRes.comment.id
123   );
124   expect(unremoveCommentRes.comment.removed).toBe(false);
125
126   // Make sure that comment is unremoved on beta
127   let searchBeta2 = await searchComment(beta, commentRes.comment);
128   let betaComment2 = searchBeta2.comments[0];
129   expect(betaComment2.removed).toBe(false);
130 });
131
132 test('Unlike a comment', async () => {
133   let commentRes = await createComment(alpha, postRes.post.id);
134   let unlike = await likeComment(alpha, 0, commentRes.comment);
135   expect(unlike.comment.score).toBe(0);
136
137   // Make sure that post is unliked on beta
138   let searchBeta = await searchComment(beta, commentRes.comment);
139   let betaComment = searchBeta.comments[0];
140   expect(betaComment).toBeDefined();
141   expect(betaComment.community_local).toBe(true);
142   expect(betaComment.creator_local).toBe(false);
143   expect(betaComment.score).toBe(0);
144 });
145
146 test('Federated comment like', async () => {
147   let commentRes = await createComment(alpha, postRes.post.id);
148
149   // Find the comment on beta
150   let searchBeta = await searchComment(beta, commentRes.comment);
151   let betaComment = searchBeta.comments[0];
152
153   let like = await likeComment(beta, 1, betaComment);
154   expect(like.comment.score).toBe(2);
155
156   // Get the post from alpha, check the likes
157   let post = await getPost(alpha, postRes.post.id);
158   expect(post.comments[0].score).toBe(2);
159 });
160
161 test('Reply to a comment', async () => {
162   // Create a comment on alpha, find it on beta
163   let commentRes = await createComment(alpha, postRes.post.id);
164   let searchBeta = await searchComment(beta, commentRes.comment);
165   let betaComment = searchBeta.comments[0];
166
167   // find that comment id on beta
168
169   // Reply from beta
170   let replyRes = await createComment(beta, betaComment.post_id, betaComment.id);
171   expect(replyRes.comment.content).toBeDefined();
172   expect(replyRes.comment.community_local).toBe(true);
173   expect(replyRes.comment.creator_local).toBe(true);
174   expect(replyRes.comment.parent_id).toBe(betaComment.id);
175   expect(replyRes.comment.score).toBe(1);
176
177   // Make sure that comment is seen on alpha
178   // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
179   // comment, isn't working.
180   // let searchAlpha = await searchComment(alpha, replyRes.comment);
181   let post = await getPost(alpha, postRes.post.id);
182   let alphaComment = post.comments[0];
183   expect(alphaComment.content).toBeDefined();
184   expect(alphaComment.parent_id).toBe(post.comments[1].id);
185   expect(alphaComment.community_local).toBe(false);
186   expect(alphaComment.creator_local).toBe(false);
187   expect(alphaComment.score).toBe(1);
188 });
189
190 test('Mention beta', async () => {
191   // Create a mention on alpha
192   let mentionContent = 'A test mention of @lemmy_beta@lemmy-beta:8550';
193   let commentRes = await createComment(alpha, postRes.post.id);
194   let mentionRes = await createComment(
195     alpha,
196     postRes.post.id,
197     commentRes.comment.id,
198     mentionContent
199   );
200   expect(mentionRes.comment.content).toBeDefined();
201   expect(mentionRes.comment.community_local).toBe(false);
202   expect(mentionRes.comment.creator_local).toBe(true);
203   expect(mentionRes.comment.score).toBe(1);
204
205   let mentionsRes = await getMentions(beta);
206   expect(mentionsRes.mentions[0].content).toBeDefined();
207   expect(mentionsRes.mentions[0].community_local).toBe(true);
208   expect(mentionsRes.mentions[0].creator_local).toBe(false);
209   expect(mentionsRes.mentions[0].score).toBe(1);
210 });
211
212 test('Comment Search', async () => {
213   let commentRes = await createComment(alpha, postRes.post.id);
214   let searchBeta = await searchComment(beta, commentRes.comment);
215   expect(searchBeta.comments[0].ap_id).toBe(commentRes.comment.ap_id);
216 });
217
218 test('A and G subscribe to B (center) A posts, G mentions B, it gets announced to A', async () => {
219   // Create a local post
220   let alphaPost = await createPost(alpha, 2);
221   expect(alphaPost.post.community_local).toBe(true);
222
223   // Make sure gamma sees it
224   let search = await searchPost(gamma, alphaPost.post);
225   let gammaPost = search.posts[0];
226
227   let commentContent =
228     'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8550';
229   let commentRes = await createComment(
230     gamma,
231     gammaPost.id,
232     undefined,
233     commentContent
234   );
235   expect(commentRes.comment.content).toBe(commentContent);
236   expect(commentRes.comment.community_local).toBe(false);
237   expect(commentRes.comment.creator_local).toBe(true);
238   expect(commentRes.comment.score).toBe(1);
239
240   // Make sure alpha sees it
241   let alphaPost2 = await getPost(alpha, alphaPost.post.id);
242   expect(alphaPost2.comments[0].content).toBe(commentContent);
243   expect(alphaPost2.comments[0].community_local).toBe(true);
244   expect(alphaPost2.comments[0].creator_local).toBe(false);
245   expect(alphaPost2.comments[0].score).toBe(1);
246
247   // Make sure beta has mentions
248   let mentionsRes = await getMentions(beta);
249   expect(mentionsRes.mentions[0].content).toBe(commentContent);
250   expect(mentionsRes.mentions[0].community_local).toBe(false);
251   expect(mentionsRes.mentions[0].creator_local).toBe(false);
252   // TODO this is failing because fetchInReplyTos aren't getting score
253   // expect(mentionsRes.mentions[0].score).toBe(1);
254 });
255
256 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 () => {
257   // Unfollow all remote communities
258   let followed = await unfollowRemotes(alpha);
259   expect(
260     followed.communities.filter(c => c.community_local == false).length
261   ).toBe(0);
262
263   // B creates a post, and two comments, should be invisible to A
264   let postRes = await createPost(beta, 2);
265   expect(postRes.post.name).toBeDefined();
266
267   let parentCommentContent = 'An invisible top level comment from beta';
268   let parentCommentRes = await createComment(
269     beta,
270     postRes.post.id,
271     undefined,
272     parentCommentContent
273   );
274   expect(parentCommentRes.comment.content).toBe(parentCommentContent);
275
276   // B creates a comment, then a child one of that.
277   let childCommentContent = 'An invisible child comment from beta';
278   let childCommentRes = await createComment(
279     beta,
280     postRes.post.id,
281     parentCommentRes.comment.id,
282     childCommentContent
283   );
284   expect(childCommentRes.comment.content).toBe(childCommentContent);
285
286   // Follow beta again
287   let follow = await followBeta(alpha);
288   expect(follow.community.local).toBe(false);
289   expect(follow.community.name).toBe('main');
290
291   // An update to the child comment on beta, should push the post, parent, and child to alpha now
292   let updatedCommentContent = 'An update child comment from beta';
293   let updateRes = await updateComment(
294     beta,
295     childCommentRes.comment.id,
296     updatedCommentContent
297   );
298   expect(updateRes.comment.content).toBe(updatedCommentContent);
299
300   // Get the post from alpha
301   let createFakeAlphaPostToGetId = await createPost(alpha, 2);
302   let alphaPost = await getPost(alpha, createFakeAlphaPostToGetId.post.id - 1);
303   expect(alphaPost.post.name).toBeDefined();
304   expect(alphaPost.comments[1].content).toBe(parentCommentContent);
305   expect(alphaPost.comments[0].content).toBe(updatedCommentContent);
306   expect(alphaPost.post.community_local).toBe(false);
307   expect(alphaPost.post.creator_local).toBe(false);
308 });