Skip to content

Commit

Permalink
Reverts to http.fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
spong committed Jan 31, 2024
1 parent 6856015 commit 2d4161f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { API_ERROR } from '../../translations';
jest.mock('@kbn/core-http-browser');

const mockHttp = {
get: jest.fn(),
fetch: jest.fn(),
} as unknown as HttpSetup;

describe('Capabilities API tests', () => {
Expand All @@ -25,14 +25,15 @@ describe('Capabilities API tests', () => {
it('calls the internal assistant API for fetching assistant capabilities', async () => {
await getCapabilities({ http: mockHttp });

expect(mockHttp.get).toHaveBeenCalledWith('/internal/elastic_assistant/capabilities', {
expect(mockHttp.fetch).toHaveBeenCalledWith('/internal/elastic_assistant/capabilities', {
method: 'GET',
signal: undefined,
version: '1',
});
});

it('returns API_ERROR when the response status is error', async () => {
(mockHttp.get as jest.Mock).mockResolvedValue({ status: API_ERROR });
(mockHttp.fetch as jest.Mock).mockResolvedValue({ status: API_ERROR });

const result = await getCapabilities({ http: mockHttp });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { HttpSetup, IHttpFetchError } from '@kbn/core-http-browser';
import { API_VERSIONS, GetCapabilitiesResponse } from '@kbn/elastic-assistant-common';
import { GetCapabilitiesResponse } from '@kbn/elastic-assistant-common';

export interface GetCapabilitiesParams {
http: HttpSetup;
Expand All @@ -29,10 +29,13 @@ export const getCapabilities = async ({
try {
const path = `/internal/elastic_assistant/capabilities`;

return await http.get<GetCapabilitiesResponse>(path, {
const response = await http.fetch(path, {
method: 'GET',
signal,
version: API_VERSIONS.internal.v1,
version: '1',
});

return response as GetCapabilitiesResponse;
} catch (error) {
return error as IHttpFetchError;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import type { ReactNode } from 'react';
import React from 'react';
import { useCapabilities, UseCapabilitiesParams } from './use_capabilities';
import { API_VERSIONS } from '@kbn/elastic-assistant-common';

const statusResponse = { assistantModelEvaluation: true, assistantStreamingEnabled: false };

const http = {
get: jest.fn().mockResolvedValue(statusResponse),
fetch: jest.fn().mockResolvedValue(statusResponse),
};
const toasts = {
addError: jest.fn(),
Expand All @@ -37,10 +36,14 @@ describe('useFetchRelatedCases', () => {
wrapper: createWrapper(),
});

expect(defaultProps.http.get).toHaveBeenCalledWith('/internal/elastic_assistant/capabilities', {
version: API_VERSIONS.internal.v1,
signal: new AbortController().signal,
});
expect(defaultProps.http.fetch).toHaveBeenCalledWith(
'/internal/elastic_assistant/capabilities',
{
method: 'GET',
version: '1',
signal: new AbortController().signal,
}
);
expect(toasts.addError).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@

import { postEvaluation } from './evaluate';
import { HttpSetup } from '@kbn/core-http-browser';
import { API_VERSIONS } from '@kbn/elastic-assistant-common';

jest.mock('@kbn/core-http-browser');

const mockHttp = {
post: jest.fn(),
fetch: jest.fn(),
} as unknown as HttpSetup;

describe('postEvaluation', () => {
it('calls the knowledge base API when correct resource path', async () => {
(mockHttp.post as jest.Mock).mockResolvedValue({ success: true });
(mockHttp.fetch as jest.Mock).mockResolvedValue({ success: true });
const testProps = {
http: mockHttp,
evalParams: {
Expand All @@ -36,7 +35,8 @@ describe('postEvaluation', () => {

await postEvaluation(testProps);

expect(mockHttp.post).toHaveBeenCalledWith('/internal/elastic_assistant/evaluate', {
expect(mockHttp.fetch).toHaveBeenCalledWith('/internal/elastic_assistant/evaluate', {
method: 'POST',
body: '{"dataset":{},"evalPrompt":"evalPrompt"}',
headers: { 'Content-Type': 'application/json' },
query: {
Expand All @@ -50,12 +50,11 @@ describe('postEvaluation', () => {
runName: 'Test Run Name',
},
signal: undefined,
version: API_VERSIONS.internal.v1,
});
});
it('returns error when error is an error', async () => {
const error = 'simulated error';
(mockHttp.post as jest.Mock).mockImplementation(() => {
(mockHttp.fetch as jest.Mock).mockImplementation(() => {
throw new Error(error);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ export const postEvaluation = async ({
runName: evalParams?.runName,
};

return await http.post<PostEvaluateResponse>(path, {
const response = await http.fetch(path, {
method: 'POST',
body: JSON.stringify({
dataset: JSON.parse(evalParams?.dataset ?? '[]'),
evalPrompt: evalParams?.evalPrompt ?? '',
Expand All @@ -57,8 +58,9 @@ export const postEvaluation = async ({
},
query,
signal,
version: API_VERSIONS.internal.v1,
});

return response as PostEvaluateResponse;
} catch (error) {
return error as IHttpFetchError;
}
Expand All @@ -85,7 +87,8 @@ export const getEvaluation = async ({
try {
const path = `/internal/elastic_assistant/evaluate`;

return await http.get<GetEvaluateResponse>(path, {
return await http.fetch<GetEvaluateResponse>(path, {
method: 'GET',
signal,
version: API_VERSIONS.internal.v1,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import { postEvaluateRoute } from './post_evaluate';
import { serverMock } from '../../__mocks__/server';
import { requestContextMock } from '../../__mocks__/request_context';
import { getPostEvaluateRequest } from '../../__mocks__/request';
import type {
PostEvaluateRequestBodyInput,
PostEvaluateRequestQueryInput,
} from '@kbn/elastic-assistant-common';

const defaultBody = {
const defaultBody: PostEvaluateRequestBodyInput = {
dataset: undefined,
evalPrompt: undefined,
};

const defaultQueryParams = {
const defaultQueryParams: PostEvaluateRequestQueryInput = {
agents: 'agents',
datasetName: undefined,
evaluationType: undefined,
Expand All @@ -26,7 +30,7 @@ const defaultQueryParams = {
runName: undefined,
};

describe.skip('Post Evaluate Route', () => {
describe('Post Evaluate Route', () => {
let server: ReturnType<typeof serverMock.create>;
let { context } = requestContextMock.createTools();
const mockGetElser = jest.fn().mockResolvedValue('.elser_model_2');
Expand Down

0 comments on commit 2d4161f

Please sign in to comment.