mirror of
https://github.com/stablyai/orca.git
synced 2026-10-26 08:02:44 +00:00
50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
import { mergeNativeChatMessages } from '../../../src/shared/native-chat-merge'
|
|
import type { NativeChatMessage } from '../../../src/shared/native-chat-types'
|
|
|
|
function msg(id: string, overrides: Partial<NativeChatMessage> = {}): NativeChatMessage {
|
|
return {
|
|
id,
|
|
role: 'assistant',
|
|
blocks: [{ type: 'text', text: id }],
|
|
timestamp: 0,
|
|
source: 'transcript',
|
|
...overrides
|
|
}
|
|
}
|
|
|
|
describe('mergeNativeChatMessages', () => {
|
|
it('appends new ids in arrival order', () => {
|
|
const merged = mergeNativeChatMessages([msg('a')], [msg('b'), msg('c')])
|
|
expect(merged.map((m) => m.id)).toEqual(['a', 'b', 'c'])
|
|
})
|
|
|
|
it('replaces a re-emitted id in place without reordering', () => {
|
|
const merged = mergeNativeChatMessages(
|
|
[msg('a'), msg('b'), msg('c')],
|
|
[msg('b', { blocks: [{ type: 'text', text: 'updated' }] })]
|
|
)
|
|
expect(merged.map((m) => m.id)).toEqual(['a', 'b', 'c'])
|
|
expect(merged[1]!.blocks).toEqual([{ type: 'text', text: 'updated' }])
|
|
})
|
|
|
|
it('does not let a lower-priority source overwrite a higher one', () => {
|
|
const merged = mergeNativeChatMessages(
|
|
[msg('a', { source: 'transcript', blocks: [{ type: 'text', text: 'real' }] })],
|
|
[msg('a', { source: 'scrape', blocks: [{ type: 'text', text: 'scraped' }] })]
|
|
)
|
|
expect(merged[0]!.blocks).toEqual([{ type: 'text', text: 'real' }])
|
|
})
|
|
|
|
it('returns the existing array unchanged for an empty batch', () => {
|
|
const existing = [msg('a')]
|
|
expect(mergeNativeChatMessages(existing, [])).toBe(existing)
|
|
})
|
|
|
|
it('does not mutate the existing array', () => {
|
|
const existing = [msg('a')]
|
|
mergeNativeChatMessages(existing, [msg('b')])
|
|
expect(existing.map((m) => m.id)).toEqual(['a'])
|
|
})
|
|
})
|