diff --git a/backend/test/unit/controller/resetController.test.ts b/backend/test/unit/controller/resetController.test.ts index ad18c9ebf..4c9a3f362 100644 --- a/backend/test/unit/controller/resetController.test.ts +++ b/backend/test/unit/controller/resetController.test.ts @@ -1,9 +1,13 @@ import { expect, test, jest, afterEach, describe } from '@jest/globals'; import { Response } from 'express'; -import { handleResetProgress } from '@src/controller/resetController'; +import { + handleResetLevel, + 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 { defaultChatModel } from '@src/models/chat'; import { LEVEL_NAMES } from '@src/models/level'; @@ -158,3 +162,98 @@ describe('reset progress', () => { expect(mockSend).toHaveBeenCalledWith('Invalid level'); }); }); + +describe('reset level', () => { + test.each(Object.values(LEVEL_NAMES))( + `GIVEN level [%s] WHEN client asks to reset level progress THEN sent messages and emails are cleared AND the backend sends an info message`, + (level) => { + 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', + }, + ], + chatModel: 'chat model', + }, + } as unknown as LevelResetRequest; + const res = responseMock(); + + handleResetLevel(req, res); + + expect(req.session.levelState[level].chatHistory).toEqual([]); + expect(req.session.levelState[level].sentEmails).toEqual([]); + + expect(mockSend).toHaveBeenCalledWith({ + resultingChatMessage: { + infoMessage: 'Level progress reset', + chatMessageType: 'RESET_LEVEL', + }, + }); + } + ); + + test('WHEN client does not provide a level THEN the backend responds with BadRequest', () => { + const req = { + query: {}, + session: { + levelState: [ + {}, + { + sentEmails: [], + chatHistory: [], + defences: [], + }, + ], + }, + } as unknown as LevelResetRequest; + const res = responseMock(); + + handleResetLevel(req, res); + + expect(res.status).toHaveBeenCalledWith(400); + expect(mockSend).toHaveBeenCalledWith('Level not provided'); + }); + + test('WHEN client provides an invalid level THEN the backend responds with BadRequest', () => { + const req = { + query: { level: 5 }, + session: { + levelState: [ + {}, + { + sentEmails: [], + chatHistory: [], + defences: [], + }, + ], + }, + } as unknown as LevelResetRequest; + const res = responseMock(); + + handleResetLevel(req, res); + + expect(res.status).toHaveBeenCalledWith(400); + expect(mockSend).toHaveBeenCalledWith('Invalid level'); + }); +});