Skip to content

Commit 1e49029

Browse files
author
Roman Snapko
committed
fix: test
1 parent 3d96feb commit 1e49029

2 files changed

Lines changed: 65 additions & 47 deletions

File tree

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

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,10 @@ describe('AI MCP Chat Controller - Tool Service Interactions', () => {
367367
{ role: 'assistant', content: 'You can use AWS Cost Explorer...' },
368368
],
369369
});
370-
(generateChatName as jest.Mock).mockResolvedValue(
371-
'AWS Cost Optimization',
372-
);
370+
(generateChatName as jest.Mock).mockResolvedValue({
371+
isGenerated: true,
372+
name: 'AWS Cost Optimization',
373+
});
373374

374375
await postHandler(
375376
{ ...mockRequest, body: { chatId: 'test-chat-id' } } as FastifyRequest,
@@ -378,10 +379,14 @@ describe('AI MCP Chat Controller - Tool Service Interactions', () => {
378379

379380
expect(mockReply.code).toHaveBeenCalledWith(200);
380381
expect(mockReply.send).toHaveBeenCalledWith({
381-
chatName: 'AWS Cost Optimization',
382+
isGenerated: true,
383+
name: 'AWS Cost Optimization',
382384
});
383385
expect(generateChatName).toHaveBeenCalledWith(
384-
[{ role: 'user', content: 'How do I optimize AWS costs?' }],
386+
[
387+
{ role: 'user', content: 'How do I optimize AWS costs?' },
388+
{ role: 'assistant', content: 'You can use AWS Cost Explorer...' },
389+
],
385390
'test-project-id',
386391
);
387392
expect(updateChatName).toHaveBeenCalledWith(
@@ -402,23 +407,26 @@ describe('AI MCP Chat Controller - Tool Service Interactions', () => {
402407

403408
expect(mockReply.code).toHaveBeenCalledWith(200);
404409
expect(mockReply.send).toHaveBeenCalledWith({
405-
chatName: 'New Chat',
410+
name: 'New Chat',
411+
isGenerated: false,
406412
});
407413
expect(generateChatName).not.toHaveBeenCalled();
408414
expect(updateChatName).not.toHaveBeenCalled();
409415
});
410416

411417
it('should return "New Chat" when chatHistory has no user messages', async () => {
418+
const chatHistory = [
419+
{ role: 'assistant', content: 'Some response' },
420+
{ role: 'system', content: 'System message' },
421+
{
422+
role: 'tool',
423+
content: [{ type: 'tool-result', toolCallId: '123' }],
424+
},
425+
];
412426
(getConversation as jest.Mock).mockResolvedValue({
413-
chatHistory: [
414-
{ role: 'assistant', content: 'Some response' },
415-
{ role: 'system', content: 'System message' },
416-
{
417-
role: 'tool',
418-
content: [{ type: 'tool-result', toolCallId: '123' }],
419-
},
420-
],
427+
chatHistory,
421428
});
429+
(generateChatName as jest.Mock).mockResolvedValue({ isGenerated: false });
422430

423431
await postHandler(
424432
{ ...mockRequest, body: { chatId: 'test-chat-id' } } as FastifyRequest,
@@ -427,13 +435,17 @@ describe('AI MCP Chat Controller - Tool Service Interactions', () => {
427435

428436
expect(mockReply.code).toHaveBeenCalledWith(200);
429437
expect(mockReply.send).toHaveBeenCalledWith({
430-
chatName: 'New Chat',
438+
name: 'New Chat',
439+
isGenerated: false,
431440
});
432-
expect(generateChatName).not.toHaveBeenCalled();
441+
expect(generateChatName).toHaveBeenCalledWith(
442+
chatHistory,
443+
'test-project-id',
444+
);
433445
expect(updateChatName).not.toHaveBeenCalled();
434446
});
435447

436-
it('should filter out invalid message objects without role property', async () => {
448+
it('should pass the raw chatHistory to generateChatName (no filtering at controller)', async () => {
437449
(getConversation as jest.Mock).mockResolvedValue({
438450
chatHistory: [
439451
{ role: 'user', content: 'Valid message' },
@@ -443,7 +455,10 @@ describe('AI MCP Chat Controller - Tool Service Interactions', () => {
443455
{ role: 'assistant', content: 'Response' },
444456
],
445457
});
446-
(generateChatName as jest.Mock).mockResolvedValue('Filtered Name');
458+
(generateChatName as jest.Mock).mockResolvedValue({
459+
isGenerated: true,
460+
name: 'Filtered Name',
461+
});
447462

448463
await postHandler(
449464
{ ...mockRequest, body: { chatId: 'test-chat-id' } } as FastifyRequest,
@@ -452,18 +467,22 @@ describe('AI MCP Chat Controller - Tool Service Interactions', () => {
452467

453468
expect(mockReply.code).toHaveBeenCalledWith(200);
454469
expect(mockReply.send).toHaveBeenCalledWith({
455-
chatName: 'Filtered Name',
470+
isGenerated: true,
471+
name: 'Filtered Name',
456472
});
457473
expect(generateChatName).toHaveBeenCalledWith(
458474
[
459475
{ role: 'user', content: 'Valid message' },
476+
{ content: 'Invalid message object' },
477+
{ type: 'reasoning', text: 'Some reasoning' },
460478
{ role: 'user', content: 'Another valid message' },
479+
{ role: 'assistant', content: 'Response' },
461480
],
462481
'test-project-id',
463482
);
464483
});
465484

466-
it('should call generateChatName with all user messages (filtered)', async () => {
485+
it('should call generateChatName with full chatHistory', async () => {
467486
const mockChatHistory = [
468487
{ role: 'user', content: 'First question' },
469488
{ role: 'assistant', content: 'First answer' },
@@ -475,35 +494,37 @@ describe('AI MCP Chat Controller - Tool Service Interactions', () => {
475494
(getConversation as jest.Mock).mockResolvedValue({
476495
chatHistory: mockChatHistory,
477496
});
478-
(generateChatName as jest.Mock).mockResolvedValue('Multi-Question Chat');
497+
(generateChatName as jest.Mock).mockResolvedValue({
498+
isGenerated: true,
499+
name: 'Multi-Question Chat',
500+
});
479501

480502
await postHandler(
481503
{ ...mockRequest, body: { chatId: 'test-chat-id' } } as FastifyRequest,
482504
mockReply as unknown as FastifyReply,
483505
);
484506

485507
expect(generateChatName).toHaveBeenCalledWith(
486-
[
487-
{ role: 'user', content: 'First question' },
488-
{ role: 'user', content: 'Second question' },
489-
{ role: 'user', content: 'Third question' },
490-
],
508+
mockChatHistory,
491509
'test-project-id',
492510
);
493511
expect(mockReply.code).toHaveBeenCalledWith(200);
494512
expect(mockReply.send).toHaveBeenCalledWith({
495-
chatName: 'Multi-Question Chat',
513+
isGenerated: true,
514+
name: 'Multi-Question Chat',
496515
});
497516
});
498517

499-
it('should return "New Chat" if LLM returns empty/whitespace', async () => {
518+
it('should return "New Chat" if LLM indicates not generated', async () => {
500519
(getConversation as jest.Mock).mockResolvedValue({
501520
chatHistory: [
502521
{ role: 'user', content: 'Hello?' },
503522
{ role: 'assistant', content: 'Hi!' },
504523
],
505524
});
506-
(generateChatName as jest.Mock).mockResolvedValue(' ');
525+
(generateChatName as jest.Mock).mockResolvedValue({
526+
isGenerated: false,
527+
});
507528

508529
await postHandler(
509530
{ ...mockRequest, body: { chatId: 'test-chat-id' } } as FastifyRequest,
@@ -512,21 +533,17 @@ describe('AI MCP Chat Controller - Tool Service Interactions', () => {
512533

513534
expect(mockReply.code).toHaveBeenCalledWith(200);
514535
expect(mockReply.send).toHaveBeenCalledWith({
515-
chatName: 'New Chat',
536+
name: 'New Chat',
537+
isGenerated: false,
516538
});
517-
expect(updateChatName).toHaveBeenCalledWith(
518-
'test-chat-id',
519-
'test-user-id',
520-
'test-project-id',
521-
'New Chat',
522-
);
539+
expect(updateChatName).not.toHaveBeenCalled();
523540
});
524541

525-
it('should return "New Chat" if LLM returns empty string', async () => {
542+
it('should return "New Chat" if LLM returns empty string (not generated)', async () => {
526543
(getConversation as jest.Mock).mockResolvedValue({
527544
chatHistory: [{ role: 'user', content: 'Test question' }],
528545
});
529-
(generateChatName as jest.Mock).mockResolvedValue('');
546+
(generateChatName as jest.Mock).mockResolvedValue({ isGenerated: false });
530547

531548
await postHandler(
532549
{ ...mockRequest, body: { chatId: 'test-chat-id' } } as FastifyRequest,
@@ -535,7 +552,8 @@ describe('AI MCP Chat Controller - Tool Service Interactions', () => {
535552

536553
expect(mockReply.code).toHaveBeenCalledWith(200);
537554
expect(mockReply.send).toHaveBeenCalledWith({
538-
chatName: 'New Chat',
555+
name: 'New Chat',
556+
isGenerated: false,
539557
});
540558
});
541559

@@ -546,9 +564,10 @@ describe('AI MCP Chat Controller - Tool Service Interactions', () => {
546564
{ role: 'assistant', content: 'OpenOps is a platform...' },
547565
],
548566
});
549-
(generateChatName as jest.Mock).mockResolvedValue(
550-
'OpenOps Platform Overview',
551-
);
567+
(generateChatName as jest.Mock).mockResolvedValue({
568+
isGenerated: true,
569+
name: 'OpenOps Platform Overview',
570+
});
552571

553572
await postHandler(
554573
{ ...mockRequest, body: { chatId: 'test-chat-id' } } as FastifyRequest,
@@ -563,7 +582,8 @@ describe('AI MCP Chat Controller - Tool Service Interactions', () => {
563582
);
564583
expect(mockReply.code).toHaveBeenCalledWith(200);
565584
expect(mockReply.send).toHaveBeenCalledWith({
566-
chatName: 'OpenOps Platform Overview',
585+
isGenerated: true,
586+
name: 'OpenOps Platform Overview',
567587
});
568588
});
569589

packages/server/api/test/unit/ai/utils.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { ModelMessage } from 'ai';
3-
import {
4-
mergeToolResultsIntoMessages,
5-
sanitizeMessagesForChatName,
6-
} from '../../../src/app/ai/chat/utils';
3+
import { mergeToolResultsIntoMessages, sanitizeMessagesForChatName, } from '../../../src/app/ai/chat/utils';
74

85
describe('mergeToolResultsIntoMessages', () => {
96
describe('basic message handling', () => {
@@ -29,6 +26,7 @@ describe('mergeToolResultsIntoMessages', () => {
2926
type: 'text',
3027
text: 'Hello, how are you?',
3128
state: 'done',
29+
3230
},
3331
],
3432
});

0 commit comments

Comments
 (0)