UNPKG

@thoughtspot/visual-embed-sdk

Version:
227 lines 11.9 kB
"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(undefined)); jest.spyOn(window, 'alert').mockImplementation(() => undefined); 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 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'); }); test('should have sendMessageData method', () => { const viewConfig = { worksheetId: 'worksheetId', }; const spotterAgentEmbed = new bodyless_conversation_1.SpotterAgentEmbed(viewConfig); expect(typeof spotterAgentEmbed.sendMessageData).toBe('function'); }); test('should export ConversationMessage class', () => { expect(typeof bodyless_conversation_1.ConversationMessage).toBe('function'); }); }); //# sourceMappingURL=bodyless-conversation.spec.js.map