// 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("ExportClient", () => {
    test("enqueueANewReportingDataExportJob (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            dataset_id: "conversation",
            attribute_ids: ["conversation_id", "conversation_started_at"],
            start_time: 1717490000,
            end_time: 1717510000,
        };
        const rawResponseBody = {
            job_identifier: "job1",
            status: "pending",
            download_url: "download_url",
            download_expires_at: "download_expires_at",
        };
        server
            .mockEndpoint()
            .post("/export/reporting_data/enqueue")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.export.enqueueANewReportingDataExportJob({
            dataset_id: "conversation",
            attribute_ids: ["conversation_id", "conversation_started_at"],
            start_time: 1717490000,
            end_time: 1717510000,
        });
        expect(response).toEqual({
            job_identifier: "job1",
            status: "pending",
            download_url: "download_url",
            download_expires_at: "download_expires_at",
        });
    });

    test("enqueueANewReportingDataExportJob (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            dataset_id: "dataset_id",
            attribute_ids: ["attribute_ids", "attribute_ids"],
            start_time: 1000000,
            end_time: 1000000,
        };
        const rawResponseBody = { key: "value" };
        server
            .mockEndpoint()
            .post("/export/reporting_data/enqueue")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(400)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.export.enqueueANewReportingDataExportJob({
                dataset_id: "dataset_id",
                attribute_ids: ["attribute_ids", "attribute_ids"],
                start_time: 1000000,
                end_time: 1000000,
            });
        }).rejects.toThrow(Intercom.BadRequestError);
    });

    test("enqueueANewReportingDataExportJob (3)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            dataset_id: "dataset_id",
            attribute_ids: ["attribute_ids", "attribute_ids"],
            start_time: 1000000,
            end_time: 1000000,
        };
        const rawResponseBody = { type: "error.list", errors: [{ code: "code" }, { code: "code" }] };
        server
            .mockEndpoint()
            .post("/export/reporting_data/enqueue")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.export.enqueueANewReportingDataExportJob({
                dataset_id: "dataset_id",
                attribute_ids: ["attribute_ids", "attribute_ids"],
                start_time: 1000000,
                end_time: 1000000,
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    test("enqueueANewReportingDataExportJob (4)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            dataset_id: "dataset_id",
            attribute_ids: ["attribute_ids", "attribute_ids"],
            start_time: 1000000,
            end_time: 1000000,
        };
        const rawResponseBody = { type: "error.list", errors: [{ code: "code" }, { code: "code" }] };
        server
            .mockEndpoint()
            .post("/export/reporting_data/enqueue")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(429)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.export.enqueueANewReportingDataExportJob({
                dataset_id: "dataset_id",
                attribute_ids: ["attribute_ids", "attribute_ids"],
                start_time: 1000000,
                end_time: 1000000,
            });
        }).rejects.toThrow(Intercom.TooManyRequestsError);
    });

    test("listAvailableDatasetsAndAttributes", 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: "conversation",
                    name: "Conversation",
                    description: "Conversation-level details: status, channel, assignee.",
                    default_time_attribute_id: "conversation_started_at",
                    attributes: [{ id: "conversation_id", name: "Conversation ID" }],
                },
            ],
        };
        server
            .mockEndpoint()
            .get("/export/reporting_data/get_datasets")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.export.listAvailableDatasetsAndAttributes();
        expect(response).toEqual({
            type: "list",
            data: [
                {
                    id: "conversation",
                    name: "Conversation",
                    description: "Conversation-level details: status, channel, assignee.",
                    default_time_attribute_id: "conversation_started_at",
                    attributes: [
                        {
                            id: "conversation_id",
                            name: "Conversation ID",
                        },
                    ],
                },
            ],
        });
    });
});
