// 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("list (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] },
                    assignment_limit: 10,
                    distribution_method: "round_robin",
                },
            ],
        };
        server.mockEndpoint().get("/teams").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.teams.list();
        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],
                    },
                    assignment_limit: 10,
                    distribution_method: "round_robin",
                },
            ],
        });
    });

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

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

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

    test("find (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] },
            assignment_limit: 10,
            distribution_method: "round_robin",
        };
        server.mockEndpoint().get("/teams/123").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.teams.find({
            team_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],
            },
            assignment_limit: 10,
            distribution_method: "round_robin",
        });
    });

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

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

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

    test("find (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/team_id").respondWith().statusCode(404).jsonBody(rawResponseBody).build();

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