history-records.client.spec.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /** Packed history records become one event-shaped Client value per wire record. */
  2. import { describe, expect, it } from 'vitest'
  3. import { CallId } from '@deepseek-ai/dsh-llm/brand'
  4. import type { SessionHistoryRecord } from '../src/types.ts'
  5. import {
  6. historyEntries,
  7. historyRecordFirstSeq,
  8. historyRecordLastSeq,
  9. } from '../src/client/sessions/history-records.ts'
  10. describe('Session history record projection', () => {
  11. it('retains an ordinary event and its point cursor', () => {
  12. const ordinary: SessionHistoryRecord = {
  13. type: 'event',
  14. event: { type: 'turn/start', seq: 7, time: 1, data: { turn: 1 } },
  15. }
  16. const records = [ordinary]
  17. const [entry] = historyEntries(records)
  18. expect(historyEntries(records)).toBe(records)
  19. expect(entry).toBe(ordinary)
  20. expect(historyRecordFirstSeq(ordinary)).toBe(7)
  21. expect(entry?.event.time).toBe(1)
  22. expect(historyRecordLastSeq(ordinary)).toBe(7)
  23. })
  24. it('retains one packed text row without copying or reshaping it', () => {
  25. const packed: SessionHistoryRecord = {
  26. type: 'chunks',
  27. event: {
  28. type: 'chunkrow/text-chunks',
  29. seq: 11,
  30. time: 20,
  31. data: { turn: 1, step: 2, index: 0, dt: [1, 2, 3], texts: ['a', 'b', 'c', 'd'] },
  32. },
  33. }
  34. const [entry] = historyEntries([packed])
  35. if (entry?.type !== 'chunks') throw new Error('expected packed history entry')
  36. const { event } = entry
  37. expect(entry).toBe(packed)
  38. expect(event).toBe(packed.event)
  39. expect(historyRecordFirstSeq(packed)).toBe(11)
  40. expect(event.time).toBe(20)
  41. expect(historyRecordLastSeq(packed)).toBe(14)
  42. })
  43. it('preserves a packed tool-call row and optional-name absence', () => {
  44. const packed: SessionHistoryRecord = {
  45. type: 'chunks',
  46. event: {
  47. type: 'chunkrow/tool-call-chunks',
  48. seq: 20,
  49. time: 200,
  50. data: {
  51. turn: 2,
  52. step: 4,
  53. index: 1,
  54. id: CallId('call-1'),
  55. dt: [2, 3],
  56. args: ['', '{"x":', '1}'],
  57. },
  58. },
  59. }
  60. const [entry] = historyEntries([packed])
  61. if (entry?.type !== 'chunks') throw new Error('expected packed history entry')
  62. const { event } = entry
  63. if (event.type !== 'chunkrow/tool-call-chunks') throw new Error('expected packed history event')
  64. expect(event).toBe(packed.event)
  65. expect(Object.hasOwn(event.data, 'name')).toBe(false)
  66. expect(historyRecordLastSeq(packed)).toBe(22)
  67. })
  68. })