workspacePresentation.test.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. })
  17. import { mount } from '@vue/test-utils'
  18. import { nextTick } from 'vue'
  19. import ConversationList from '@/components/workspace/ConversationList.vue'
  20. import ResourceSidebar from '@/components/app/ResourceSidebar.vue'
  21. import TeamAvatar from '@/components/common/TeamAvatar.vue'
  22. import type { WorkspaceAgent, WorkspaceConversation } from '../../src/shared/workspace'
  23. it('renders Bot unread as a dot and treats explicit false as authoritative over legacy counts', async () => {
  24. 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 }
  25. const wrapper = mount(ConversationList, { props: { conversations: [conversation], agents: [] } })
  26. expect(wrapper.find('[aria-label="未读"]').exists()).toBe(true)
  27. expect(wrapper.find('.sidebar-item__row b').exists()).toBe(false)
  28. await wrapper.setProps({ conversations: [{ ...conversation, unread: false }] })
  29. expect(wrapper.find('[aria-label="未读"]').exists()).toBe(false)
  30. await wrapper.setProps({ conversations: [{ ...conversation, unread: undefined }] })
  31. expect(wrapper.find('[aria-label="未读"]').exists()).toBe(true)
  32. wrapper.unmount()
  33. })
  34. it('composes real member avatars and refreshes them without changing group membership', async () => {
  35. 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}))
  36. 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()}
  37. const wrapper = mount(ConversationList, {props:{conversations:[c],agents}})
  38. expect(wrapper.getComponent(TeamAvatar).props('members')!.map((m: {name:string}) => m.name)).toEqual(['second','first'])
  39. expect(wrapper.findAll('.team-avatar__member')).toHaveLength(2)
  40. expect(wrapper.find('.team-avatar__image').exists()).toBe(false)
  41. expect(wrapper.find('.sidebar-item__row small').text()).toMatch(/\d{2}:\d{2}/)
  42. await wrapper.setProps({agents:agents.map(a=>a.id==='first'?{...a,avatar:'yaoyao-mascot:v1:triangle:d94b52:curious'}:a)})
  43. expect(wrapper.getComponent(TeamAvatar).props('members')![1]!.avatar).toBe('yaoyao-mascot:v1:triangle:d94b52:curious')
  44. await wrapper.setProps({conversations:[{...c,activeRunId:'run',activeAgentId:'second',activeRunStatus:'running'}]})
  45. expect(wrapper.findAll('.team-avatar__member.agent-avatar--working')).toHaveLength(1)
  46. expect(wrapper.find('.team-avatar__member--1').classes()).toContain('agent-avatar--working')
  47. await wrapper.setProps({conversations:[{...c,activeRunId:'run',activeAgentId:'second',activeRunStatus:'waiting'}]})
  48. expect(wrapper.findAll('.team-avatar__member.agent-avatar--waiting')).toHaveLength(1)
  49. await wrapper.setProps({conversations:[c]})
  50. expect(wrapper.findAll('.team-avatar__member.agent-avatar--working')).toHaveLength(0)
  51. expect(wrapper.findAll('.team-avatar__member.agent-avatar--waiting')).toHaveLength(0)
  52. await wrapper.setProps({conversations:[{...c,kind:'direct',memberIds:['first'],activeRunId:'direct-run',activeAgentId:'first',activeRunStatus:'running'}]})
  53. expect(wrapper.find('.sidebar-item__icon .agent-avatar').exists() || wrapper.find('.sidebar-item .agent-avatar').exists()).toBe(true)
  54. expect(wrapper.find('.sidebar-item .agent-avatar--working').exists()).toBe(true)
  55. await wrapper.setProps({conversations:[{...c,kind:'direct',memberIds:['first'],activeRunId:'direct-run',activeAgentId:'first',activeRunStatus:'waiting'}]})
  56. expect(wrapper.find('.sidebar-item .agent-avatar--waiting').exists()).toBe(true)
  57. wrapper.unmount()
  58. })
  59. it.each([
  60. ['direct', '机器人设置'],
  61. ['group', '群聊设置'],
  62. ] as const)('opens settings for a %s conversation from its actions menu', async (kind, label) => {
  63. const conversation: WorkspaceConversation = {
  64. id: kind,
  65. kind,
  66. name: kind === 'direct' ? '机器人' : '群聊',
  67. avatar: '',
  68. memberIds: [],
  69. instructions: '',
  70. administratorId: '',
  71. mode: 'host',
  72. autoReplyIds: [],
  73. maxReplyRounds: 1,
  74. archived: false,
  75. pinned: false,
  76. readSeq: 0,
  77. lastSeq: 0,
  78. preview: '',
  79. createdAt: 1,
  80. updatedAt: 1,
  81. }
  82. const wrapper = mount(ConversationList, { attachTo: document.body, props: { conversations: [conversation] } })
  83. wrapper.getComponent(ResourceSidebar).vm.$emit('more', conversation.id, new MouseEvent('click', { clientX: 100, clientY: 100 }))
  84. await nextTick()
  85. const menu = document.body.querySelector<HTMLElement>('[aria-label="聊天操作"]')!
  86. const view = [...menu.querySelectorAll<HTMLButtonElement>('button')].find(button => button.textContent === label)!
  87. view.click()
  88. await nextTick()
  89. expect(wrapper.emitted('settings')).toEqual([[conversation.id]])
  90. expect(wrapper.emitted('select')).toBeUndefined()
  91. expect(document.body.querySelector('[aria-label="聊天操作"]')).toBeNull()
  92. wrapper.unmount()
  93. })
  94. it('keeps inline thinking in the process trace and its media out of the message body', () => {
  95. const messages = workspaceMessagesToUi([{
  96. id: 'inline-thinking', conversationId: 'chat', seq: 1, role: 'assistant',
  97. content: '<think>![过程图](/tmp/process.png)</think>[报告](/tmp/report.pdf)',
  98. reasoning: '', status: 'complete', attachments: [], tools: [], createdAt: 1,
  99. }])
  100. expect(messages[0].content).toBe('[报告](/tmp/report.pdf)')
  101. expect(messages[0].reasoning).toBe('![过程图](/tmp/process.png)')
  102. })
  103. it('keeps a Bot busy across group replies until its final active conversation finishes', async () => {
  104. 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 }
  105. const group: WorkspaceConversation = { ...base, id: 'group', kind: 'group', memberIds: ['a', 'b'], activeRunId: 'g-run', activeAgentStates: { a: 'running', b: 'waiting' } }
  106. const other: WorkspaceConversation = { ...group, id: 'other', activeRunId: 'other-run', activeAgentStates: { a: 'waiting' } }
  107. const wrapper = mount(ConversationList, { props: { conversations: [base, group, other, { ...base, id: 'unrelated', memberIds: ['unrelated'] }] } })
  108. const bot = () => wrapper.get('[data-sidebar-id="direct"]')
  109. expect(bot().find('.agent-avatar--working').exists()).toBe(true)
  110. expect(bot().find('.presence--working').exists()).toBe(true)
  111. expect(wrapper.get('[data-sidebar-id="unrelated"]').find('.agent-avatar--working').exists()).toBe(false)
  112. await wrapper.setProps({ conversations: [base, { ...group, activeRunId: undefined, activeAgentStates: {}, avatarSignals: { a: { id: 'done', state: 'success', at: Date.now() } } }, other] })
  113. expect(bot().find('.agent-avatar--waiting').exists()).toBe(true)
  114. await wrapper.setProps({ conversations: [base, { ...other, activeAgentStates: { a: 'queued' } }] })
  115. expect(bot().find('.agent-avatar--loading').exists()).toBe(true)
  116. await wrapper.setProps({ conversations: [base] })
  117. expect(bot().find('.agent-avatar--idle').exists()).toBe(true)
  118. expect(bot().find('.presence--working').exists()).toBe(false)
  119. wrapper.unmount()
  120. })