Skip to content

Commit

Permalink
Fix tests following component and service rework
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswilty committed Jul 9, 2024
1 parent ddf60dd commit a1ffa64
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 140 deletions.
10 changes: 8 additions & 2 deletions backend/src/controller/levelController.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { Response } from 'express';

import { getSandboxDocumentMetas } from '@src/document';
import { LevelGetRequest } from '@src/models/api/LevelGetRequest';
import {
LevelGetRequest,
LevelGetResponseBody,
} from '@src/models/api/LevelGetRequest';
import { LEVEL_NAMES } from '@src/models/level';

import { validateLevel } from './requestValidators';

function handleLoadLevel(req: LevelGetRequest, res: Response) {
function handleLoadLevel(
req: LevelGetRequest,
res: Response<LevelGetResponseBody>
) {
const level = validateLevel(res, req.query.level);
if (level === null) return;

Expand Down
23 changes: 17 additions & 6 deletions backend/src/controller/resetController.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { Response } from 'express';

import { getSandboxDocumentMetas } from '@src/document';
import { LevelResetRequest } from '@src/models/api/LevelResetRequest';
import { ProgressResetRequest } from '@src/models/api/ProgressResetRequest';
import {
LevelResetRequest,
LevelResetResponseBody,
} from '@src/models/api/LevelResetRequest';
import {
ProgressResetRequest,
ProgressResetResponseBody,
} from '@src/models/api/ProgressResetRequest';
import { defaultChatModel } from '@src/models/chat';
import { ChatInfoMessage } from '@src/models/chatMessage';
import { LEVEL_NAMES, getInitialLevelStates } from '@src/models/level';

import { validateLevel } from './requestValidators';

function handleResetProgress(req: ProgressResetRequest, res: Response) {
function handleResetProgress(
req: ProgressResetRequest,
res: Response<ProgressResetResponseBody>
) {
const level = validateLevel(res, req.query.level);
if (level === null) return;

Expand All @@ -31,7 +39,10 @@ function handleResetProgress(req: ProgressResetRequest, res: Response) {
});
}

function handleResetLevel(req: LevelResetRequest, res: Response) {
function handleResetLevel(
req: LevelResetRequest,
res: Response<LevelResetResponseBody>
) {
const level = validateLevel(res, Number(req.params.level) as LEVEL_NAMES);
if (level === null) return;

Expand All @@ -42,7 +53,7 @@ function handleResetLevel(req: LevelResetRequest, res: Response) {
chatInfoMessage: {
infoMessage: `Level progress reset`,
chatMessageType: 'RESET_LEVEL',
} as ChatInfoMessage,
},
});
}

Expand Down
9 changes: 6 additions & 3 deletions backend/src/controller/startController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Response } from 'express';
import { getSandboxDocumentMetas } from '@src/document';
import {
StartGetRequest,
StartResponse,
StartGetResponseBody,
} from '@src/models/api/StartGetRequest';
import { LEVEL_NAMES } from '@src/models/level';
import { getValidOpenAIModels } from '@src/openai';
Expand All @@ -15,7 +15,10 @@ import {

import { validateLevel } from './requestValidators';

function handleStart(req: StartGetRequest, res: Response) {
function handleStart(
req: StartGetRequest,
res: Response<StartGetResponseBody>
) {
const level = validateLevel(res, req.query.level);
if (level === null) return;

Expand All @@ -37,7 +40,7 @@ function handleStart(req: StartGetRequest, res: Response) {
availableDocs,
availableModels: getValidOpenAIModels(),
systemRoles,
} as StartResponse);
});
}

export { handleStart };
16 changes: 9 additions & 7 deletions backend/src/models/api/LevelGetRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import { DocumentMeta } from '@src/models/document';
import { EmailInfo } from '@src/models/email';
import { LEVEL_NAMES } from '@src/models/level';

export type LevelGetResponseBody = {
emails: EmailInfo[];
chatHistory: ChatMessage[];
defences?: Defence[];
chatModel?: ChatModel;
availableDocs?: DocumentMeta[];
};

export type LevelGetRequest = Request<
never,
{
emails: EmailInfo[];
chatHistory: ChatMessage[];
defences: Defence[];
chatModel?: ChatModel;
availableDocs?: DocumentMeta[];
},
LevelGetResponseBody,
never,
{
level?: LEVEL_NAMES;
Expand Down
8 changes: 5 additions & 3 deletions backend/src/models/api/LevelResetRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { Request } from 'express';

import { ChatMessage } from '@src/models/chatMessage';

export type LevelResetResponseBody = {
chatInfoMessage?: ChatMessage;
};

export type LevelResetRequest = Request<
{ level: string },
{
chatInfoMessage?: ChatMessage;
},
LevelResetResponseBody,
never,
never
>;
16 changes: 9 additions & 7 deletions backend/src/models/api/ProgressResetRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import { DocumentMeta } from '@src/models/document';
import { EmailInfo } from '@src/models/email';
import { LEVEL_NAMES } from '@src/models/level';

export type ProgressResetResponseBody = {
emails: EmailInfo[];
chatHistory: ChatMessage[];
defences?: Defence[];
chatModel?: ChatModel;
availableDocs?: DocumentMeta[];
};

export type ProgressResetRequest = Request<
never,
{
emails: EmailInfo[];
chatHistory: ChatMessage[];
defences?: Defence[];
chatModel?: ChatModel;
availableDocs?: DocumentMeta[];
},
ProgressResetResponseBody,
never,
{
level?: LEVEL_NAMES;
Expand Down
6 changes: 3 additions & 3 deletions backend/src/models/api/StartGetRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { DocumentMeta } from '@src/models/document';
import { EmailInfo } from '@src/models/email';
import { LEVEL_NAMES } from '@src/models/level';

export type StartResponse = {
export type StartGetResponseBody = {
emails: EmailInfo[];
chatHistory: ChatMessage[];
defences: Defence[];
defences?: Defence[];
availableModels: string[];
systemRoles: {
level: LEVEL_NAMES;
Expand All @@ -22,7 +22,7 @@ export type StartResponse = {

export type StartGetRequest = Request<
never,
StartResponse,
StartGetResponseBody,
never,
{
level?: LEVEL_NAMES;
Expand Down
4 changes: 2 additions & 2 deletions backend/test/api/start.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { beforeAll, describe, expect, it, jest } from '@jest/globals';
import { OpenAI } from 'openai';
import request from 'supertest';

import { StartResponse } from '@src/models/api/StartGetRequest';
import { StartGetResponseBody } from '@src/models/api/StartGetRequest';
import { LEVEL_NAMES } from '@src/models/level';
import app from '@src/server/app';

Expand Down Expand Up @@ -35,7 +35,7 @@ describe('/start endpoints', () => {
.expect(200)
.expect('Content-Type', /application\/json/)
.then((response) => {
const { chatHistory, emails } = response.body as StartResponse;
const { chatHistory, emails } = response.body as StartGetResponseBody;
expect(chatHistory).toEqual([]);
expect(emails).toEqual([]);
})
Expand Down
21 changes: 19 additions & 2 deletions backend/test/unit/controller/levelController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@ import { expect, test, jest, afterEach } from '@jest/globals';
import { Response } from 'express';

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

const mockDocuments = [
{
fileName: 'any-file.txt',
fileType: 'txt',
folder: 'anyfolder',
} as DocumentMeta,
];

jest.mock('@src/document', () => ({
getSandboxDocumentMetas: () => mockDocuments,
}));

const mockSend = jest.fn();

function responseMock() {
return {
send: mockSend,
status: jest.fn().mockReturnValue({ send: mockSend }),
} as unknown as Response;
} as unknown as Response<LevelGetResponseBody>;
}

afterEach(() => {
Expand Down Expand Up @@ -60,6 +76,7 @@ test.each(Object.values(LEVEL_NAMES))(
chatHistory: `level ${level + 1} chat history`,
defences: `level ${level + 1} defences`,
chatModel: level === LEVEL_NAMES.SANDBOX ? 'chat model' : undefined,
availableDocs: level === LEVEL_NAMES.SANDBOX ? mockDocuments : undefined,
});
}
);
Expand Down
45 changes: 33 additions & 12 deletions backend/test/unit/controller/resetController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,37 @@ import {
handleResetProgress,
} from '@src/controller/resetController';
import { defaultDefences } from '@src/defaultDefences';
import { LevelGetRequest } from '@src/models/api/LevelGetRequest';
import { LevelResetRequest } from '@src/models/api/LevelResetRequest';
import {
LevelResetRequest,
LevelResetResponseBody,
} from '@src/models/api/LevelResetRequest';
import {
ProgressResetRequest,
ProgressResetResponseBody,
} from '@src/models/api/ProgressResetRequest';
import { defaultChatModel } from '@src/models/chat';
import { DocumentMeta } from '@src/models/document';
import { LEVEL_NAMES } from '@src/models/level';

const mockDocuments = [
{
fileName: 'any-file.txt',
fileType: 'txt',
folder: 'anyfolder',
} as DocumentMeta,
];

jest.mock('@src/document', () => ({
getSandboxDocumentMetas: () => mockDocuments,
}));

const mockSend = jest.fn();

function responseMock() {
function responseMock<ResBody>() {
return {
send: mockSend,
status: jest.fn().mockReturnValue({ send: mockSend }),
} as unknown as Response;
} as unknown as Response<ResBody>;
}

afterEach(() => {
Expand Down Expand Up @@ -78,8 +97,8 @@ describe('reset progress', () => {
],
chatModel: 'chat model',
},
} as unknown as LevelGetRequest;
const res = responseMock();
} as unknown as ProgressResetRequest;
const res = responseMock<ProgressResetResponseBody>();

mockGetInitialLevelStates.mockReturnValue([
{
Expand Down Expand Up @@ -114,6 +133,8 @@ describe('reset progress', () => {
? undefined
: defaultDefences,
chatModel: level === LEVEL_NAMES.SANDBOX ? defaultChatModel : undefined,
availableDocs:
level === LEVEL_NAMES.SANDBOX ? mockDocuments : undefined,
});
}
);
Expand All @@ -131,8 +152,8 @@ describe('reset progress', () => {
},
],
},
} as unknown as LevelGetRequest;
const res = responseMock();
} as unknown as ProgressResetRequest;
const res = responseMock<ProgressResetResponseBody>();

handleResetProgress(req, res);

Expand All @@ -153,8 +174,8 @@ describe('reset progress', () => {
},
],
},
} as unknown as LevelGetRequest;
const res = responseMock();
} as unknown as ProgressResetRequest;
const res = responseMock<ProgressResetResponseBody>();

handleResetProgress(req, res);

Expand Down Expand Up @@ -197,7 +218,7 @@ describe('reset level', () => {
chatModel: 'chat model',
},
} as unknown as LevelResetRequest;
const res = responseMock();
const res = responseMock<LevelResetResponseBody>();

handleResetLevel(req, res);

Expand Down Expand Up @@ -227,7 +248,7 @@ describe('reset level', () => {
],
},
} as unknown as LevelResetRequest;
const res = responseMock();
const res = responseMock<LevelResetResponseBody>();

handleResetLevel(req, res);

Expand Down
21 changes: 19 additions & 2 deletions backend/test/unit/controller/startController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@ import { expect, test, jest, afterEach } from '@jest/globals';
import { Response } from 'express';

import { handleStart } from '@src/controller/startController';
import { StartGetRequest } from '@src/models/api/StartGetRequest';
import {
StartGetRequest,
StartGetResponseBody,
} from '@src/models/api/StartGetRequest';
import { DocumentMeta } from '@src/models/document';
import { LEVEL_NAMES } from '@src/models/level';

const mockDocuments = [
{
fileName: 'any-file.txt',
fileType: 'txt',
folder: 'anyfolder',
} as DocumentMeta,
];

jest.mock('@src/document', () => ({
getSandboxDocumentMetas: () => mockDocuments,
}));

jest.mock('@src/promptTemplates', () => ({
systemRoleLevel1: 'systemRoleLevel1',
systemRoleLevel2: 'systemRoleLevel2',
Expand All @@ -17,7 +33,7 @@ function responseMock() {
return {
send: mockSend,
status: jest.fn().mockReturnValue({ send: mockSend }),
} as unknown as Response;
} as unknown as Response<StartGetResponseBody>;
}

afterEach(() => {
Expand Down Expand Up @@ -73,6 +89,7 @@ test.each(Object.values(LEVEL_NAMES))(
{ level: 2, systemRole: 'systemRoleLevel3' },
],
chatModel: level === LEVEL_NAMES.SANDBOX ? 'chat model' : undefined,
availableDocs: level === LEVEL_NAMES.SANDBOX ? mockDocuments : undefined,
});
}
);
Expand Down
Loading

0 comments on commit a1ffa64

Please sign in to comment.