conversation.client.spec.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /** Chat-owned event-to-view projection. */
  2. import { describe, expect, it } from 'vitest'
  3. import { AttachmentId } from '@deepseek-ai/dsh-attachment'
  4. import type { ContentBlock } from '@deepseek-ai/dsh-api-remotes/client'
  5. import {
  6. displayFailure, emptyAssistantBlock, toAssistantBlock, toAssistantBlocks,
  7. isTokenDelta,
  8. } from '../src/client/conversation-nodes/event-projection.ts'
  9. describe('toAssistantBlock', () => {
  10. it('classifies the four block shapes', () => {
  11. const attachment = {
  12. attachmentId: AttachmentId(`sha256:${'a'.repeat(64)}`),
  13. mediaType: 'image/png' as const,
  14. bytes: 68,
  15. width: 1,
  16. height: 1,
  17. }
  18. const blocks: ContentBlock[] = [
  19. { type: 'text', text: '正文' },
  20. { type: 'reasoning', text: '思考' },
  21. { type: 'tool-call', id: 'c1', name: 'echo', arguments: '{}' } as ContentBlock,
  22. { type: 'image', attachment },
  23. ]
  24. expect(toAssistantBlocks(blocks)).toEqual([
  25. { kind: 'text', text: '正文' },
  26. { kind: 'reasoning', text: '思考' },
  27. { kind: 'tool-call', callId: 'c1', name: 'echo', argsRaw: '{}' },
  28. { kind: 'image', attachment },
  29. ])
  30. expect(toAssistantBlock(blocks[0] as ContentBlock)).toEqual({ kind: 'text', text: '正文' })
  31. expect(toAssistantBlock({ type: 'future' } as unknown as ContentBlock))
  32. .toEqual({ kind: 'other', block: { type: 'future' } })
  33. })
  34. it('creates empty streamed block projections', () => {
  35. expect(emptyAssistantBlock('text')).toEqual({ kind: 'text', text: '' })
  36. expect(emptyAssistantBlock('reasoning')).toEqual({ kind: 'reasoning', text: '' })
  37. expect(emptyAssistantBlock('tool-call')).toEqual({ kind: 'tool-call', callId: '', name: '', argsRaw: '' })
  38. expect(emptyAssistantBlock('future')).toEqual({ kind: 'other', block: null })
  39. })
  40. it('redacts auth failures and presents the remaining durable values', () => {
  41. expect(displayFailure({ code: 'AUTH', message: 'secret' })).toEqual({ code: 'AUTH', message: '' })
  42. expect(displayFailure({ code: 'TRANSPORT', message: 'offline' }))
  43. .toEqual({ code: 'TRANSPORT', message: 'offline' })
  44. expect(displayFailure({ code: 'UNKNOWN' })).toEqual({ code: 'UNKNOWN', message: '{"code":"UNKNOWN"}' })
  45. expect(displayFailure(null)).toEqual({ message: 'null' })
  46. })
  47. it('recognizes only non-empty token deltas', () => {
  48. expect(isTokenDelta({ type: 'text-delta', index: 0, text: 'x' } as never)).toBe(true)
  49. expect(isTokenDelta({ type: 'reasoning-delta', index: 0, text: '' } as never)).toBe(false)
  50. expect(isTokenDelta({ type: 'tool-call-delta', index: 0, id: 'c', argumentsDelta: '', name: 'tool' } as never)).toBe(true)
  51. expect(isTokenDelta({ type: 'tool-call-delta', index: 0, id: 'c', argumentsDelta: '' } as never)).toBe(false)
  52. expect(isTokenDelta({ type: 'finish', reason: 'stop' } as never)).toBe(false)
  53. })
  54. })