workspacePresentation.test.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { afterEach, describe, expect, it, vi } from 'vitest'
  2. import { createUuid } from '@/utils/id'
  3. import { workspaceMessagesToUi } from '@/components/workspace/viewModels'
  4. afterEach(() => vi.unstubAllGlobals())
  5. describe('workspace chat uses the established presentation', () => {
  6. it('creates valid request ids on LAN HTTP without crypto.randomUUID', () => {
  7. vi.stubGlobal('crypto', { getRandomValues: (bytes: Uint8Array) => { for (let i=0;i<bytes.length;i++) bytes[i]=i; return bytes } })
  8. expect(createUuid()).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/)
  9. })
  10. it('preserves role identity, reasoning, files and tools for MessageTimeline', () => {
  11. const [message] = workspaceMessagesToUi([{id:'m',conversationId:'c',seq:1,role:'assistant',agentId:'a',agentName:'编辑',content:'[报告](/tmp/report.txt)',reasoning:'检查资料',status:'complete',createdAt:10,attachments:[{id:'f',name:'report.txt',mimeType:'text/plain',size:4,createdAt:10,sender:'agent',sourcePath:'/tmp/report.txt'}],tools:[{id:'t',name:'read_file',status:'tool.complete',result:'ok'}]}])
  12. expect(message).toMatchObject({profile:'a',author:'编辑',reasoning:'检查资料',status:'settled',content:'[报告](/api/app/files/f/download)'})
  13. expect(message?.tools?.[0]).toMatchObject({name:'read_file',status:'success',output:'ok'})
  14. expect(message?.attachments?.[0]?.url).toBe('/api/app/files/f/download')
  15. })
  16. it('keeps peer direction outside the body without presenting it as a user message', () => {
  17. const communication = { direction: 'incoming' as const, peerId: 'b', peerName: '研究员', peerAvatar: '', peerKind: 'agent' as const, content: '当前为 v2' }
  18. const [message] = workspaceMessagesToUi([{ id: 'peer', conversationId: 'c', seq: 1, role: 'system', peerMessageId: 'p', communication, content: '来自 Bot「研究员」的回复:当前为 v2', reasoning: '', tools: [], attachments: [], status: 'complete', createdAt: 1 }])
  19. expect(message?.communication).toEqual(communication)
  20. expect(message?.content).toBe('当前为 v2')
  21. expect(message?.role).toBe('system')
  22. })
  23. })
  24. import { mount } from '@vue/test-utils'
  25. import { nextTick } from 'vue'
  26. import ConversationList from '@/components/workspace/ConversationList.vue'
  27. import ResourceSidebar from '@/components/app/ResourceSidebar.vue'
  28. import TeamAvatar from '@/components/common/TeamAvatar.vue'
  29. import type { WorkspaceAgent, WorkspaceConversation } from '../../src/shared/workspace'
  30. it('renders Bot unread as a dot and treats explicit false as authoritative over legacy counts', async () => {
  31. const conversation: WorkspaceConversation = { id: 'dot', kind: 'direct', name: '未读测试', avatar: '', memberIds: [], instructions: '', administratorId: '', mode: 'host', autoReplyIds: [], maxReplyRounds: 1, archived: false, pinned: false, readSeq: 0, lastSeq: 999, unreadCount: 999, unread: true, preview: '完成', createdAt: 1, updatedAt: 1 }
  32. const wrapper = mount(ConversationList, { props: { conversations: [conversation], agents: [] } })
  33. expect(wrapper.find('[aria-label="未读"]').exists()).toBe(true)
  34. expect(wrapper.find('.sidebar-item__row b').exists()).toBe(false)
  35. await wrapper.setProps({ conversations: [{ ...conversation, unread: false }] })
  36. expect(wrapper.find('[aria-label="未读"]').exists()).toBe(false)
  37. await wrapper.setProps({ conversations: [{ ...conversation, unread: undefined }] })
  38. expect(wrapper.find('[aria-label="未读"]').exists()).toBe(true)
  39. wrapper.unmount()
  40. })
  41. it('composes real member avatars and refreshes them without changing group membership', async () => {
  42. const agents: WorkspaceAgent[] = ['first','second'].map((id,index) => ({id,name:id,avatar:`yaoyao-mascot:v1:${index ? 'square' : 'circle'}:377fe6:friendly`,instructions:'',nodeId:'local',profile:'default',archived:false,revision:1,createdAt:1,updatedAt:1}))
  43. const c: WorkspaceConversation = {id:'g',kind:'group',name:'群聊',avatar:'data:image/png;base64,AA==',memberIds:['second','first'],instructions:'',administratorId:'first',mode:'host',autoReplyIds:[],maxReplyRounds:1,archived:false,pinned:false,readSeq:0,lastSeq:0,preview:'',createdAt:Date.now(),updatedAt:Date.now(),lastMessageAt:Date.now()}
  44. const wrapper = mount(ConversationList, {props:{conversations:[c],agents}})
  45. expect(wrapper.getComponent(TeamAvatar).props('members')!.map((m: {name:string}) => m.name)).toEqual(['second','first'])
  46. expect(wrapper.findAll('.team-avatar__member')).toHaveLength(2)
  47. expect(wrapper.find('.team-avatar__image').exists()).toBe(false)
  48. expect(wrapper.find('.sidebar-item__row small').text()).toMatch(/\d{2}:\d{2}/)
  49. await wrapper.setProps({agents:agents.map(a=>a.id==='first'?{...a,avatar:'yaoyao-mascot:v1:triangle:d94b52:curious'}:a)})
  50. expect(wrapper.getComponent(TeamAvatar).props('members')![1]!.avatar).toBe('yaoyao-mascot:v1:triangle:d94b52:curious')
  51. await wrapper.setProps({conversations:[{...c,activeRunId:'run',activeAgentId:'second',activeRunStatus:'running'}]})
  52. expect(wrapper.findAll('.team-avatar__member.agent-avatar--working')).toHaveLength(1)
  53. expect(wrapper.find('.team-avatar__member--1').classes()).toContain('agent-avatar--working')
  54. await wrapper.setProps({conversations:[{...c,activeRunId:'run',activeAgentId:'second',activeRunStatus:'waiting'}]})
  55. expect(wrapper.findAll('.team-avatar__member.agent-avatar--waiting')).toHaveLength(1)
  56. await wrapper.setProps({conversations:[c]})
  57. expect(wrapper.findAll('.team-avatar__member.agent-avatar--working')).toHaveLength(0)
  58. expect(wrapper.findAll('.team-avatar__member.agent-avatar--waiting')).toHaveLength(0)
  59. await wrapper.setProps({conversations:[{...c,kind:'direct',memberIds:['first'],activeRunId:'direct-run',activeAgentId:'first',activeRunStatus:'running'}]})
  60. expect(wrapper.find('.sidebar-item__icon .agent-avatar').exists() || wrapper.find('.sidebar-item .agent-avatar').exists()).toBe(true)
  61. expect(wrapper.find('.sidebar-item .agent-avatar--working').exists()).toBe(true)
  62. await wrapper.setProps({conversations:[{...c,kind:'direct',memberIds:['first'],activeRunId:'direct-run',activeAgentId:'first',activeRunStatus:'waiting'}]})
  63. expect(wrapper.find('.sidebar-item .agent-avatar--waiting').exists()).toBe(true)
  64. wrapper.unmount()
  65. })
  66. it.each([
  67. ['direct', '编辑资料'],
  68. ['group', '群聊设置'],
  69. ] as const)('opens settings for a %s conversation from its actions menu', async (kind, label) => {
  70. const conversation: WorkspaceConversation = {
  71. id: kind,
  72. kind,
  73. name: kind === 'direct' ? '机器人' : '群聊',
  74. avatar: '',
  75. memberIds: [],
  76. instructions: '',
  77. administratorId: '',
  78. mode: 'host',
  79. autoReplyIds: [],
  80. maxReplyRounds: 1,
  81. archived: false,
  82. pinned: false,
  83. readSeq: 0,
  84. lastSeq: 0,
  85. preview: '',
  86. createdAt: 1,
  87. updatedAt: 1,
  88. }
  89. const wrapper = mount(ConversationList, { attachTo: document.body, props: { conversations: [conversation] } })
  90. wrapper.getComponent(ResourceSidebar).vm.$emit('more', conversation.id, new MouseEvent('click', { clientX: 100, clientY: 100 }))
  91. await nextTick()
  92. const menu = document.body.querySelector<HTMLElement>('[aria-label="聊天操作"]')!
  93. const view = [...menu.querySelectorAll<HTMLButtonElement>('button')].find(button => button.textContent === label)!
  94. view.click()
  95. await nextTick()
  96. expect(wrapper.emitted('settings')).toEqual([[conversation.id]])
  97. expect(wrapper.emitted('select')).toBeUndefined()
  98. expect(document.body.querySelector('[aria-label="聊天操作"]')).toBeNull()
  99. wrapper.unmount()
  100. })
  101. it('keeps inline thinking in the process trace and its media out of the message body', () => {
  102. const messages = workspaceMessagesToUi([{
  103. id: 'inline-thinking', conversationId: 'chat', seq: 1, role: 'assistant',
  104. content: '<think>![过程图](/tmp/process.png)</think>[报告](/tmp/report.pdf)',
  105. reasoning: '', status: 'complete', attachments: [], tools: [], createdAt: 1,
  106. }])
  107. expect(messages[0].content).toBe('[报告](/tmp/report.pdf)')
  108. expect(messages[0].reasoning).toBe('![过程图](/tmp/process.png)')
  109. })
  110. it('keeps a Bot busy across group replies until its final active conversation finishes', async () => {
  111. const base: WorkspaceConversation = { id: 'direct', kind: 'direct', name: 'Bot', avatar: '', memberIds: ['a'], instructions: '', administratorId: 'a', mode: 'host', autoReplyIds: [], maxReplyRounds: 1, archived: false, pinned: false, readSeq: 0, lastSeq: 0, preview: '', createdAt: 1, updatedAt: 1 }
  112. const group: WorkspaceConversation = { ...base, id: 'group', kind: 'group', memberIds: ['a', 'b'], activeRunId: 'g-run', activeAgentStates: { a: 'running', b: 'waiting' } }
  113. const other: WorkspaceConversation = { ...group, id: 'other', activeRunId: 'other-run', activeAgentStates: { a: 'waiting' } }
  114. const wrapper = mount(ConversationList, { props: { conversations: [base, group, other, { ...base, id: 'unrelated', memberIds: ['unrelated'] }] } })
  115. const bot = () => wrapper.get('[data-sidebar-id="direct"]')
  116. expect(bot().find('.agent-avatar--working').exists()).toBe(true)
  117. expect(bot().find('.presence--working').exists()).toBe(true)
  118. expect(wrapper.get('[data-sidebar-id="unrelated"]').find('.agent-avatar--working').exists()).toBe(false)
  119. await wrapper.setProps({ conversations: [base, { ...group, activeRunId: undefined, activeAgentStates: {}, avatarSignals: { a: { id: 'done', state: 'success', at: Date.now() } } }, other] })
  120. expect(bot().find('.agent-avatar--waiting').exists()).toBe(true)
  121. await wrapper.setProps({ conversations: [base, { ...other, activeAgentStates: { a: 'queued' } }] })
  122. expect(bot().find('.agent-avatar--loading').exists()).toBe(true)
  123. await wrapper.setProps({ conversations: [base] })
  124. expect(bot().find('.agent-avatar--idle').exists()).toBe(true)
  125. expect(bot().find('.presence--working').exists()).toBe(false)
  126. wrapper.unmount()
  127. })