// 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("HelpCentersClient", () => {
    test("find (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = {
            id: "93",
            workspace_id: "this_is_an_id124_that_should_be_at_least_",
            created_at: 1734537325,
            updated_at: 1734537325,
            identifier: "help-center-1",
            website_turned_on: false,
            display_name: "Intercom Help Center",
            url: "https://help.mycompany.com",
            custom_domain: "help.mycompany.com",
        };
        server
            .mockEndpoint()
            .get("/help_center/help_centers/1")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.helpCenters.find({
            help_center_id: 1,
        });
        expect(response).toEqual({
            id: "93",
            workspace_id: "this_is_an_id124_that_should_be_at_least_",
            created_at: 1734537325,
            updated_at: 1734537325,
            identifier: "help-center-1",
            website_turned_on: false,
            display_name: "Intercom Help Center",
            url: "https://help.mycompany.com",
            custom_domain: "help.mycompany.com",
        });
    });

    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("/help_center/help_centers/1")
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.helpCenters.find({
                help_center_id: 1,
            });
        }).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("/help_center/help_centers/1")
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.helpCenters.find({
                help_center_id: 1,
            });
        }).rejects.toThrow(Intercom.NotFoundError);
    });

    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: "list",
            data: [
                {
                    id: "id",
                    workspace_id: "workspace_id",
                    created_at: 1,
                    updated_at: 1,
                    identifier: "identifier",
                    website_turned_on: true,
                    display_name: "display_name",
                    url: "url",
                    custom_domain: "custom_domain",
                },
                {
                    id: "id",
                    workspace_id: "workspace_id",
                    created_at: 1,
                    updated_at: 1,
                    identifier: "identifier",
                    website_turned_on: true,
                    display_name: "display_name",
                    url: "url",
                    custom_domain: "custom_domain",
                },
            ],
        };
        server
            .mockEndpoint({ once: false })
            .get("/help_center/help_centers")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const expected = {
            type: "list",
            data: [
                {
                    id: "id",
                    workspace_id: "workspace_id",
                    created_at: 1,
                    updated_at: 1,
                    identifier: "identifier",
                    website_turned_on: true,
                    display_name: "display_name",
                    url: "url",
                    custom_domain: "custom_domain",
                },
                {
                    id: "id",
                    workspace_id: "workspace_id",
                    created_at: 1,
                    updated_at: 1,
                    identifier: "identifier",
                    website_turned_on: true,
                    display_name: "display_name",
                    url: "url",
                    custom_domain: "custom_domain",
                },
            ],
        };
        const page = await client.helpCenters.list();

        expect(expected.data).toEqual(page.data);
        expect(page.hasNextPage()).toBe(true);
        const nextPage = await page.getNextPage();
        expect(expected.data).toEqual(nextPage.data);
    });

    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({ once: false })
            .get("/help_center/help_centers")
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

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