// 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("FeedsClient", () => {
    test("listItems (1)", async () => {
        const server = mockServerPool.createServer();
        const client = new IntercomClient({
            maxRetries: 0,
            token: "test",
            version: "2.14",
            environment: server.baseUrl,
        });

        const rawResponseBody = {
            type: "list",
            pages: {
                type: "pages",
                page: 1,
                next: { per_page: 2, starting_after: "your-cursor-from-response" },
                per_page: 20,
                total_pages: 0,
            },
            total_count: 0,
            data: [
                {
                    type: "news-item",
                    id: "141",
                    workspace_id: "t74hdn32",
                    title: "New feature: News Items",
                    body: "We are excited to announce the launch of News Items, a new content type in Intercom enabling you to announce product updates, company news, promotions, events and more with your customers.",
                    sender_id: 123,
                    state: "draft",
                    newsfeed_assignments: [{ newsfeed_id: 198313, published_at: 1674917488 }],
                    labels: ["Product Update"],
                    cover_image_url: "https://example.com/cover.jpg",
                    reactions: ["👍"],
                    deliver_silently: true,
                    created_at: 1610589632,
                    updated_at: 1610589632,
                },
            ],
        };
        server
            .mockEndpoint()
            .get("/news/newsfeeds/123/items")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.news.feeds.listItems({
            newsfeed_id: "123",
        });
        expect(response).toEqual({
            type: "list",
            pages: {
                type: "pages",
                page: 1,
                next: {
                    per_page: 2,
                    starting_after: "your-cursor-from-response",
                },
                per_page: 20,
                total_pages: 0,
            },
            total_count: 0,
            data: [
                {
                    type: "news-item",
                    id: "141",
                    workspace_id: "t74hdn32",
                    title: "New feature: News Items",
                    body: "We are excited to announce the launch of News Items, a new content type in Intercom enabling you to announce product updates, company news, promotions, events and more with your customers.",
                    sender_id: 123,
                    state: "draft",
                    newsfeed_assignments: [
                        {
                            newsfeed_id: 198313,
                            published_at: 1674917488,
                        },
                    ],
                    labels: ["Product Update"],
                    cover_image_url: "https://example.com/cover.jpg",
                    reactions: ["\uD83D\uDC4D"],
                    deliver_silently: true,
                    created_at: 1610589632,
                    updated_at: 1610589632,
                },
            ],
        });
    });

    test("listItems (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("/news/newsfeeds/newsfeed_id/items")
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.news.feeds.listItems({
                newsfeed_id: "newsfeed_id",
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

    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: "list",
            pages: {
                type: "pages",
                page: 1,
                next: { per_page: 2, starting_after: "your-cursor-from-response" },
                per_page: 10,
                total_pages: 1,
            },
            total_count: 2,
            data: [
                { type: "newsfeed", id: "12312", name: "My Newsfeed", created_at: 1674917488, updated_at: 1674917488 },
                { type: "newsfeed", id: "12312", name: "My Newsfeed", created_at: 1674917488, updated_at: 1674917488 },
            ],
        };
        server.mockEndpoint().get("/news/newsfeeds").respondWith().statusCode(200).jsonBody(rawResponseBody).build();

        const response = await client.news.feeds.list();
        expect(response).toEqual({
            type: "list",
            pages: {
                type: "pages",
                page: 1,
                next: {
                    per_page: 2,
                    starting_after: "your-cursor-from-response",
                },
                per_page: 10,
                total_pages: 1,
            },
            total_count: 2,
            data: [
                {
                    id: "12312",
                    type: "newsfeed",
                    name: "My Newsfeed",
                    created_at: 1674917488,
                    updated_at: 1674917488,
                },
                {
                    id: "12312",
                    type: "newsfeed",
                    name: "My Newsfeed",
                    created_at: 1674917488,
                    updated_at: 1674917488,
                },
            ],
        });
    });

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

        await expect(async () => {
            return await client.news.feeds.list();
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });

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

        const rawResponseBody = {
            id: "72",
            type: "newsfeed",
            name: "Visitor Feed",
            created_at: 1734537815,
            updated_at: 1734537815,
        };
        server
            .mockEndpoint()
            .get("/news/newsfeeds/123")
            .respondWith()
            .statusCode(200)
            .jsonBody(rawResponseBody)
            .build();

        const response = await client.news.feeds.find({
            newsfeed_id: "123",
        });
        expect(response).toEqual({
            id: "72",
            type: "newsfeed",
            name: "Visitor Feed",
            created_at: 1734537815,
            updated_at: 1734537815,
        });
    });

    test("find (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("/news/newsfeeds/newsfeed_id")
            .respondWith()
            .statusCode(401)
            .jsonBody(rawResponseBody)
            .build();

        await expect(async () => {
            return await client.news.feeds.find({
                newsfeed_id: "newsfeed_id",
            });
        }).rejects.toThrow(Intercom.UnauthorizedError);
    });
});
