Skip to content

Commit

Permalink
clean up tests better
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinse12 committed Nov 1, 2024
1 parent b40ae87 commit 3101fc8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/test/test-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ export async function mockRpcServerPlebbit(plebbitOptions?: InputPlebbitOptions)
}

export async function mockRpcRemotePlebbit(plebbitOptions?: InputPlebbitOptions) {
if (!isRpcFlagOn()) throw Error("Can't connect to RPC server without RPC flag on");
// This instance will connect to an rpc server that has no local subs
const plebbit = await mockPlebbit({ plebbitRpcClientsOptions: ["ws://localhost:39653"], dataPath: undefined, ...plebbitOptions });
return plebbit;
Expand Down
43 changes: 24 additions & 19 deletions test/node-and-browser/publications/comment/update.fields.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,28 @@ getRemotePlebbitConfigs().map((config) => {
let plebbit, post, reply;
before(async () => {
plebbit = await config.plebbitInstancePromise();
post = await publishRandomPost(subplebbitAddress, plebbit, {}, false);
post = await publishRandomPost(subplebbitAddress, plebbit);
await post.update();
await resolveWhenConditionIsTrue(post, () => typeof post.updatedAt === "number");
expect(post.replyCount).to.equal(0);
});

after(() => post.stop() && reply.stop());
after(async () => {
await post.stop();
await reply.stop();
await plebbit.destroy();
});

it(`post.replyCount increases with a direct reply`, async () => {
reply = await publishRandomReply(post, plebbit, {}, false);
reply = await publishRandomReply(post, plebbit);
await reply.update();
await new Promise((resolve) => reply.once("update", resolve));
await resolveWhenConditionIsTrue(post, () => post.replyCount === 1);
expect(post.replyCount).to.equal(1);
});

it(`post.replyCount increases with a reply of a reply`, async () => {
await publishRandomReply(reply, plebbit, {}, false);
await publishRandomReply(reply, plebbit);
await resolveWhenConditionIsTrue(post, () => post.replyCount === 2);
await resolveWhenConditionIsTrue(reply, () => reply.replyCount === 1);
expect(post.replyCount).to.equal(2);
Expand All @@ -48,7 +52,7 @@ getRemotePlebbitConfigs().map((config) => {
let post, plebbit;
before(async () => {
plebbit = await config.plebbitInstancePromise();
post = await publishRandomPost(subplebbitAddress, plebbit, {}, false);
post = await publishRandomPost(subplebbitAddress, plebbit);
await post.update();
await resolveWhenConditionIsTrue(post, () => typeof post.updatedAt === "number");
expect(post.lastChildCid).to.be.undefined;
Expand All @@ -59,14 +63,14 @@ getRemotePlebbitConfigs().map((config) => {
});

it(`commentUpdate.lastChildCid updates to the latest child comment when replying to post directly`, async () => {
const reply = await publishRandomReply(post, plebbit, {}, false);
const reply = await publishRandomReply(post, plebbit);
await resolveWhenConditionIsTrue(post, () => post.replyCount === 1);
expect(post.replyCount).to.equal(1);
expect(post.lastChildCid).to.equal(reply.cid);
});

it(`commentUpdate.lastChildCid of a post does not update when replying to a comment under one of its replies`, async () => {
await publishRandomReply(post.replies.pages.topAll.comments[0], plebbit, {}, false);
await publishRandomReply(post.replies.pages.topAll.comments[0], plebbit);
await resolveWhenConditionIsTrue(post, () => post.replyCount === 2);
expect(post.replyCount).to.equal(2);
expect(post.lastChildCid).to.equal(post.replies.pages.topAll.comments[0].cid);
Expand All @@ -77,8 +81,8 @@ getRemotePlebbitConfigs().map((config) => {
let post, plebbit, reply;
before(async () => {
plebbit = await config.plebbitInstancePromise();
post = await publishRandomPost(subplebbitAddress, plebbit, {}, false);
post.update();
post = await publishRandomPost(subplebbitAddress, plebbit);
await post.update();
await resolveWhenConditionIsTrue(post, () => typeof post.updatedAt === "number");
expect(post.lastReplyTimestamp).to.be.undefined;
});
Expand All @@ -88,14 +92,14 @@ getRemotePlebbitConfigs().map((config) => {
});

it(`commentUpdate.lastReplyTimestamp updates to the latest child comment's timestamp`, async () => {
reply = await publishRandomReply(post, plebbit, {}, false);
reply = await publishRandomReply(post, plebbit);
await resolveWhenConditionIsTrue(post, () => post.replyCount === 1);
expect(post.replyCount).to.equal(1);
expect(post.lastReplyTimestamp).to.equal(reply.timestamp);
});

it(`commentUpdate.lastChildCid of a post does not update when replying to a comment under one of its replies`, async () => {
const replyOfReply = await publishRandomReply(reply, plebbit, {}, false);
const replyOfReply = await publishRandomReply(reply, plebbit);
await resolveWhenConditionIsTrue(post, () => post.replyCount === 2);
expect(post.replyCount).to.equal(2);
expect(post.lastReplyTimestamp).to.equal(replyOfReply.timestamp);
Expand All @@ -107,15 +111,16 @@ getRemotePlebbitConfigs().map((config) => {

before(async () => {
plebbit = await config.plebbitInstancePromise();
post = await publishRandomPost(subplebbitAddress, plebbit, {}, false);
post = await publishRandomPost(subplebbitAddress, plebbit);
await post.update();
await resolveWhenConditionIsTrue(post, () => typeof post.updatedAt === "number");
});

after(async () => await post.stop());
after(async () => {
await post.stop();
await plebbit.destroy();
});

it(`post.author.subplebbit.postScore increases with upvote to post`, async () => {
expect(post.cid).to.be.a("string");
await publishVote(post.cid, post.subplebbitAddress, 1, plebbit);
await resolveWhenConditionIsTrue(post, () => post.upvoteCount === 1);
expect(post.upvoteCount).to.equal(1);
Expand All @@ -124,7 +129,7 @@ getRemotePlebbitConfigs().map((config) => {
});

it(`post.author.subplebbit.postScore increases with upvote to another post`, async () => {
const anotherPost = await publishRandomPost(subplebbitAddress, plebbit, { signer: post.signer }, false);
const anotherPost = await publishRandomPost(subplebbitAddress, plebbit, { signer: post.signer });
await anotherPost.update();
await publishVote(anotherPost.cid, anotherPost.subplebbitAddress, 1, plebbit);
await resolveWhenConditionIsTrue(post, () => post.author.subplebbit.postScore === 2);
Expand All @@ -141,7 +146,7 @@ getRemotePlebbitConfigs().map((config) => {
});

it(`post.author.subplebbit.replyScore increases with upvote to author replies`, async () => {
const reply = await publishRandomReply(post, plebbit, { signer: post.signer }, false);
const reply = await publishRandomReply(post, plebbit, { signer: post.signer });
await reply.update();
await publishVote(reply.cid, reply.subplebbitAddress, 1, plebbit);
await resolveWhenConditionIsTrue(reply, () => reply.upvoteCount === 1);
Expand All @@ -160,7 +165,7 @@ getRemotePlebbitConfigs().map((config) => {
});

it(`author.subplebbit.lastCommentCid is updated with every new post of author`, async () => {
const anotherPost = await publishRandomPost(subplebbitAddress, plebbit, { signer: post.signer }, false);
const anotherPost = await publishRandomPost(subplebbitAddress, plebbit, { signer: post.signer });
await anotherPost.update();

await resolveWhenConditionIsTrue(post, () => post.author.subplebbit.lastCommentCid === anotherPost.cid);
Expand All @@ -173,7 +178,7 @@ getRemotePlebbitConfigs().map((config) => {
});

it(`author.subplebbit.lastCommentCid is updated with every new reply of author`, async () => {
const reply = await publishRandomReply(post, plebbit, { signer: post.signer }, false);
const reply = await publishRandomReply(post, plebbit, { signer: post.signer });
await reply.update();
await resolveWhenConditionIsTrue(post, () => post.replyCount === 2);
await resolveWhenConditionIsTrue(reply, () => typeof reply.updatedAt === "number");
Expand Down

0 comments on commit 3101fc8

Please sign in to comment.