Skip to content

Commit 1a845e3

Browse files
authored
Add tests for delete chat endpoint (#1567)
Fixes OPS-2975. ## Additional Notes
1 parent 2c20486 commit 1a845e3

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

packages/server/api/test/unit/ai/ai-mcp-chat.controller.test.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from 'fastify';
99
import {
1010
createChatContext,
11+
deleteChatHistory,
1112
generateChatName,
1213
getAllChats,
1314
getChatContext,
@@ -90,6 +91,7 @@ jest.mock('../../../src/app/ai/chat/ai-chat.service', () => ({
9091
generateChatName: jest.fn(),
9192
updateChatName: jest.fn(),
9293
getAllChats: jest.fn(),
94+
deleteChatHistory: jest.fn(),
9395
}));
9496

9597
jest.mock('@openops/common', () => ({
@@ -846,4 +848,118 @@ describe('AI MCP Chat Controller - Tool Service Interactions', () => {
846848
);
847849
});
848850
});
851+
852+
describe('DELETE /:chatId (delete chat)', () => {
853+
let deleteHandler: RouteHandler;
854+
855+
beforeEach(async () => {
856+
jest.clearAllMocks();
857+
handlers = {};
858+
await aiMCPChatController(mockApp, {} as FastifyPluginOptions);
859+
deleteHandler = handlers['/:chatId'];
860+
});
861+
862+
it('should successfully delete a chat', async () => {
863+
(deleteChatHistory as jest.Mock).mockResolvedValue(undefined);
864+
865+
const request = {
866+
...mockRequest,
867+
params: { chatId: 'test-chat-id' },
868+
} as FastifyRequest;
869+
870+
await deleteHandler(request, mockReply as unknown as FastifyReply);
871+
872+
expect(deleteChatHistory).toHaveBeenCalledWith(
873+
'test-chat-id',
874+
'test-user-id',
875+
'test-project-id',
876+
);
877+
expect(mockReply.code).toHaveBeenCalledWith(200);
878+
expect(mockReply.send).toHaveBeenCalled();
879+
});
880+
881+
it('should handle deletion of non-existent chat gracefully', async () => {
882+
(deleteChatHistory as jest.Mock).mockResolvedValue(undefined);
883+
884+
const request = {
885+
...mockRequest,
886+
params: { chatId: 'non-existent-chat-id' },
887+
} as FastifyRequest;
888+
889+
await deleteHandler(request, mockReply as unknown as FastifyReply);
890+
891+
expect(deleteChatHistory).toHaveBeenCalledWith(
892+
'non-existent-chat-id',
893+
'test-user-id',
894+
'test-project-id',
895+
);
896+
expect(mockReply.code).toHaveBeenCalledWith(200);
897+
expect(mockReply.send).toHaveBeenCalled();
898+
});
899+
900+
it('should handle timeout errors', async () => {
901+
(deleteChatHistory as jest.Mock).mockRejectedValue(
902+
new Error('Operation timed out'),
903+
);
904+
905+
const request = {
906+
...mockRequest,
907+
params: { chatId: 'test-chat-id' },
908+
} as FastifyRequest;
909+
910+
await deleteHandler(request, mockReply as unknown as FastifyReply);
911+
912+
expect(deleteChatHistory).toHaveBeenCalledWith(
913+
'test-chat-id',
914+
'test-user-id',
915+
'test-project-id',
916+
);
917+
expect(mockReply.code).toHaveBeenCalledWith(500);
918+
expect(mockReply.send).toHaveBeenCalledWith({
919+
message: 'Internal server error',
920+
});
921+
});
922+
923+
it('should call deleteChatHistory with correct user and project parameters', async () => {
924+
(deleteChatHistory as jest.Mock).mockResolvedValue(undefined);
925+
926+
const customRequest = {
927+
...mockRequest,
928+
principal: {
929+
id: 'different-user-id',
930+
projectId: 'different-project-id',
931+
type: PrincipalType.USER,
932+
},
933+
params: { chatId: 'custom-chat-id' },
934+
} as FastifyRequest;
935+
936+
await deleteHandler(customRequest, mockReply as unknown as FastifyReply);
937+
938+
expect(deleteChatHistory).toHaveBeenCalledWith(
939+
'custom-chat-id',
940+
'different-user-id',
941+
'different-project-id',
942+
);
943+
expect(mockReply.code).toHaveBeenCalledWith(200);
944+
});
945+
946+
it('should handle chat IDs with special characters', async () => {
947+
(deleteChatHistory as jest.Mock).mockResolvedValue(undefined);
948+
949+
const specialChatId = 'chat-id-with-special-chars-123!@#';
950+
const request = {
951+
...mockRequest,
952+
params: { chatId: specialChatId },
953+
} as FastifyRequest;
954+
955+
await deleteHandler(request, mockReply as unknown as FastifyReply);
956+
957+
expect(deleteChatHistory).toHaveBeenCalledWith(
958+
specialChatId,
959+
'test-user-id',
960+
'test-project-id',
961+
);
962+
expect(mockReply.code).toHaveBeenCalledWith(200);
963+
});
964+
});
849965
});

0 commit comments

Comments
 (0)