// 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("EmailsClient", () => {
    test("listEmails (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: [
                {
                    type: "email_setting",
                    id: "1",
                    email: "support@company.com",
                    verified: true,
                    domain: "company.com",
                    brand_id: "9",
                    forwarding_enabled: true,
                    forwarded_email_last_received_at: 1710498600,
                    created_at: 1692530400,
                    updated_at: 1710498600,
                },
                {
                    type: "email_setting",
                    id: "2",
                    email: "hello@company.com",
                    verified: true,
                    domain: "company.com",
                    brand_id: "10",
                    forwarding_enabled: false,
                    forwarded_email_last_received_at: 1710498600,
                    created_at: 1683729000,
                    updated_at: 1701424500,
                },
            ],
        };
        server.mockEndpoint().get("/emails").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.unstable.emails.listEmails();
        expect(response).toEqual({
            type: "list",
            data: [
                {
                    type: "email_setting",
                    id: "1",
                    email: "support@company.com",
                    verified: true,
                    domain: "company.com",
                    brand_id: "9",
                    forwarding_enabled: true,
                    forwarded_email_last_received_at: 1710498600,
                    created_at: 1692530400,
                    updated_at: 1710498600,
                },
                {
                    type: "email_setting",
                    id: "2",
                    email: "hello@company.com",
                    verified: true,
                    domain: "company.com",
                    brand_id: "10",
                    forwarding_enabled: false,
                    forwarded_email_last_received_at: 1710498600,
                    created_at: 1683729000,
                    updated_at: 1701424500,
                },
            ],
        });
    });

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

        const rawResponseBody = { type: "type", errors: [{ code: "code" }, { code: "code" }] };
        server.mockEndpoint().get("/emails").respondWith().statusCode(401).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.unstable.emails.listEmails();
        }).rejects.toThrow(Intercom.unstable.UnauthorizedError);
    });

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

        const rawResponseBody = {
            type: "email_setting",
            id: "10",
            email: "support@company.com",
            verified: true,
            domain: "company.com",
            brand_id: "15",
            forwarding_enabled: true,
            forwarded_email_last_received_at: 1710498600,
            created_at: 1692530400,
            updated_at: 1710498600,
        };
        server.mockEndpoint().get("/emails/id").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.unstable.emails.retrieveEmail({
            id: "id",
        });
        expect(response).toEqual({
            type: "email_setting",
            id: "10",
            email: "support@company.com",
            verified: true,
            domain: "company.com",
            brand_id: "15",
            forwarding_enabled: true,
            forwarded_email_last_received_at: 1710498600,
            created_at: 1692530400,
            updated_at: 1710498600,
        });
    });

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

        const rawResponseBody = { type: "type", errors: [{ code: "code" }, { code: "code" }] };
        server.mockEndpoint().get("/emails/id").respondWith().statusCode(401).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.unstable.emails.retrieveEmail({
                id: "id",
            });
        }).rejects.toThrow(Intercom.unstable.UnauthorizedError);
    });

    test("retrieveEmail (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("/emails/id").respondWith().statusCode(404).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.unstable.emails.retrieveEmail({
                id: "id",
            });
        }).rejects.toThrow(Intercom.unstable.NotFoundError);
    });
});
