Skip to content

Commit

Permalink
add missing unit tests (#90)
Browse files Browse the repository at this point in the history
* test: add unit tests for notebook utils

Signed-off-by: Lin Wang <wonglam@amazon.com>

* test: add unit tests for message content

Signed-off-by: Lin Wang <wonglam@amazon.com>

* test: add unit tests for chat context

Signed-off-by: Lin Wang <wonglam@amazon.com>

---------

Signed-off-by: Lin Wang <wonglam@amazon.com>
  • Loading branch information
wanglam authored Jan 10, 2024
1 parent e278a47 commit 83fcde2
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 1 deletion.
41 changes: 41 additions & 0 deletions public/contexts/__tests__/chat_context.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import { useChatContext, ChatContext } from '../chat_context';

describe('useChatContext', () => {
it('should return chat context after useChatContext called', () => {
const chatContextValueMock = {
setSessionId: jest.fn(),
selectedTabId: 'chat' as const,
setSelectedTabId: jest.fn(),
flyoutVisible: true,
flyoutFullScreen: true,
setFlyoutVisible: jest.fn(),
setFlyoutComponent: jest.fn(),
userHasAccess: true,
contentRenderers: {},
actionExecutors: {},
currentAccount: { username: 'foo', tenant: '' },
setTitle: jest.fn(),
setTraceId: jest.fn(),
};
const { result } = renderHook(useChatContext, {
wrapper: ({ children }) => (
<ChatContext.Provider value={chatContextValueMock}>{children}</ChatContext.Provider>
),
});

expect(result.current).toBe(chatContextValueMock);
});

it('should return error if context provider missed', () => {
const { result } = renderHook(useChatContext);

expect(result.error).toMatchInlineSnapshot(`[Error: ChatContext is not set]`);
});
});
19 changes: 18 additions & 1 deletion public/tabs/chat/messages/message_content.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ jest.mock('../../../components/core_visualization', () => {

describe('<MessageContent />', () => {
const pplVisualizationRenderMock = jest.fn();
const customizedRenderMock = jest.fn();

beforeEach(() => {
jest.spyOn(chatContextExports, 'useChatContext').mockReturnValue({
contentRenderers: { ppl_visualization: pplVisualizationRenderMock },
contentRenderers: {
ppl_visualization: pplVisualizationRenderMock,
customized_content_type: customizedRenderMock,
},
});
});

Expand Down Expand Up @@ -87,4 +91,17 @@ describe('<MessageContent />', () => {
);
expect(pplVisualizationRenderMock).toHaveBeenCalledWith({ query: 'mock ppl query' });
});

it('should render customized render content', () => {
render(
<MessageContent
message={{
type: 'output',
contentType: 'customized_content_type',
content: 'mock customized content',
}}
/>
);
expect(customizedRenderMock).toHaveBeenCalledWith('mock customized content');
});
});
160 changes: 160 additions & 0 deletions public/utils/tests/notebook.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import * as uuidExports from 'uuid';
import { convertMessagesToParagraphs } from '../notebook';

jest.mock('uuid', () => ({
...jest.requireActual('uuid'),
__esModule: true,
}));

describe('notebook convertMessagesToParagraphs utils', () => {
beforeAll(() => {
jest.useFakeTimers();
jest.setSystemTime(new Date(2023, 11, 26));
jest.spyOn(uuidExports, 'v4').mockReturnValue('a-fixed-uuid');
});
afterAll(() => {
jest.useRealTimers();
jest.spyOn(uuidExports, 'v4').mockRestore();
});
it('should return consistent paragraphs if contentType is text', () => {
expect(
convertMessagesToParagraphs([{ type: 'input', contentType: 'text', content: 'bar' }], 'foo')
).toMatchInlineSnapshot(`
Array [
Object {
"dateCreated": "2023-12-26T00:00:00.000Z",
"dateModified": "2023-12-26T00:00:00.000Z",
"id": "paragraph_a-fixed-uuid",
"input": Object {
"inputText": "%md
foo: bar",
"inputType": "MARKDOWN",
},
"output": Array [
Object {
"execution_time": "0 ms",
"outputType": "MARKDOWN",
"result": "foo: bar",
},
],
},
]
`);
});

it('should return consistent paragraphs if contentType is markdown', () => {
expect(
convertMessagesToParagraphs(
[{ type: 'output', contentType: 'markdown', content: 'bar' }],
'foo'
)
).toMatchInlineSnapshot(`
Array [
Object {
"dateCreated": "2023-12-26T00:00:00.000Z",
"dateModified": "2023-12-26T00:00:00.000Z",
"id": "paragraph_a-fixed-uuid",
"input": Object {
"inputText": "%md
OpenSearch Assistant: bar",
"inputType": "MARKDOWN",
},
"output": Array [
Object {
"execution_time": "0 ms",
"outputType": "MARKDOWN",
"result": "OpenSearch Assistant: bar",
},
],
},
]
`);
});

it('should return consistent paragraphs if contentType is error', () => {
expect(
convertMessagesToParagraphs([{ type: 'output', contentType: 'error', content: 'bar' }], 'foo')
).toMatchInlineSnapshot(`
Array [
Object {
"dateCreated": "2023-12-26T00:00:00.000Z",
"dateModified": "2023-12-26T00:00:00.000Z",
"id": "paragraph_a-fixed-uuid",
"input": Object {
"inputText": "%md
OpenSearch Assistant: bar",
"inputType": "MARKDOWN",
},
"output": Array [
Object {
"execution_time": "0 ms",
"outputType": "MARKDOWN",
"result": "OpenSearch Assistant: bar",
},
],
},
]
`);
});

it('should return consistent paragraphs if contentType is visualization', () => {
expect(
convertMessagesToParagraphs(
[{ type: 'output', contentType: 'visualization', content: 'bar' }],
'foo'
)
).toMatchInlineSnapshot(`
Array [
Object {
"dateCreated": "2023-12-26T00:00:00.000Z",
"dateModified": "2023-12-26T00:00:00.000Z",
"id": "paragraph_a-fixed-uuid",
"input": Object {
"inputText": "{\\"viewMode\\":\\"view\\",\\"panels\\":{\\"1\\":{\\"gridData\\":{\\"x\\":0,\\"y\\":0,\\"w\\":50,\\"h\\":20,\\"i\\":\\"1\\"},\\"type\\":\\"visualization\\",\\"explicitInput\\":{\\"id\\":\\"1\\",\\"savedObjectId\\":\\"bar\\"}}},\\"isFullScreenMode\\":false,\\"filters\\":[],\\"useMargins\\":false,\\"id\\":\\"random_html_id\\",\\"timeRange\\":{\\"to\\":\\"now\\",\\"from\\":\\"now-15m\\"},\\"title\\":\\"embed_viz_random_html_id\\",\\"query\\":{\\"query\\":\\"\\",\\"language\\":\\"lucene\\"},\\"refreshConfig\\":{\\"pause\\":true,\\"value\\":15}}",
"inputType": "VISUALIZATION",
},
"output": Array [
Object {
"execution_time": "0 ms",
"outputType": "VISUALIZATION",
"result": "",
},
],
},
]
`);
});

it('should return consistent paragraphs if contentType is not supported', () => {
expect(
convertMessagesToParagraphs(
[{ type: 'output', contentType: 'not-supported', content: 'bar' }],
'foo'
)
).toMatchInlineSnapshot(`
Array [
Object {
"dateCreated": "2023-12-26T00:00:00.000Z",
"dateModified": "2023-12-26T00:00:00.000Z",
"id": "paragraph_a-fixed-uuid",
"input": Object {
"inputText": "",
"inputType": "",
},
"output": Array [
Object {
"execution_time": "0 ms",
"outputType": "",
"result": "",
},
],
},
]
`);
});
});

0 comments on commit 83fcde2

Please sign in to comment.