]> Untitled Git - lemmy.git/blobdiff - api_tests/src/comment.spec.ts
Merge branch 'drone-io-dess' into move_views_to_diesel_drone
[lemmy.git] / api_tests / src / comment.spec.ts
index 57756a39e3779749eb18c0ad919314453080d116..b0ca55d0d648fa8d0d8795248ff9fda6a31a816f 100644 (file)
@@ -11,7 +11,7 @@ import {
   followBeta,
   searchForBetaCommunity,
   createComment,
-  updateComment,
+  editComment,
   deleteComment,
   removeComment,
   getMentions,
@@ -21,9 +21,7 @@ import {
   registerUser,
   API,
 } from './shared';
-import {
-  Comment,
-} from 'lemmy-js-client';
+import { CommentView } from 'lemmy-js-client';
 
 import { PostResponse } from 'lemmy-js-client';
 
@@ -36,7 +34,7 @@ beforeAll(async () => {
   let search = await searchForBetaCommunity(alpha);
   postRes = await createPost(
     alpha,
-    search.communities.filter(c => c.local == false)[0].id
+    search.communities.find(c => c.community.local == false).community.id
   );
 });
 
@@ -46,33 +44,34 @@ afterAll(async () => {
 });
 
 function assertCommentFederation(
-  commentOne: Comment,
-  commentTwo: Comment) {
-  expect(commentOne.ap_id).toBe(commentOne.ap_id);
-  expect(commentOne.content).toBe(commentTwo.content);
-  expect(commentOne.creator_name).toBe(commentTwo.creator_name);
-  expect(commentOne.community_actor_id).toBe(commentTwo.community_actor_id);
-  expect(commentOne.published).toBe(commentTwo.published);
-  expect(commentOne.updated).toBe(commentOne.updated);
-  expect(commentOne.deleted).toBe(commentOne.deleted);
-  expect(commentOne.removed).toBe(commentOne.removed);
+  commentOne: CommentView,
+  commentTwo: CommentView
+) {
+  expect(commentOne.comment.ap_id).toBe(commentOne.comment.ap_id);
+  expect(commentOne.comment.content).toBe(commentTwo.comment.content);
+  expect(commentOne.creator.name).toBe(commentTwo.creator.name);
+  expect(commentOne.community.actor_id).toBe(commentTwo.community.actor_id);
+  expect(commentOne.comment.published).toBe(commentTwo.comment.published);
+  expect(commentOne.comment.updated).toBe(commentOne.comment.updated);
+  expect(commentOne.comment.deleted).toBe(commentOne.comment.deleted);
+  expect(commentOne.comment.removed).toBe(commentOne.comment.removed);
 }
 
 test('Create a comment', async () => {
-  let commentRes = await createComment(alpha, postRes.post.id);
-  expect(commentRes.comment.content).toBeDefined();
-  expect(commentRes.comment.community_local).toBe(false);
-  expect(commentRes.comment.creator_local).toBe(true);
-  expect(commentRes.comment.score).toBe(1);
+  let commentRes = await createComment(alpha, postRes.post_view.post.id);
+  expect(commentRes.comment_view.comment.content).toBeDefined();
+  expect(commentRes.comment_view.community.local).toBe(false);
+  expect(commentRes.comment_view.creator.local).toBe(true);
+  expect(commentRes.comment_view.counts.score).toBe(1);
 
   // Make sure that comment is liked on beta
-  let searchBeta = await searchComment(beta, commentRes.comment);
+  let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
   let betaComment = searchBeta.comments[0];
   expect(betaComment).toBeDefined();
-  expect(betaComment.community_local).toBe(true);
-  expect(betaComment.creator_local).toBe(false);
-  expect(betaComment.score).toBe(1);
-  assertCommentFederation(betaComment, commentRes.comment);
+  expect(betaComment.community.local).toBe(true);
+  expect(betaComment.creator.local).toBe(false);
+  expect(betaComment.counts.score).toBe(1);
+  assertCommentFederation(betaComment, commentRes.comment_view);
 });
 
 test('Create a comment in a non-existent post', async () => {
@@ -81,74 +80,90 @@ test('Create a comment in a non-existent post', async () => {
 });
 
 test('Update a comment', async () => {
-  let commentRes = await createComment(alpha, postRes.post.id);
+  let commentRes = await createComment(alpha, postRes.post_view.post.id);
   // Federate the comment first
-  let searchBeta = await searchComment(beta, commentRes.comment);
-  assertCommentFederation(searchBeta.comments[0], commentRes.comment);
+  let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
+  assertCommentFederation(searchBeta.comments[0], commentRes.comment_view);
 
-  let updateCommentRes = await updateComment(alpha, commentRes.comment.id);
-  expect(updateCommentRes.comment.content).toBe(
+  let updateCommentRes = await editComment(
+    alpha,
+    commentRes.comment_view.comment.id
+  );
+  expect(updateCommentRes.comment_view.comment.content).toBe(
     'A jest test federated comment update'
   );
-  expect(updateCommentRes.comment.community_local).toBe(false);
-  expect(updateCommentRes.comment.creator_local).toBe(true);
+  expect(updateCommentRes.comment_view.community.local).toBe(false);
+  expect(updateCommentRes.comment_view.creator.local).toBe(true);
 
   // Make sure that post is updated on beta
-  let searchBetaUpdated = await searchComment(beta, commentRes.comment);
-  assertCommentFederation(searchBetaUpdated.comments[0], updateCommentRes.comment);
+  let searchBetaUpdated = await searchComment(
+    beta,
+    commentRes.comment_view.comment
+  );
+  assertCommentFederation(
+    searchBetaUpdated.comments[0],
+    updateCommentRes.comment_view
+  );
 });
 
 test('Delete a comment', async () => {
-  let commentRes = await createComment(alpha, postRes.post.id);
+  let commentRes = await createComment(alpha, postRes.post_view.post.id);
 
   let deleteCommentRes = await deleteComment(
     alpha,
     true,
-    commentRes.comment.id
+    commentRes.comment_view.comment.id
   );
-  expect(deleteCommentRes.comment.deleted).toBe(true);
+  expect(deleteCommentRes.comment_view.comment.deleted).toBe(true);
 
   // Make sure that comment is undefined on beta
-  let searchBeta = await searchComment(beta, commentRes.comment);
+  let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
   let betaComment = searchBeta.comments[0];
   expect(betaComment).toBeUndefined();
 
   let undeleteCommentRes = await deleteComment(
     alpha,
     false,
-    commentRes.comment.id
+    commentRes.comment_view.comment.id
   );
-  expect(undeleteCommentRes.comment.deleted).toBe(false);
+  expect(undeleteCommentRes.comment_view.comment.deleted).toBe(false);
 
   // Make sure that comment is undeleted on beta
-  let searchBeta2 = await searchComment(beta, commentRes.comment);
+  let searchBeta2 = await searchComment(beta, commentRes.comment_view.comment);
   let betaComment2 = searchBeta2.comments[0];
-  expect(betaComment2.deleted).toBe(false);
-  assertCommentFederation(searchBeta2.comments[0], undeleteCommentRes.comment);
+  expect(betaComment2.comment.deleted).toBe(false);
+  assertCommentFederation(
+    searchBeta2.comments[0],
+    undeleteCommentRes.comment_view
+  );
 });
 
 test('Remove a comment from admin and community on the same instance', async () => {
-  let commentRes = await createComment(alpha, postRes.post.id);
+  let commentRes = await createComment(alpha, postRes.post_view.post.id);
 
   // Get the id for beta
-  let betaCommentId = (await searchComment(beta, commentRes.comment))
-    .comments[0].id;
+  let betaCommentId = (
+    await searchComment(beta, commentRes.comment_view.comment)
+  ).comments[0].comment.id;
 
   // The beta admin removes it (the community lives on beta)
   let removeCommentRes = await removeComment(beta, true, betaCommentId);
-  expect(removeCommentRes.comment.removed).toBe(true);
+  expect(removeCommentRes.comment_view.comment.removed).toBe(true);
 
   // Make sure that comment is removed on alpha (it gets pushed since an admin from beta removed it)
-  let refetchedPost = await getPost(alpha, postRes.post.id);
-  expect(refetchedPost.comments[0].removed).toBe(true);
+  let refetchedPost = await getPost(alpha, postRes.post_view.post.id);
+  expect(refetchedPost.comments[0].comment.removed).toBe(true);
 
   let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
-  expect(unremoveCommentRes.comment.removed).toBe(false);
+  expect(unremoveCommentRes.comment_view.comment.removed).toBe(false);
 
   // Make sure that comment is unremoved on beta
-  let refetchedPost2 = await getPost(alpha, postRes.post.id);
-  expect(refetchedPost2.comments[0].removed).toBe(false);
-  assertCommentFederation(refetchedPost2.comments[0], unremoveCommentRes.comment);
+  let refetchedPost2 = await getPost(alpha, postRes.post_view.post.id);
+  expect(refetchedPost2.comments[0].comment.removed).toBe(false);
+  assertCommentFederation(
+    refetchedPost2.comments[0],
+    unremoveCommentRes.comment_view
+  );
 });
 
 test('Remove a comment from admin and community on different instance', async () => {
@@ -160,144 +175,155 @@ test('Remove a comment from admin and community on different instance', async ()
 
   // New alpha user creates a community, post, and comment.
   let newCommunity = await createCommunity(newAlphaApi);
-  let newPost = await createPost(newAlphaApi, newCommunity.community.id);
-  let commentRes = await createComment(newAlphaApi, newPost.post.id);
-  expect(commentRes.comment.content).toBeDefined();
+  let newPost = await createPost(
+    newAlphaApi,
+    newCommunity.community_view.community.id
+  );
+  let commentRes = await createComment(newAlphaApi, newPost.post_view.post.id);
+  expect(commentRes.comment_view.comment.content).toBeDefined();
 
   // Beta searches that to cache it, then removes it
-  let searchBeta = await searchComment(beta, commentRes.comment);
+  let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
   let betaComment = searchBeta.comments[0];
-  let removeCommentRes = await removeComment(beta, true, betaComment.id);
-  expect(removeCommentRes.comment.removed).toBe(true);
+  let removeCommentRes = await removeComment(
+    beta,
+    true,
+    betaComment.comment.id
+  );
+  expect(removeCommentRes.comment_view.comment.removed).toBe(true);
 
   // Make sure its not removed on alpha
-  let refetchedPost = await getPost(newAlphaApi, newPost.post.id);
-  expect(refetchedPost.comments[0].removed).toBe(false);
-  assertCommentFederation(refetchedPost.comments[0], commentRes.comment);
+  let refetchedPost = await getPost(newAlphaApi, newPost.post_view.post.id);
+  expect(refetchedPost.comments[0].comment.removed).toBe(false);
+  assertCommentFederation(refetchedPost.comments[0], commentRes.comment_view);
 });
 
 test('Unlike a comment', async () => {
-  let commentRes = await createComment(alpha, postRes.post.id);
-  let unlike = await likeComment(alpha, 0, commentRes.comment);
-  expect(unlike.comment.score).toBe(0);
+  let commentRes = await createComment(alpha, postRes.post_view.post.id);
+  let unlike = await likeComment(alpha, 0, commentRes.comment_view.comment);
+  expect(unlike.comment_view.counts.score).toBe(0);
 
   // Make sure that post is unliked on beta
-  let searchBeta = await searchComment(beta, commentRes.comment);
+  let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
   let betaComment = searchBeta.comments[0];
   expect(betaComment).toBeDefined();
-  expect(betaComment.community_local).toBe(true);
-  expect(betaComment.creator_local).toBe(false);
-  expect(betaComment.score).toBe(0);
+  expect(betaComment.community.local).toBe(true);
+  expect(betaComment.creator.local).toBe(false);
+  expect(betaComment.counts.score).toBe(0);
 });
 
 test('Federated comment like', async () => {
-  let commentRes = await createComment(alpha, postRes.post.id);
+  let commentRes = await createComment(alpha, postRes.post_view.post.id);
 
   // Find the comment on beta
-  let searchBeta = await searchComment(beta, commentRes.comment);
+  let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
   let betaComment = searchBeta.comments[0];
 
-  let like = await likeComment(beta, 1, betaComment);
-  expect(like.comment.score).toBe(2);
+  let like = await likeComment(beta, 1, betaComment.comment);
+  expect(like.comment_view.counts.score).toBe(2);
 
   // Get the post from alpha, check the likes
-  let post = await getPost(alpha, postRes.post.id);
-  expect(post.comments[0].score).toBe(2);
+  let post = await getPost(alpha, postRes.post_view.post.id);
+  expect(post.comments[0].counts.score).toBe(2);
 });
 
 test('Reply to a comment', async () => {
   // Create a comment on alpha, find it on beta
-  let commentRes = await createComment(alpha, postRes.post.id);
-  let searchBeta = await searchComment(beta, commentRes.comment);
+  let commentRes = await createComment(alpha, postRes.post_view.post.id);
+  let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
   let betaComment = searchBeta.comments[0];
 
   // find that comment id on beta
 
   // Reply from beta
-  let replyRes = await createComment(beta, betaComment.post_id, betaComment.id);
-  expect(replyRes.comment.content).toBeDefined();
-  expect(replyRes.comment.community_local).toBe(true);
-  expect(replyRes.comment.creator_local).toBe(true);
-  expect(replyRes.comment.parent_id).toBe(betaComment.id);
-  expect(replyRes.comment.score).toBe(1);
+  let replyRes = await createComment(
+    beta,
+    betaComment.post.id,
+    betaComment.comment.id
+  );
+  expect(replyRes.comment_view.comment.content).toBeDefined();
+  expect(replyRes.comment_view.community.local).toBe(true);
+  expect(replyRes.comment_view.creator.local).toBe(true);
+  expect(replyRes.comment_view.comment.parent_id).toBe(betaComment.comment.id);
+  expect(replyRes.comment_view.counts.score).toBe(1);
 
   // Make sure that comment is seen on alpha
   // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
   // comment, isn't working.
   // let searchAlpha = await searchComment(alpha, replyRes.comment);
-  let post = await getPost(alpha, postRes.post.id);
+  let post = await getPost(alpha, postRes.post_view.post.id);
   let alphaComment = post.comments[0];
-  expect(alphaComment.content).toBeDefined();
-  expect(alphaComment.parent_id).toBe(post.comments[1].id);
-  expect(alphaComment.community_local).toBe(false);
-  expect(alphaComment.creator_local).toBe(false);
-  expect(alphaComment.score).toBe(1);
-  assertCommentFederation(alphaComment, replyRes.comment);
+  expect(alphaComment.comment.content).toBeDefined();
+  expect(alphaComment.comment.parent_id).toBe(post.comments[1].comment.id);
+  expect(alphaComment.community.local).toBe(false);
+  expect(alphaComment.creator.local).toBe(false);
+  expect(alphaComment.counts.score).toBe(1);
+  assertCommentFederation(alphaComment, replyRes.comment_view);
 });
 
 test('Mention beta', async () => {
   // Create a mention on alpha
   let mentionContent = 'A test mention of @lemmy_beta@lemmy-beta:8551';
-  let commentRes = await createComment(alpha, postRes.post.id);
+  let commentRes = await createComment(alpha, postRes.post_view.post.id);
   let mentionRes = await createComment(
     alpha,
-    postRes.post.id,
-    commentRes.comment.id,
+    postRes.post_view.post.id,
+    commentRes.comment_view.comment.id,
     mentionContent
   );
-  expect(mentionRes.comment.content).toBeDefined();
-  expect(mentionRes.comment.community_local).toBe(false);
-  expect(mentionRes.comment.creator_local).toBe(true);
-  expect(mentionRes.comment.score).toBe(1);
+  expect(mentionRes.comment_view.comment.content).toBeDefined();
+  expect(mentionRes.comment_view.community.local).toBe(false);
+  expect(mentionRes.comment_view.creator.local).toBe(true);
+  expect(mentionRes.comment_view.counts.score).toBe(1);
 
   let mentionsRes = await getMentions(beta);
-  expect(mentionsRes.mentions[0].content).toBeDefined();
-  expect(mentionsRes.mentions[0].community_local).toBe(true);
-  expect(mentionsRes.mentions[0].creator_local).toBe(false);
-  expect(mentionsRes.mentions[0].score).toBe(1);
+  expect(mentionsRes.mentions[0].comment.content).toBeDefined();
+  expect(mentionsRes.mentions[0].community.local).toBe(true);
+  expect(mentionsRes.mentions[0].creator.local).toBe(false);
+  expect(mentionsRes.mentions[0].counts.score).toBe(1);
 });
 
 test('Comment Search', async () => {
-  let commentRes = await createComment(alpha, postRes.post.id);
-  let searchBeta = await searchComment(beta, commentRes.comment);
-  assertCommentFederation(searchBeta.comments[0], commentRes.comment);
+  let commentRes = await createComment(alpha, postRes.post_view.post.id);
+  let searchBeta = await searchComment(beta, commentRes.comment_view.comment);
+  assertCommentFederation(searchBeta.comments[0], commentRes.comment_view);
 });
 
 test('A and G subscribe to B (center) A posts, G mentions B, it gets announced to A', async () => {
   // Create a local post
   let alphaPost = await createPost(alpha, 2);
-  expect(alphaPost.post.community_local).toBe(true);
+  expect(alphaPost.post_view.community.local).toBe(true);
 
   // Make sure gamma sees it
-  let search = await searchPost(gamma, alphaPost.post);
+  let search = await searchPost(gamma, alphaPost.post_view.post);
   let gammaPost = search.posts[0];
 
   let commentContent =
     'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551';
   let commentRes = await createComment(
     gamma,
-    gammaPost.id,
+    gammaPost.post.id,
     undefined,
     commentContent
   );
-  expect(commentRes.comment.content).toBe(commentContent);
-  expect(commentRes.comment.community_local).toBe(false);
-  expect(commentRes.comment.creator_local).toBe(true);
-  expect(commentRes.comment.score).toBe(1);
+  expect(commentRes.comment_view.comment.content).toBe(commentContent);
+  expect(commentRes.comment_view.community.local).toBe(false);
+  expect(commentRes.comment_view.creator.local).toBe(true);
+  expect(commentRes.comment_view.counts.score).toBe(1);
 
   // Make sure alpha sees it
-  let alphaPost2 = await getPost(alpha, alphaPost.post.id);
-  expect(alphaPost2.comments[0].content).toBe(commentContent);
-  expect(alphaPost2.comments[0].community_local).toBe(true);
-  expect(alphaPost2.comments[0].creator_local).toBe(false);
-  expect(alphaPost2.comments[0].score).toBe(1);
-  assertCommentFederation(alphaPost2.comments[0], commentRes.comment);
+  let alphaPost2 = await getPost(alpha, alphaPost.post_view.post.id);
+  expect(alphaPost2.comments[0].comment.content).toBe(commentContent);
+  expect(alphaPost2.comments[0].community.local).toBe(true);
+  expect(alphaPost2.comments[0].creator.local).toBe(false);
+  expect(alphaPost2.comments[0].counts.score).toBe(1);
+  assertCommentFederation(alphaPost2.comments[0], commentRes.comment_view);
 
   // Make sure beta has mentions
   let mentionsRes = await getMentions(beta);
-  expect(mentionsRes.mentions[0].content).toBe(commentContent);
-  expect(mentionsRes.mentions[0].community_local).toBe(false);
-  expect(mentionsRes.mentions[0].creator_local).toBe(false);
+  expect(mentionsRes.mentions[0].comment.content).toBe(commentContent);
+  expect(mentionsRes.mentions[0].community.local).toBe(false);
+  expect(mentionsRes.mentions[0].creator.local).toBe(false);
   // TODO this is failing because fetchInReplyTos aren't getting score
   // expect(mentionsRes.mentions[0].score).toBe(1);
 });
@@ -306,56 +332,60 @@ test('Fetch in_reply_tos: A is unsubbed from B, B makes a post, and some embedde
   // Unfollow all remote communities
   let followed = await unfollowRemotes(alpha);
   expect(
-    followed.communities.filter(c => c.community_local == false).length
+    followed.communities.filter(c => c.community.local == false).length
   ).toBe(0);
 
   // B creates a post, and two comments, should be invisible to A
   let postRes = await createPost(beta, 2);
-  expect(postRes.post.name).toBeDefined();
+  expect(postRes.post_view.post.name).toBeDefined();
 
   let parentCommentContent = 'An invisible top level comment from beta';
   let parentCommentRes = await createComment(
     beta,
-    postRes.post.id,
+    postRes.post_view.post.id,
     undefined,
     parentCommentContent
   );
-  expect(parentCommentRes.comment.content).toBe(parentCommentContent);
+  expect(parentCommentRes.comment_view.comment.content).toBe(
+    parentCommentContent
+  );
 
   // B creates a comment, then a child one of that.
   let childCommentContent = 'An invisible child comment from beta';
   let childCommentRes = await createComment(
     beta,
-    postRes.post.id,
-    parentCommentRes.comment.id,
+    postRes.post_view.post.id,
+    parentCommentRes.comment_view.comment.id,
+    childCommentContent
+  );
+  expect(childCommentRes.comment_view.comment.content).toBe(
     childCommentContent
   );
-  expect(childCommentRes.comment.content).toBe(childCommentContent);
 
   // Follow beta again
   let follow = await followBeta(alpha);
-  expect(follow.community.local).toBe(false);
-  expect(follow.community.name).toBe('main');
+  expect(follow.community_view.community.local).toBe(false);
+  expect(follow.community_view.community.name).toBe('main');
 
   // An update to the child comment on beta, should push the post, parent, and child to alpha now
   let updatedCommentContent = 'An update child comment from beta';
-  let updateRes = await updateComment(
+  let updateRes = await editComment(
     beta,
-    childCommentRes.comment.id,
+    childCommentRes.comment_view.comment.id,
     updatedCommentContent
   );
-  expect(updateRes.comment.content).toBe(updatedCommentContent);
+  expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent);
 
   // Get the post from alpha
-  let search = await searchPost(alpha, postRes.post);
+  let search = await searchPost(alpha, postRes.post_view.post);
   let alphaPostB = search.posts[0];
 
-  let alphaPost = await getPost(alpha, alphaPostB.id);
-  expect(alphaPost.post.name).toBeDefined();
-  assertCommentFederation(alphaPost.comments[1], parentCommentRes.comment);
-  assertCommentFederation(alphaPost.comments[0], updateRes.comment);
-  expect(alphaPost.post.community_local).toBe(false);
-  expect(alphaPost.post.creator_local).toBe(false);
+  let alphaPost = await getPost(alpha, alphaPostB.post.id);
+  expect(alphaPost.post_view.post.name).toBeDefined();
+  assertCommentFederation(alphaPost.comments[1], parentCommentRes.comment_view);
+  assertCommentFederation(alphaPost.comments[0], updateRes.comment_view);
+  expect(alphaPost.post_view.community.local).toBe(false);
+  expect(alphaPost.post_view.creator.local).toBe(false);
 
   await unfollowRemotes(alpha);
 });