Skip to content

Commit

Permalink
add cleanup to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
szuperaz committed Nov 7, 2023
1 parent 943a880 commit c8896ba
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 62 deletions.
23 changes: 15 additions & 8 deletions __tests__/block-lists.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "dotenv/config";
import { beforeAll, beforeEach, describe, expect, it } from "vitest";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { BlockList, StreamClient } from "../";
import { v4 as uuidv4 } from "uuid";

Expand All @@ -10,17 +10,14 @@ describe("block lists CRUD API", () => {
let client: StreamClient;
let blockList: BlockList;

beforeEach(() => {
beforeAll(() => {
client = new StreamClient(apiKey, secret);
blockList = {
name: "F1" + uuidv4(),
name: "streamnodetest-F1" + uuidv4(),
words: ["Ricciardo should retire"],
};
});

beforeAll(() => {
client = new StreamClient(apiKey, secret);
});

it("create", async () => {
const response = await client.chat.createBlockList(blockList);

Expand Down Expand Up @@ -59,7 +56,17 @@ describe("block lists CRUD API", () => {
const response = await client.chat.deleteBlockList({
name: blockList.name,
});

expect(response).toBeDefined();
});

afterAll(async () => {
const blockLists = (await client.chat.listBlockLists()).blocklists;
const customBlockLists = blockLists.filter((b) =>
b.name.startsWith("streamnodetest-F1")
);

await Promise.all(
customBlockLists.map((b) => client.chat.deleteBlockList({ name: b.name }))
);
});
});
15 changes: 13 additions & 2 deletions __tests__/call-types.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "dotenv/config";
import { beforeAll, describe, expect, it } from "vitest";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { v4 as uuidv4 } from "uuid";
import {
StreamClient,
Expand All @@ -14,7 +14,7 @@ const secret = process.env.STREAM_SECRET!;

describe("call types CRUD API", () => {
let client: StreamClient;
const callTypeName = `calltype${uuidv4()}`;
const callTypeName = `streamnodetest${uuidv4()}`;

beforeAll(() => {
client = new StreamClient(apiKey, secret);
Expand Down Expand Up @@ -165,4 +165,15 @@ describe("call types CRUD API", () => {
client.video.getCallType({ name: callTypeName })
).rejects.toThrowError();
});

afterAll(async () => {
const callTypes = (await client.video.listCallTypes()).call_types;
const customCallTypes = Object.keys(callTypes).filter(
(t) => t.startsWith("streamnodetest") || t.startsWith("calltype")
);

await Promise.all(
customCallTypes.map((t) => client.video.deleteCallType({ name: t }))
);
});
});
36 changes: 27 additions & 9 deletions __tests__/channel-types.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import "dotenv/config";
import { beforeAll, describe, expect, it } from "vitest";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { v4 as uuidv4 } from "uuid";
import { CreateChannelTypeRequestAutomodEnum, StreamClient } from "../";


const apiKey = process.env.STREAM_API_KEY!;
const secret = process.env.STREAM_SECRET!;

describe("channel types CRUD API", () => {
let client: StreamClient;
const channelType = uuidv4();
const channelType = "streamnodetest" + uuidv4();

beforeAll(() => {
client = new StreamClient(apiKey, secret);
});

it("create", async () => {
const response = await client.chat.createChannelType({name: channelType, automod: CreateChannelTypeRequestAutomodEnum.DISABLED});
const response = await client.chat.createChannelType({
name: channelType,
automod: CreateChannelTypeRequestAutomodEnum.DISABLED,
});

expect(response.name).toBe(channelType);
});
Expand All @@ -28,19 +30,35 @@ describe("channel types CRUD API", () => {
});

it("update", async () => {
const response = await client.chat.updateChannelType(channelType, {automod: CreateChannelTypeRequestAutomodEnum.SIMPLE});
const response = await client.chat.updateChannelType(channelType, {
automod: CreateChannelTypeRequestAutomodEnum.SIMPLE,
});

expect(response.automod).toBe(CreateChannelTypeRequestAutomodEnum.SIMPLE);

const getResponse = await client.chat.getChannelType({name: channelType});
const getResponse = await client.chat.getChannelType({ name: channelType });

//@ts-expect-error typing problem
expect(getResponse.automod).toBe(CreateChannelTypeRequestAutomodEnum.SIMPLE);
expect(getResponse.automod).toBe(
CreateChannelTypeRequestAutomodEnum.SIMPLE
);
});

it("delete", async () => {
const response = await client.chat.deleteChannelType({name: channelType});

const response = await client.chat.deleteChannelType({ name: channelType });
expect(response).toBeDefined();
});

afterAll(async () => {
const channelTypes = (await client.chat.listChannelTypes()).channel_types;
const customChannelTypes = Object.keys(channelTypes).filter((type) =>
type.startsWith("streamnodetest")
);

await Promise.all(
customChannelTypes.map((t) =>
client.chat.deleteChannelType({ name: channelType })
)
);
});
});
92 changes: 67 additions & 25 deletions __tests__/channel.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "dotenv/config";
import { beforeAll, describe, expect, it } from "vitest";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { v4 as uuidv4 } from "uuid";
import { StreamClient, StreamChannel } from "../";

Expand All @@ -8,7 +8,7 @@ const secret = process.env.STREAM_SECRET!;

describe("channel API", () => {
let client: StreamClient;
const channelId = uuidv4();
const channelId = "streamnodetest" + uuidv4();
let channel: StreamChannel;
const user = {
id: "stream-node-test-user",
Expand All @@ -19,23 +19,25 @@ describe("channel API", () => {
id: "stream-node-test-user2",
name: "Stream Node Test User 2",
role: "admin",
}
};

beforeAll(async () => {
client = new StreamClient(apiKey, secret);

await client.upsertUsers({
users: {
[user.id]: { ...user },
[user2.id]: {...user2 }
},
users: {
[user.id]: { ...user },
[user2.id]: { ...user2 },
},
});

channel = client.chat.channel('messaging', channelId);
channel = client.chat.channel("messaging", channelId);
});

it("create", async () => {
const response = await channel.getOrCreate({data: {created_by_id: user.id}});
const response = await channel.getOrCreate({
data: { created_by_id: user.id, name: channelId },
});

expect(response.channel?.cid).toBe(`${channel.type}:${channel.id}`);
});
Expand All @@ -50,66 +52,106 @@ describe("channel API", () => {
// });

it("update", async () => {
const response = await channel.update({add_members: [{user_id: user.id}, {user_id: user2.id}], add_moderators: [], demote_moderators: [], remove_members: []});
const response = await channel.update({
add_members: [{ user_id: user.id }, { user_id: user2.id }],
add_moderators: [],
demote_moderators: [],
remove_members: [],
});

expect(response.members.length).toBe(2);
});

it("update partial", async () => {
const response = await channel.updatePartial({set: {cooldown: 100}, unset: []});
const response = await channel.updatePartial({
set: { cooldown: 100 },
unset: [],
});

expect(response.channel?.cooldown).toBe(100);
});

it("query members", async () => {
const response = await channel.queryMembers({filter_conditions: {
name: {$autocomplete: '2'}
}});
const response = await channel.queryMembers({
filter_conditions: {
name: { $autocomplete: "2" },
},
});

expect(response.members.length).toBe(1);
});

it("show and hide", async () => {
const hideResponse = await channel.hide({user_id: user2.id});
const hideResponse = await channel.hide({ user_id: user2.id });

expect(hideResponse).toBeDefined();

const queryResponse = await client.chat.queryChannels({filter_conditions: {hidden: true}, sort: [], user_id: user2.id});
const queryResponse = await client.chat.queryChannels({
filter_conditions: { hidden: true },
sort: [],
user_id: user2.id,
});

expect(queryResponse.channels.find(c => c.channel?.id === channel.id)).toBeDefined();
expect(
queryResponse.channels.find((c) => c.channel?.id === channel.id)
).toBeDefined();

const showResponse = await channel.show({user_id: user2.id});
const showResponse = await channel.show({ user_id: user2.id });

expect(showResponse).toBeDefined();
});

it("mute and unmute", async () => {
const muteResponse = await channel.mute({user_id: user2.id});
const muteResponse = await channel.mute({ user_id: user2.id });

expect(muteResponse.channel_mute?.channel?.id).toBe(channel.id);

await channel.unmute({user_id: user2.id});
await channel.unmute({ user_id: user2.id });

const queryResponse = await client.chat.queryChannels({filter_conditions: {muted: true}, sort: [], user_id: user2.id});
const queryResponse = await client.chat.queryChannels({
filter_conditions: { muted: true },
sort: [],
user_id: user2.id,
});

expect(queryResponse.channels.length).toBe(0);
});

it("export", async () => {
const response = await client.chat.exportChannels({channels: [{cid: channel.cid}]});
const statusResponse = await client.chat.getExportStatus({id: response.task_id});
const response = await client.chat.exportChannels({
channels: [{ cid: channel.cid }],
});
const statusResponse = await client.chat.getExportStatus({
id: response.task_id,
});

expect(statusResponse).toBeDefined();
});

it("custom event", async () => {
const response = await channel.sendCustomEvent({type: 'my-event', user_id: user.id});
const response = await channel.sendCustomEvent({
type: "my-event",
user_id: user.id,
});

expect(response.event?.type).toBe('my-event');
expect(response.event?.type).toBe("my-event");
});

it("delete", async () => {
await channel.delete();
});

afterAll(async () => {
const channels = (
await client.chat.queryChannels({
filter_conditions: { name: { $autocomplete: "streamnodetest" } },
})
).channels;

await Promise.all(
channels.map((c) =>
client.chat.channel(c.channel!.type, c.channel!.id).delete()
)
);
});
});
36 changes: 25 additions & 11 deletions __tests__/command.test.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,62 @@
import "dotenv/config";
import { beforeAll, describe, expect, it } from "vitest";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { v4 as uuidv4 } from "uuid";
import { CreateChannelTypeRequestAutomodEnum, StreamClient } from "../";

import { StreamClient } from "../";

const apiKey = process.env.STREAM_API_KEY!;
const secret = process.env.STREAM_SECRET!;

describe("commands CRUD API", () => {
let client: StreamClient;
const commandName = 'stream-node-test-command';
const commandName = "stream-node-test-command" + uuidv4();

beforeAll(() => {
client = new StreamClient(apiKey, secret);
});

it("create", async () => {
const response = await client.chat.createCommand({name: commandName, description: 'This is a test command', args: "[description]",
set: "test_commands_set",});
const response = await client.chat.createCommand({
name: commandName,
description: "This is a test command",
args: "[description]",
set: "test_commands_set",
});

expect(response.command?.name).toBe(commandName);
});

it("read", async () => {
const response = await client.chat.listCommands();

expect(response.commands.find(c => c.name === commandName)).toBeDefined();
expect(response.commands.find((c) => c.name === commandName)).toBeDefined();
});

it("update", async () => {
const response = await client.chat.updateCommand(commandName, {
description: 'Updated descrpition'
description: "Updated descrpition",
});

expect(response.command?.description).toBe('Updated descrpition');
expect(response.command?.description).toBe("Updated descrpition");
});

it("delete", async () => {
const response = await client.chat.deleteCommand({name: commandName});
const response = await client.chat.deleteCommand({ name: commandName });

expect(response.name).toBe(commandName);

await expect(() =>
client.chat.getCommand({name: commandName})
client.chat.getCommand({ name: commandName })
).rejects.toThrowError();
});

afterAll(async () => {
const commands = (await client.chat.listCommands()).commands;
const customCommands = commands.filter((c) =>
c.name.startsWith("stream-node-test-command")
);

await Promise.all(
customCommands.map((c) => client.chat.deleteCommand({ name: c.name }))
);
});
});
Loading

0 comments on commit c8896ba

Please sign in to comment.