// 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.unstable.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: "type", errors: [{ code: "code" }, { code: "code" }] };
        server.mockEndpoint().get("/calls").respondWith().statusCode(401).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.unstable.calls.listCalls();
        }).rejects.toThrow(Intercom.unstable.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/id").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.unstable.calls.showCall({
            id: "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: "type", errors: [{ code: "code" }, { code: "code" }] };
        server.mockEndpoint().get("/calls/id").respondWith().statusCode(401).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.unstable.calls.showCall({
                id: "id",
            });
        }).rejects.toThrow(Intercom.unstable.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/id").respondWith().statusCode(404).jsonBody(rawResponseBody).build();

        await expect(async () => {
            return await client.unstable.calls.showCall({
                id: "id",
            });
        }).rejects.toThrow(Intercom.unstable.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/id/recording").respondWith().statusCode(200).build();

        const response = await client.unstable.calls.showCallRecording({
            id: "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: "type", errors: [{ code: "code" }, { code: "code" }] };
        server
            .mockEndpoint()
            .get("/calls/id/recording")
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.unstable.calls.showCallRecording({
                id: "id",
            });
        }).rejects.toThrow(Intercom.unstable.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/id/recording")
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.unstable.calls.showCallRecording({
                id: "id",
            });
        }).rejects.toThrow(Intercom.unstable.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.unstable.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.unstable.calls.listCallsWithTranscripts({
                conversation_ids: ["conversation_ids", "conversation_ids"],
            });
        }).rejects.toThrow(Intercom.unstable.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: "type", errors: [{ code: "code" }, { code: "code" }] };
        server
            .mockEndpoint()
            .post("/calls/search")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

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

    test("registerFinVoiceCall (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });
        const rawRequestBody = { phone_number: "+1234567890", call_id: "call-123-abc" };
        const rawResponseBody = {
            id: 12345,
            app_id: 12345,
            user_phone_number: "+1234567890",
            status: "registered",
            intercom_call_id: "1234",
            external_call_id: "call-123-abc",
            intercom_conversation_id: "5678",
            call_transcript: [{ key: "value" }],
            call_summary: "Customer called about billing issue...",
            intent: [{ key: "value" }],
        };
        server
            .mockEndpoint()
            .post("/fin_voice/register")
            .jsonBody(rawRequestBody)
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.unstable.calls.registerFinVoiceCall({
            phone_number: "+1234567890",
            call_id: "call-123-abc",
        });
        expect(response).toEqual({
            id: 12345,
            app_id: 12345,
            user_phone_number: "+1234567890",
            status: "registered",
            intercom_call_id: "1234",
            external_call_id: "call-123-abc",
            intercom_conversation_id: "5678",
            call_transcript: [
                {
                    key: "value",
                },
            ],
            call_summary: "Customer called about billing issue...",
            intent: [
                {
                    key: "value",
                },
            ],
        });
    });

    test("registerFinVoiceCall (2)", 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()
            .post("/fin_voice/register")
            .respondWith()
            .statusCode(400)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.unstable.calls.registerFinVoiceCall(undefined);
        }).rejects.toThrow(Intercom.unstable.BadRequestError);
    });

    test("registerFinVoiceCall (3)", 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()
            .post("/fin_voice/register")
            .respondWith()
            .statusCode(409)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.unstable.calls.registerFinVoiceCall(undefined);
        }).rejects.toThrow(Intercom.unstable.ConflictError);
    });

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

        const rawResponseBody = {
            id: 12345,
            app_id: 12345,
            user_phone_number: "+1234567890",
            status: "registered",
            intercom_call_id: "1234",
            external_call_id: "call-123-abc",
            intercom_conversation_id: "5678",
            call_transcript: [{ key: "value" }],
            call_summary: "Customer called about billing issue...",
            intent: [{ key: "value" }],
        };
        server
            .mockEndpoint()
            .get("/fin_voice/collect/1")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.unstable.calls.collectFinVoiceCallById({
            id: 1,
        });
        expect(response).toEqual({
            id: 12345,
            app_id: 12345,
            user_phone_number: "+1234567890",
            status: "registered",
            intercom_call_id: "1234",
            external_call_id: "call-123-abc",
            intercom_conversation_id: "5678",
            call_transcript: [
                {
                    key: "value",
                },
            ],
            call_summary: "Customer called about billing issue...",
            intent: [
                {
                    key: "value",
                },
            ],
        });
    });

    test("collectFinVoiceCallById (2)", 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("/fin_voice/collect/1")
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.unstable.calls.collectFinVoiceCallById({
                id: 1,
            });
        }).rejects.toThrow(Intercom.unstable.NotFoundError);
    });

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

        const rawResponseBody = {
            id: 12345,
            app_id: 12345,
            user_phone_number: "+1234567890",
            status: "registered",
            intercom_call_id: "1234",
            external_call_id: "call-123-abc",
            intercom_conversation_id: "5678",
            call_transcript: [{ key: "value" }],
            call_summary: "Customer called about billing issue...",
            intent: [{ key: "value" }],
        };
        server
            .mockEndpoint()
            .get("/fin_voice/external_id/external_id")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.unstable.calls.collectFinVoiceCallByExternalId({
            external_id: "external_id",
        });
        expect(response).toEqual({
            id: 12345,
            app_id: 12345,
            user_phone_number: "+1234567890",
            status: "registered",
            intercom_call_id: "1234",
            external_call_id: "call-123-abc",
            intercom_conversation_id: "5678",
            call_transcript: [
                {
                    key: "value",
                },
            ],
            call_summary: "Customer called about billing issue...",
            intent: [
                {
                    key: "value",
                },
            ],
        });
    });

    test("collectFinVoiceCallByExternalId (2)", 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("/fin_voice/external_id/external_id")
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

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

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

        const rawResponseBody = {
            type: "error.list",
            request_id: "f93ecfa8-d08a-4325-8694-89aeb89c8f85",
            errors: [{ code: "unauthorized", message: "Access Token Invalid", field: "email" }],
        };
        server
            .mockEndpoint()
            .get("/fin_voice/phone_number/phone_number")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.unstable.calls.collectFinVoiceCallByPhoneNumber({
            phone_number: "phone_number",
        });
        expect(response).toEqual({
            type: "error.list",
            request_id: "f93ecfa8-d08a-4325-8694-89aeb89c8f85",
            errors: [
                {
                    code: "unauthorized",
                    message: "Access Token Invalid",
                    field: "email",
                },
            ],
        });
    });

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

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

    test("collectFinVoiceCallByPhoneNumber (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("/fin_voice/phone_number/phone_number")
            .respondWith()
            .statusCode(404)
            .jsonBody(rawResponseBody)
            .build();

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