// 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("SegmentsClient", () => {
    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: "segment.list",
            segments: [
                {
                    type: "segment",
                    id: "6762f25c1bb69f9f2193bc22",
                    name: "John segment",
                    created_at: 1734537820,
                    updated_at: 1734537820,
                    person_type: "user",
                    count: 3,
                },
                {
                    type: "segment",
                    id: "6762f25c1bb69f9f2193bc23",
                    name: "Jane segment",
                    created_at: 1734537820,
                    updated_at: 1734537820,
                    person_type: "user",
                    count: 3,
                },
            ],
            pages: { key: "value" },
        };
        server.mockEndpoint().get("/segments").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.segments.list({
            include_count: true,
        });
        expect(response).toEqual({
            type: "segment.list",
            segments: [
                {
                    type: "segment",
                    id: "6762f25c1bb69f9f2193bc22",
                    name: "John segment",
                    created_at: 1734537820,
                    updated_at: 1734537820,
                    person_type: "user",
                    count: 3,
                },
                {
                    type: "segment",
                    id: "6762f25c1bb69f9f2193bc23",
                    name: "Jane segment",
                    created_at: 1734537820,
                    updated_at: 1734537820,
                    person_type: "user",
                    count: 3,
                },
            ],
            pages: {
                key: "value",
            },
        });
    });

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

        await expect(async () => {
            return await client.segments.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: "segment",
            id: "6762f25f1bb69f9f2193bc26",
            name: "John segment",
            created_at: 1734537823,
            updated_at: 1734537823,
            person_type: "user",
            count: 3,
        };
        server.mockEndpoint().get("/segments/123").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.segments.find({
            segment_id: "123",
        });
        expect(response).toEqual({
            type: "segment",
            id: "6762f25f1bb69f9f2193bc26",
            name: "John segment",
            created_at: 1734537823,
            updated_at: 1734537823,
            person_type: "user",
            count: 3,
        });
    });

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

        await expect(async () => {
            return await client.segments.find({
                segment_id: "segment_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("/segments/segment_id")
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

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