typingIndicator.test.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { mount } from '@vue/test-utils'
  2. import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
  3. import { defineComponent, nextTick, type Component } from 'vue'
  4. import TypingIndicator from '@/components/common/TypingIndicator.vue'
  5. import WorkspaceMessageTimeline from '@/components/workspace/WorkspaceMessageTimeline.vue'
  6. import type { UiMessage } from '@/components/messages/types'
  7. const AgentAvatarStub = defineComponent({
  8. props: {
  9. name: { type: String, required: true },
  10. avatar: { type: String, default: '' },
  11. size: { type: Number, required: true },
  12. state: { type: String, required: true },
  13. },
  14. template: `<span class="agent-avatar-stub" :data-name="name" :data-avatar="avatar" :data-size="size" :data-state="state" />`,
  15. })
  16. const stubs = {
  17. AgentAvatar: AgentAvatarStub,
  18. EmptyState: true,
  19. InteractionCard: true,
  20. MarkdownContent: true,
  21. ToolTrace: true,
  22. TurnTrace: true,
  23. WorkspaceApprovalCard: true,
  24. }
  25. const user: UiMessage = { id: 'user', role: 'user', content: '请继续' }
  26. function streamingAssistant(): UiMessage {
  27. return {
  28. id: 'assistant-streaming',
  29. role: 'assistant',
  30. author: '流式助手',
  31. profile: 'streaming-profile',
  32. content: '正文已经开始输出',
  33. reasoning: '思考也已经出现',
  34. status: 'streaming',
  35. tools: [{ id: 'tool', name: 'read_file', status: 'running' }],
  36. }
  37. }
  38. function mountTimeline(component: Component, messages: UiMessage[], thinking: boolean) {
  39. return mount(component, {
  40. props: {
  41. ...(component === WorkspaceMessageTimeline ? { identity: 'timeline' } : {}),
  42. messages,
  43. thinking,
  44. agentAvatars: {
  45. 'streaming-profile': 'avatar-streaming',
  46. 'history-profile': 'avatar-history',
  47. },
  48. },
  49. global: { stubs },
  50. })
  51. }
  52. describe('TypingIndicator', () => {
  53. it('renders a compact avatar, three dots and status semantics', () => {
  54. const wrapper = mount(TypingIndicator, {
  55. props: { avatarName: '丫头', avatar: '' },
  56. })
  57. expect(wrapper.attributes('role')).toBe('status')
  58. expect(wrapper.attributes('aria-live')).toBe('polite')
  59. expect(wrapper.attributes('aria-label')).toBe('机器人正在输入')
  60. expect(wrapper.attributes('data-testid')).toBe('chat-run-thinking-dots')
  61. expect(wrapper.get('.agent-avatar').attributes('style')).toContain('width: 20px')
  62. expect(wrapper.findAll('.thinking-indicator__dot')).toHaveLength(3)
  63. expect(wrapper.text()).not.toMatch(/\d+ 秒/)
  64. wrapper.unmount()
  65. })
  66. })
  67. describe('message timeline typing state', () => {
  68. const timelines: Array<{ name: string; component: Component }> = [
  69. { name: 'workspace', component: WorkspaceMessageTimeline },
  70. ]
  71. beforeEach(() => {
  72. Element.prototype.scrollTo = vi.fn()
  73. vi.stubGlobal('ResizeObserver', class {
  74. observe() {}
  75. unobserve() {}
  76. disconnect() {}
  77. })
  78. })
  79. afterEach(() => {
  80. delete (Element.prototype as { scrollTo?: () => void }).scrollTo
  81. vi.unstubAllGlobals()
  82. vi.useRealTimers()
  83. })
  84. it.each(timelines)('keeps the typing row visible after content, reasoning and tools start ($name)', async ({ component }) => {
  85. const wrapper = mountTimeline(component, [user, streamingAssistant()], true)
  86. const indicator = wrapper.get('[data-testid="chat-run-thinking-dots"]')
  87. expect(indicator.attributes('role')).toBe('status')
  88. expect(indicator.attributes('aria-label')).toBe('机器人正在输入')
  89. expect(wrapper.findAll('.thinking-indicator__dot')).toHaveLength(3)
  90. expect(wrapper.text()).not.toMatch(/\d+ 秒/)
  91. const avatar = wrapper.get('[data-testid="chat-run-thinking-dots"] .agent-avatar-stub')
  92. expect(avatar.attributes('data-name')).toBe('流式助手')
  93. expect(avatar.attributes('data-avatar')).toBe('avatar-streaming')
  94. wrapper.unmount()
  95. })
  96. it.each(timelines)('hides the typing row when the run is no longer thinking ($name)', ({ component }) => {
  97. const wrapper = mountTimeline(component, [user, streamingAssistant()], false)
  98. expect(wrapper.find('[data-testid="chat-run-thinking-dots"]').exists()).toBe(false)
  99. wrapper.unmount()
  100. })
  101. it('shows compression progress before any body text and removes it when the reply settles', async () => {
  102. const message: UiMessage = { id: 'compacting', role: 'assistant', content: '', status: 'streaming', activity: 'compacting' }
  103. const wrapper = mountTimeline(WorkspaceMessageTimeline, [user, message], true)
  104. expect(wrapper.get('.message__activity').text()).toBe('正在整理历史对话…')
  105. expect(wrapper.get('.message__activity').attributes('role')).toBe('status')
  106. await wrapper.setProps({ messages: [user, { ...message, status: 'settled', content: '回复完成' }], thinking: false })
  107. expect(wrapper.find('.message__activity').exists()).toBe(false)
  108. wrapper.unmount()
  109. })
  110. it('keeps the workspace header avatar and name in sync with reactive identity props', async () => {
  111. const wrapper = mount(WorkspaceMessageTimeline, {
  112. props: {
  113. identity: 'header-identity',
  114. messages: [],
  115. title: 'Apple Dev',
  116. headerAvatarName: 'Apple Dev',
  117. headerAvatar: 'avatar-initial',
  118. headerAvatarState: 'working',
  119. },
  120. global: { stubs },
  121. })
  122. expect(wrapper.get('.timeline-header h2').text()).toBe('Apple Dev')
  123. expect(wrapper.find('.timeline-state').exists()).toBe(false)
  124. expect(wrapper.text()).not.toContain('已同步')
  125. expect(wrapper.get('.timeline-header .agent-avatar-stub').attributes()).toMatchObject({
  126. 'data-name': 'Apple Dev',
  127. 'data-avatar': 'avatar-initial',
  128. 'data-state': 'working',
  129. })
  130. await wrapper.setProps({
  131. title: 'iOS 发布',
  132. headerAvatarName: 'iOS 发布',
  133. headerAvatar: 'avatar-updated',
  134. headerAvatarState: 'success',
  135. })
  136. expect(wrapper.get('.timeline-header h2').text()).toBe('iOS 发布')
  137. expect(wrapper.get('.timeline-header .agent-avatar-stub').attributes()).toMatchObject({
  138. 'data-name': 'iOS 发布',
  139. 'data-avatar': 'avatar-updated',
  140. 'data-state': 'success',
  141. })
  142. wrapper.unmount()
  143. })
  144. it('uses the current Bot before its first token and follows identity changes instead of old messages', async () => {
  145. const wrapper = mountTimeline(WorkspaceMessageTimeline, [user], true)
  146. await wrapper.setProps({ thinkingIdentity: { name: '当前 Bot', avatar: 'current-avatar' } })
  147. const avatar = () => wrapper.get('[data-testid="chat-run-thinking-dots"] .agent-avatar-stub')
  148. expect(avatar().attributes('data-avatar')).toBe('current-avatar')
  149. await wrapper.setProps({ messages: [streamingAssistant(), user], thinkingIdentity: { name: '接手 Bot', avatar: 'next-avatar' } })
  150. expect(avatar().attributes('data-name')).toBe('接手 Bot')
  151. expect(avatar().attributes('data-avatar')).toBe('next-avatar')
  152. await wrapper.setProps({ thinkingIdentity: { name: '接手 Bot', avatar: 'updated-avatar' } })
  153. expect(avatar().attributes('data-avatar')).toBe('updated-avatar')
  154. wrapper.unmount()
  155. })
  156. it.each(timelines)('reserves the empty logo aspect ratio before the image loads ($name)', ({ component }) => {
  157. const wrapper = mount(component, {
  158. props: {
  159. ...(component === WorkspaceMessageTimeline ? { identity: 'empty-timeline' } : {}),
  160. messages: [],
  161. emptyLogo: true,
  162. },
  163. global: { stubs },
  164. })
  165. const logo = wrapper.get('.new-chat-empty__logo')
  166. expect(logo.attributes()).toMatchObject({ width: '1024', height: '1024' })
  167. expect(wrapper.text()).toContain('聊点什么')
  168. wrapper.unmount()
  169. })
  170. it.each(timelines)('falls back from streaming agent to history agent and default agent ($name)', ({ component }) => {
  171. const streaming = mountTimeline(component, [
  172. user,
  173. { id: 'assistant-history', role: 'assistant', author: '历史助手', profile: 'history-profile', content: '旧回复' },
  174. streamingAssistant(),
  175. ], true)
  176. expect(streaming.get('[data-testid="chat-run-thinking-dots"] .agent-avatar-stub').attributes('data-name')).toBe('流式助手')
  177. streaming.unmount()
  178. const history = mountTimeline(component, [
  179. user,
  180. { id: 'assistant-history', role: 'assistant', author: '历史助手', profile: 'history-profile', content: '旧回复' },
  181. ], true)
  182. expect(history.get('[data-testid="chat-run-thinking-dots"] .agent-avatar-stub').attributes('data-name')).toBe('历史助手')
  183. expect(history.get('[data-testid="chat-run-thinking-dots"] .agent-avatar-stub').attributes('data-avatar')).toBe('avatar-history')
  184. history.unmount()
  185. const fallback = mountTimeline(component, [user], true)
  186. expect(fallback.get('[data-testid="chat-run-thinking-dots"] .agent-avatar-stub').attributes('data-name')).toBe('夭')
  187. expect(fallback.get('[data-testid="chat-run-thinking-dots"] .agent-avatar-stub').attributes('data-avatar')).toBe('')
  188. fallback.unmount()
  189. })
  190. })