systemMessageNotice.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { describe, expect, it, vi } from 'vitest'
  2. import { nextTick } from 'vue'
  3. import { mount } from '@vue/test-utils'
  4. import { systemMessageNotice } from '@/utils/systemMessageNotice'
  5. import WorkspaceMessageTimeline from '@/components/workspace/WorkspaceMessageTimeline.vue'
  6. import MessageTimeline from '@/components/messages/MessageTimeline.vue'
  7. import type { UiMessage } from '@/components/messages/types'
  8. const assignment: UiMessage = { id: 'assigned', role: 'system', content: '由管理员分派的子任务:终审文章\n\n- 核验来源\n- 检查配图' }
  9. describe('system event disclosures', () => {
  10. it('classifies role-only assignments without treating real replies as notices', () => {
  11. expect(systemMessageNotice(assignment)).toEqual({ title: '已分派子任务', icon: 'branch' })
  12. expect(systemMessageNotice({ ...assignment, role: 'assistant' })).toBeUndefined()
  13. expect(systemMessageNotice({ ...assignment, role: 'user' })).toBeUndefined()
  14. expect(systemMessageNotice({ ...assignment, communication: { direction: 'incoming', peerId: 'p', peerName: '竹儿', peerAvatar: '', peerKind: 'agent', content: assignment.content } })).toBeUndefined()
  15. })
  16. it('distinguishes background completion, interruption and missing exit status', () => {
  17. const message: UiMessage = { id: 'process', role: 'system', content: 'process', timelineKind: 'background-process' }
  18. expect(systemMessageNotice(message)?.title).toBe('后台子任务已结束')
  19. expect(systemMessageNotice({ ...message, timelineMetadata: { exit_code: null } })?.title).toBe('后台子任务已结束')
  20. expect(systemMessageNotice({ ...message, timelineMetadata: { exit_code: 0 } })?.title).toBe('后台子任务已完成')
  21. expect(systemMessageNotice({ ...message, timelineMetadata: { exit_code: 1 } })?.title).toBe('后台子任务失败')
  22. expect(systemMessageNotice({ ...message, timelineMetadata: { exit_code: 143, signal: 'SIGTERM' } })?.title).toBe('后台子任务已终止')
  23. })
  24. it.each([MessageTimeline, WorkspaceMessageTimeline])('keeps full requirements behind a short title in both timelines', async component => {
  25. vi.stubGlobal('ResizeObserver', class { observe() {} disconnect() {} })
  26. Element.prototype.scrollTo = vi.fn()
  27. const wrapper = mount(component, { props: { identity: 'test', messages: [assignment] }, global: { stubs: { AgentAvatar: true, MarkdownContent: { props: ['content'], template: '<p>{{ content }}</p>' } } } })
  28. expect(wrapper.get('summary').text()).toBe('已分派子任务')
  29. expect(wrapper.get('details').attributes('open')).toBeUndefined()
  30. expect(wrapper.get('.system-message-notice__details').text()).toContain('核验来源')
  31. expect(wrapper.find('.message__content').exists()).toBe(false)
  32. await nextTick()
  33. wrapper.unmount()
  34. vi.unstubAllGlobals()
  35. })
  36. })