// 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("CallsClient", () => {
    test("listCalls (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: "call",
                    id: "123",
                    conversation_id: "456",
                    admin_id: "789",
                    contact_id: "6762f0dd1bb69f9f2193bb83",
                    state: "completed",
                    initiated_at: "2024-01-15T09:30:00Z",
                    answered_at: "2024-01-15T09:30:00Z",
                    ended_at: "2024-01-15T09:30:00Z",
                    created_at: "2024-01-15T09:30:00Z",
                    updated_at: "2024-01-15T09:30:00Z",
                    recording_url: "https://api.intercom.io/calls/123/recording",
                    transcription_url: "https://api.intercom.io/calls/123/transcript",
                    call_type: "phone",
                    direction: "outbound",
                    ended_reason: "completed",
                    phone: "+15551234567",
                    fin_recording_url: "fin_recording_url",
                    fin_transcription_url: "fin_transcription_url",
                },
            ],
            total_count: 0,
            pages: {
                type: "pages",
                page: 1,
                next: { per_page: 2, starting_after: "your-cursor-from-response" },
                per_page: 25,
                total_pages: 0,
            },
        };
        server.mockEndpoint().get("/calls").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.calls.listCalls({
            page: 1,
            per_page: 1,
        });
        expect(response).toEqual({
            type: "list",
            data: [
                {
                    type: "call",
                    id: "123",
                    conversation_id: "456",
                    admin_id: "789",
                    contact_id: "6762f0dd1bb69f9f2193bb83",
                    state: "completed",
                    initiated_at: "2024-01-15T09:30:00Z",
                    answered_at: "2024-01-15T09:30:00Z",
                    ended_at: "2024-01-15T09:30:00Z",
                    created_at: "2024-01-15T09:30:00Z",
                    updated_at: "2024-01-15T09:30:00Z",
                    recording_url: "https://api.intercom.io/calls/123/recording",
                    transcription_url: "https://api.intercom.io/calls/123/transcript",
                    call_type: "phone",
                    direction: "outbound",
                    ended_reason: "completed",
                    phone: "+15551234567",
                    fin_recording_url: "fin_recording_url",
                    fin_transcription_url: "fin_transcription_url",
                },
            ],
            total_count: 0,
            pages: {
                type: "pages",
                page: 1,
                next: {
                    per_page: 2,
                    starting_after: "your-cursor-from-response",
                },
                per_page: 25,
                total_pages: 0,
            },
        });
    });

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

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

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

        const rawResponseBody = {
            type: "call",
            id: "123",
            conversation_id: "456",
            admin_id: "789",
            contact_id: "6762f0dd1bb69f9f2193bb83",
            state: "completed",
            initiated_at: 1734537437,
            answered_at: 1734537440,
            ended_at: 1734537530,
            created_at: 1734537437,
            updated_at: 1734537531,
            recording_url: "https://api.intercom.io/calls/123/recording",
            transcription_url: "https://api.intercom.io/calls/123/transcript",
            call_type: "phone",
            direction: "outbound",
            ended_reason: "answered",
            phone: "+15551234567",
            fin_recording_url: "https://api.intercom.io/calls/124/recording",
            fin_transcription_url: "https://api.intercom.io/calls/124/transcript",
        };
        server.mockEndpoint().get("/calls/call_id").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.calls.showCall({
            call_id: "call_id",
        });
        expect(response).toEqual({
            type: "call",
            id: "123",
            conversation_id: "456",
            admin_id: "789",
            contact_id: "6762f0dd1bb69f9f2193bb83",
            state: "completed",
            initiated_at: 1734537437,
            answered_at: 1734537440,
            ended_at: 1734537530,
            created_at: 1734537437,
            updated_at: 1734537531,
            recording_url: "https://api.intercom.io/calls/123/recording",
            transcription_url: "https://api.intercom.io/calls/123/transcript",
            call_type: "phone",
            direction: "outbound",
            ended_reason: "answered",
            phone: "+15551234567",
            fin_recording_url: "https://api.intercom.io/calls/124/recording",
            fin_transcription_url: "https://api.intercom.io/calls/124/transcript",
        });
    });

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

        await expect(async () => {
            return await client.calls.showCall({
                call_id: "call_id",
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    test("showCall (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("/calls/call_id").respondWith().statusCode(404).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.calls.showCall({
                call_id: "call_id",
            });
        }).rejects.toThrow(Intercom.NotFoundError);
    });

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

        server.mockEndpoint().get("/calls/call_id/recording").respondWith().statusCode(200).build();

        const response = await client.calls.showCallRecording({
            call_id: "call_id",
        });
        expect(response).toEqual(undefined);
    });

    test("showCallRecording (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("/calls/call_id/recording")
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.calls.showCallRecording({
                call_id: "call_id",
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    test("showCallRecording (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("/calls/call_id/recording")
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.calls.showCallRecording({
                call_id: "call_id",
            });
        }).rejects.toThrow(Intercom.NotFoundError);
    });

    test("listCallsWithTranscripts (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { conversation_ids: ["64619700005694", "64619700005695"] };
        const rawResponseBody = {
            type: "list",
            data: [
                {
                    type: "call",
                    id: "123",
                    conversation_id: "64619700005694",
                    admin_id: "789",
                    contact_id: "6762f0dd1bb69f9f2193bb83",
                    state: "completed",
                    initiated_at: "2024-01-15T09:30:00Z",
                    answered_at: "2024-01-15T09:30:00Z",
                    ended_at: "2024-01-15T09:30:00Z",
                    created_at: "2024-01-15T09:30:00Z",
                    updated_at: "2024-01-15T09:30:00Z",
                    recording_url: "https://api.intercom.io/calls/123/recording",
                    transcription_url: "https://api.intercom.io/calls/123/transcript",
                    call_type: "phone",
                    direction: "outbound",
                    ended_reason: "completed",
                    phone: "+15551234567",
                    fin_recording_url: "fin_recording_url",
                    fin_transcription_url: "fin_transcription_url",
                    transcript: [{ key: "value" }],
                    transcript_status: "completed",
                },
            ],
        };
        server
            .mockEndpoint()
            .post("/calls/search")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.calls.listCallsWithTranscripts({
            conversation_ids: ["64619700005694", "64619700005695"],
        });
        expect(response).toEqual({
            type: "list",
            data: [
                {
                    type: "call",
                    id: "123",
                    conversation_id: "64619700005694",
                    admin_id: "789",
                    contact_id: "6762f0dd1bb69f9f2193bb83",
                    state: "completed",
                    initiated_at: "2024-01-15T09:30:00Z",
                    answered_at: "2024-01-15T09:30:00Z",
                    ended_at: "2024-01-15T09:30:00Z",
                    created_at: "2024-01-15T09:30:00Z",
                    updated_at: "2024-01-15T09:30:00Z",
                    recording_url: "https://api.intercom.io/calls/123/recording",
                    transcription_url: "https://api.intercom.io/calls/123/transcript",
                    call_type: "phone",
                    direction: "outbound",
                    ended_reason: "completed",
                    phone: "+15551234567",
                    fin_recording_url: "fin_recording_url",
                    fin_transcription_url: "fin_transcription_url",
                    transcript: [
                        {
                            key: "value",
                        },
                    ],
                    transcript_status: "completed",
                },
            ],
        });
    });

    test("listCallsWithTranscripts (2)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { conversation_ids: ["conversation_ids", "conversation_ids"] };
        const rawResponseBody = { key: "value" };
        server
            .mockEndpoint()
            .post("/calls/search")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(400)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.calls.listCallsWithTranscripts({
                conversation_ids: ["conversation_ids", "conversation_ids"],
            });
        }).rejects.toThrow(Intercom.BadRequestError);
    });

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

        await expect(async () => {
            return await client.calls.listCallsWithTranscripts({
                conversation_ids: ["conversation_ids", "conversation_ids"],
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });
});
