workspaceTranscript.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { describe, expect, it } from 'vitest'
  2. import { foldDetail, WorkspaceTranscriptStore, type WorkspaceDetail } from '@/components/workspace/transcriptStore'
  3. import type { WorkspaceConversation, WorkspaceEvent, WorkspaceMessage } from '@shared/workspace'
  4. const conversation: WorkspaceConversation = { id: 'c', kind: 'direct', name: 'Bot', avatar: '', memberIds: ['a'], instructions: '', administratorId: 'a', mode: 'host', autoReplyIds: [], maxReplyRounds: 3, archived: false, pinned: false, readSeq: 0, lastSeq: 3, preview: '', createdAt: 1, updatedAt: 1 }
  5. const message = (id: string, seq: number, content = id): WorkspaceMessage => ({ id, seq, conversationId: 'c', role: 'assistant', content, reasoning: '', status: 'complete', attachments: [], tools: [], createdAt: seq })
  6. const detail = (): WorkspaceDetail => ({ conversation, messages: [message('old', 1), message('live', 2)], cursor: 10, hasOlder: true, run: null, interactions: [], context: null })
  7. const event = (seq: number, value: WorkspaceMessage): WorkspaceEvent => ({ seq, type: 'message.changed', conversationId: value.conversationId, data: value })
  8. describe('Bot transcript cache', () => {
  9. it('returns a warm transcript without losing its older pages and preserves settled identities on a live update', () => {
  10. const store = new WorkspaceTranscriptStore(), initial = detail()
  11. store.hydrate({ agents: [], conversations: [conversation], details: [initial], cursor: 10 })
  12. expect(store.get('c')).toBe(initial)
  13. store.apply(event(11, { ...message('live', 2, '正在生成'), status: 'streaming' }))
  14. expect(store.get('c')!.messages[0]).toBe(initial.messages[0])
  15. expect(store.get('c')!.messages[1]!.content).toBe('正在生成')
  16. expect(store.get('c')!.hasOlder).toBe(true)
  17. })
  18. it('replays events received during a cold detail request instead of rolling back new text', () => {
  19. const store = new WorkspaceTranscriptStore(), token = store.beginRead()
  20. store.apply(event(12, message('live', 2, '更新后的内容')))
  21. const current = store.finishRead(token, detail())
  22. expect(current.messages.at(-1)!.content).toBe('更新后的内容')
  23. })
  24. it('does not apply pre-snapshot replay over a newer snapshot', () => {
  25. const store = new WorkspaceTranscriptStore(), token = store.beginRead()
  26. store.apply(event(9, message('live', 2, '旧事件')))
  27. expect(store.finishRead(token, detail()).messages.at(-1)!.content).toBe('live')
  28. })
  29. it('removes hidden messages, ignores duplicate events, and evicts deleted conversations', () => {
  30. const store = new WorkspaceTranscriptStore()
  31. store.hydrate({ agents: [], conversations: [conversation], details: [detail()], cursor: 10 })
  32. store.apply(event(11, { ...message('live', 2), visible: false }))
  33. store.apply(event(11, message('live', 2)))
  34. expect(store.get('c')!.messages.map(m => m.id)).toEqual(['old'])
  35. store.apply({ seq: 12, type: 'conversation.deleted', conversationId: 'c', data: { id: 'c' } })
  36. expect(store.get('c')).toBeUndefined()
  37. expect(store.conversations).toEqual([])
  38. })
  39. it('does not resurrect a conversation deleted while its detail request was in flight', () => {
  40. const store = new WorkspaceTranscriptStore(), token = store.beginRead()
  41. store.apply({ seq: 12, type: 'conversation.deleted', conversationId: 'c', data: { id: 'c' } })
  42. expect(() => store.finishRead(token, detail())).toThrow('已被删除')
  43. expect(store.get('c')).toBeUndefined()
  44. })
  45. it('keeps background tasks separate and settles approval cards directly from events', () => {
  46. const initial = detail()
  47. expect(foldDetail(initial, event(11, { ...message('other', 3), conversationTaskId: 'other' }))).toBe(initial)
  48. const pending = foldDetail(initial, { seq: 12, conversationId: 'c', type: 'interaction.changed', data: { id: 'approval', kind: 'approval', choices: [], message: '允许?' } })
  49. expect(pending.interactions).toHaveLength(1)
  50. expect(foldDetail(pending, { seq: 13, conversationId: 'c', type: 'interaction.changed', data: { id: 'approval', resolved: true } }).interactions).toHaveLength(0)
  51. })
  52. })