serviceWarnings.test.ts 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { afterEach, expect, it, vi } from 'vitest'
  2. import { flushPromises, mount } from '@vue/test-utils'
  3. import { createPinia } from 'pinia'
  4. import WorkspaceTimeline from '@/components/workspace/WorkspaceMessageTimeline.vue'
  5. import { workspaceMessagesToUi } from '@/components/workspace/viewModels'
  6. import { WorkspaceTranscriptStore } from '@/components/workspace/transcriptStore'
  7. import { buildMessageTimelineRows } from '@/utils/turnTrace'
  8. import type { WorkspaceMessage } from '@shared/workspace'
  9. const warning={code:'plugin_unavailable',service:'天气服务',message:'连接暂不可用,本次任务继续使用其他能力。'}
  10. const message:WorkspaceMessage={id:'warning-message',conversationId:'chat',agentId:'bot',agentName:'竹儿',role:'assistant',content:'',reasoning:'',status:'complete',seq:1,createdAt:1,tools:[{id:'tool',name:'天气服务',status:'tool.complete'}],attachments:[],serviceWarnings:[warning]}
  11. afterEach(()=>{vi.restoreAllMocks();vi.unstubAllGlobals()})
  12. it('keeps warning-only messages visible independently of tool trace aggregation and model text',()=>{
  13. const [ui]=workspaceMessagesToUi([JSON.parse(JSON.stringify(message))])
  14. expect(ui?.serviceWarnings).toEqual([warning])
  15. expect(ui?.status).toBe('settled')
  16. expect(ui?.error).toBeUndefined()
  17. const rows=buildMessageTimelineRows([ui!])
  18. expect(rows.map(row=>row.kind)).toEqual(['trace','message'])
  19. expect(rows[1]?.kind==='message'&&rows[1].message.serviceWarnings).toEqual([warning])
  20. const [textOnly]=workspaceMessagesToUi([{...message,serviceWarnings:undefined,content:JSON.stringify({serviceWarnings:[warning]})}])
  21. expect(textOnly?.serviceWarnings).toBeUndefined()
  22. })
  23. it.each(['workspace'])('shows non-blocking, escaped service notices with tools hidden in the %s timeline',async(timeline)=>{
  24. vi.stubGlobal('ResizeObserver',class {observe(){}disconnect(){}})
  25. const scrollTo=HTMLElement.prototype.scrollTo
  26. HTMLElement.prototype.scrollTo=vi.fn()
  27. const unsafe={code:'plugin_unavailable',service:'<img src=x onerror="alert(1)">',message:'<script>alert(2)</script> [修复](javascript:alert(3))'}
  28. const messages=workspaceMessagesToUi([{...message,serviceWarnings:[warning,unsafe]}])
  29. const wrapper=mount(WorkspaceTimeline,{props:{identity:'chat',messages,showTools:false},global:{plugins:[createPinia()],stubs:{AppIcon:true,AgentAvatar:true,MarkdownContent:{props:['content'],template:'<div class="fixture-content">{{content}}</div>'}}}})
  30. try{
  31. await flushPromises()
  32. const notices=wrapper.get('[aria-label="服务状态提示"]')
  33. expect(notices.findAll('[role="status"]')).toHaveLength(2)
  34. expect(notices.text()).toContain(`${warning.service}:${warning.message}`)
  35. expect(notices.text()).toContain(`${unsafe.service}:${unsafe.message}`)
  36. expect(notices.find('img,script,a,[role="alert"],details').exists()).toBe(false)
  37. expect(wrapper.find('.turn-trace').exists()).toBe(false)
  38. expect(wrapper.find('.message-failure').exists()).toBe(false)
  39. await wrapper.setProps({messages:workspaceMessagesToUi([{...message,status:'streaming',content:'正在继续处理任务。'}])})
  40. expect(wrapper.text()).toContain('正在继续处理任务。')
  41. expect(wrapper.get('[aria-label="服务状态提示"]').text()).toContain(warning.message)
  42. }finally{wrapper.unmount();HTMLElement.prototype.scrollTo=scrollTo}
  43. })
  44. it('retains service warnings from live transcript events through a history reload',()=>{
  45. const conversation={id:'chat',kind:'direct',memberIds:['bot'],lastSeq:1} as any
  46. const store=new WorkspaceTranscriptStore()
  47. store.hydrate({agents:[],conversations:[conversation],details:[{conversation,messages:[],cursor:0,hasOlder:false,run:null,interactions:[],context:null}],cursor:0})
  48. store.apply({type:'message.changed',seq:1,conversationId:'chat',data:message})
  49. expect(workspaceMessagesToUi(store.get('chat')!.messages)[0]?.serviceWarnings).toEqual([warning])
  50. const refreshed=new WorkspaceTranscriptStore()
  51. refreshed.hydrate({agents:[],conversations:[conversation],details:[JSON.parse(JSON.stringify(store.get('chat')))],cursor:1})
  52. const messages=workspaceMessagesToUi(refreshed.get('chat')!.messages)
  53. expect(messages[0]?.serviceWarnings).toEqual([warning])
  54. expect(buildMessageTimelineRows(messages).some(row=>row.kind==='message'&&row.message.id===message.id)).toBe(true)
  55. })