Skip to content

Commit

Permalink
tests fix
Browse files Browse the repository at this point in the history
  • Loading branch information
deekshas8 committed Aug 7, 2024
1 parent 707bc56 commit bba2f34
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
7 changes: 6 additions & 1 deletion packages/gen-ai-hub/src/client/openai/openai-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import nock from 'nock';
import { jest } from '@jest/globals';
import { HttpDestination } from '@sap-cloud-sdk/connectivity';
import { mockGetAiCoreDestination } from '../../test-util/mock-context.js';
import {
BaseLlmParametersWithDeploymentId,
EndpointOptions
} from '../../core/http-client.js';
import { mockInference, parseMockResponse } from '../../test-util/mock-http.js';
import { OpenAiClient } from './openai-client.js';
import {
OpenAiChatCompletionOutput,
OpenAiChatCompletionParameters,
OpenAiChatMessage,
OpenAiEmbeddingOutput,
OpenAiEmbeddingParameters
} from './openai-types.js';
const { OpenAiClient } = await import('./openai-client.js');

describe('openai client', () => {
let destination: HttpDestination;
Expand Down Expand Up @@ -41,6 +42,10 @@ describe('openai client', () => {
nock.cleanAll();
});

afterAll(() => {
jest.restoreAllMocks();
});

describe('chatCompletion', () => {
it('parses a successful response', async () => {
const prompt = {
Expand Down
7 changes: 6 additions & 1 deletion packages/gen-ai-hub/src/core/http-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { HttpDestination } from '@sap-cloud-sdk/connectivity';
import { jest } from '@jest/globals';
import { mockGetAiCoreDestination } from '../test-util/mock-context.js';
import { mockInference } from '../test-util/mock-http.js';
import { executeRequest } from './http-client.js';
const { executeRequest } = await import('./http-client.js');

describe('http-client', () => {
let destination: HttpDestination;
Expand All @@ -10,6 +11,10 @@ describe('http-client', () => {
destination = mockGetAiCoreDestination();
});

afterAll(() => {
jest.restoreAllMocks();
});

it('should execute a request to the AI Core service', async () => {
const mockPrompt = { prompt: 'some test prompt' };
const mockPromptResponse = { completion: 'some test completion' };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
import nock from 'nock';
import { jest } from '@jest/globals';
import { HttpDestination } from '@sap-cloud-sdk/connectivity';
import { mockGetAiCoreDestination } from '../test-util/mock-context.js';
import { mockInference, parseMockResponse } from '../test-util/mock-http.js';
import { BaseLlmParametersWithDeploymentId } from '../core/index.js';
import {
GenAiHubClient,
constructCompletionPostRequest
} from './orchestration-client.js';
const { GenAiHubClient, constructCompletionPostRequest } = await import(
'./orchestration-client.js'
);
import { CompletionPostResponse } from './api/index.js';
import { GenAiHubCompletionParameters } from './orchestration-types.js';

describe('GenAiHubClient', () => {
let destination: HttpDestination;
let client: GenAiHubClient;
const client = new GenAiHubClient();
const deploymentConfiguration: BaseLlmParametersWithDeploymentId = {
deploymentId: 'deployment-id'
};

beforeAll(() => {
destination = mockGetAiCoreDestination();
client = new GenAiHubClient();
});

afterEach(() => {
nock.cleanAll();
});

afterAll(() => {
jest.restoreAllMocks();
});

it('calls chatCompletion with minimum configuration and parses response', async () => {
const request: GenAiHubCompletionParameters = {
deploymentConfiguration,
Expand Down
13 changes: 7 additions & 6 deletions packages/gen-ai-hub/src/test-util/mock-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import {
ServiceCredentials
} from '@sap-cloud-sdk/connectivity';
import nock from 'nock';
import * as context from '../core/context.js';
import { jest } from '@jest/globals';

jest.unstable_mockModule('../core/context.js', () => ({
getAiCoreDestination: jest.fn(() =>
Promise.resolve(mockGetAiCoreDestination())
)
}));

export const aiCoreServiceBinding = {
label: 'aicore',
Expand Down Expand Up @@ -54,11 +60,6 @@ export function mockGetAiCoreDestination(
authentication: 'OAuth2ClientCredentials',
...createDestinationTokens()
};

jest
.spyOn(context, 'getAiCoreDestination')
.mockResolvedValue(mockDestination);

return mockDestination;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/gen-ai-hub/src/test-util/mock-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function mockInference<D extends BaseLlmParameters>(

export function parseMockResponse<T>(client: string, fileName: string): T {
const fileContent = fs.readFileSync(
path.join('test-util', 'mock-data', client, fileName),
path.join('src', 'test-util', 'mock-data', client, fileName),
'utf-8'
);

Expand Down
2 changes: 0 additions & 2 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ packages:
# e2e tests
- 'tests/e2e-tests'
# exclude packages that are inside test directories
- '!poc/'
- '!test-util'
- '!tests/type-tests'

0 comments on commit bba2f34

Please sign in to comment.