// This file was auto-generated by Fern from our API Definition.

import * as Intercom from "../../../src/api/index";
import { IntercomClient } from "../../../src/Client";
import { mockServerPool } from "../../mock-server/MockServerPool";

describe("TeamsClient", () => {
    test("listTeams (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = {
            type: "team.list",
            teams: [
                {
                    type: "team",
                    id: "814865",
                    name: "Example Team",
                    admin_ids: [493881],
                    admin_priority_level: { primary_admin_ids: [493881], secondary_admin_ids: [814865] },
                },
            ],
        };
        server.mockEndpoint().get("/teams").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.unstable.teams.listTeams();
        expect(response).toEqual({
            type: "team.list",
            teams: [
                {
                    type: "team",
                    id: "814865",
                    name: "Example Team",
                    admin_ids: [493881],
                    admin_priority_level: {
                        primary_admin_ids: [493881],
                        secondary_admin_ids: [814865],
                    },
                },
            ],
        });
    });

    test("listTeams (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = { type: "type", errors: [{ code: "code" }, { code: "code" }] };
        server.mockEndpoint().get("/teams").respondWith().statusCode(401).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.unstable.teams.listTeams();
        }).rejects.toThrow(Intercom.unstable.UnauthorizedError);
    });

    test("retrieveTeam (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = {
            type: "team",
            id: "991267902",
            name: "team 1",
            admin_ids: [493881],
            admin_priority_level: { primary_admin_ids: [493881], secondary_admin_ids: [814865] },
        };
        server.mockEndpoint().get("/teams/123").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.unstable.teams.retrieveTeam({
            id: "123",
        });
        expect(response).toEqual({
            type: "team",
            id: "991267902",
            name: "team 1",
            admin_ids: [493881],
            admin_priority_level: {
                primary_admin_ids: [493881],
                secondary_admin_ids: [814865],
            },
        });
    });

    test("retrieveTeam (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = { type: "type", errors: [{ code: "code" }, { code: "code" }] };
        server.mockEndpoint().get("/teams/id").respondWith().statusCode(401).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.unstable.teams.retrieveTeam({
                id: "id",
            });
        }).rejects.toThrow(Intercom.unstable.UnauthorizedError);
    });

    test("retrieveTeam (3)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = { key: "value" };
        server.mockEndpoint().get("/teams/id").respondWith().statusCode(404).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.unstable.teams.retrieveTeam({
                id: "id",
            });
        }).rejects.toThrow(Intercom.unstable.NotFoundError);
    });
});
