Skip to content

Commit db59171

Browse files
committed
add unit test for useAssetInventoryAssistant
1 parent ec511aa commit db59171

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import type { RenderHookResult } from '@testing-library/react';
9+
import { renderHook } from '@testing-library/react';
10+
import type {
11+
UseAssetInventoryAssistantParams,
12+
UseAssetInventoryAssistantResult,
13+
} from './use_asset_inventory_assistant';
14+
import { useAssetInventoryAssistant } from './use_asset_inventory_assistant';
15+
import { useAssistantContext, useAssistantOverlay } from '@kbn/elastic-assistant';
16+
import { useAssistantAvailability } from '../../../../assistant/use_assistant_availability';
17+
18+
jest.mock('../../../../assistant/use_assistant_availability');
19+
jest.mock('@kbn/elastic-assistant');
20+
21+
const mockEntityFields: Record<string, string[]> = {
22+
'host.name': ['test-host'],
23+
'host.ip': ['192.168.1.1'],
24+
'host.os.name': ['Ubuntu'],
25+
'user.name': ['test-user'],
26+
'process.name': ['nginx'],
27+
};
28+
29+
const entityId = 'test-entity-id';
30+
31+
const renderUseAssetInventoryAssistant = () =>
32+
renderHook((props: UseAssetInventoryAssistantParams) => useAssetInventoryAssistant(props), {
33+
initialProps: { entityId, entityFields: mockEntityFields },
34+
});
35+
36+
const useAssistantOverlayMock = useAssistantOverlay as jest.Mock;
37+
38+
describe('useAssetInventoryAssistant', () => {
39+
beforeEach(() => {
40+
jest.clearAllMocks();
41+
jest.mocked(useAssistantAvailability).mockReturnValue({
42+
hasSearchAILakeConfigurations: false,
43+
hasAssistantPrivilege: true,
44+
hasConnectorsAllPrivilege: true,
45+
hasConnectorsReadPrivilege: true,
46+
hasUpdateAIAssistantAnonymization: true,
47+
hasManageGlobalKnowledgeBase: true,
48+
isAssistantEnabled: true,
49+
isAssistantVisible: true,
50+
});
51+
useAssistantOverlayMock.mockReturnValue({
52+
showAssistantOverlay: jest.fn(),
53+
promptContextId: 'asset-inventory-123',
54+
});
55+
56+
(useAssistantContext as jest.Mock).mockReturnValue({
57+
basePromptContexts: [
58+
{
59+
category: 'alert',
60+
description: 'Alert (from view)',
61+
suggestedUserPrompt: 'ALERT EVALUATION',
62+
tooltip: 'Add this alert as context',
63+
},
64+
{
65+
category: 'asset',
66+
description: 'Asset (from inventory)',
67+
suggestedUserPrompt: 'ASSET ANALYSIS',
68+
tooltip: 'Add this asset as context',
69+
},
70+
{
71+
category: 'event',
72+
description: 'Event (from view)',
73+
suggestedUserPrompt: 'EVENT EVALUATION',
74+
tooltip: 'Add this event as context',
75+
},
76+
],
77+
});
78+
});
79+
80+
let hookResult: RenderHookResult<
81+
UseAssetInventoryAssistantResult,
82+
UseAssetInventoryAssistantParams
83+
>;
84+
85+
it('should return showAssistant true and a value for promptContextId', () => {
86+
hookResult = renderUseAssetInventoryAssistant();
87+
expect(hookResult.result.current.showAssistant).toEqual(true);
88+
expect(hookResult.result.current.promptContextId).toEqual('asset-inventory-123');
89+
});
90+
91+
it('should return showAssistant false if isAssistantEnabled is false', () => {
92+
jest.mocked(useAssistantAvailability).mockReturnValue({
93+
hasSearchAILakeConfigurations: false,
94+
hasAssistantPrivilege: true,
95+
hasConnectorsAllPrivilege: true,
96+
hasConnectorsReadPrivilege: true,
97+
hasUpdateAIAssistantAnonymization: true,
98+
hasManageGlobalKnowledgeBase: true,
99+
isAssistantEnabled: false,
100+
isAssistantVisible: true,
101+
});
102+
103+
hookResult = renderUseAssetInventoryAssistant();
104+
105+
expect(hookResult.result.current.showAssistant).toEqual(false);
106+
});
107+
108+
it('should return showAssistant false if hasAssistantPrivilege is false', () => {
109+
jest.mocked(useAssistantAvailability).mockReturnValue({
110+
hasSearchAILakeConfigurations: false,
111+
hasAssistantPrivilege: false,
112+
hasConnectorsAllPrivilege: true,
113+
hasConnectorsReadPrivilege: true,
114+
hasUpdateAIAssistantAnonymization: true,
115+
hasManageGlobalKnowledgeBase: true,
116+
isAssistantEnabled: true,
117+
isAssistantVisible: true,
118+
});
119+
120+
hookResult = renderUseAssetInventoryAssistant();
121+
122+
expect(hookResult.result.current.showAssistant).toEqual(false);
123+
expect(hookResult.result.current.promptContextId).toEqual('');
124+
});
125+
126+
it('should return showAssistant false if isAssistantVisible is false', () => {
127+
jest.mocked(useAssistantAvailability).mockReturnValue({
128+
hasSearchAILakeConfigurations: false,
129+
hasAssistantPrivilege: true,
130+
hasConnectorsAllPrivilege: true,
131+
hasConnectorsReadPrivilege: true,
132+
hasUpdateAIAssistantAnonymization: true,
133+
hasManageGlobalKnowledgeBase: true,
134+
isAssistantEnabled: true,
135+
isAssistantVisible: false,
136+
});
137+
138+
hookResult = renderUseAssetInventoryAssistant();
139+
140+
expect(hookResult.result.current.showAssistant).toEqual(false);
141+
});
142+
143+
it('should return showAssistant false if promptContextId is null', () => {
144+
useAssistantOverlayMock.mockReturnValue({
145+
showAssistantOverlay: jest.fn(),
146+
promptContextId: null,
147+
});
148+
149+
hookResult = renderUseAssetInventoryAssistant();
150+
151+
expect(hookResult.result.current.showAssistant).toEqual(false);
152+
});
153+
154+
it('returns entity fields as prompt context data', async () => {
155+
hookResult = renderUseAssetInventoryAssistant();
156+
157+
const getPromptContext = (useAssistantOverlay as jest.Mock).mock.calls[0][3];
158+
159+
expect(await getPromptContext()).toEqual(mockEntityFields);
160+
});
161+
162+
it('returns correct prompt context parameters for asset category', () => {
163+
renderUseAssetInventoryAssistant();
164+
165+
expect(useAssistantOverlayMock.mock.calls[0][0]).toEqual('entity');
166+
expect(useAssistantOverlayMock.mock.calls[0][1]).toEqual('test-entity-id');
167+
expect(useAssistantOverlayMock.mock.calls[0][5]).toEqual('ASSET ANALYSIS');
168+
});
169+
170+
it('falls back to default conversation ID when entityId is not provided', () => {
171+
renderHook((props: UseAssetInventoryAssistantParams) => useAssetInventoryAssistant(props), {
172+
initialProps: { entityId: '', entityFields: mockEntityFields },
173+
});
174+
175+
expect(useAssistantOverlayMock.mock.calls[0][1]).toEqual('Entity Summary');
176+
});
177+
178+
it('returns empty prompt context when entityFields is empty', async () => {
179+
renderHook((props: UseAssetInventoryAssistantParams) => useAssetInventoryAssistant(props), {
180+
initialProps: { entityId, entityFields: {} },
181+
});
182+
183+
const getPromptContext = (useAssistantOverlay as jest.Mock).mock.calls[0][3];
184+
185+
expect(await getPromptContext()).toEqual({});
186+
});
187+
188+
it('uses noop function when hasAssistantPrivilege is false', () => {
189+
jest.mocked(useAssistantAvailability).mockReturnValue({
190+
hasSearchAILakeConfigurations: false,
191+
hasAssistantPrivilege: false,
192+
hasConnectorsAllPrivilege: true,
193+
hasConnectorsReadPrivilege: true,
194+
hasUpdateAIAssistantAnonymization: true,
195+
hasManageGlobalKnowledgeBase: true,
196+
isAssistantEnabled: true,
197+
isAssistantVisible: true,
198+
});
199+
200+
const result = renderUseAssetInventoryAssistant();
201+
202+
expect(result.result.current.promptContextId).toEqual('');
203+
expect(result.result.current.showAssistant).toEqual(false);
204+
});
205+
});

0 commit comments

Comments
 (0)