Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

824 streamline frontend change level network calls #839

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ffc1f60
deleted console log
pmarsh-scottlogic Feb 22, 2024
dcb9546
adds backend endpoint for get level and renames the request type
pmarsh-scottlogic Feb 22, 2024
e15ff88
renames levelService to resetService
pmarsh-scottlogic Feb 22, 2024
a1dec07
adds load level service to frontend
pmarsh-scottlogic Feb 22, 2024
2774b9c
adds levelSErvice to servvice index
pmarsh-scottlogic Feb 22, 2024
46f8f17
replaces ssitch level calls with new service method
pmarsh-scottlogic Feb 22, 2024
2ba78d6
removes unused getCHatHistory endpoint
pmarsh-scottlogic Feb 22, 2024
7f9ffd7
removes unused get defence endpoint
pmarsh-scottlogic Feb 22, 2024
e9410e9
removes endpoint for getting emails
pmarsh-scottlogic Feb 22, 2024
9edadbc
lil' tidy
pmarsh-scottlogic Feb 22, 2024
a2f73aa
deletes obsolete tests
pmarsh-scottlogic Feb 22, 2024
372aae5
start skeleton for startController test
pmarsh-scottlogic Feb 23, 2024
3d5c363
adds getStart test
pmarsh-scottlogic Feb 23, 2024
167d337
adds handling for when there is no level provided
pmarsh-scottlogic Feb 23, 2024
c9192cd
writes startController tests
pmarsh-scottlogic Feb 23, 2024
ba9f9c6
adds unit test for levelController
pmarsh-scottlogic Feb 23, 2024
14119c2
move res.status and res.send to one line
pmarsh-scottlogic Feb 27, 2024
5e3301b
removes redundant LevelState declaration from session module declaration
pmarsh-scottlogic Feb 27, 2024
e7e36f5
Extract espress-session typings
chriswilty Mar 1, 2024
c59a542
remove trailing slash
pmarsh-scottlogic Mar 1, 2024
fb4eee8
merge dev
pmarsh-scottlogic Mar 1, 2024
4c27c3f
adds test for invalid level
pmarsh-scottlogic Mar 1, 2024
76833b9
corrects expect statement
pmarsh-scottlogic Mar 1, 2024
c755895
adds isValidLevel method
pmarsh-scottlogic Mar 1, 2024
63b14b7
tests all levels for levelController
pmarsh-scottlogic Mar 1, 2024
7ab2845
adds validation and test for invalid level for startController
pmarsh-scottlogic Mar 1, 2024
b80d2b5
tests startController across all levels
pmarsh-scottlogic Mar 1, 2024
96531e5
corrects test names
pmarsh-scottlogic Mar 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 1 addition & 17 deletions backend/src/controller/chatController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { OpenAiAddInfoToChatHistoryRequest } from '@src/models/api/OpenAiAddInfoToChatHistoryRequest';
import { OpenAiChatRequest } from '@src/models/api/OpenAiChatRequest';
import { OpenAiClearRequest } from '@src/models/api/OpenAiClearRequest';
import { OpenAiGetHistoryRequest } from '@src/models/api/OpenAiGetHistoryRequest';
import {
DefenceReport,
ChatHttpResponse,
Expand Down Expand Up @@ -359,16 +358,6 @@ function addErrorToChatHistory(
});
}

function handleGetChatHistory(req: OpenAiGetHistoryRequest, res: Response) {
const level: number | undefined = req.query.level as number | undefined;
if (level !== undefined) {
res.send(req.session.levelState[level].chatHistory);
} else {
res.status(400);
res.send('Missing level');
}
}

function handleAddInfoToChatHistory(
req: OpenAiAddInfoToChatHistoryRequest,
res: Response
Expand Down Expand Up @@ -407,9 +396,4 @@ function handleClearChatHistory(req: OpenAiClearRequest, res: Response) {
}
}

export {
handleChatToGPT,
handleGetChatHistory,
handleAddInfoToChatHistory as handleAddInfoToChatHistory,
handleClearChatHistory,
};
export { handleChatToGPT, handleAddInfoToChatHistory, handleClearChatHistory };
12 changes: 0 additions & 12 deletions backend/src/controller/defenceController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import { DefenceActivateRequest } from '@src/models/api/DefenceActivateRequest';
import { DefenceConfigResetRequest } from '@src/models/api/DefenceConfigResetRequest';
import { DefenceConfigureRequest } from '@src/models/api/DefenceConfigureRequest';
import { DefenceStatusRequest } from '@src/models/api/DefenceStatusRequest';
import { DefenceConfigItem } from '@src/models/defence';
import { LEVEL_NAMES } from '@src/models/level';

Expand Down Expand Up @@ -112,20 +111,9 @@ function handleResetSingleDefence(
}
}

function handleGetDefenceStatus(req: DefenceStatusRequest, res: Response) {
const level: number | undefined = req.query.level as number | undefined;
if (level !== undefined) {
res.send(req.session.levelState[level].defences);
} else {
res.status(400);
res.send('Missing level');
}
}

export {
handleDefenceActivation,
handleDefenceDeactivation,
handleConfigureDefence,
handleResetSingleDefence,
handleGetDefenceStatus,
};
13 changes: 1 addition & 12 deletions backend/src/controller/emailController.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import { Response } from 'express';

import { EmailClearRequest } from '@src/models/api/EmailClearRequest';
import { EmailGetRequest } from '@src/models/api/EmailGetRequest';
import { LEVEL_NAMES } from '@src/models/level';

function handleGetEmails(req: EmailGetRequest, res: Response) {
const level: number | undefined = req.query.level as number | undefined;
if (level !== undefined) {
res.send(req.session.levelState[level].sentEmails);
} else {
res.status(400);
res.send('Missing level');
}
}

function handleClearEmails(req: EmailClearRequest, res: Response) {
const level = req.body.level;
if (level !== undefined && level >= LEVEL_NAMES.LEVEL_1) {
Expand All @@ -26,4 +15,4 @@ function handleClearEmails(req: EmailClearRequest, res: Response) {
}
}

export { handleGetEmails, handleClearEmails };
export { handleClearEmails };
15 changes: 15 additions & 0 deletions backend/src/controller/levelController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Response } from 'express';

import { LevelGetRequest } from '@src/models/api/LevelGetRequest';

function handleLoadLevel(req: LevelGetRequest, res: Response) {
const { level } = req.query;

res.send({
emails: req.session.levelState[level].sentEmails,
chatHistory: req.session.levelState[level].chatHistory,
defences: req.session.levelState[level].defences,
});
}

export { handleLoadLevel };
4 changes: 2 additions & 2 deletions backend/src/controller/startController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Response } from 'express';

import { GetStartRequest } from '@src/models/api/getStartRequest';
import { StartGetRequest } from '@src/models/api/StartGetRequest';
import { LEVEL_NAMES } from '@src/models/level';
import { getValidOpenAIModels } from '@src/openai';
import {
Expand All @@ -9,7 +9,7 @@ import {
systemRoleLevel3,
} from '@src/promptTemplates';

function handleStart(req: GetStartRequest, res: Response) {
function handleStart(req: StartGetRequest, res: Response) {
pmarsh-scottlogic marked this conversation as resolved.
Show resolved Hide resolved
const { level } = req.query;

const systemRoles = [
Expand Down
12 changes: 0 additions & 12 deletions backend/src/models/api/DefenceStatusRequest.ts

This file was deleted.

13 changes: 0 additions & 13 deletions backend/src/models/api/EmailGetRequest.ts

This file was deleted.

19 changes: 19 additions & 0 deletions backend/src/models/api/LevelGetRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Request } from 'express';

import { ChatMessage } from '@src/models/chatMessage';
import { Defence } from '@src/models/defence';
import { EmailInfo } from '@src/models/email';
import { LEVEL_NAMES } from '@src/models/level';

export type LevelGetRequest = Request<
never,
{
emails: EmailInfo[];
chatHistory: ChatMessage[];
defences: Defence[];
},
never,
{
level: LEVEL_NAMES;
}
>;
12 changes: 0 additions & 12 deletions backend/src/models/api/OpenAiGetHistoryRequest.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Defence } from '@src/models/defence';
import { EmailInfo } from '@src/models/email';
import { LEVEL_NAMES } from '@src/models/level';

export type GetStartRequest = Request<
export type StartGetRequest = Request<
never,
{
emails: EmailInfo[];
Expand Down
13 changes: 4 additions & 9 deletions backend/src/sessionRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ import memoryStoreFactory from 'memorystore';

import {
handleChatToGPT,
handleGetChatHistory,
handleAddInfoToChatHistory,
handleClearChatHistory,
} from './controller/chatController';
import {
handleConfigureDefence,
handleDefenceActivation,
handleDefenceDeactivation,
handleGetDefenceStatus,
handleResetSingleDefence,
} from './controller/defenceController';
import {
handleClearEmails,
handleGetEmails,
} from './controller/emailController';
import { handleClearEmails } from './controller/emailController';
import { handleLoadLevel } from './controller/levelController';
import {
handleConfigureModel,
handleGetModel,
Expand Down Expand Up @@ -97,19 +93,18 @@ router.use((req, _res, next) => {
// handshake
router.get('/start', handleStart);

router.get('/level', handleLoadLevel);

// defences
router.get('/defence/status', handleGetDefenceStatus);
router.post('/defence/activate', handleDefenceActivation);
router.post('/defence/deactivate', handleDefenceDeactivation);
router.post('/defence/configure', handleConfigureDefence);
router.post('/defence/resetConfig', handleResetSingleDefence);

// emails
router.get('/email/get', handleGetEmails);
router.post('/email/clear', handleClearEmails);

// chat
router.get('/openai/history', handleGetChatHistory);
router.post('/openai/chat', handleChatToGPT);
router.post('/openai/addInfoToHistory', handleAddInfoToChatHistory);
router.post('/openai/clear', handleClearChatHistory);
Expand Down
51 changes: 0 additions & 51 deletions backend/test/unit/controller/chatController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import {
handleAddInfoToChatHistory,
handleChatToGPT,
handleClearChatHistory,
handleGetChatHistory,
} from '@src/controller/chatController';
import { detectTriggeredInputDefences, transformMessage } from '@src/defence';
import { OpenAiAddInfoToChatHistoryRequest } from '@src/models/api/OpenAiAddInfoToChatHistoryRequest';
import { OpenAiChatRequest } from '@src/models/api/OpenAiChatRequest';
import { OpenAiClearRequest } from '@src/models/api/OpenAiClearRequest';
import { OpenAiGetHistoryRequest } from '@src/models/api/OpenAiGetHistoryRequest';
import {
DefenceReport,
ChatModel,
Expand Down Expand Up @@ -741,55 +739,6 @@ describe('handleChatToGPT unit tests', () => {
});
});

describe('handleGetChatHistory', () => {
pmarsh-scottlogic marked this conversation as resolved.
Show resolved Hide resolved
function getRequestMock(level?: LEVEL_NAMES, chatHistory?: ChatMessage[]) {
return {
query: {
level: level ?? undefined,
},
session: {
levelState: [
{
chatHistory: chatHistory ?? [],
},
],
},
} as OpenAiGetHistoryRequest;
}

const chatHistory: ChatMessage[] = [
{
completion: { role: 'system', content: 'You are a helpful chatbot' },
chatMessageType: 'SYSTEM',
},
{
completion: { role: 'assistant', content: 'Hello human' },
chatMessageType: 'BOT',
},
{
completion: { role: 'user', content: 'How are you?' },
chatMessageType: 'USER',
},
];
test('GIVEN a valid level WHEN handleGetChatHistory called THEN return chat history', () => {
const req = getRequestMock(LEVEL_NAMES.LEVEL_1, chatHistory);
const res = responseMock();

handleGetChatHistory(req, res);
expect(res.send).toHaveBeenCalledWith(chatHistory);
});

test('GIVEN undefined level WHEN handleGetChatHistory called THEN return 400', () => {
const req = getRequestMock();
const res = responseMock();

handleGetChatHistory(req, res);

expect(res.status).toHaveBeenCalledWith(400);
expect(res.send).toHaveBeenCalledWith('Missing level');
});
});

describe('handleAddInfoToChatHistory', () => {
function getAddInfoToChatHistoryRequestMock(
infoMessage: string,
Expand Down
41 changes: 1 addition & 40 deletions backend/test/unit/controller/emailController.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { expect, test, jest, describe } from '@jest/globals';
import { Response } from 'express';

import {
handleClearEmails,
handleGetEmails,
} from '@src/controller/emailController';
import { handleClearEmails } from '@src/controller/emailController';
import { EmailClearRequest } from '@src/models/api/EmailClearRequest';
import { EmailGetRequest } from '@src/models/api/EmailGetRequest';
import { ChatModel } from '@src/models/chat';
import { ChatMessage } from '@src/models/chatMessage';
import { Defence } from '@src/models/defence';
Expand Down Expand Up @@ -47,41 +43,6 @@ const emails: EmailInfo[] = [
},
];

describe('handleGetEmails', () => {
test('GIVEN valid level WHEN handleGetEmails called THEN returns sent emails', () => {
const req = {
query: {
level: 0,
},
session: {
levelState: [
{
sentEmails: emails,
},
],
},
} as EmailGetRequest;

const res = responseMock();

handleGetEmails(req, res);

expect(res.send).toHaveBeenCalledWith(emails);
});

test('GIVEN missing level WHEN handleGetEmails called THEN returns 400 ', () => {
const req = {
query: {},
} as EmailGetRequest;

const res = responseMock();
handleGetEmails(req, res);

expect(res.status).toHaveBeenCalledWith(400);
expect(res.send).toHaveBeenCalledWith('Missing level');
});
});

describe('handleClearEmails', () => {
test('GIVEN valid level WHEN handleClearEmails called THEN sets emails to empty', () => {
const req = {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import MissionInformation from './components/Overlay/MissionInformation';
import OverlayWelcome from './components/Overlay/OverlayWelcome';
import ResetProgressOverlay from './components/Overlay/ResetProgress';
import { LEVEL_NAMES } from './models/level';
import { levelService } from './service';
import { resetService } from './service';
chriswilty marked this conversation as resolved.
Show resolved Hide resolved

import './App.css';
import './Theme.css';
Expand Down Expand Up @@ -205,7 +205,7 @@ function App() {
console.log('resetting progress for all levels');

// reset on the backend
await levelService.resetAllLevelProgress();
await resetService.resetAllLevelProgress();

localStorage.setItem('numCompletedLevels', '0');
setNumCompletedLevels(0);
Expand Down
Loading
Loading