|
8 | 8 | } from 'fastify'; |
9 | 9 | import { |
10 | 10 | createChatContext, |
| 11 | + deleteChatHistory, |
11 | 12 | generateChatName, |
12 | 13 | getAllChats, |
13 | 14 | getChatContext, |
@@ -90,6 +91,7 @@ jest.mock('../../../src/app/ai/chat/ai-chat.service', () => ({ |
90 | 91 | generateChatName: jest.fn(), |
91 | 92 | updateChatName: jest.fn(), |
92 | 93 | getAllChats: jest.fn(), |
| 94 | + deleteChatHistory: jest.fn(), |
93 | 95 | })); |
94 | 96 |
|
95 | 97 | jest.mock('@openops/common', () => ({ |
@@ -846,4 +848,118 @@ describe('AI MCP Chat Controller - Tool Service Interactions', () => { |
846 | 848 | ); |
847 | 849 | }); |
848 | 850 | }); |
| 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 | + }); |
849 | 965 | }); |
0 commit comments