-
Notifications
You must be signed in to change notification settings - Fork 671
AI Assistant: Integration leftovers #33585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dmirgaev
merged 3 commits into
DevExpress:26_1
from
dmirgaev:26_1__ai_assistant_leftovers
May 15, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
335 changes: 335 additions & 0 deletions
335
...reme/js/__internal/grids/data_grid/ai_assistant/__tests__/ai_assistant_controller.test.ts
This file contains hidden or 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,335 @@ | ||
| import { | ||
| beforeEach, | ||
| describe, | ||
| expect, | ||
| it, | ||
| jest, | ||
| } from '@jest/globals'; | ||
| import type { ExecuteGridAssistantCommandResult } from '@js/common/ai-integration'; | ||
| import type { ArrayStore } from '@js/common/data'; | ||
| import type { Message } from '@js/ui/chat'; | ||
| import { coreCommands } from '@ts/grids/grid_core/ai_assistant/commands'; | ||
| import { | ||
| AI_ASSISTANT_AUTHOR, | ||
| AI_ASSISTANT_AUTHOR_ID, | ||
| MessageStatus, | ||
| } from '@ts/grids/grid_core/ai_assistant/const'; | ||
| import { GridCommands } from '@ts/grids/grid_core/ai_assistant/grid_commands'; | ||
| import type { | ||
| AIAssistantRequestCallbacks, | ||
| AIMessage, | ||
| CommandResult, | ||
| } from '@ts/grids/grid_core/ai_assistant/types'; | ||
| import type { InternalGrid } from '@ts/grids/grid_core/m_types'; | ||
|
|
||
| import { DataGridAIAssistantController } from '../ai_assistant_controller'; | ||
| import { DataGridAIAssistantIntegrationController } from '../ai_assistant_integration_controller'; | ||
| import { dataGridCommands } from '../commands/index'; | ||
|
|
||
| jest.mock('@ts/grids/grid_core/ai_assistant/grid_commands'); | ||
| jest.mock('../ai_assistant_integration_controller'); | ||
|
|
||
| const MockedGridCommands = GridCommands as jest.MockedClass<typeof GridCommands>; | ||
| const MockedDataGridAIAssistantIntegrationController = DataGridAIAssistantIntegrationController as | ||
| jest.MockedClass<typeof DataGridAIAssistantIntegrationController>; | ||
|
|
||
| let sendRequestCallbacks: AIAssistantRequestCallbacks<ExecuteGridAssistantCommandResult> = {}; | ||
|
|
||
| const createController = ( | ||
| options: Record<string, unknown> = {}, | ||
| ): DataGridAIAssistantController => { | ||
| const mockComponent = { | ||
| _optionCache: {}, | ||
| _controllers: {}, | ||
| option: jest.fn((name?: string) => { | ||
| if (name === undefined) { | ||
| return options; | ||
| } | ||
|
|
||
| return options[name]; | ||
| }), | ||
| _createActionByOption: jest.fn(() => jest.fn()), | ||
| }; | ||
|
|
||
| const controller = new DataGridAIAssistantController( | ||
| mockComponent as unknown as InternalGrid, | ||
| ); | ||
| controller.init(); | ||
|
|
||
| return controller; | ||
| }; | ||
|
|
||
| const getStore = ( | ||
| controller: DataGridAIAssistantController, | ||
| ): ArrayStore<Message, string> => { | ||
| const dataSource = controller.getMessageDataSource() as { | ||
| store: ArrayStore<Message, string>; | ||
| }; | ||
| return dataSource.store; | ||
| }; | ||
|
|
||
| describe('DataGridAIAssistantController', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
|
|
||
| (MockedGridCommands.mockImplementation as jest.Mock).call( | ||
| MockedGridCommands, | ||
| () => ({ | ||
| validate: jest.fn().mockReturnValue(true), | ||
| executeCommands: jest.fn<() => Promise<CommandResult[]>>() | ||
| .mockResolvedValue([{ status: 'success', message: 'sort' }]), | ||
| abort: jest.fn(), | ||
| buildResponseSchema: jest.fn().mockReturnValue({ type: 'object' }), | ||
| isExecuting: jest.fn().mockReturnValue(false), | ||
| }), | ||
| ); | ||
|
|
||
| (MockedDataGridAIAssistantIntegrationController | ||
| .mockImplementation as jest.Mock).call( | ||
| MockedDataGridAIAssistantIntegrationController, | ||
| () => ({ | ||
| init: jest.fn(), | ||
| dispose: jest.fn(), | ||
| sendRequest: jest.fn(( | ||
| _text: string, | ||
| _responseSchema: unknown, | ||
| callbacks?: AIAssistantRequestCallbacks<ExecuteGridAssistantCommandResult>, | ||
| ) => { | ||
| sendRequestCallbacks = callbacks ?? {}; | ||
| }), | ||
| abortRequest: jest.fn(() => { | ||
| sendRequestCallbacks.onAbort?.(); | ||
| }), | ||
| isRequestAwaitingCompletion: jest.fn().mockReturnValue(false), | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| describe('getGridCommandList', () => { | ||
| it('should include all core commands', () => { | ||
| createController(); | ||
|
|
||
| const constructorCall = MockedGridCommands.mock.calls[0]; | ||
| const commandList = constructorCall[1]; | ||
| const commandNames = commandList.map((c) => c.name); | ||
|
|
||
| for (const coreCommand of coreCommands) { | ||
| expect(commandNames).toContain(coreCommand.name); | ||
| } | ||
| }); | ||
|
|
||
| it('should include all data grid specific commands', () => { | ||
| createController(); | ||
|
|
||
| const constructorCall = MockedGridCommands.mock.calls[0]; | ||
| const commandList = constructorCall[1]; | ||
| const commandNames = commandList.map((c) => c.name); | ||
|
|
||
| expect(commandNames).toContain('grouping'); | ||
| expect(commandNames).toContain('clearGrouping'); | ||
| expect(commandNames).toContain('summary'); | ||
| expect(commandNames).toContain('clearSummary'); | ||
| }); | ||
|
|
||
| it('should extend core commands with data grid commands', () => { | ||
| createController(); | ||
|
|
||
| const constructorCall = MockedGridCommands.mock.calls[0]; | ||
| const commandList = constructorCall[1]; | ||
|
|
||
| expect(commandList).toHaveLength( | ||
| coreCommands.length + dataGridCommands.length, | ||
| ); | ||
| }); | ||
|
|
||
| it('should place core commands before data grid commands', () => { | ||
| createController(); | ||
|
|
||
| const constructorCall = MockedGridCommands.mock.calls[0]; | ||
| const commandList = constructorCall[1]; | ||
| const commandNames = commandList.map((c) => c.name); | ||
|
|
||
| const firstDataGridCommandIndex = commandNames.indexOf('grouping'); | ||
| const lastCoreCommandIndex = commandNames.indexOf( | ||
| coreCommands[coreCommands.length - 1].name, | ||
| ); | ||
|
|
||
| expect(firstDataGridCommandIndex).toBeGreaterThan(lastCoreCommandIndex); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getAiAssistantIntegrationController', () => { | ||
| it('should create DataGridAIAssistantIntegrationController', () => { | ||
| createController(); | ||
|
|
||
| expect(MockedDataGridAIAssistantIntegrationController).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('inherited behavior', () => { | ||
| it('should return dataSource with store', () => { | ||
| const controller = createController(); | ||
| const dataSource = controller.getMessageDataSource() as { | ||
| store: ArrayStore<Message, string>; | ||
| }; | ||
|
|
||
| expect(dataSource.store).toBeDefined(); | ||
| }); | ||
|
|
||
| it('should create pending message in store', async () => { | ||
| const controller = createController(); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
| controller.sendRequestToAI({ | ||
| author: { id: 'user', name: 'User' }, | ||
| text: 'Group by category', | ||
| timestamp: '2026-04-16T10:00:00.000Z', | ||
| } as Message); | ||
|
|
||
| const messages = await getStore(controller).load(); | ||
|
|
||
| expect(messages).toEqual([ | ||
| expect.objectContaining({ | ||
| id: expect.stringContaining(AI_ASSISTANT_AUTHOR_ID), | ||
| author: AI_ASSISTANT_AUTHOR, | ||
| status: MessageStatus.Pending, | ||
| }), | ||
| ]); | ||
| }); | ||
|
|
||
| it('should complete message as success when command succeed', async () => { | ||
| const controller = createController(); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
| controller.sendRequestToAI({ | ||
| author: { id: 'user', name: 'User' }, | ||
| text: 'Group by category', | ||
| timestamp: '2026-04-16T10:00:00.000Z', | ||
| } as Message); | ||
|
|
||
| const actions = [{ name: 'grouping', args: { dataField: 'Category', groupIndex: 0 } }]; | ||
| sendRequestCallbacks.onComplete?.({ actions }); | ||
| await Promise.resolve(); | ||
|
|
||
| const messages = await getStore(controller).load(); | ||
|
|
||
| expect(messages).toEqual([ | ||
| expect.objectContaining({ | ||
| status: MessageStatus.Success, | ||
| commands: [{ status: 'success', message: 'sort' }], | ||
| }), | ||
| ]); | ||
| }); | ||
|
|
||
| it('should fail message when onError callback is called', async () => { | ||
| const controller = createController(); | ||
|
|
||
| const promise = controller.sendRequestToAI({ | ||
| author: { id: 'user', name: 'User' }, | ||
| text: 'Group by category', | ||
| timestamp: '2026-04-16T10:00:00.000Z', | ||
| } as Message); | ||
| promise.catch(() => {}); | ||
|
|
||
| sendRequestCallbacks.onError?.(new Error('Network error')); | ||
|
|
||
| const messages = await getStore(controller).load(); | ||
|
|
||
| expect(messages).toEqual([ | ||
| expect.objectContaining({ | ||
| status: MessageStatus.Failure, | ||
| errorText: 'Network error', | ||
| }), | ||
| ]); | ||
|
|
||
| await expect(promise).rejects.toThrow('Network error'); | ||
| }); | ||
|
|
||
| it('should abort request and fail message', async () => { | ||
| const controller = createController(); | ||
|
|
||
| const promise = controller.sendRequestToAI({ | ||
| author: { id: 'user', name: 'User' }, | ||
| text: 'Group by category', | ||
| timestamp: '2026-04-16T10:00:00.000Z', | ||
| } as Message); | ||
| promise.catch(() => {}); | ||
|
|
||
| controller.abortRequest(); | ||
|
|
||
| const messages = await getStore(controller).load(); | ||
|
|
||
| expect(messages).toEqual([ | ||
| expect.objectContaining({ | ||
| status: MessageStatus.Failure, | ||
| }), | ||
| ]); | ||
|
|
||
| await expect(promise).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it('should call integration controller dispose on dispose', () => { | ||
| const controller = createController(); | ||
|
|
||
| const integrationInstance = MockedDataGridAIAssistantIntegrationController | ||
| .mock.results[0].value as { dispose: jest.Mock }; | ||
|
|
||
| controller.dispose(); | ||
|
|
||
| expect(integrationInstance.dispose).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should reject second request while first is processing', async () => { | ||
| const controller = createController(); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
| controller.sendRequestToAI({ | ||
| author: { id: 'user', name: 'User' }, | ||
| text: 'First request', | ||
| timestamp: '2026-04-16T10:00:00.000Z', | ||
| } as Message); | ||
|
|
||
| const secondPromise = controller.sendRequestToAI({ | ||
| author: { id: 'user', name: 'User' }, | ||
| text: 'Second request', | ||
| timestamp: '2026-04-16T10:00:01.000Z', | ||
| } as Message); | ||
| secondPromise.catch(() => {}); | ||
|
|
||
| await expect(secondPromise).rejects.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should support regenerating failed AIMessage', async () => { | ||
| const controller = createController(); | ||
|
|
||
| const aiMessage: AIMessage = { | ||
| id: 'assistant-123', | ||
| author: AI_ASSISTANT_AUTHOR, | ||
| text: MessageStatus.Failure, | ||
| prompt: 'Group by category', | ||
| status: MessageStatus.Failure, | ||
| headerText: 'Failed to process request', | ||
| errorText: 'Network error', | ||
| }; | ||
|
|
||
| const store = getStore(controller); | ||
| await store.insert(aiMessage); | ||
|
|
||
| const promise = controller.sendRequestToAI(aiMessage); | ||
|
|
||
| const actions = [{ name: 'grouping', args: { dataField: 'Category', groupIndex: 0 } }]; | ||
| sendRequestCallbacks.onComplete?.({ actions }); | ||
|
|
||
| await promise; | ||
|
|
||
| const messages = await store.load(); | ||
|
|
||
| expect(messages).toEqual([ | ||
| expect.objectContaining({ | ||
| id: 'assistant-123', | ||
| status: MessageStatus.Success, | ||
| }), | ||
| ]); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: looks excessive, usually we do not test inherited behavior