typingIndicator.test.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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('keeps the workspace header avatar and name in sync with reactive identity props', async () => {
  102. const wrapper = mount(WorkspaceMessageTimeline, {
  103. props: {
  104. identity: 'header-identity',
  105. messages: [],
  106. title: 'Apple Dev',
  107. headerAvatarName: 'Apple Dev',
  108. headerAvatar: 'avatar-initial',
  109. headerAvatarState: 'working',
  110. },
  111. global: { stubs },
  112. })
  113. expect(wrapper.get('.timeline-header h2').text()).toBe('Apple Dev')
  114. expect(wrapper.find('.timeline-state').exists()).toBe(false)
  115. expect(wrapper.text()).not.toContain('已同步')
  116. expect(wrapper.get('.timeline-header .agent-avatar-stub').attributes()).toMatchObject({
  117. 'data-name': 'Apple Dev',
  118. 'data-avatar': 'avatar-initial',
  119. 'data-state': 'working',
  120. })
  121. await wrapper.setProps({
  122. title: 'iOS 发布',
  123. headerAvatarName: 'iOS 发布',
  124. headerAvatar: 'avatar-updated',
  125. headerAvatarState: 'success',
  126. })
  127. expect(wrapper.get('.timeline-header h2').text()).toBe('iOS 发布')
  128. expect(wrapper.get('.timeline-header .agent-avatar-stub').attributes()).toMatchObject({
  129. 'data-name': 'iOS 发布',
  130. 'data-avatar': 'avatar-updated',
  131. 'data-state': 'success',
  132. })
  133. wrapper.unmount()
  134. })
  135. it('uses the current Bot before its first token and follows identity changes instead of old messages', async () => {
  136. const wrapper = mountTimeline(WorkspaceMessageTimeline, [user], true)
  137. await wrapper.setProps({ thinkingIdentity: { name: '当前 Bot', avatar: 'current-avatar' } })
  138. const avatar = () => wrapper.get('[data-testid="chat-run-thinking-dots"] .agent-avatar-stub')
  139. expect(avatar().attributes('data-avatar')).toBe('current-avatar')
  140. await wrapper.setProps({ messages: [streamingAssistant(), user], thinkingIdentity: { name: '接手 Bot', avatar: 'next-avatar' } })
  141. expect(avatar().attributes('data-name')).toBe('接手 Bot')
  142. expect(avatar().attributes('data-avatar')).toBe('next-avatar')
  143. await wrapper.setProps({ thinkingIdentity: { name: '接手 Bot', avatar: 'updated-avatar' } })
  144. expect(avatar().attributes('data-avatar')).toBe('updated-avatar')
  145. wrapper.unmount()
  146. })
  147. it.each(timelines)('reserves the empty logo aspect ratio before the image loads ($name)', ({ component }) => {
  148. const wrapper = mount(component, {
  149. props: {
  150. ...(component === WorkspaceMessageTimeline ? { identity: 'empty-timeline' } : {}),
  151. messages: [],
  152. emptyLogo: true,
  153. },
  154. global: { stubs },
  155. })
  156. const logo = wrapper.get('.new-chat-empty__logo')
  157. expect(logo.attributes()).toMatchObject({ width: '1024', height: '1024' })
  158. expect(wrapper.text()).toContain('聊点什么')
  159. wrapper.unmount()
  160. })
  161. it.each(timelines)('falls back from streaming agent to history agent and default agent ($name)', ({ component }) => {
  162. const streaming = mountTimeline(component, [
  163. user,
  164. { id: 'assistant-history', role: 'assistant', author: '历史助手', profile: 'history-profile', content: '旧回复' },
  165. streamingAssistant(),
  166. ], true)
  167. expect(streaming.get('[data-testid="chat-run-thinking-dots"] .agent-avatar-stub').attributes('data-name')).toBe('流式助手')
  168. streaming.unmount()
  169. const history = mountTimeline(component, [
  170. user,
  171. { id: 'assistant-history', role: 'assistant', author: '历史助手', profile: 'history-profile', content: '旧回复' },
  172. ], true)
  173. expect(history.get('[data-testid="chat-run-thinking-dots"] .agent-avatar-stub').attributes('data-name')).toBe('历史助手')
  174. expect(history.get('[data-testid="chat-run-thinking-dots"] .agent-avatar-stub').attributes('data-avatar')).toBe('avatar-history')
  175. history.unmount()
  176. const fallback = mountTimeline(component, [user], true)
  177. expect(fallback.get('[data-testid="chat-run-thinking-dots"] .agent-avatar-stub').attributes('data-name')).toBe('夭')
  178. expect(fallback.get('[data-testid="chat-run-thinking-dots"] .agent-avatar-stub').attributes('data-avatar')).toBe('')
  179. fallback.unmount()
  180. })
  181. })