Skip to content

Commit

Permalink
chore(test): Improve test readability
Browse files Browse the repository at this point in the history
  • Loading branch information
neet committed Jul 27, 2023
1 parent d9806ed commit 6079201
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 196 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ jobs:
run: yarn install --frozen-lockfile

- name: Run tests
run: yarn run test:e2e --max-workers=1
run: yarn run test:e2e --max-workers=2

- name: Codecov
uses: codecov/codecov-action@v3
Expand Down
51 changes: 31 additions & 20 deletions tests/streaming/errors.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable unicorn/no-array-callback-reference */
import { type mastodon } from "../../src";
import { MastoUnexpectedError } from "../../src/adapters/errors";
import { sleep } from "../../src/utils";
Expand Down Expand Up @@ -28,37 +29,47 @@ it("supports multiplex subscription", () => {
const s2 = alice.ws.public.local.subscribe();
const s1 = alice.ws.hashtag.subscribe({ tag: "test" });

let id!: string;
const e1Promise = s1
.values()
.filter(isUpdate)
.filter(isFrom(alice.id))
.take(1)
.toArray();
const e2Promise = s2
.values()
.filter(isUpdate)
.filter(isFrom(alice.id))
.take(1)
.toArray();
const e3Promise = s3
.values()
.filter(isUpdate)
.filter(isFrom(alice.id))
.take(1)
.toArray();

const dispatch = async () => {
await sleep(1000);
await sleep(1000);

const status = await alice.rest.v1.statuses.create({
status: "#test",
visibility: "public",
});

id = status.id;
};
const status = await alice.rest.v1.statuses.create({
status: "#test",
visibility: "public",
});

try {
/* eslint-disable unicorn/no-array-callback-reference */
const [[e1], [e2], [e3]] = await Promise.all([
s1.values().filter(isUpdate).filter(isFrom(alice.id)).take(1).toArray(),
s2.values().filter(isUpdate).filter(isFrom(alice.id)).take(1).toArray(),
s3.values().filter(isUpdate).filter(isFrom(alice.id)).take(1).toArray(),
dispatch(),
e1Promise,
e2Promise,
e3Promise,
]);
/* eslint-enable unicorn/no-array-callback-reference */

expect(e1.payload.id).toBe(id);
expect(e2.payload.id).toBe(id);
expect(e3.payload.id).toBe(id);
expect(e1.payload.id).toBe(status.id);
expect(e2.payload.id).toBe(status.id);
expect(e3.payload.id).toBe(status.id);
} finally {
s1.unsubscribe();
s2.unsubscribe();
s3.unsubscribe();
await alice.rest.v1.statuses.$select(id).remove();
await alice.rest.v1.statuses.$select(status.id).remove();
}
});
});
97 changes: 36 additions & 61 deletions tests/streaming/events.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,30 @@ import { sleep } from "../../src/utils";
describe("events", () => {
it.concurrent("streams update, status.update, and delete event", () => {
return sessions.use(async (session) => {
let id!: string;
const tag = `tag_${crypto.randomBytes(4).toString("hex")}`;
const subscription = session.ws.hashtag.local.subscribe({ tag });

const dispatch = async () => {
await sleep(1000);
const status = await session.rest.v1.statuses.create({
status: `test1 #${tag}`,
});
id = status.id;
await sleep(1000);
await session.rest.v1.statuses.$select(status.id).update({
status: `test2 #${tag}`,
});
await sleep(1000);
await session.rest.v1.statuses.$select(status.id).remove();
};
const eventsPromise = subscription.values().take(3).toArray();

try {
const [[e1, e2, e3]] = await Promise.all([
subscription.values().take(3).toArray(),
dispatch(),
]);
await sleep(1000);
const status = await session.rest.v1.statuses.create({
status: `test1 #${tag}`,
});
await sleep(1000);
await session.rest.v1.statuses.$select(status.id).update({
status: `test2 #${tag}`,
});
await sleep(1000);
await session.rest.v1.statuses.$select(status.id).remove();

try {
const [e1, e2, e3] = await eventsPromise;
assert(e1.event === "update");
expect(e1.payload.content).toMatch(/test1/);
assert(e2.event === "status.update");
expect(e2.payload.content).toMatch(/test2/);
assert(e3.event === "delete");
expect(e3.payload).toBe(id);
expect(e3.payload).toBe(status.id);
} finally {
subscription.unsubscribe();
}
Expand All @@ -45,23 +39,18 @@ describe("events", () => {
it.concurrent("streams filters_changed event", () => {
return sessions.use(async (session) => {
const subscription = session.ws.user.subscribe();
const eventsPromise = subscription.values().take(1).toArray();

const dispatch = async () => {
await sleep(1000);
const filter = await session.rest.v2.filters.create({
title: "test",
context: ["public"],
keywordsAttributes: [{ keyword: "TypeScript" }],
});
await session.rest.v2.filters.$select(filter.id).remove();
};
await sleep(1000);
const filter = await session.rest.v2.filters.create({
title: "test",
context: ["public"],
keywordsAttributes: [{ keyword: "TypeScript" }],
});
await session.rest.v2.filters.$select(filter.id).remove();

try {
const [[e]] = await Promise.all([
subscription.values().take(1).toArray(),
dispatch(),
]);

const [e] = await eventsPromise;
assert(e.event === "filters_changed");
expect(e.payload).toBeUndefined();
} finally {
Expand All @@ -72,23 +61,16 @@ describe("events", () => {

it.concurrent("streams notification", () => {
return sessions.use(2, async ([alice, bob]) => {
let id!: string;
const subscription = alice.ws.user.notification.subscribe();
const eventsPromise = subscription.values().take(1).toArray();

const dispatch = async () => {
await sleep(1000);
await bob.rest.v1.accounts.$select(alice.id).follow();
};
await sleep(1000);
await bob.rest.v1.accounts.$select(alice.id).follow();

try {
const [[e]] = await Promise.all([
subscription.values().take(1).toArray(),
dispatch(),
]);

const [e] = await eventsPromise;
assert(e.event === "notification");
expect(e.payload.account.id).toBe(bob.id);
expect(e.payload.status?.id).toBe(id);
} finally {
await bob.rest.v1.accounts.$select(alice.id).unfollow();
subscription.unsubscribe();
Expand All @@ -98,28 +80,21 @@ describe("events", () => {

it.concurrent("streams conversation", () => {
return sessions.use(2, async ([alice, bob]) => {
let id!: string;
const subscription = alice.ws.direct.subscribe();
const eventsPromise = subscription.values().take(1).toArray();

const dispatch = async () => {
await sleep(1000);
const status = await bob.rest.v1.statuses.create({
status: `@${alice.acct} Hello there`,
visibility: "direct",
});
id = status.id;
};
await sleep(1000);
const status = await bob.rest.v1.statuses.create({
status: `@${alice.acct} Hello there`,
visibility: "direct",
});

try {
const [[e]] = await Promise.all([
subscription.values().take(1).toArray(),
dispatch(),
]);

const [e] = await eventsPromise;
assert(e.event === "conversation");
expect(e.payload.lastStatus?.id).toBe(id);
expect(e.payload.lastStatus?.id).toBe(status.id);
} finally {
await bob.rest.v1.statuses.$select(id).remove();
await bob.rest.v1.statuses.$select(status.id).remove();
subscription.unsubscribe();
}
});
Expand Down
Loading

0 comments on commit 6079201

Please sign in to comment.