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

        const rawResponseBody = { type: "ip_allowlist", enabled: true, ip_allowlist: ["192.168.1.0/24", "10.0.0.1"] };
        server.mockEndpoint().get("/ip_allowlist").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.ipAllowlist.getIpAllowlist();
        expect(response).toEqual({
            type: "ip_allowlist",
            enabled: true,
            ip_allowlist: ["192.168.1.0/24", "10.0.0.1"],
        });
    });

    test("getIpAllowlist (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("/ip_allowlist").respondWith().statusCode(401).jsonBody(rawResponseBody).build();

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

    test("updateIpAllowlist (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { enabled: true, ip_allowlist: ["192.168.1.0/24", "10.0.0.1"] };
        const rawResponseBody = { type: "ip_allowlist", enabled: true, ip_allowlist: ["192.168.1.0/24", "10.0.0.1"] };
        server
            .mockEndpoint()
            .put("/ip_allowlist")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.ipAllowlist.updateIpAllowlist({
            enabled: true,
            ip_allowlist: ["192.168.1.0/24", "10.0.0.1"],
        });
        expect(response).toEqual({
            type: "ip_allowlist",
            enabled: true,
            ip_allowlist: ["192.168.1.0/24", "10.0.0.1"],
        });
    });

    test("updateIpAllowlist (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {};
        const rawResponseBody = { type: "error.list", errors: [{ code: "code" }, { code: "code" }] };
        server
            .mockEndpoint()
            .put("/ip_allowlist")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

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

    test("updateIpAllowlist (3)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {};
        const rawResponseBody = { key: "value" };
        server
            .mockEndpoint()
            .put("/ip_allowlist")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(422)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.ipAllowlist.updateIpAllowlist({});
        }).rejects.toThrow(Intercom.UnprocessableEntityError);
    });
});
