Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: tygao <tygao@amazon.com>
  • Loading branch information
raintygao committed Sep 20, 2024
1 parent 764156f commit 3a1c7c6
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 4 deletions.
135 changes: 135 additions & 0 deletions public/components/incontext_insight/generate_popover_body.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ import { GeneratePopoverBody } from './generate_popover_body';
import { HttpSetup } from '../../../../../src/core/public';
import { SUMMARY_ASSISTANT_API } from '../../../common/constants/llm';
import { usageCollectionPluginMock } from '../../../../../src/plugins/usage_collection/public/mocks';
import { coreMock } from '../../../../../src/core/public/mocks';
import { dataPluginMock } from '../../../../../src/plugins/data/public/mocks';

jest.mock('../../services');

jest.mock('../../utils', () => ({
createIndexPatterns: jest.fn().mockResolvedValue('index pattern'),
buildUrlQuery: jest.fn().mockResolvedValue('query'),
}));

const mockToasts = {
addDanger: jest.fn(),
};
Expand All @@ -33,6 +40,36 @@ const mockHttpSetup: HttpSetup = ({
post: mockPost,
} as unknown) as HttpSetup; // Mocking HttpSetup

const mockDSL = `{
"query": {
"bool": {
"filter": [
{
"range": {
"timestamp": {
"from": "2024-09-06T04:02:52||-1h",
"to": "2024-09-06T04:02:52",
"include_lower": true,
"include_upper": true,
"boost": 1
}
}
},
{
"term": {
"FlightDelay": {
"value": "true",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
}`;

describe('GeneratePopoverBody', () => {
const incontextInsightMock = {
contextProvider: jest.fn(),
Expand Down Expand Up @@ -240,4 +277,102 @@ describe('GeneratePopoverBody', () => {
// insight tip icon is not visible for this alert
expect(screen.queryAllByLabelText('How was this generated?')).toHaveLength(0);
});

it('should not display discover link if monitor type is not query_level_monitor or bucket_level_monitor', async () => {
incontextInsightMock.contextProvider = jest.fn().mockResolvedValue({
additionalInfo: {
dsl: mockDSL,
index: 'mock_index',
dataSourceId: `test-data-source-id`,
monitorType: 'mock_type',
},
});
mockPost.mockImplementation((path: string, body) => {
let value;
switch (path) {
case SUMMARY_ASSISTANT_API.SUMMARIZE:
value = {
summary: 'Generated summary content',
insightAgentIdExists: true,
};
break;

case SUMMARY_ASSISTANT_API.INSIGHT:
value = 'Generated insight content';
break;

default:
return null;
}
return Promise.resolve(value);
});

const { queryByText } = render(
<GeneratePopoverBody
incontextInsight={incontextInsightMock}
httpSetup={mockHttpSetup}
closePopover={closePopoverMock}
/>
);

await waitFor(() => {
expect(queryByText('Discover details')).not.toBeInTheDocument();
});
});

it('handle navigate to discover after clicking link', async () => {
incontextInsightMock.contextProvider = jest.fn().mockResolvedValue({
additionalInfo: {
dsl: mockDSL,
index: 'mock_index',
dataSourceId: `test-data-source-id`,
monitorType: 'query_level_monitor',
},
});
mockPost.mockImplementation((path: string, body) => {
let value;
switch (path) {
case SUMMARY_ASSISTANT_API.SUMMARIZE:
value = {
summary: 'Generated summary content',
insightAgentIdExists: true,
};
break;

case SUMMARY_ASSISTANT_API.INSIGHT:
value = 'Generated insight content';
break;

default:
return null;
}
return Promise.resolve(value);
});

const coreStart = coreMock.createStart();
const dataStart = dataPluginMock.createStartContract();
const getStartServices = jest.fn().mockResolvedValue([
coreStart,
{
data: dataStart,
},
]);
const { getByText } = render(
<GeneratePopoverBody
incontextInsight={incontextInsightMock}
httpSetup={mockHttpSetup}
closePopover={closePopoverMock}
getStartServices={getStartServices}
/>
);

await waitFor(() => {
const button = getByText('Discover details');
expect(button).toBeInTheDocument();
fireEvent.click(button);
expect(coreStart.application.navigateToUrl).toHaveBeenCalledWith(
'data-explorer/discover#?query'
);
});
});
});
5 changes: 2 additions & 3 deletions public/components/incontext_insight/generate_popover_body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const GeneratePopoverBody: React.FC<{
defaultMessage: 'Generate summary error',
})
);
// closePopover();
closePopover();
});
};

Expand Down Expand Up @@ -181,7 +181,6 @@ export const GeneratePopoverBody: React.FC<{

const handleNavigateToDiscover = async () => {
const context = await incontextInsight?.contextProvider?.();
console.log('handleNavigateToDiscover context', context);
const dsl = context?.additionalInfo?.dsl;
const indexName = context?.additionalInfo?.index;
if (!dsl || !indexName) return;
Expand Down Expand Up @@ -316,7 +315,7 @@ export const GeneratePopoverBody: React.FC<{
{displayDiscoverButton && (
<EuiButton onClick={handleNavigateToDiscover}>
{i18n.translate('assistantDashboards.incontextInsight.discover', {
defaultMessage: 'Deep dive in Discover',
defaultMessage: 'Discover details',
})}
</EuiButton>
)}
Expand Down
1 change: 0 additions & 1 deletion public/utils/alerting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export const createIndexPatterns = async (
id: dataSourceId,
}
: undefined;
console.log('dataSourceRef', dataSourceId, dataSourceRef);
try {
pattern = await dataStart.indexPatterns.createAndSave({
id: '',
Expand Down

0 comments on commit 3a1c7c6

Please sign in to comment.