From 66bac0ad82292d0397ee63efddb460f45ffb7b5a Mon Sep 17 00:00:00 2001 From: Zita Szupera Date: Tue, 7 Nov 2023 10:17:45 +0100 Subject: [PATCH] restore timeout to be 3000ms, use 7500ms timeout for tests --- __tests__/block-lists.test.ts | 7 +- __tests__/call-members.test.ts | 18 ++- __tests__/call-types.test.ts | 7 +- __tests__/call.test.ts | 15 ++- __tests__/channel-types.test.ts | 32 +++--- __tests__/channel.test.ts | 7 +- __tests__/command.test.ts | 7 +- __tests__/create-test-client.ts | 9 ++ __tests__/create-token.test.ts | 4 +- __tests__/devices-push.test.ts | 124 +++++++++++---------- __tests__/messages.test.ts | 7 +- __tests__/permissions-app-settings.test.ts | 7 +- __tests__/user-compat.test.ts | 27 +++-- __tests__/users.test.ts | 7 +- src/StreamClient.ts | 2 +- 15 files changed, 142 insertions(+), 138 deletions(-) create mode 100644 __tests__/create-test-client.ts diff --git a/__tests__/block-lists.test.ts b/__tests__/block-lists.test.ts index 4412fd8..378f671 100644 --- a/__tests__/block-lists.test.ts +++ b/__tests__/block-lists.test.ts @@ -1,17 +1,14 @@ -import "dotenv/config"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { BlockList, StreamClient } from "../"; import { v4 as uuidv4 } from "uuid"; - -const apiKey = process.env.STREAM_API_KEY!; -const secret = process.env.STREAM_SECRET!; +import { createTestClient } from "./create-test-client"; describe("block lists CRUD API", () => { let client: StreamClient; let blockList: BlockList; beforeAll(() => { - client = new StreamClient(apiKey, secret); + client = createTestClient(); blockList = { name: "streamnodetest-F1" + uuidv4(), words: ["Ricciardo should retire"], diff --git a/__tests__/call-members.test.ts b/__tests__/call-members.test.ts index f87bbb9..aabfdbe 100644 --- a/__tests__/call-members.test.ts +++ b/__tests__/call-members.test.ts @@ -1,20 +1,26 @@ -import "dotenv/config"; import { beforeAll, describe, expect, it } from "vitest"; import { v4 as uuidv4 } from "uuid"; import { StreamCall, StreamClient, VideoOwnCapability } from "../"; - -const apiKey = process.env.STREAM_API_KEY!; -const secret = process.env.STREAM_SECRET!; +import { createTestClient } from "./create-test-client"; describe("call members API", () => { let client: StreamClient; const callId = `call${uuidv4()}`; let call: StreamCall; - beforeAll(() => { - client = new StreamClient(apiKey, secret); + beforeAll(async () => { + client = createTestClient(); call = client.video.call("default", callId); + + await client.upsertUsers({ + users: { + john: { name: "John", id: "john" }, + jack: { name: "Jack", id: "jack" }, + jane: { name: "Jane", id: "jane" }, + sara: { name: "Sara", id: "sara" }, + }, + }); }); it("create with members", async () => { diff --git a/__tests__/call-types.test.ts b/__tests__/call-types.test.ts index ba7a87c..bc5bd5a 100644 --- a/__tests__/call-types.test.ts +++ b/__tests__/call-types.test.ts @@ -1,4 +1,3 @@ -import "dotenv/config"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { v4 as uuidv4 } from "uuid"; import { @@ -8,16 +7,14 @@ import { VideoRecordSettingsRequestModeEnum, VideoRecordSettingsRequestQualityEnum, } from "../"; - -const apiKey = process.env.STREAM_API_KEY!; -const secret = process.env.STREAM_SECRET!; +import { createTestClient } from "./create-test-client"; describe("call types CRUD API", () => { let client: StreamClient; const callTypeName = `streamnodetest${uuidv4()}`; beforeAll(() => { - client = new StreamClient(apiKey, secret); + client = createTestClient(); }); it("create", async () => { diff --git a/__tests__/call.test.ts b/__tests__/call.test.ts index ad69ea8..b1979da 100644 --- a/__tests__/call.test.ts +++ b/__tests__/call.test.ts @@ -1,4 +1,3 @@ -import "dotenv/config"; import { beforeAll, describe, expect, it } from "vitest"; import { v4 as uuidv4 } from "uuid"; import { @@ -7,19 +6,23 @@ import { VideoRecordSettingsRequestModeEnum, VideoRecordSettingsRequestQualityEnum, } from "../"; - -const apiKey = process.env.STREAM_API_KEY!; -const secret = process.env.STREAM_SECRET!; +import { createTestClient } from "./create-test-client"; describe("call API", () => { let client: StreamClient; const callId = `call${uuidv4()}`; let call: StreamCall; - beforeAll(() => { - client = new StreamClient(apiKey, secret); + beforeAll(async () => { + client = createTestClient(); call = client.video.call("default", callId); + + await client.upsertUsers({ + users: { + john: { name: "John", id: "john" }, + }, + }); }); it("create", async () => { diff --git a/__tests__/channel-types.test.ts b/__tests__/channel-types.test.ts index 4da15e2..9d193c0 100644 --- a/__tests__/channel-types.test.ts +++ b/__tests__/channel-types.test.ts @@ -1,17 +1,14 @@ -import "dotenv/config"; 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!; +import { createTestClient } from "./create-test-client"; describe("channel types CRUD API", () => { let client: StreamClient; const channelType = "streamnodetest" + uuidv4(); beforeAll(() => { - client = new StreamClient(apiKey, secret); + client = createTestClient(); }); it("create", async () => { @@ -49,16 +46,19 @@ describe("channel types CRUD API", () => { expect(response).toBeDefined(); }); - afterAll(async () => { - const channelTypes = (await client.chat.listChannelTypes()).channel_types; - const customChannelTypes = Object.keys(channelTypes).filter((type) => - type.startsWith("streamnodetest") - ); + // TODO: why does this doesn't work? + // afterAll(async () => { + // await new Promise((resolve) => setTimeout(resolve, 5000)); - await Promise.all( - customChannelTypes.map((t) => - client.chat.deleteChannelType({ name: channelType }) - ) - ); - }); + // 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 }) + // ) + // ); + // }); }); diff --git a/__tests__/channel.test.ts b/__tests__/channel.test.ts index 660e68c..e235447 100644 --- a/__tests__/channel.test.ts +++ b/__tests__/channel.test.ts @@ -1,10 +1,7 @@ -import "dotenv/config"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { v4 as uuidv4 } from "uuid"; import { StreamClient, StreamChannel } from "../"; - -const apiKey = process.env.STREAM_API_KEY!; -const secret = process.env.STREAM_SECRET!; +import { createTestClient } from "./create-test-client"; describe("channel API", () => { let client: StreamClient; @@ -22,7 +19,7 @@ describe("channel API", () => { }; beforeAll(async () => { - client = new StreamClient(apiKey, secret); + client = createTestClient(); await client.upsertUsers({ users: { diff --git a/__tests__/command.test.ts b/__tests__/command.test.ts index ac31d63..88f611b 100644 --- a/__tests__/command.test.ts +++ b/__tests__/command.test.ts @@ -1,17 +1,14 @@ -import "dotenv/config"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { v4 as uuidv4 } from "uuid"; import { StreamClient } from "../"; - -const apiKey = process.env.STREAM_API_KEY!; -const secret = process.env.STREAM_SECRET!; +import { createTestClient } from "./create-test-client"; describe("commands CRUD API", () => { let client: StreamClient; const commandName = "stream-node-test-command" + uuidv4(); beforeAll(() => { - client = new StreamClient(apiKey, secret); + client = createTestClient(); }); it("create", async () => { diff --git a/__tests__/create-test-client.ts b/__tests__/create-test-client.ts new file mode 100644 index 0000000..0705084 --- /dev/null +++ b/__tests__/create-test-client.ts @@ -0,0 +1,9 @@ +import "dotenv/config"; +import { StreamClient } from "../src/StreamClient"; + +const apiKey = process.env.STREAM_API_KEY!; +const secret = process.env.STREAM_SECRET!; + +export const createTestClient = () => { + return new StreamClient(apiKey, secret, undefined, { timeout: 7500 }); +}; diff --git a/__tests__/create-token.test.ts b/__tests__/create-token.test.ts index df2a143..69adb09 100644 --- a/__tests__/create-token.test.ts +++ b/__tests__/create-token.test.ts @@ -2,8 +2,8 @@ import "dotenv/config"; import jwt from "jsonwebtoken"; import { beforeAll, describe, expect, it } from "vitest"; import { StreamClient } from "../src/StreamClient"; +import { createTestClient } from "./create-test-client"; -const apiKey = process.env.STREAM_API_KEY!; const secret = process.env.STREAM_SECRET!; describe("creating tokens", () => { @@ -11,7 +11,7 @@ describe("creating tokens", () => { const userId = "john"; beforeAll(() => { - client = new StreamClient(apiKey, secret); + client = createTestClient(); }); it("with default expiration", () => { diff --git a/__tests__/devices-push.test.ts b/__tests__/devices-push.test.ts index 0037f59..186c310 100644 --- a/__tests__/devices-push.test.ts +++ b/__tests__/devices-push.test.ts @@ -1,75 +1,83 @@ -import "dotenv/config"; import { beforeAll, describe, expect, it } from "vitest"; -import { CreateDeviceRequest, CreateDeviceRequestPushProviderEnum, StreamClient, UpsertPushProviderRequest, UserObjectRequest } from ".."; +import { + CreateDeviceRequest, + CreateDeviceRequestPushProviderEnum, + StreamClient, +} from ".."; import { v4 as uuidv4 } from "uuid"; import { PushProviderRequest } from "../src/gen/chat"; +import { createTestClient } from "./create-test-client"; -const apiKey = process.env.STREAM_API_KEY!; -const secret = process.env.STREAM_SECRET!; +describe("devices and push", () => { + let client: StreamClient; + const user = { + id: "stream-node-test-user", + role: "admin", + }; + const device: CreateDeviceRequest = { + id: uuidv4(), + push_provider: CreateDeviceRequestPushProviderEnum.FIREBASE, + push_provider_name: "firebase", + user_id: user.id, + }; + const pushProvider: PushProviderRequest = { + name: "test-push-provider", + type: "xiaomi" as any as number, + xiaomi_app_secret: "", + xiaomi_package_name: "", + }; -describe('devices and push', () => { - let client: StreamClient; - const user = { - id: "stream-node-test-user", - role: "admin", - }; - const device: CreateDeviceRequest = { - id: uuidv4(), - push_provider: CreateDeviceRequestPushProviderEnum.FIREBASE, - push_provider_name: 'firebase', - user_id: user.id - } - const pushProvider: PushProviderRequest = { - name: 'test-push-provider', - type: 'xiaomi' as any as number, - xiaomi_app_secret: '', - xiaomi_package_name: '' - } - - beforeAll(async () => { - client = new StreamClient(apiKey, secret); - await client.upsertUsers({ - users: { - [user.id]: { ...user }, - }, - }); + beforeAll(async () => { + client = createTestClient(); + await client.upsertUsers({ + users: { + [user.id]: { ...user }, + }, }); + }); - it('create device', async () => { - expect(async () => await client.createDevice(device)).not.toThrowError(); - }); + it("create device", async () => { + expect(async () => await client.createDevice(device)).not.toThrowError(); + }); - it('list devices', async () => { - const response = await client.listDevices({userId: user.id}); + it("list devices", async () => { + const response = await client.listDevices({ userId: user.id }); - expect(response.devices.find(d => d.id === device.id)).toBeDefined(); - }) + expect(response.devices.find((d) => d.id === device.id)).toBeDefined(); + }); - it('delete device', async () => { - const response = await client.deleteDevice({id: device.id, userId: user.id}); + it("delete device", async () => { + const response = await client.deleteDevice({ + id: device.id, + userId: user.id, + }); - expect(response).toBeDefined(); - }) + expect(response).toBeDefined(); + }); - it('create push provider', async () => { - // Can't properly test upsert without valid credentials - await expect(() => client.upsertPushProvider(pushProvider)).rejects.toThrowError('Stream error code 4: UpsertPushProvider failed with error: "xiaomi credentials are invalid"'); - }); + it("create push provider", async () => { + // Can't properly test upsert without valid credentials + await expect(() => + client.upsertPushProvider(pushProvider) + ).rejects.toThrowError( + 'Stream error code 4: UpsertPushProvider failed with error: "xiaomi credentials are invalid"' + ); + }); - it('list push provider', async () => { - const response = await client.listPushProviders(); + it("list push provider", async () => { + const response = await client.listPushProviders(); - expect(response.push_providers).toBeDefined(); - }); + expect(response.push_providers).toBeDefined(); + }); - it('test push provider', async () => { - const response = await client.checkPush({user_id: user.id}); + it("test push provider", async () => { + const response = await client.checkPush({ user_id: user.id }); - expect(response).toBeDefined(); - }) + expect(response).toBeDefined(); + }); - // TODO: can't test delete because we can't upsert - // it('delete push provider', async () => { - // const response = await client.deletePushProvider({name: pushProvider.push_provider!.name, type: DeletePushProviderTypeEnum.FIREBASE}); - // }); -}); \ No newline at end of file + // TODO: can't test delete because we can't upsert + // it('delete push provider', async () => { + // const response = await client.deletePushProvider({name: pushProvider.push_provider!.name, type: DeletePushProviderTypeEnum.FIREBASE}); + // }); +}); diff --git a/__tests__/messages.test.ts b/__tests__/messages.test.ts index ab03769..8daf85b 100644 --- a/__tests__/messages.test.ts +++ b/__tests__/messages.test.ts @@ -1,14 +1,11 @@ import "dotenv/config"; import { beforeAll, describe, expect, it } from "vitest"; -import { v4 as uuidv4 } from "uuid"; import { StreamClient, StreamChannel, TranslateMessageRequestLanguageEnum, } from "../"; - -const apiKey = process.env.STREAM_API_KEY!; -const secret = process.env.STREAM_SECRET!; +import { createTestClient } from "./create-test-client"; describe("messages API", () => { let client: StreamClient; @@ -27,7 +24,7 @@ describe("messages API", () => { let messageId: string | undefined; beforeAll(async () => { - client = new StreamClient(apiKey, secret); + client = createTestClient(); await client.upsertUsers({ users: { diff --git a/__tests__/permissions-app-settings.test.ts b/__tests__/permissions-app-settings.test.ts index 2241077..b7933a2 100644 --- a/__tests__/permissions-app-settings.test.ts +++ b/__tests__/permissions-app-settings.test.ts @@ -1,10 +1,7 @@ -import "dotenv/config"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { CreateRoleRequest, StreamClient, VideoOwnCapability } from ".."; import { v4 as uuidv4 } from "uuid"; - -const apiKey = process.env.STREAM_API_KEY!; -const secret = process.env.STREAM_SECRET!; +import { createTestClient } from "./create-test-client"; describe("permissions and app settings API", () => { let client: StreamClient; @@ -14,7 +11,7 @@ describe("permissions and app settings API", () => { role = { name: "streamnodetest" + uuidv4(), }; - client = new StreamClient(apiKey, secret); + client = createTestClient(); }); it("list permissions", async () => { diff --git a/__tests__/user-compat.test.ts b/__tests__/user-compat.test.ts index 3ab93f7..e48e6bd 100644 --- a/__tests__/user-compat.test.ts +++ b/__tests__/user-compat.test.ts @@ -1,24 +1,21 @@ -import "dotenv/config"; import { beforeAll, describe, expect, it } from "vitest"; -import { StreamClient, UserObjectRequest } from "../"; +import { StreamClient } from "../"; import { v4 as uuidv4 } from "uuid"; - -const apiKey = process.env.STREAM_API_KEY!; -const secret = process.env.STREAM_SECRET!; +import { createTestClient } from "./create-test-client"; describe("user-video compatibility API", () => { let client: StreamClient; const user = { id: "stream-node-test-user", role: "admin", - name: 'Test User for user API compatibily', + name: "Test User for user API compatibily", custom: { - note: 'compatibilty test' - } + note: "compatibilty test", + }, }; beforeAll(async () => { - client = new StreamClient(apiKey, secret); + client = createTestClient(); await client.upsertUsers({ users: { [user.id]: { ...user }, @@ -26,11 +23,13 @@ describe("user-video compatibility API", () => { }); }); - it('create call', async () => { - const call = client.video.call('default', uuidv4()); - const response = await call.create({data: {created_by: user}}); + it("create call", async () => { + const call = client.video.call("default", uuidv4()); + const response = await call.create({ data: { created_by: user } }); - expect(response.call.created_by.custom.note).toBe('compatibilty test'); - expect(response.call.created_by.name).toBe('Test User for user API compatibily'); + expect(response.call.created_by.custom.note).toBe("compatibilty test"); + expect(response.call.created_by.name).toBe( + "Test User for user API compatibily" + ); }); }); diff --git a/__tests__/users.test.ts b/__tests__/users.test.ts index 88dc12a..5baa341 100644 --- a/__tests__/users.test.ts +++ b/__tests__/users.test.ts @@ -1,4 +1,3 @@ -import "dotenv/config"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { DeleteUsersRequestUserEnum, @@ -6,9 +5,7 @@ import { UserObjectRequest, } from "../"; import { v4 as uuidv4 } from "uuid"; - -const apiKey = process.env.STREAM_API_KEY!; -const secret = process.env.STREAM_SECRET!; +import { createTestClient } from "./create-test-client"; describe("user API", () => { let client: StreamClient; @@ -28,7 +25,7 @@ describe("user API", () => { }; beforeAll(async () => { - client = new StreamClient(apiKey, secret); + client = createTestClient(); await client.upsertUsers({ users: { [user.id]: { ...user }, diff --git a/src/StreamClient.ts b/src/StreamClient.ts index e4df696..4a77ec8 100644 --- a/src/StreamClient.ts +++ b/src/StreamClient.ts @@ -75,7 +75,7 @@ export class StreamClient { private readonly eventsApi: EventsApi; private readonly tasksApi: TasksApi; private token: string; - private static readonly defaultTimeout = 5000; + private static readonly defaultTimeout = 3000; constructor( private apiKey: string,