-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
3 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "", | ||
}, | ||
], | ||
}, | ||
] | ||
`); | ||
}); | ||
}); |