|
1 | 1 | import { expect, test, describe, vi } from 'vitest' |
2 | | -import { createUIMessage, fileReferenceToString, getAnswerPartFromAssistantMessage, getLastStepParts, getTurnProgressState, groupMessageIntoSteps, repairReferences } from './utils' |
| 2 | +import { createUIMessage, fileReferenceToString, getAnswerPartFromAssistantMessage, getLastStepParts, getTurnProgressState, getUserMessageText, groupMessageIntoSteps, repairReferences } from './utils' |
3 | 3 | import { FILE_REFERENCE_REGEX, ANSWER_TAG } from './constants'; |
4 | 4 | import { SBChatMessage, SBChatMessagePart } from './types'; |
5 | 5 |
|
@@ -537,6 +537,75 @@ test('getAnswerPartFromAssistantMessage returns undefined when turn is in progre |
537 | 537 | expect(result).toBeUndefined(); |
538 | 538 | }); |
539 | 539 |
|
| 540 | +describe('getUserMessageText', () => { |
| 541 | + test('returns the text when the text part is first', () => { |
| 542 | + const message: SBChatMessage = { |
| 543 | + role: 'user', |
| 544 | + parts: [ |
| 545 | + { |
| 546 | + type: 'text', |
| 547 | + text: 'Hello, world!', |
| 548 | + }, |
| 549 | + ], |
| 550 | + } as SBChatMessage; |
| 551 | + |
| 552 | + expect(getUserMessageText(message)).toBe('Hello, world!'); |
| 553 | + }); |
| 554 | + |
| 555 | + test('returns the text when a non-text part precedes the text part', () => { |
| 556 | + const message: SBChatMessage = { |
| 557 | + role: 'user', |
| 558 | + parts: [ |
| 559 | + { |
| 560 | + type: 'data-source', |
| 561 | + data: { |
| 562 | + type: 'file', |
| 563 | + path: 'auth.ts', |
| 564 | + repo: 'github.com/sourcebot-dev/sourcebot', |
| 565 | + name: 'auth.ts', |
| 566 | + revision: 'main', |
| 567 | + }, |
| 568 | + }, |
| 569 | + { |
| 570 | + type: 'text', |
| 571 | + text: 'Explain this file', |
| 572 | + }, |
| 573 | + ], |
| 574 | + } as SBChatMessage; |
| 575 | + |
| 576 | + expect(getUserMessageText(message)).toBe('Explain this file'); |
| 577 | + }); |
| 578 | + |
| 579 | + test('returns an empty string when there is no text part', () => { |
| 580 | + const message: SBChatMessage = { |
| 581 | + role: 'user', |
| 582 | + parts: [ |
| 583 | + { |
| 584 | + type: 'data-source', |
| 585 | + data: { |
| 586 | + type: 'file', |
| 587 | + path: 'auth.ts', |
| 588 | + repo: 'github.com/sourcebot-dev/sourcebot', |
| 589 | + name: 'auth.ts', |
| 590 | + revision: 'main', |
| 591 | + }, |
| 592 | + }, |
| 593 | + ], |
| 594 | + } as SBChatMessage; |
| 595 | + |
| 596 | + expect(getUserMessageText(message)).toBe(''); |
| 597 | + }); |
| 598 | + |
| 599 | + test('returns an empty string when there are no parts', () => { |
| 600 | + const message: SBChatMessage = { |
| 601 | + role: 'user', |
| 602 | + parts: [], |
| 603 | + } as unknown as SBChatMessage; |
| 604 | + |
| 605 | + expect(getUserMessageText(message)).toBe(''); |
| 606 | + }); |
| 607 | +}); |
| 608 | + |
540 | 609 | test('repairReferences fixes missing colon after @file', () => { |
541 | 610 | const input = 'See the function in @file{github.com/sourcebot-dev/sourcebot::auth.ts} for details.'; |
542 | 611 | const expected = 'See the function in @file:{github.com/sourcebot-dev/sourcebot::auth.ts} for details.'; |
|
0 commit comments