groupHost.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { mount } from '@vue/test-utils'
  2. import { nextTick } from 'vue'
  3. import { describe, expect, it } from 'vitest'
  4. import type { GroupAgent, GroupMessage } from '@shared/types'
  5. import CreateGroupDialog from '@/components/groups/CreateGroupDialog.vue'
  6. import GroupManager from '@/components/groups/GroupManager.vue'
  7. import { groupMessageToUi } from '@/components/workspace/viewModels'
  8. const profiles = (...names: string[]) => names.map(name => ({
  9. id: `local|${name}`, profile: name, displayName: name, nodeId: 'local', nodeLabel: '当前 Hermes',
  10. }))
  11. function agent(id: string, profile: string, displayName: string, isHost: boolean, replyWithoutMention: boolean): GroupAgent {
  12. return {
  13. id, roomId: 'room-1', profile, nodeId: 'local', nodeLabel: '', displayName, description: `${displayName} 的职责`, storedSessionId: null,
  14. lastContextMessageSeq: 0, enabled: true, replyWithoutMention, isHost,
  15. model: null, provider: null, reasoningEffort: null, fastMode: null,
  16. createdAt: 1, updatedAt: 1, status: 'idle',
  17. }
  18. }
  19. describe('group host controls', () => {
  20. it('keeps same-name Profiles distinct across paired Hermes nodes', async () => {
  21. const local = profiles('default')[0]!
  22. const remote = {
  23. id: '11111111-1111-4111-8111-111111111111|default',
  24. profile: 'default',
  25. displayName: '远端夭夭',
  26. nodeId: '11111111-1111-4111-8111-111111111111',
  27. nodeLabel: 'MacBook Pro',
  28. }
  29. const wrapper = mount(CreateGroupDialog, {
  30. attachTo: document.body,
  31. props: { open: true, profiles: [local, remote], hostEnabled: true },
  32. global: { stubs: { teleport: true } },
  33. })
  34. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('跨节点群')
  35. await wrapper.findAll('.agent-picker > button')[1]!.trigger('click')
  36. await wrapper.get('.solid-button').trigger('click')
  37. expect(wrapper.emitted('create')?.[0]?.[0]).toMatchObject({
  38. members: [local, remote],
  39. hostProfile: local.id,
  40. })
  41. wrapper.unmount()
  42. })
  43. it('keeps one independent host selection when creating a v5 room', async () => {
  44. const wrapper = mount(CreateGroupDialog, {
  45. attachTo: document.body,
  46. props: { open: true, profiles: profiles('yaoyao', 'yaoer'), hostEnabled: true },
  47. global: { stubs: { teleport: true } },
  48. })
  49. await nextTick()
  50. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('主持人验收')
  51. const profileButtons = wrapper.findAll('.agent-picker > button')
  52. await profileButtons[1]!.trigger('click')
  53. const host = wrapper.get<HTMLSelectElement>('select[aria-label="主持人"]')
  54. await host.setValue('local|yaoer')
  55. await profileButtons[1]!.trigger('click')
  56. expect(host.element.value).toBe('local|yaoyao')
  57. await profileButtons[1]!.trigger('click')
  58. await host.setValue('local|yaoer')
  59. expect(wrapper.get<HTMLInputElement>('input[aria-label="所有成员无需 @ 也回复"]').element.checked).toBe(true)
  60. await wrapper.get('.solid-button').trigger('click')
  61. expect(wrapper.emitted('create')?.[0]?.[0]).toEqual({
  62. name: '主持人验收', members: profiles('yaoyao', 'yaoer'), hostProfile: 'local|yaoer', autoReply: true, replyRounds: 3,
  63. })
  64. wrapper.unmount()
  65. })
  66. it('keeps the legacy v4 create UI and payload free of host fields', async () => {
  67. const wrapper = mount(CreateGroupDialog, {
  68. attachTo: document.body,
  69. props: { open: true, profiles: profiles('yaoyao'), hostEnabled: false },
  70. global: { stubs: { teleport: true } },
  71. })
  72. await nextTick()
  73. expect(wrapper.find('select[aria-label="主持人"]').exists()).toBe(false)
  74. expect(wrapper.find('input[aria-label="启用自动回复"]').exists()).toBe(true)
  75. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('v4 房间')
  76. await wrapper.get('.solid-button').trigger('click')
  77. expect(wrapper.emitted('create')?.[0]?.[0]).not.toHaveProperty('hostProfile')
  78. expect(wrapper.emitted('create')?.[0]?.[0]).not.toHaveProperty('orchestrationMode')
  79. expect(wrapper.emitted('create')?.[0]?.[0]).not.toHaveProperty('instructions')
  80. wrapper.unmount()
  81. })
  82. it('offers host flow only when the server advertises the capability', async () => {
  83. const wrapper = mount(CreateGroupDialog, {
  84. attachTo: document.body,
  85. props: {
  86. open: true,
  87. profiles: profiles('yaoyao', 'yaoer'),
  88. hostEnabled: true,
  89. hostFlowEnabled: true,
  90. roomInstructionsEnabled: true,
  91. },
  92. global: { stubs: { teleport: true } },
  93. })
  94. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('顺序协作')
  95. await wrapper.get<HTMLTextAreaElement>('textarea').setValue('先核对事实,再输出中文结论。')
  96. await wrapper.get<HTMLSelectElement>('select[aria-label="协作模式"]').setValue('host')
  97. await wrapper.get('.solid-button').trigger('click')
  98. expect(wrapper.emitted('create')?.[0]?.[0]).toMatchObject({
  99. name: '顺序协作',
  100. instructions: '先核对事实,再输出中文结论。',
  101. orchestrationMode: 'host',
  102. })
  103. wrapper.unmount()
  104. })
  105. it('emits one host promotion and explains the independent auto-reply setting', async () => {
  106. const first = agent('agent-1', 'yaoyao', '夭夭', true, true)
  107. const second = agent('agent-2', 'yaoer', '瑶儿', false, false)
  108. const wrapper = mount(GroupManager, {
  109. attachTo: document.body,
  110. props: {
  111. room: { id: 'room-1', name: '群聊', memberIds: [first.id, second.id], replyRounds: 3 },
  112. agents: [first, second],
  113. hostEnabled: true,
  114. },
  115. })
  116. const selector = wrapper.get<HTMLSelectElement>('select[aria-label="主持人"]')
  117. expect(selector.element.value).toBe('agent-1')
  118. expect(wrapper.findAll('.host-badge')).toHaveLength(1)
  119. await selector.setValue('agent-2')
  120. expect(wrapper.emitted('updateAgent')?.[0]).toEqual(['agent-2', { isHost: true }])
  121. await wrapper.setProps({ agents: [{ ...first, isHost: false }, { ...second, isHost: true }] })
  122. await wrapper.get('button[aria-label="设置瑶儿"]').trigger('click')
  123. await nextTick()
  124. const dialog = document.querySelector('[role="dialog"][aria-label="瑶儿 Agent 设置"]')
  125. expect(dialog?.textContent).toContain('主持人始终处理用户无 @ 消息')
  126. expect(dialog?.querySelector('input[aria-label="无需 @ 也回复"]')).not.toBeNull()
  127. wrapper.unmount()
  128. })
  129. it('blocks removing a host when no enabled replacement exists and shows manager errors', () => {
  130. const first = agent('agent-1', 'yaoyao', '夭夭', true, true)
  131. const second = { ...agent('agent-2', 'yaoer', '瑶儿', false, false), enabled: false }
  132. const wrapper = mount(GroupManager, {
  133. props: {
  134. room: { id: 'room-1', name: '群聊', memberIds: [first.id, second.id], replyRounds: 3 },
  135. agents: [first, second],
  136. hostEnabled: true,
  137. managerError: '需要另一位已启用成员',
  138. },
  139. })
  140. const removeHost = wrapper.get<HTMLButtonElement>('button[aria-label="移除夭夭"]')
  141. expect(removeHost.element.disabled).toBe(true)
  142. expect(removeHost.attributes('title')).toContain('已启用成员')
  143. expect(wrapper.get('[role="alert"]').text()).toBe('需要另一位已启用成员')
  144. })
  145. it('emits a room update when switching collaboration mode', async () => {
  146. const first = agent('agent-1', 'yaoyao', '夭夭', true, false)
  147. const wrapper = mount(GroupManager, {
  148. props: {
  149. room: { id: 'room-1', name: '群聊', memberIds: [first.id], replyRounds: 3, orchestrationMode: 'free' },
  150. agents: [first],
  151. hostEnabled: true,
  152. hostFlowEnabled: true,
  153. },
  154. })
  155. await wrapper.get<HTMLSelectElement>('select[aria-label="协作模式"]').setValue('host')
  156. expect(wrapper.emitted('updateRoom')?.[0]?.[0]).toEqual({
  157. name: '群聊',
  158. replyRounds: 3,
  159. orchestrationMode: 'host',
  160. })
  161. })
  162. it('edits room instructions when the capability is available', async () => {
  163. const first = agent('agent-1', 'yaoyao', '夭夭', true, false)
  164. const wrapper = mount(GroupManager, {
  165. props: {
  166. room: {
  167. id: 'room-1', name: '群聊', memberIds: [first.id], replyRounds: 3,
  168. orchestrationMode: 'free', instructions: '旧规则',
  169. },
  170. agents: [first],
  171. hostEnabled: true,
  172. hostFlowEnabled: true,
  173. roomInstructionsEnabled: true,
  174. },
  175. })
  176. const instructions = wrapper.get<HTMLTextAreaElement>('textarea[aria-label="房间说明"]')
  177. await instructions.setValue('新规则\n输出中文')
  178. await instructions.trigger('change')
  179. expect(wrapper.emitted('updateRoom')?.at(-1)?.[0]).toMatchObject({
  180. instructions: '新规则\n输出中文',
  181. })
  182. })
  183. it('only allows remote Agents to rename themselves within the room', async () => {
  184. const local = agent('agent-1', 'yaoyao', '夭夭', false, false)
  185. const remote = {
  186. ...agent('agent-2', 'yaoer', '远端夭夭', false, false),
  187. nodeId: '11111111-1111-4111-8111-111111111111',
  188. nodeLabel: '工作室 Mac',
  189. }
  190. const wrapper = mount(GroupManager, {
  191. props: {
  192. room: { id: 'room-1', name: '群聊', memberIds: [local.id, remote.id], replyRounds: 3 },
  193. agents: [local, remote],
  194. remoteServerAddresses: { [remote.nodeId]: '192.168.1.20:9119' },
  195. },
  196. global: { stubs: { teleport: true } },
  197. })
  198. await wrapper.get('button[aria-label="设置远端夭夭"]').trigger('click')
  199. const remoteName = wrapper.get<HTMLInputElement>('input[aria-label="群内名称"]')
  200. await remoteName.setValue('群内审查员')
  201. await wrapper.get('.save-agent').trigger('click')
  202. expect(wrapper.emitted('updateAgent')?.at(-1)).toEqual([
  203. remote.id,
  204. { displayName: '群内审查员' },
  205. ])
  206. expect(wrapper.text()).toContain('192.168.1.20:9119')
  207. await wrapper.get('button[aria-label="设置夭夭"]').trigger('click')
  208. expect(wrapper.find('input[aria-label="群内名称"]').exists()).toBe(false)
  209. })
  210. it('uses the synced remote Agent name and marks its topic message', () => {
  211. const remote = {
  212. ...agent('agent-remote', 'reviewer', '群内审查员', false, false),
  213. nodeId: '11111111-1111-4111-8111-111111111111',
  214. nodeLabel: '工作室 Mac',
  215. }
  216. const message: GroupMessage = {
  217. seq: 1,
  218. id: 'message-1',
  219. roomId: 'room-1',
  220. senderKind: 'agent',
  221. senderId: remote.id,
  222. senderName: '旧名称',
  223. rootMessageId: 'message-1',
  224. content: '已完成',
  225. reasoning: '',
  226. toolState: [],
  227. status: 'completed',
  228. error: '',
  229. createdAt: 1,
  230. updatedAt: 1,
  231. }
  232. expect(groupMessageToUi(message, [remote])).toMatchObject({
  233. author: '群内审查员',
  234. isRemoteAgent: true,
  235. })
  236. })
  237. })