|
| 1 | +import 'jest-fetch-mock'; |
| 2 | +import { BodylessConversation, BodylessConversationViewConfig } from './bodyless-conversation'; |
| 3 | +import * as authInstance from '../auth'; |
| 4 | +import { init } from '../index'; |
| 5 | +import { Action, AuthType, RuntimeFilterOp } from '../types'; |
| 6 | +import { |
| 7 | + executeAfterWait, |
| 8 | + getDocumentBody, |
| 9 | + getIFrameSrc, |
| 10 | + getRootEl, |
| 11 | + fixedEncodeURI, |
| 12 | + defaultParamsWithoutHiddenActions as defaultParams, |
| 13 | + expectUrlMatchesWithParams, |
| 14 | + expectUrlToHaveParamsWithValues, |
| 15 | +} from '../test/test-utils'; |
| 16 | + |
| 17 | +describe('BodylessConversation', () => { |
| 18 | + const thoughtSpotHost = 'tshost'; |
| 19 | + |
| 20 | + beforeAll(() => { |
| 21 | + init({ |
| 22 | + thoughtSpotHost, |
| 23 | + authType: AuthType.None, |
| 24 | + }); |
| 25 | + jest.spyOn(authInstance, 'postLoginService').mockImplementation(() => Promise.resolve({})); |
| 26 | + spyOn(window, 'alert'); |
| 27 | + document.body.innerHTML = getDocumentBody(); |
| 28 | + }); |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + fetchMock.resetMocks(); |
| 32 | + }); |
| 33 | + |
| 34 | + test('should render the bodyless conversation embed', async () => { |
| 35 | + fetchMock.mockResponses( |
| 36 | + JSON.stringify({ |
| 37 | + data: { |
| 38 | + ConvAssist__createConversation: { |
| 39 | + convId: 'conversationId', |
| 40 | + initialCtx: { |
| 41 | + type: 'TS_ANSWER', |
| 42 | + tsAnsCtx: { |
| 43 | + sessionId: 'sessionId', |
| 44 | + genNo: 1, |
| 45 | + stateKey: { |
| 46 | + transactionId: 'transactionId', |
| 47 | + generationNumber: 1, |
| 48 | + }, |
| 49 | + worksheet: { |
| 50 | + worksheetId: 'worksheetId', |
| 51 | + worksheetName: 'GTM', |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }), |
| 58 | + JSON.stringify({ |
| 59 | + data: { |
| 60 | + ConvAssist__sendMessage: { |
| 61 | + responses: [ |
| 62 | + { |
| 63 | + msgId: 'msgId', |
| 64 | + data: { |
| 65 | + asstRespData: { |
| 66 | + tool: 'TS_NLS', |
| 67 | + asstRespText: '', |
| 68 | + nlsAnsData: { |
| 69 | + sageQuerySuggestions: [ |
| 70 | + { |
| 71 | + llmReasoning: { |
| 72 | + assumptions: 'You want the total [COL|sales] for [COL|item_type] [VAL|jackets] for [VAL|this year].', |
| 73 | + clarifications: '', |
| 74 | + interpretation: '', |
| 75 | + __typename: 'eureka_SageQuerySuggestion_LLMReasoning', |
| 76 | + }, |
| 77 | + tokens: [ |
| 78 | + 'sum sales', |
| 79 | + "item type = 'jackets'", |
| 80 | + "date = 'this year'", |
| 81 | + ], |
| 82 | + tmlTokens: [ |
| 83 | + 'sum [sales]', |
| 84 | + "[date] = [date].'this year'", |
| 85 | + "[item type] = [item type].'jackets'", |
| 86 | + ], |
| 87 | + worksheetId: 'worksheetId', |
| 88 | + description: '', |
| 89 | + title: '', |
| 90 | + cached: false, |
| 91 | + sqlQuery: "SELECT SUM(sales) FROM __Sample_Retail_Apparel WHERE item_type = 'jackets' AND date = _this_year();", |
| 92 | + sessionId: 'sessionId', |
| 93 | + genNo: 2, |
| 94 | + formulaInfo: [], |
| 95 | + tmlPhrases: [], |
| 96 | + stateKey: { |
| 97 | + transactionId: 'transactionId', |
| 98 | + generationNumber: 1, |
| 99 | + __typename: 'sage_auto_complete_v2_ACStateKey', |
| 100 | + }, |
| 101 | + __typename: 'eureka_SageQuerySuggestion', |
| 102 | + }, |
| 103 | + ], |
| 104 | + responseType: 'ANSWER', |
| 105 | + __typename: 'convassist_nls_tool_NLSToolAsstRespData', |
| 106 | + }, |
| 107 | + __typename: 'convassist_AsstResponseData', |
| 108 | + }, |
| 109 | + __typename: 'convassist_MessageData', |
| 110 | + }, |
| 111 | + type: 'ASST_RESPONSE', |
| 112 | + __typename: 'convassist_MessagePayload', |
| 113 | + }, |
| 114 | + ], |
| 115 | + __typename: 'convassist_SendMessageResponse', |
| 116 | + }, |
| 117 | + }, |
| 118 | + }), |
| 119 | + ); |
| 120 | + const viewConfig: BodylessConversationViewConfig = { |
| 121 | + worksheetId: 'worksheetId', |
| 122 | + }; |
| 123 | + |
| 124 | + const conversationEmbed = new BodylessConversation(viewConfig); |
| 125 | + const result = await conversationEmbed.sendMessage('userMessage'); |
| 126 | + console.log(result.container); |
| 127 | + const iframeSrc = getIFrameSrc(result.container); |
| 128 | + expectUrlToHaveParamsWithValues(iframeSrc, { |
| 129 | + sessionId: 'sessionId', |
| 130 | + genNo: 2, |
| 131 | + acSessionId: 'transactionId', |
| 132 | + acGenNo: 1, |
| 133 | + }); |
| 134 | + |
| 135 | + fetchMock.mockRejectOnce( |
| 136 | + new Error('error'), |
| 137 | + ); |
| 138 | + const errorResult = await conversationEmbed.sendMessage('userMessage'); |
| 139 | + expect(errorResult.error instanceof Error).toBeTruthy(); |
| 140 | + }); |
| 141 | +}); |
0 commit comments