// 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("EventsClient", () => {
    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: "event.summary",
            email: "user26@email.com",
            intercom_user_id: "6762f22b1bb69f9f2193bc12",
            user_id: "3ecf64d0-9ed1-4e9f-88e1-da7d6e6782f3",
            events: [
                {
                    name: "placed-order",
                    first: "2014-01-16T23:12:21.000+00:00",
                    last: "2014-01-16T23:12:21.000+00:00 ",
                    count: 1,
                    description: "A user placed an order",
                },
            ],
        };
        server.mockEndpoint().get("/events").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.events.list({
            user_id: "user_id",
            intercom_user_id: "intercom_user_id",
            email: "email",
            type: "type",
            summary: true,
            per_page: 1,
        });
        expect(response).toEqual({
            type: "event.summary",
            email: "user26@email.com",
            intercom_user_id: "6762f22b1bb69f9f2193bc12",
            user_id: "3ecf64d0-9ed1-4e9f-88e1-da7d6e6782f3",
            events: [
                {
                    name: "placed-order",
                    first: "2014-01-16T23:12:21.000+00:00",
                    last: "2014-01-16T23:12:21.000+00:00 ",
                    count: 1,
                    description: "A user placed an order",
                },
            ],
        });
    });

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

        await expect(async () => {
            return await client.events.list({
                type: "type",
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    test("create (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = {
            id: "8a88a590-e1c3-41e2-a502-e0649dbf721c",
            event_name: "invited-friend",
            created_at: 1671028894,
        };

        server.mockEndpoint().post("/events").jsonBody(rawRequestBody).respondWith().statusCode(200).build();

        const response = await client.events.create({
            id: "8a88a590-e1c3-41e2-a502-e0649dbf721c",
            event_name: "invited-friend",
            created_at: 1671028894,
        });
        expect(response).toEqual(undefined);
    });

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

        await expect(async () => {
            return await client.events.create({
                id: "id",
                event_name: "event_name",
                created_at: 1,
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

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

        server.mockEndpoint().post("/events/summaries").jsonBody(rawRequestBody).respondWith().statusCode(200).build();

        const response = await client.events.summaries();
        expect(response).toEqual(undefined);
    });

    test("summaries (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()
            .post("/events/summaries")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

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