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 9 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
6 changes: 6 additions & 0 deletions backend/src/controller/levelController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Response } from 'express';

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

function handleLoadLevel(req: LevelGetRequest, res: Response) {
const { level } = req.query;
Expand All @@ -10,6 +11,11 @@ function handleLoadLevel(req: LevelGetRequest, res: Response) {
return;
}

if (!isValidLevel(level)) {
res.status(400).send('Invalid level');
return;
}

res.send({
emails: req.session.levelState[level].sentEmails,
chatHistory: req.session.levelState[level].chatHistory,
Expand Down
16 changes: 6 additions & 10 deletions backend/src/controller/startController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Response } from 'express';

import { StartGetRequest } from '@src/models/api/StartGetRequest';
import { ChatModel } from '@src/models/chat';
import { LEVEL_NAMES, LevelState } from '@src/models/level';
import { LEVEL_NAMES, isValidLevel } from '@src/models/level';
import { getValidOpenAIModels } from '@src/openai';
import {
systemRoleLevel1,
Expand All @@ -12,14 +11,6 @@ import {

import { sendErrorResponse } from './handleError';

declare module 'express-session' {
interface Session {
initialised: boolean;
chatModel: ChatModel;
levelState: LevelState[];
}
}

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

Expand All @@ -28,6 +19,11 @@ function handleStart(req: StartGetRequest, res: Response) {
return;
}

if (!isValidLevel(level)) {
sendErrorResponse(res, 400, 'Invalid level');
return;
}

const systemRoles = [
{ level: LEVEL_NAMES.LEVEL_1, systemRole: systemRoleLevel1 },
{ level: LEVEL_NAMES.LEVEL_2, systemRole: systemRoleLevel2 },
Expand Down
6 changes: 5 additions & 1 deletion backend/src/models/level.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const LEVEL_NAMES = {

type LEVEL_NAMES = (typeof LEVEL_NAMES)[keyof typeof LEVEL_NAMES];

function isValidLevel(levelValue: unknown) {
return Object.values(LEVEL_NAMES).includes(levelValue as LEVEL_NAMES);
}

interface LevelState {
level: LEVEL_NAMES;
chatHistory: ChatMessage[];
Expand All @@ -32,5 +36,5 @@ function getInitialLevelStates() {
);
}

export { getInitialLevelStates, LEVEL_NAMES };
export { getInitialLevelStates, LEVEL_NAMES, isValidLevel };
export type { LevelState };
12 changes: 2 additions & 10 deletions backend/src/sessionRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,8 @@ import {
import { handleResetProgress } from './controller/resetController';
import { handleStart } from './controller/startController';
import { handleTest } from './controller/testController';
import { ChatModel, defaultChatModel } from './models/chat';
import { LevelState, getInitialLevelStates } from './models/level';

declare module 'express-session' {
interface Session {
initialised: boolean;
chatModel: ChatModel;
levelState: LevelState[];
}
}
import { defaultChatModel } from './models/chat';
import { getInitialLevelStates } from './models/level';

const sessionSigningSecret = process.env.SESSION_SECRET;
if (!sessionSigningSecret) {
Expand Down
9 changes: 0 additions & 9 deletions backend/test/integration/chatController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,12 @@ import { Response } from 'express';

import { handleChatToGPT } from '@src/controller/chatController';
import { OpenAiChatRequest } from '@src/models/api/OpenAiChatRequest';
import { ChatModel } from '@src/models/chat';
import { ChatMessage } from '@src/models/chatMessage';
import { Defence } from '@src/models/defence';
import { EmailInfo } from '@src/models/email';
import { LEVEL_NAMES, LevelState } from '@src/models/level';
import { systemRoleLevel1 } from '@src/promptTemplates';

declare module 'express-session' {
interface Session {
initialised: boolean;
chatModel: ChatModel;
levelState: LevelState[];
}
}

// mock the api call
const mockCreateChatCompletion =
jest.fn<
Expand Down
2 changes: 1 addition & 1 deletion backend/test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"noEmit": true
},
"include": ["./**/*.ts", "../jest.config.ts"]
"include": ["./**/*.ts", "../typings", "../jest.config.ts"]
}
9 changes: 0 additions & 9 deletions backend/test/unit/controller/chatController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { OpenAiChatRequest } from '@src/models/api/OpenAiChatRequest';
import { OpenAiClearRequest } from '@src/models/api/OpenAiClearRequest';
import {
DefenceReport,
ChatModel,
ChatResponse,
MessageTransformation,
} from '@src/models/chat';
Expand All @@ -26,14 +25,6 @@ import {
setSystemRoleInChatHistory,
} from '@src/utils/chat';

declare module 'express-session' {
interface Session {
initialised: boolean;
chatModel: ChatModel;
levelState: LevelState[];
}
}

jest.mock('@src/openai');
const mockChatGptSendMessage = chatGptSendMessage as jest.MockedFunction<
typeof chatGptSendMessage
Expand Down
11 changes: 1 addition & 10 deletions backend/test/unit/controller/defenceController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@ import { Response } from 'express';
import { handleConfigureDefence } from '@src/controller/defenceController';
import { configureDefence } from '@src/defence';
import { DefenceConfigureRequest } from '@src/models/api/DefenceConfigureRequest';
import { ChatModel } from '@src/models/chat';
import { ChatMessage } from '@src/models/chatMessage';
import { DEFENCE_ID, Defence } from '@src/models/defence';
import { EmailInfo } from '@src/models/email';
import { LEVEL_NAMES, LevelState } from '@src/models/level';

declare module 'express-session' {
interface Session {
initialised: boolean;
chatModel: ChatModel;
levelState: LevelState[];
}
}
import { LEVEL_NAMES } from '@src/models/level';

jest.mock('@src/defence');
const mockConfigureDefence = configureDefence as jest.MockedFunction<
Expand Down
10 changes: 0 additions & 10 deletions backend/test/unit/controller/emailController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@ import { Response } from 'express';

import { handleClearEmails } from '@src/controller/emailController';
import { EmailClearRequest } from '@src/models/api/EmailClearRequest';
import { ChatModel } from '@src/models/chat';
import { EmailInfo } from '@src/models/email';
import { LevelState } from '@src/models/level';

declare module 'express-session' {
interface Session {
initialised: boolean;
chatModel: ChatModel;
levelState: LevelState[];
}
}

function responseMock() {
return {
Expand Down
70 changes: 58 additions & 12 deletions backend/test/unit/controller/levelController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Response } from 'express';

import { handleLoadLevel } from '@src/controller/levelController';
import { LevelGetRequest } from '@src/models/api/LevelGetRequest';
import { LEVEL_NAMES } from '@src/models/level';

jest.mock('@src/promptTemplates', () => ({
systemRoleLevel1: 'systemRoleLevel1',
Expand All @@ -23,11 +24,59 @@ afterEach(() => {
mockSend.mockClear();
});

test('WHEN client asks to load level 1 THEN the backend sends the level information for the given level', () => {
[
LEVEL_NAMES.LEVEL_1,
LEVEL_NAMES.LEVEL_2,
LEVEL_NAMES.LEVEL_3,
LEVEL_NAMES.SANDBOX,
].forEach((level) => {
test(`GIVEN level ${
level + 1
} WHEN client asks to load the level THEN the backend sends the correct level information`, () => {
const req = {
query: {
level,
},
session: {
levelState: [
{
sentEmails: 'level 1 emails',
chatHistory: 'level 1 chat history',
defences: 'level 1 defences',
},
{
sentEmails: 'level 2 emails',
chatHistory: 'level 2 chat history',
defences: 'level 2 defences',
},
{
sentEmails: 'level 3 emails',
chatHistory: 'level 3 chat history',
defences: 'level 3 defences',
},
{
sentEmails: 'level 4 emails',
chatHistory: 'level 4 chat history',
defences: 'level 4 defences',
},
],
},
} as unknown as LevelGetRequest;
const res = responseMock();

handleLoadLevel(req, res);

expect(mockSend).toHaveBeenCalledWith({
emails: `level ${level + 1} emails`,
chatHistory: `level ${level + 1} chat history`,
defences: `level ${level + 1} defences`,
});
});
});

test('WHEN client does not provide a level THEN the backend sends the level information for the given level', () => {
const req = {
query: {
level: 1,
},
query: {},
session: {
levelState: [
{},
Expand All @@ -43,16 +92,13 @@ test('WHEN client asks to load level 1 THEN the backend sends the level informat

handleLoadLevel(req, res);

expect(mockSend).toHaveBeenCalledWith({
emails: [],
chatHistory: [],
defences: [],
});
expect(res.status).toHaveBeenCalledWith(400);
expect(mockSend).toHaveBeenCalledWith('Level not provided');
});

test('WHEN client does not provide a level THEN the backend sends the level information for the given level', () => {
test('WHEN client provides an invalid level THEN the backend sends the level information for the given level', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THEN the backend responds with BadRequest ?

const req = {
query: {},
query: { level: 5 },
session: {
levelState: [
{},
Expand All @@ -69,5 +115,5 @@ test('WHEN client does not provide a level THEN the backend sends the level info
handleLoadLevel(req, res);

expect(res.status).toHaveBeenCalledWith(400);
expect(mockSend).toHaveBeenCalledWith('Level not provided');
expect(mockSend).toHaveBeenCalledWith('Invalid level');
});
9 changes: 0 additions & 9 deletions backend/test/unit/controller/resetController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Request, Response } from 'express';

import { handleResetProgress } from '@src/controller/resetController';
import { defaultDefences } from '@src/defaultDefences';
import { ChatModel } from '@src/models/chat';
import { ChatMessage } from '@src/models/chatMessage';
import { DEFENCE_ID, Defence, DefenceConfigItem } from '@src/models/defence';
import { EmailInfo } from '@src/models/email';
Expand All @@ -13,14 +12,6 @@ import {
getInitialLevelStates,
} from '@src/models/level';

declare module 'express-session' {
interface Session {
initialised: boolean;
chatModel: ChatModel;
levelState: LevelState[];
}
}

function responseMock() {
return {
send: jest.fn(),
Expand Down
Loading
Loading