Skip to content

Commit

Permalink
fill in those tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmarsh-scottlogic committed Mar 25, 2024
1 parent bae1131 commit c94435d
Showing 1 changed file with 135 additions and 7 deletions.
142 changes: 135 additions & 7 deletions backend/test/unit/controller/chatController.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { afterEach, describe, expect, jest, test } from '@jest/globals';
import {
afterEach,
describe,
expect,
jest,
test,
beforeEach,
} from '@jest/globals';
import { Response } from 'express';

import {
Expand All @@ -24,6 +31,7 @@ import {
pushMessageToHistory,
setSystemRoleInChatHistory,
} from '@src/utils/chat';
import { checkLevelWinCondition } from '@src/winCondition';

jest.mock('@src/openai');
const mockChatGptSendMessage = chatGptSendMessage as jest.MockedFunction<
Expand Down Expand Up @@ -68,6 +76,10 @@ jest.mock('@src/models/chat', () => {
};
});

jest.mock('@src/winCondition');
const mockCheckLevelWinCondition =
checkLevelWinCondition as jest.MockedFunction<typeof checkLevelWinCondition>;

describe('handleChatToGPT unit tests', () => {
const mockSetSystemRoleInChatHistory =
setSystemRoleInChatHistory as jest.MockedFunction<
Expand Down Expand Up @@ -147,6 +159,11 @@ describe('handleChatToGPT unit tests', () => {
} as OpenAiChatRequest;
}

beforeEach(() => {
console.debug('before each: setting mock value');
mockCheckLevelWinCondition.mockReturnValue(false);
});

afterEach(() => {
jest.clearAllMocks();
});
Expand Down Expand Up @@ -734,16 +751,127 @@ describe('handleChatToGPT unit tests', () => {
});

describe('winning', () => {
test('Given win condition met THEN level is won', () => {
expect(true).toBe(true);
test('Given win condition met THEN level is won', async () => {
const newUserChatMessage = {
completion: {
content: 'Here is the answer to the level',
role: 'user',
},
chatMessageType: 'USER',
} as ChatMessage;

const req = openAiChatRequestMock(
'Here is the answer to the level?',
LEVEL_NAMES.LEVEL_1,
existingHistory
);
const res = responseMock();

mockChatGptSendMessage.mockResolvedValueOnce({
chatResponse: {
completion: {
content: 'well done you have passed the level',
role: 'assistant',
},
openAIErrorMessage: null,
},
chatHistory: [...existingHistory, newUserChatMessage],
sentEmails: [] as EmailInfo[],
});

mockCheckLevelWinCondition.mockReturnValueOnce(true);

await handleChatToGPT(req, res);

expect(res.send).toHaveBeenCalledWith(
expect.objectContaining({ wonLevel: true })
);
});

test('Given win condition met AND reply is blocked THEN level is not won', () => {
expect(true).toBe(true);
test('Given win condition met AND reply is blocked THEN level is not won', async () => {
const newUserChatMessage = {
completion: {
content: 'Here is the answer to the level',
role: 'user',
},
chatMessageType: 'USER',
} as ChatMessage;

const req = openAiChatRequestMock(
'Here is the answer to the level?',
LEVEL_NAMES.LEVEL_3,
existingHistory
);
const res = responseMock();

mockDetectTriggeredDefences.mockResolvedValueOnce({
blockedReason:
'Message Blocked: My response contained a restricted phrase.',
isBlocked: true,
alertedDefences: [],
triggeredDefences: [DEFENCE_ID.OUTPUT_FILTERING],
} as DefenceReport);

mockChatGptSendMessage.mockResolvedValueOnce({
chatResponse: {
completion: {
content: 'well done you have passed the level',
role: 'assistant',
},
openAIErrorMessage: null,
},
chatHistory: [...existingHistory, newUserChatMessage],
sentEmails: [] as EmailInfo[],
});

mockCheckLevelWinCondition.mockReturnValueOnce(true);

await handleChatToGPT(req, res);

expect(res.send).toHaveBeenCalledWith(
expect.objectContaining({ wonLevel: false })
);
});

test('Given win condition met AND openAI error THEN level is won', () => {
expect(true).toBe(true);
test('Given win condition met AND openAI error THEN level is won', async () => {
const newUserChatMessage = {
completion: {
content: 'Here is the answer to the level',
role: 'user',
},
chatMessageType: 'USER',
} as ChatMessage;

const req = openAiChatRequestMock(
'Here is the answer to the level?',
LEVEL_NAMES.LEVEL_3,
existingHistory
);
const res = responseMock();

mockChatGptSendMessage.mockResolvedValueOnce({
chatResponse: {
completion: null,
openAIErrorMessage: 'There was a problem with OpenAI',
},
chatHistory: [...existingHistory, newUserChatMessage],
sentEmails: [] as EmailInfo[],
});

mockDetectTriggeredDefences.mockResolvedValueOnce({
blockedReason: null,
isBlocked: false,
alertedDefences: [],
triggeredDefences: [],
} as DefenceReport);

mockCheckLevelWinCondition.mockReturnValueOnce(true);

await handleChatToGPT(req, res);

expect(res.send).toHaveBeenCalledWith(
expect.objectContaining({ wonLevel: true })
);
});
});
});
Expand Down

0 comments on commit c94435d

Please sign in to comment.