Enhanced testing of comments. Validate reply notifications, mentions (#3686)

* shared.ts first test of getReplies

* comment testing now validates reply notifications and mentions, some code comment cleanup in other functions

* comments revised

* first use of getUnreadCount in testing

* test notification of new comment replies, clarify usage of getReplies

* killall moved earlier in bash script

* api-test jest run does not need directory prefix, make consistent with other jest runs

* do not put my testing system password into script

* fix, killall exits script when no process found

* killall now moved to parent script to release locks before database create

* need to run killall a second time, before database drop

* first use of getReplies getPosts saveUserSettings

* accidental duplication of functions, removed

* try to sync shared library with main

* Nutomic feedback: Better to rename the var instead of putting a comment which can easily get outdated.

* Correct logic to meet join-lemmy requirement, don't have closed signups. Allows Open and Applications. (#3761)

Co-authored-by: Josh Bernardini <josh.bernardini@cologix.com>

* Fix fetch instance software version from nodeinfo (#3772)

Fixes #3771

* remove unused code, revert killall change

---------

Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
Co-authored-by: figure-0e <133478007+figure-0e@users.noreply.github.com>
Co-authored-by: Josh Bernardini <josh.bernardini@cologix.com>
Co-authored-by: Denis Dzyubenko <denis@ddenis.info>
Co-authored-by: Felix Ableitner <me@nutomic.com>
This commit is contained in:
RocketDerp 2023-08-01 06:14:40 -07:00 committed by GitHub
parent 55e383ae38
commit 05a7fced65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 114 additions and 38 deletions

View File

@ -9,7 +9,7 @@
"scripts": { "scripts": {
"lint": "tsc --noEmit && eslint --report-unused-disable-directives --ext .js,.ts,.tsx src && prettier --check 'src/**/*.ts'", "lint": "tsc --noEmit && eslint --report-unused-disable-directives --ext .js,.ts,.tsx src && prettier --check 'src/**/*.ts'",
"fix": "prettier --write src && eslint --fix src", "fix": "prettier --write src && eslint --fix src",
"api-test": "jest -i follow.spec.ts && jest -i src/post.spec.ts && jest -i comment.spec.ts && jest -i private_message.spec.ts && jest -i user.spec.ts && jest -i community.spec.ts" "api-test": "jest -i follow.spec.ts && jest -i post.spec.ts && jest -i comment.spec.ts && jest -i private_message.spec.ts && jest -i user.spec.ts && jest -i community.spec.ts"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^29.5.1", "@types/jest": "^29.5.1",

View File

@ -31,7 +31,7 @@ else
fi fi
echo "killall existing lemmy_server processes" echo "killall existing lemmy_server processes"
killall lemmy_server || true killall -s1 lemmy_server || true
echo "$PWD" echo "$PWD"

View File

@ -13,8 +13,6 @@ popd
yarn yarn
yarn api-test || true yarn api-test || true
killall -s1 lemmy_server
for INSTANCE in lemmy_alpha lemmy_beta lemmy_gamma lemmy_delta lemmy_epsilon; do for INSTANCE in lemmy_alpha lemmy_beta lemmy_gamma lemmy_delta lemmy_epsilon; do
psql "$LEMMY_DATABASE_URL" -c "DROP DATABASE $INSTANCE" psql "$LEMMY_DATABASE_URL" -c "DROP DATABASE $INSTANCE"
done done

View File

@ -30,10 +30,12 @@ import {
getCommentParentId, getCommentParentId,
resolveCommunity, resolveCommunity,
getPersonDetails, getPersonDetails,
getReplies,
getUnreadCount,
} from "./shared"; } from "./shared";
import { CommentView } from "lemmy-js-client/dist/types/CommentView"; import { CommentView } from "lemmy-js-client/dist/types/CommentView";
let postRes: PostResponse; let postOnAlphaRes: PostResponse;
beforeAll(async () => { beforeAll(async () => {
await setupLogins(); await setupLogins();
@ -42,7 +44,7 @@ beforeAll(async () => {
await followBeta(gamma); await followBeta(gamma);
let betaCommunity = (await resolveBetaCommunity(alpha)).community; let betaCommunity = (await resolveBetaCommunity(alpha)).community;
if (betaCommunity) { if (betaCommunity) {
postRes = await createPost(alpha, betaCommunity.community.id); postOnAlphaRes = await createPost(alpha, betaCommunity.community.id);
} }
}); });
@ -65,7 +67,7 @@ function assertCommentFederation(
} }
test("Create a comment", async () => { test("Create a comment", async () => {
let commentRes = await createComment(alpha, postRes.post_view.post.id); let commentRes = await createComment(alpha, postOnAlphaRes.post_view.post.id);
expect(commentRes.comment_view.comment.content).toBeDefined(); expect(commentRes.comment_view.comment.content).toBeDefined();
expect(commentRes.comment_view.community.local).toBe(false); expect(commentRes.comment_view.community.local).toBe(false);
expect(commentRes.comment_view.creator.local).toBe(true); expect(commentRes.comment_view.creator.local).toBe(true);
@ -87,7 +89,7 @@ test("Create a comment in a non-existent post", async () => {
}); });
test("Update a comment", async () => { test("Update a comment", async () => {
let commentRes = await createComment(alpha, postRes.post_view.post.id); let commentRes = await createComment(alpha, postOnAlphaRes.post_view.post.id);
// Federate the comment first // Federate the comment first
let betaComment = ( let betaComment = (
await resolveComment(beta, commentRes.comment_view.comment) await resolveComment(beta, commentRes.comment_view.comment)
@ -113,7 +115,7 @@ test("Update a comment", async () => {
test("Delete a comment", async () => { test("Delete a comment", async () => {
// creating a comment on alpha (remote from home of community) // creating a comment on alpha (remote from home of community)
let commentRes = await createComment(alpha, postRes.post_view.post.id); let commentRes = await createComment(alpha, postOnAlphaRes.post_view.post.id);
// Find the comment on beta (home of community) // Find the comment on beta (home of community)
let betaComment = ( let betaComment = (
@ -167,7 +169,7 @@ test("Delete a comment", async () => {
}); });
test.skip("Remove a comment from admin and community on the same instance", async () => { test.skip("Remove a comment from admin and community on the same instance", async () => {
let commentRes = await createComment(alpha, postRes.post_view.post.id); let commentRes = await createComment(alpha, postOnAlphaRes.post_view.post.id);
// Get the id for beta // Get the id for beta
let betaCommentId = ( let betaCommentId = (
@ -189,13 +191,14 @@ test.skip("Remove a comment from admin and community on the same instance", asyn
); );
expect(refetchedPostComments.comments[0].comment.removed).toBe(true); expect(refetchedPostComments.comments[0].comment.removed).toBe(true);
// beta will unremove the comment
let unremoveCommentRes = await removeComment(beta, false, betaCommentId); let unremoveCommentRes = await removeComment(beta, false, betaCommentId);
expect(unremoveCommentRes.comment_view.comment.removed).toBe(false); expect(unremoveCommentRes.comment_view.comment.removed).toBe(false);
// Make sure that comment is unremoved on beta // Make sure that comment is unremoved on alpha
let refetchedPostComments2 = await getComments( let refetchedPostComments2 = await getComments(
alpha, alpha,
postRes.post_view.post.id, postOnAlphaRes.post_view.post.id,
); );
expect(refetchedPostComments2.comments[0].comment.removed).toBe(false); expect(refetchedPostComments2.comments[0].comment.removed).toBe(false);
assertCommentFederation( assertCommentFederation(
@ -249,7 +252,7 @@ test("Remove a comment from admin and community on different instance", async ()
}); });
test("Unlike a comment", async () => { test("Unlike a comment", async () => {
let commentRes = await createComment(alpha, postRes.post_view.post.id); let commentRes = await createComment(alpha, postOnAlphaRes.post_view.post.id);
// Lemmy automatically creates 1 like (vote) by author of comment. // Lemmy automatically creates 1 like (vote) by author of comment.
// Make sure that comment is liked (voted up) on gamma, downstream peer // Make sure that comment is liked (voted up) on gamma, downstream peer
@ -286,7 +289,7 @@ test("Unlike a comment", async () => {
}); });
test("Federated comment like", async () => { test("Federated comment like", async () => {
let commentRes = await createComment(alpha, postRes.post_view.post.id); let commentRes = await createComment(alpha, postOnAlphaRes.post_view.post.id);
// Find the comment on beta // Find the comment on beta
let betaComment = ( let betaComment = (
@ -301,13 +304,14 @@ test("Federated comment like", async () => {
expect(like.comment_view.counts.score).toBe(2); expect(like.comment_view.counts.score).toBe(2);
// Get the post from alpha, check the likes // Get the post from alpha, check the likes
let postComments = await getComments(alpha, postRes.post_view.post.id); let postComments = await getComments(alpha, postOnAlphaRes.post_view.post.id);
expect(postComments.comments[0].counts.score).toBe(2); expect(postComments.comments[0].counts.score).toBe(2);
}); });
test("Reply to a comment", async () => { test("Reply to a comment from another instance, get notification", async () => {
// Create a comment on alpha, find it on beta // Create a root-level trunk-branch comment on alpha
let commentRes = await createComment(alpha, postRes.post_view.post.id); let commentRes = await createComment(alpha, postOnAlphaRes.post_view.post.id);
// find that comment id on beta
let betaComment = ( let betaComment = (
await resolveComment(beta, commentRes.comment_view.comment) await resolveComment(beta, commentRes.comment_view.comment)
).comment; ).comment;
@ -316,9 +320,7 @@ test("Reply to a comment", async () => {
throw "Missing beta comment"; throw "Missing beta comment";
} }
// find that comment id on beta // Reply from beta, extending the branch
// Reply from beta
let replyRes = await createComment( let replyRes = await createComment(
beta, beta,
betaComment.post.id, betaComment.post.id,
@ -332,11 +334,13 @@ test("Reply to a comment", async () => {
); );
expect(replyRes.comment_view.counts.score).toBe(1); expect(replyRes.comment_view.counts.score).toBe(1);
// Make sure that comment is seen on alpha // Make sure that reply comment is seen on alpha
// TODO not sure why, but a searchComment back to alpha, for the ap_id of betas // TODO not sure why, but a searchComment back to alpha, for the ap_id of betas
// comment, isn't working. // comment, isn't working.
// let searchAlpha = await searchComment(alpha, replyRes.comment); // let searchAlpha = await searchComment(alpha, replyRes.comment);
let postComments = await getComments(alpha, postRes.post_view.post.id); let postComments = await getComments(alpha, postOnAlphaRes.post_view.post.id);
// Note: in Lemmy 0.18.3 pre-release this is coming up 7
expect(postComments.comments.length).toBeGreaterThanOrEqual(2);
let alphaComment = postComments.comments[0]; let alphaComment = postComments.comments[0];
expect(alphaComment.comment.content).toBeDefined(); expect(alphaComment.comment.content).toBeDefined();
expect(getCommentParentId(alphaComment.comment)).toBe( expect(getCommentParentId(alphaComment.comment)).toBe(
@ -346,15 +350,33 @@ test("Reply to a comment", async () => {
expect(alphaComment.creator.local).toBe(false); expect(alphaComment.creator.local).toBe(false);
expect(alphaComment.counts.score).toBe(1); expect(alphaComment.counts.score).toBe(1);
assertCommentFederation(alphaComment, replyRes.comment_view); assertCommentFederation(alphaComment, replyRes.comment_view);
// Did alpha get notified of the reply from beta?
let alphaUnreadCountRes = await getUnreadCount(alpha);
expect(alphaUnreadCountRes.replies).toBe(1);
// check inbox of replies on alpha, fetching read/unread both
let alphaRepliesRes = await getReplies(alpha);
expect(alphaRepliesRes.replies.length).toBe(1);
expect(alphaRepliesRes.replies[0].comment.content).toBeDefined();
expect(alphaRepliesRes.replies[0].community.local).toBe(false);
expect(alphaRepliesRes.replies[0].creator.local).toBe(false);
expect(alphaRepliesRes.replies[0].counts.score).toBe(1);
// ToDo: interesting alphaRepliesRes.replies[0].comment_reply.id is 1, meaning? how did that come about?
expect(alphaRepliesRes.replies[0].comment.id).toBe(alphaComment.comment.id);
// this is a new notification, getReplies fetch was for read/unread both, confirm it is unread.
expect(alphaRepliesRes.replies[0].comment_reply.read).toBe(false);
assertCommentFederation(alphaRepliesRes.replies[0], replyRes.comment_view);
}); });
test("Mention beta", async () => { test("Mention beta from alpha", async () => {
// Create a mention on alpha // Create a new branch, trunk-level comment branch, from alpha instance
let commentRes = await createComment(alpha, postOnAlphaRes.post_view.post.id);
// Create a reply comment to previous comment, this has a mention in body
let mentionContent = "A test mention of @lemmy_beta@lemmy-beta:8551"; let mentionContent = "A test mention of @lemmy_beta@lemmy-beta:8551";
let commentRes = await createComment(alpha, postRes.post_view.post.id);
let mentionRes = await createComment( let mentionRes = await createComment(
alpha, alpha,
postRes.post_view.post.id, postOnAlphaRes.post_view.post.id,
commentRes.comment_view.comment.id, commentRes.comment_view.comment.id,
mentionContent, mentionContent,
); );
@ -363,15 +385,44 @@ test("Mention beta", async () => {
expect(mentionRes.comment_view.creator.local).toBe(true); expect(mentionRes.comment_view.creator.local).toBe(true);
expect(mentionRes.comment_view.counts.score).toBe(1); expect(mentionRes.comment_view.counts.score).toBe(1);
// get beta's localized copy of the alpha post
let betaPost = (await resolvePost(beta, postOnAlphaRes.post_view.post)).post;
if (!betaPost) {
throw "unable to locate post on beta";
}
expect(betaPost.post.ap_id).toBe(postOnAlphaRes.post_view.post.ap_id);
expect(betaPost.post.name).toBe(postOnAlphaRes.post_view.post.name);
// Make sure that both new comments are seen on beta and have parent/child relationship
let betaPostComments = await getComments(beta, betaPost.post.id);
expect(betaPostComments.comments.length).toBeGreaterThanOrEqual(2);
// the trunk-branch root comment will be older than the mention reply comment, so index 1
let betaRootComment = betaPostComments.comments[1];
// the trunk-branch root comment should not have a parent
expect(getCommentParentId(betaRootComment.comment)).toBeUndefined();
expect(betaRootComment.comment.content).toBeDefined();
// the mention reply comment should have parent that points to the branch root level comment
expect(getCommentParentId(betaPostComments.comments[0].comment)).toBe(
betaPostComments.comments[1].comment.id,
);
expect(betaRootComment.community.local).toBe(true);
expect(betaRootComment.creator.local).toBe(false);
expect(betaRootComment.counts.score).toBe(1);
assertCommentFederation(betaRootComment, commentRes.comment_view);
let mentionsRes = await getMentions(beta); let mentionsRes = await getMentions(beta);
expect(mentionsRes.mentions[0].comment.content).toBeDefined(); expect(mentionsRes.mentions[0].comment.content).toBeDefined();
expect(mentionsRes.mentions[0].community.local).toBe(true); expect(mentionsRes.mentions[0].community.local).toBe(true);
expect(mentionsRes.mentions[0].creator.local).toBe(false); expect(mentionsRes.mentions[0].creator.local).toBe(false);
expect(mentionsRes.mentions[0].counts.score).toBe(1); expect(mentionsRes.mentions[0].counts.score).toBe(1);
// the reply comment with mention should be the most fresh, newest, index 0
expect(mentionsRes.mentions[0].person_mention.comment_id).toBe(
betaPostComments.comments[0].comment.id,
);
}); });
test("Comment Search", async () => { test("Comment Search", async () => {
let commentRes = await createComment(alpha, postRes.post_view.post.id); let commentRes = await createComment(alpha, postOnAlphaRes.post_view.post.id);
let betaComment = ( let betaComment = (
await resolveComment(beta, commentRes.comment_view.comment) await resolveComment(beta, commentRes.comment_view.comment)
).comment; ).comment;
@ -496,13 +547,13 @@ test("Fetch in_reply_tos: A is unsubbed from B, B makes a post, and some embedde
).toBe(0); ).toBe(0);
// B creates a post, and two comments, should be invisible to A // B creates a post, and two comments, should be invisible to A
let postRes = await createPost(beta, 2); let postOnBetaRes = await createPost(beta, 2);
expect(postRes.post_view.post.name).toBeDefined(); expect(postOnBetaRes.post_view.post.name).toBeDefined();
let parentCommentContent = "An invisible top level comment from beta"; let parentCommentContent = "An invisible top level comment from beta";
let parentCommentRes = await createComment( let parentCommentRes = await createComment(
beta, beta,
postRes.post_view.post.id, postOnBetaRes.post_view.post.id,
undefined, undefined,
parentCommentContent, parentCommentContent,
); );
@ -514,7 +565,7 @@ test("Fetch in_reply_tos: A is unsubbed from B, B makes a post, and some embedde
let childCommentContent = "An invisible child comment from beta"; let childCommentContent = "An invisible child comment from beta";
let childCommentRes = await createComment( let childCommentRes = await createComment(
beta, beta,
postRes.post_view.post.id, postOnBetaRes.post_view.post.id,
parentCommentRes.comment_view.comment.id, parentCommentRes.comment_view.comment.id,
childCommentContent, childCommentContent,
); );
@ -537,7 +588,8 @@ test("Fetch in_reply_tos: A is unsubbed from B, B makes a post, and some embedde
expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent); expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent);
// Get the post from alpha // Get the post from alpha
let alphaPostB = (await resolvePost(alpha, postRes.post_view.post)).post; let alphaPostB = (await resolvePost(alpha, postOnBetaRes.post_view.post))
.post;
if (!alphaPostB) { if (!alphaPostB) {
throw "Missing alpha post B"; throw "Missing alpha post B";
} }
@ -564,10 +616,11 @@ test("Report a comment", async () => {
if (!betaCommunity) { if (!betaCommunity) {
throw "Missing beta community"; throw "Missing beta community";
} }
let postRes = (await createPost(beta, betaCommunity.community.id)).post_view let postOnBetaRes = (await createPost(beta, betaCommunity.community.id))
.post; .post_view.post;
expect(postRes).toBeDefined(); expect(postOnBetaRes).toBeDefined();
let commentRes = (await createComment(beta, postRes.id)).comment_view.comment; let commentRes = (await createComment(beta, postOnBetaRes.id)).comment_view
.comment;
expect(commentRes).toBeDefined(); expect(commentRes).toBeDefined();
let alphaComment = (await resolveComment(alpha, commentRes)).comment?.comment; let alphaComment = (await resolveComment(alpha, commentRes)).comment?.comment;

View File

@ -1,4 +1,11 @@
import { LemmyHttp } from "lemmy-js-client"; import {
GetReplies,
GetRepliesResponse,
GetUnreadCount,
GetUnreadCountResponse,
LemmyHttp,
LocalUser,
} from "lemmy-js-client";
import { CreatePost } from "lemmy-js-client/dist/types/CreatePost"; import { CreatePost } from "lemmy-js-client/dist/types/CreatePost";
import { DeletePost } from "lemmy-js-client/dist/types/DeletePost"; import { DeletePost } from "lemmy-js-client/dist/types/DeletePost";
import { EditPost } from "lemmy-js-client/dist/types/EditPost"; import { EditPost } from "lemmy-js-client/dist/types/EditPost";
@ -325,6 +332,24 @@ export async function getComments(
return api.client.getComments(form); return api.client.getComments(form);
} }
export async function getUnreadCount(
api: API,
): Promise<GetUnreadCountResponse> {
let form: GetUnreadCount = {
auth: api.auth,
};
return api.client.getUnreadCount(form);
}
export async function getReplies(api: API): Promise<GetRepliesResponse> {
let form: GetReplies = {
sort: "New",
unread_only: false,
auth: api.auth,
};
return api.client.getReplies(form);
}
export async function resolveComment( export async function resolveComment(
api: API, api: API,
comment: Comment, comment: Comment,