"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
require("jest-fetch-mock");
const bodyless_conversation_1 = require("./bodyless-conversation");
const authInstance = tslib_1.__importStar(require("../auth"));
const index_1 = require("../index");
const types_1 = require("../types");
const test_utils_1 = require("../test/test-utils");
describe('SpotterAgentEmbed', () => {
    const thoughtSpotHost = 'tshost';
    beforeAll(() => {
        (0, index_1.init)({
            thoughtSpotHost,
            authType: types_1.AuthType.None,
        });
        jest.spyOn(authInstance, 'postLoginService').mockImplementation(() => Promise.resolve({}));
        spyOn(window, 'alert');
        document.body.innerHTML = (0, test_utils_1.getDocumentBody)();
    });
    beforeEach(() => {
        fetchMock.resetMocks();
    });
    test('should render the SpotterAgent embed', async () => {
        fetchMock.mockResponses(JSON.stringify({
            data: {
                ConvAssist__createConversation: {
                    convId: 'conversationId',
                    initialCtx: {
                        type: 'TS_ANSWER',
                        tsAnsCtx: {
                            sessionId: 'sessionId',
                            genNo: 1,
                            stateKey: {
                                transactionId: 'transactionId',
                                generationNumber: 1,
                            },
                            worksheet: {
                                worksheetId: 'worksheetId',
                                worksheetName: 'GTM',
                            },
                        },
                    },
                },
            },
        }), JSON.stringify({
            data: {
                ConvAssist__sendMessage: {
                    responses: [
                        {
                            msgId: 'msgId',
                            data: {
                                asstRespData: {
                                    tool: 'TS_NLS',
                                    asstRespText: '',
                                    nlsAnsData: {
                                        sageQuerySuggestions: [
                                            {
                                                llmReasoning: {
                                                    assumptions: 'You want the total [COL|sales] for [COL|item_type] [VAL|jackets] for [VAL|this year].',
                                                    clarifications: '',
                                                    interpretation: '',
                                                    __typename: 'eureka_SageQuerySuggestion_LLMReasoning',
                                                },
                                                tokens: [
                                                    'sum sales',
                                                    "item type = 'jackets'",
                                                    "date = 'this year'",
                                                ],
                                                tmlTokens: [
                                                    'sum [sales]',
                                                    "[date] = [date].'this year'",
                                                    "[item type] = [item type].'jackets'",
                                                ],
                                                worksheetId: 'worksheetId',
                                                description: '',
                                                title: '',
                                                cached: false,
                                                sqlQuery: "SELECT SUM(sales) FROM __Sample_Retail_Apparel WHERE item_type = 'jackets' AND date = _this_year();",
                                                sessionId: 'sessionId',
                                                genNo: 2,
                                                formulaInfo: [],
                                                tmlPhrases: [],
                                                stateKey: {
                                                    transactionId: 'transactionId',
                                                    generationNumber: 1,
                                                    __typename: 'sage_auto_complete_v2_ACStateKey',
                                                },
                                                __typename: 'eureka_SageQuerySuggestion',
                                            },
                                        ],
                                        responseType: 'ANSWER',
                                        __typename: 'convassist_nls_tool_NLSToolAsstRespData',
                                    },
                                    __typename: 'convassist_AsstResponseData',
                                },
                                __typename: 'convassist_MessageData',
                            },
                            type: 'ASST_RESPONSE',
                            __typename: 'convassist_MessagePayload',
                        },
                    ],
                    __typename: 'convassist_SendMessageResponse',
                },
            },
        }));
        const viewConfig = {
            worksheetId: 'worksheetId',
        };
        const spotterEmbed = new bodyless_conversation_1.SpotterAgentEmbed(viewConfig);
        const result = await spotterEmbed.sendMessage('userMessage');
        const iframeSrc = (0, test_utils_1.getIFrameSrc)(result.container);
        (0, test_utils_1.expectUrlToHaveParamsWithValues)(iframeSrc, {
            sessionId: 'sessionId',
            genNo: 2,
            acSessionId: 'transactionId',
            acGenNo: 1,
        });
        fetchMock.mockRejectOnce(new Error('error'));
        const errorResult = await spotterEmbed.sendMessage('userMessage');
        expect(errorResult.error instanceof Error).toBeTruthy();
    });
    test('should apply containerClassName to the container element', async () => {
        fetchMock.mockResponses(JSON.stringify({
            data: {
                ConvAssist__createConversation: {
                    convId: 'conversationId',
                    initialCtx: {
                        type: 'TS_ANSWER',
                        tsAnsCtx: {
                            sessionId: 'sessionId',
                            genNo: 1,
                            stateKey: {
                                transactionId: 'transactionId',
                                generationNumber: 1,
                            },
                            worksheet: {
                                worksheetId: 'worksheetId',
                                worksheetName: 'GTM',
                            },
                        },
                    },
                },
            },
        }), JSON.stringify({
            data: {
                ConvAssist__sendMessage: {
                    responses: [
                        {
                            msgId: 'msgId',
                            data: {
                                asstRespData: {
                                    tool: 'TS_NLS',
                                    asstRespText: '',
                                    nlsAnsData: {
                                        sageQuerySuggestions: [
                                            {
                                                llmReasoning: {
                                                    assumptions: 'You want the total [COL|sales] for [COL|item_type] [VAL|jackets] for [VAL|this year].',
                                                    clarifications: '',
                                                    interpretation: '',
                                                    __typename: 'eureka_SageQuerySuggestion_LLMReasoning',
                                                },
                                                tokens: [
                                                    'sum sales',
                                                    "item type = 'jackets'",
                                                    "date = 'this year'",
                                                ],
                                                tmlTokens: [
                                                    'sum [sales]',
                                                    "[date] = [date].'this year'",
                                                    "[item type] = [item type].'jackets'",
                                                ],
                                                worksheetId: 'worksheetId',
                                                description: '',
                                                title: '',
                                                cached: false,
                                                sqlQuery: "SELECT SUM(sales) FROM __Sample_Retail_Apparel WHERE item_type = 'jackets' AND date = _this_year();",
                                                sessionId: 'sessionId',
                                                genNo: 2,
                                                formulaInfo: [],
                                                tmlPhrases: [],
                                                stateKey: {
                                                    transactionId: 'transactionId',
                                                    generationNumber: 1,
                                                    __typename: 'sage_auto_complete_v2_ACStateKey',
                                                },
                                                __typename: 'eureka_SageQuerySuggestion',
                                            },
                                        ],
                                        responseType: 'ANSWER',
                                        __typename: 'convassist_nls_tool_NLSToolAsstRespData',
                                    },
                                    __typename: 'convassist_AsstResponseData',
                                },
                                __typename: 'convassist_MessageData',
                            },
                            type: 'ASST_RESPONSE',
                            __typename: 'convassist_MessagePayload',
                        },
                    ],
                    __typename: 'convassist_SendMessageResponse',
                },
            },
        }));
        const viewConfig = {
            worksheetId: 'worksheetId',
            containerClassName: 'custom-conversation-container',
        };
        const spotterAgentEmbed = new bodyless_conversation_1.SpotterAgentEmbed(viewConfig);
        const result = await spotterAgentEmbed.sendMessage('userMessage');
        // Verify that the container has the custom class name
        expect(result.container.className).toBe('custom-conversation-container');
        // Also verify the iframe src is correct
        const iframeSrc = (0, test_utils_1.getIFrameSrc)(result.container);
        (0, test_utils_1.expectUrlToHaveParamsWithValues)(iframeSrc, {
            sessionId: 'sessionId',
            genNo: 2,
            acSessionId: 'transactionId',
            acGenNo: 1,
        });
    });
    test('should not set className when containerClassName is not provided', async () => {
        fetchMock.mockResponses(JSON.stringify({
            data: {
                ConvAssist__createConversation: {
                    convId: 'conversationId',
                    initialCtx: {
                        type: 'TS_ANSWER',
                        tsAnsCtx: {
                            sessionId: 'sessionId',
                            genNo: 1,
                            stateKey: {
                                transactionId: 'transactionId',
                                generationNumber: 1,
                            },
                            worksheet: {
                                worksheetId: 'worksheetId',
                                worksheetName: 'GTM',
                            },
                        },
                    },
                },
            },
        }), JSON.stringify({
            data: {
                ConvAssist__sendMessage: {
                    responses: [
                        {
                            msgId: 'msgId',
                            data: {
                                asstRespData: {
                                    tool: 'TS_NLS',
                                    asstRespText: '',
                                    nlsAnsData: {
                                        sageQuerySuggestions: [
                                            {
                                                llmReasoning: {
                                                    assumptions: 'You want the total [COL|sales] for [COL|item_type] [VAL|jackets] for [VAL|this year].',
                                                    clarifications: '',
                                                    interpretation: '',
                                                    __typename: 'eureka_SageQuerySuggestion_LLMReasoning',
                                                },
                                                tokens: [
                                                    'sum sales',
                                                    "item type = 'jackets'",
                                                    "date = 'this year'",
                                                ],
                                                tmlTokens: [
                                                    'sum [sales]',
                                                    "[date] = [date].'this year'",
                                                    "[item type] = [item type].'jackets'",
                                                ],
                                                worksheetId: 'worksheetId',
                                                description: '',
                                                title: '',
                                                cached: false,
                                                sqlQuery: "SELECT SUM(sales) FROM __Sample_Retail_Apparel WHERE item_type = 'jackets' AND date = _this_year();",
                                                sessionId: 'sessionId',
                                                genNo: 2,
                                                formulaInfo: [],
                                                tmlPhrases: [],
                                                stateKey: {
                                                    transactionId: 'transactionId',
                                                    generationNumber: 1,
                                                    __typename: 'sage_auto_complete_v2_ACStateKey',
                                                },
                                                __typename: 'eureka_SageQuerySuggestion',
                                            },
                                        ],
                                        responseType: 'ANSWER',
                                        __typename: 'convassist_nls_tool_NLSToolAsstRespData',
                                    },
                                    __typename: 'convassist_AsstResponseData',
                                },
                                __typename: 'convassist_MessageData',
                            },
                            type: 'ASST_RESPONSE',
                            __typename: 'convassist_MessagePayload',
                        },
                    ],
                    __typename: 'convassist_SendMessageResponse',
                },
            },
        }));
        const viewConfig = {
            worksheetId: 'worksheetId',
            // No containerClassName provided
        };
        const spotterAgentEmbed = new bodyless_conversation_1.SpotterAgentEmbed(viewConfig);
        const result = await spotterAgentEmbed.sendMessage('userMessage');
        // Verify that the container has no class name (empty string)
        expect(result.container.className).toBe('');
    });
    test('should handle hideActions parameter correctly', async () => {
        fetchMock.mockResponses(JSON.stringify({
            data: {
                ConvAssist__createConversation: {
                    convId: 'conversationId',
                    initialCtx: {
                        type: 'TS_ANSWER',
                        tsAnsCtx: {
                            sessionId: 'sessionId',
                            genNo: 1,
                            stateKey: {
                                transactionId: 'transactionId',
                                generationNumber: 1,
                            },
                            worksheet: {
                                worksheetId: 'worksheetId',
                                worksheetName: 'GTM',
                            },
                        },
                    },
                },
            },
        }), JSON.stringify({
            data: {
                ConvAssist__sendMessage: {
                    responses: [
                        {
                            msgId: 'msgId',
                            data: {
                                asstRespData: {
                                    tool: 'TS_NLS',
                                    asstRespText: '',
                                    nlsAnsData: {
                                        sageQuerySuggestions: [
                                            {
                                                llmReasoning: {
                                                    assumptions: 'You want the total [COL|sales] for [COL|item_type] [VAL|jackets] for [VAL|this year].',
                                                    clarifications: '',
                                                    interpretation: '',
                                                    __typename: 'eureka_SageQuerySuggestion_LLMReasoning',
                                                },
                                                tokens: [
                                                    'sum sales',
                                                    "item type = 'jackets'",
                                                    "date = 'this year'",
                                                ],
                                                tmlTokens: [
                                                    'sum [sales]',
                                                    "[date] = [date].'this year'",
                                                    "[item type] = [item type].'jackets'",
                                                ],
                                                worksheetId: 'worksheetId',
                                                description: '',
                                                title: '',
                                                cached: false,
                                                sqlQuery: "SELECT SUM(sales) FROM __Sample_Retail_Apparel WHERE item_type = 'jackets' AND date = _this_year();",
                                                sessionId: 'sessionId',
                                                genNo: 2,
                                                formulaInfo: [],
                                                tmlPhrases: [],
                                                stateKey: {
                                                    transactionId: 'transactionId',
                                                    generationNumber: 1,
                                                    __typename: 'sage_auto_complete_v2_ACStateKey',
                                                },
                                                __typename: 'eureka_SageQuerySuggestion',
                                            },
                                        ],
                                        responseType: 'ANSWER',
                                        __typename: 'convassist_nls_tool_NLSToolAsstRespData',
                                    },
                                    __typename: 'convassist_AsstResponseData',
                                },
                                __typename: 'convassist_MessageData',
                            },
                            type: 'ASST_RESPONSE',
                            __typename: 'convassist_MessagePayload',
                        },
                    ],
                    __typename: 'convassist_SendMessageResponse',
                },
            },
        }));
        const viewConfig = {
            worksheetId: 'worksheetId',
            hiddenActions: [types_1.Action.Download, types_1.Action.Save], // This should trigger the HideActions branch
        };
        const spotterAgentEmbed = new bodyless_conversation_1.SpotterAgentEmbed(viewConfig);
        const result = await spotterAgentEmbed.sendMessage('userMessage');
        // Verify the iframe src contains the hideActions parameter
        const iframeSrc = (0, test_utils_1.getIFrameSrc)(result.container);
        expect(iframeSrc).toContain('hideAction');
    });
});
//# sourceMappingURL=bodyless-conversation.spec.js.map