Skip to content

Commit 340cbc9

Browse files
committed
fix unit tests
1 parent cc6985a commit 340cbc9

File tree

4 files changed

+68
-96
lines changed

4 files changed

+68
-96
lines changed

backend/src/util/__tests__/messages.test.ts

Lines changed: 54 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import {
1616
} from '../messages'
1717
import * as tokenCounter from '../token-counter'
1818

19-
import type { Message } from '@codebuff/common/types/messages/codebuff-message'
19+
import type { CodebuffToolMessage } from '@codebuff/common/tools/list'
20+
import type {
21+
Message,
22+
UserMessage,
23+
} from '@codebuff/common/types/messages/codebuff-message'
2024

2125
describe('messagesWithSystem', () => {
2226
it('prepends system message to array', () => {
@@ -49,7 +53,7 @@ describe('trimMessagesToFitTokenLimit', () => {
4953
mock.restore()
5054
})
5155

52-
const testMessages = [
56+
const testMessages: Message[] = [
5357
// Regular message without tool calls - should never be shortened, but won't fit in the final array
5458
{
5559
role: 'assistant',
@@ -142,14 +146,11 @@ describe('trimMessagesToFitTokenLimit', () => {
142146
// Regular message - should never be shortened
143147
{
144148
type: 'image',
145-
source: {
146-
type: 'base64',
147-
media_type: 'image/jpeg',
148-
data: 'xyz',
149-
},
149+
image: 'xyz',
150+
mediaType: 'image/jpeg',
150151
},
151152
],
152-
},
153+
} satisfies UserMessage,
153154
{
154155
// Terminal output 5 - should be preserved (3rd most recent)
155156
role: 'tool',
@@ -198,12 +199,14 @@ describe('trimMessagesToFitTokenLimit', () => {
198199
// Regular message - should never be shortened
199200
{
200201
role: 'assistant',
201-
content: {
202-
type: 'text',
203-
text: 'Another long message that should never be shortened because it has no tool calls in it at all',
204-
},
202+
content: [
203+
{
204+
type: 'text',
205+
text: 'Another long message that should never be shortened because it has no tool calls in it at all',
206+
},
207+
],
205208
},
206-
] as Message[]
209+
]
207210

208211
it('handles all features working together correctly', () => {
209212
const maxTotalTokens = 3000
@@ -286,7 +289,7 @@ describe('trimMessagesToFitTokenLimit', () => {
286289

287290
describe('keepDuringTruncation functionality', () => {
288291
it('preserves messages marked with keepDuringTruncation=true', () => {
289-
const messages = [
292+
const messages: Message[] = [
290293
{ role: 'user', content: 'A'.repeat(500) }, // Large message to force truncation
291294
{ role: 'user', content: 'B'.repeat(500) }, // Large message to force truncation
292295
{
@@ -300,7 +303,7 @@ describe('trimMessagesToFitTokenLimit', () => {
300303
content: 'Message 5 - keep me too!',
301304
keepDuringTruncation: true,
302305
},
303-
] as Message[]
306+
]
304307

305308
const result = trimMessagesToFitTokenLimit(messages, 0, 1000)
306309

@@ -341,12 +344,12 @@ describe('trimMessagesToFitTokenLimit', () => {
341344
})
342345

343346
it('handles consecutive replacement messages correctly', () => {
344-
const messages = [
347+
const messages: Message[] = [
345348
{ role: 'user', content: 'A'.repeat(1000) }, // Large message to be removed
346349
{ role: 'user', content: 'B'.repeat(1000) }, // Large message to be removed
347350
{ role: 'user', content: 'C'.repeat(1000) }, // Large message to be removed
348351
{ role: 'user', content: 'Keep this', keepDuringTruncation: true },
349-
] as Message[]
352+
]
350353

351354
const result = trimMessagesToFitTokenLimit(messages, 0, 1000)
352355

@@ -367,7 +370,7 @@ describe('trimMessagesToFitTokenLimit', () => {
367370
})
368371

369372
it('calculates token removal correctly with keepDuringTruncation', () => {
370-
const messages = [
373+
const messages: Message[] = [
371374
{ role: 'user', content: 'A'.repeat(500) }, // Will be removed
372375
{ role: 'user', content: 'B'.repeat(500) }, // Will be removed
373376
{
@@ -376,7 +379,7 @@ describe('trimMessagesToFitTokenLimit', () => {
376379
keepDuringTruncation: true,
377380
},
378381
{ role: 'user', content: 'C'.repeat(100) }, // Might be kept
379-
] as Message[]
382+
]
380383

381384
const result = trimMessagesToFitTokenLimit(messages, 0, 2000)
382385

@@ -394,13 +397,13 @@ describe('trimMessagesToFitTokenLimit', () => {
394397
})
395398

396399
it('handles mixed keepDuringTruncation and regular messages', () => {
397-
const messages = [
400+
const messages: Message[] = [
398401
{ role: 'user', content: 'A'.repeat(800) }, // Large message to force truncation
399402
{ role: 'user', content: 'Keep 1', keepDuringTruncation: true },
400403
{ role: 'user', content: 'B'.repeat(800) }, // Large message to force truncation
401404
{ role: 'user', content: 'Keep 2', keepDuringTruncation: true },
402405
{ role: 'user', content: 'C'.repeat(800) }, // Large message to force truncation
403-
] as Message[]
406+
]
404407

405408
const result = trimMessagesToFitTokenLimit(messages, 0, 500)
406409

@@ -439,9 +442,11 @@ describe('getPreviouslyReadFiles', () => {
439442
type: 'tool-result',
440443
toolName: 'write_file',
441444
toolCallId: 'test-id',
442-
output: [{ type: 'json', value: { file: 'test.ts' } }],
445+
output: [
446+
{ type: 'json', value: { file: 'test.ts', errorMessage: 'error' } },
447+
],
443448
},
444-
},
449+
} satisfies CodebuffToolMessage<'write_file'>,
445450
]
446451

447452
const result = getPreviouslyReadFiles(messages)
@@ -473,7 +478,7 @@ describe('getPreviouslyReadFiles', () => {
473478
},
474479
],
475480
},
476-
},
481+
} satisfies CodebuffToolMessage<'read_files'>,
477482
]
478483

479484
const result = getPreviouslyReadFiles(messages)
@@ -510,7 +515,7 @@ describe('getPreviouslyReadFiles', () => {
510515
},
511516
],
512517
},
513-
},
518+
} satisfies CodebuffToolMessage<'find_files'>,
514519
]
515520

516521
const result = getPreviouslyReadFiles(messages)
@@ -533,25 +538,25 @@ describe('getPreviouslyReadFiles', () => {
533538
output: [
534539
{
535540
type: 'json',
536-
value: [
537-
{
538-
path: 'config/database.ts',
539-
content: 'export const dbConfig = {}',
540-
referencedBy: { 'app.ts': ['line 5', 'line 20'] },
541-
},
542-
],
541+
value: {
542+
files: [
543+
{
544+
path: 'config/database.ts',
545+
content: 'export const dbConfig = {}',
546+
},
547+
],
548+
},
543549
},
544550
],
545551
},
546-
},
552+
} satisfies CodebuffToolMessage<'file_updates'>,
547553
]
548554

549555
const result = getPreviouslyReadFiles(messages)
550556
expect(result).toEqual([
551557
{
552558
path: 'config/database.ts',
553559
content: 'export const dbConfig = {}',
554-
referencedBy: { 'app.ts': ['line 5', 'line 20'] },
555560
},
556561
])
557562
})
@@ -576,7 +581,7 @@ describe('getPreviouslyReadFiles', () => {
576581
},
577582
],
578583
},
579-
},
584+
} satisfies CodebuffToolMessage<'read_files'>,
580585
{
581586
role: 'tool',
582587
content: {
@@ -595,7 +600,7 @@ describe('getPreviouslyReadFiles', () => {
595600
},
596601
],
597602
},
598-
},
603+
} satisfies CodebuffToolMessage<'find_files'>,
599604
{
600605
role: 'user',
601606
content: 'Some user message',
@@ -609,16 +614,18 @@ describe('getPreviouslyReadFiles', () => {
609614
output: [
610615
{
611616
type: 'json',
612-
value: [
613-
{
614-
path: 'file3.ts',
615-
content: 'content 3',
616-
},
617-
],
617+
value: {
618+
files: [
619+
{
620+
path: 'file3.ts',
621+
content: 'content 3',
622+
},
623+
],
624+
},
618625
},
619626
],
620627
},
621-
},
628+
} satisfies CodebuffToolMessage<'file_updates'>,
622629
]
623630

624631
const result = getPreviouslyReadFiles(messages)
@@ -657,7 +664,7 @@ describe('getPreviouslyReadFiles', () => {
657664
},
658665
],
659666
},
660-
},
667+
} satisfies CodebuffToolMessage<'read_files'>,
661668
]
662669

663670
const result = getPreviouslyReadFiles(messages)
@@ -706,7 +713,7 @@ describe('getPreviouslyReadFiles', () => {
706713
},
707714
],
708715
},
709-
},
716+
} satisfies CodebuffToolMessage<'find_files'>,
710717
]
711718

712719
const result = getPreviouslyReadFiles(messages)
@@ -736,7 +743,7 @@ describe('getPreviouslyReadFiles', () => {
736743
},
737744
],
738745
},
739-
},
746+
} satisfies CodebuffToolMessage<'read_files'>,
740747
]
741748

742749
const result = getPreviouslyReadFiles(messages)
@@ -758,47 +765,10 @@ describe('getPreviouslyReadFiles', () => {
758765
},
759766
],
760767
},
761-
},
768+
} satisfies CodebuffToolMessage<'read_files'>,
762769
]
763770

764771
const result = getPreviouslyReadFiles(messages)
765772
expect(result).toEqual([])
766773
})
767-
768-
it('handles multiple outputs in single tool message', () => {
769-
const messages: Message[] = [
770-
{
771-
role: 'tool',
772-
content: {
773-
type: 'tool-result',
774-
toolName: 'read_files',
775-
toolCallId: 'test-id',
776-
output: [
777-
{
778-
type: 'json',
779-
value: [
780-
{
781-
path: 'file1.ts',
782-
content: 'content 1',
783-
},
784-
],
785-
},
786-
{
787-
type: 'json',
788-
value: [
789-
{
790-
path: 'file2.ts',
791-
content: 'content 2',
792-
},
793-
],
794-
},
795-
],
796-
},
797-
},
798-
]
799-
800-
const result = getPreviouslyReadFiles(messages)
801-
// Function uses output[0], so only first output is processed
802-
expect(result).toEqual([{ path: 'file1.ts', content: 'content 1' }])
803-
})
804774
})

common/src/tools/list.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,7 @@ export type CodebuffToolResult<
9595

9696
export type CodebuffToolMessage<
9797
T extends ToolName | ResultOnlyToolName = ToolName,
98-
> = ToolMessage &
99-
{
100-
[K in ToolName | ResultOnlyToolName]: {
101-
toolName: K
102-
content: {
103-
output: CodebuffToolOutput<K>
104-
}
105-
}
106-
}[T]
98+
> = ToolMessage & { content: CodebuffToolResult<T> }
10799

108100
// Tool call to send to client
109101
export type ClientToolName = (typeof clientToolNames)[number]

common/src/tools/params/tool/file-updates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const fileUpdatesResultSchema = {
1414
z.object({
1515
path: z.string(),
1616
content: z.string(),
17-
referencedBy: z.record(z.string(), z.array(z.string())),
17+
referencedBy: z.record(z.string(), z.string().array()).optional(),
1818
}),
1919
),
2020
}),

common/src/types/messages/codebuff-message.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ export const userMessageSchema = z
3131
role: z.literal('user'),
3232
content: z.union([
3333
z.string(),
34-
z.union([textPartSchema, imagePartSchema, filePartSchema]).array(),
34+
z
35+
.discriminatedUnion('type', [
36+
textPartSchema,
37+
imagePartSchema,
38+
filePartSchema,
39+
])
40+
.array(),
3541
]),
3642
})
3743
.and(auxiliaryDataSchema)
@@ -43,7 +49,11 @@ export const assistantMessageSchema = z
4349
content: z.union([
4450
z.string(),
4551
z
46-
.union([textPartSchema, reasoningPartSchema, toolCallPartSchema])
52+
.discriminatedUnion('type', [
53+
textPartSchema,
54+
reasoningPartSchema,
55+
toolCallPartSchema,
56+
])
4757
.array(),
4858
]),
4959
})

0 commit comments

Comments
 (0)