groupHost.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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('adds an Agent from a paired node to an existing group', async () => {
  44. const local = agent('agent-local', 'default', '本机 Agent', true, true)
  45. const remote = {
  46. id: '11111111-1111-4111-8111-111111111111|reviewer',
  47. profile: 'reviewer',
  48. displayName: '远端评审',
  49. nodeId: '11111111-1111-4111-8111-111111111111',
  50. nodeLabel: '工作室 Mac',
  51. }
  52. const wrapper = mount(GroupManager, {
  53. props: {
  54. room: { id: 'room-1', name: '群聊', memberIds: [local.id], replyRounds: 3 },
  55. agents: [local],
  56. availableProfiles: [remote],
  57. },
  58. })
  59. await wrapper.get<HTMLSelectElement>('select[aria-label="选择要添加的 Agent"]').setValue(remote.id)
  60. expect(wrapper.emitted('addAgent')?.at(-1)).toEqual([remote.id])
  61. wrapper.unmount()
  62. })
  63. it('keeps one independent host selection when creating a v5 room', async () => {
  64. const wrapper = mount(CreateGroupDialog, {
  65. attachTo: document.body,
  66. props: { open: true, profiles: profiles('yaoyao', 'yaoer'), hostEnabled: true },
  67. global: { stubs: { teleport: true } },
  68. })
  69. await nextTick()
  70. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('主持人验收')
  71. const profileButtons = wrapper.findAll('.agent-picker > button')
  72. await profileButtons[1]!.trigger('click')
  73. const host = wrapper.get<HTMLSelectElement>('select[aria-label="主持人"]')
  74. await host.setValue('local|yaoer')
  75. await profileButtons[1]!.trigger('click')
  76. expect(host.element.value).toBe('local|yaoyao')
  77. await profileButtons[1]!.trigger('click')
  78. await host.setValue('local|yaoer')
  79. expect(wrapper.get<HTMLInputElement>('input[aria-label="所有成员无需 @ 也回复"]').element.checked).toBe(true)
  80. await wrapper.get('.solid-button').trigger('click')
  81. expect(wrapper.emitted('create')?.[0]?.[0]).toEqual({
  82. name: '主持人验收', members: profiles('yaoyao', 'yaoer'), hostProfile: 'local|yaoer', autoReply: true, replyRounds: 3,
  83. })
  84. wrapper.unmount()
  85. })
  86. it('keeps the legacy v4 create UI and payload free of host fields', async () => {
  87. const wrapper = mount(CreateGroupDialog, {
  88. attachTo: document.body,
  89. props: { open: true, profiles: profiles('yaoyao'), hostEnabled: false },
  90. global: { stubs: { teleport: true } },
  91. })
  92. await nextTick()
  93. expect(wrapper.find('select[aria-label="主持人"]').exists()).toBe(false)
  94. expect(wrapper.find('input[aria-label="启用自动回复"]').exists()).toBe(true)
  95. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('v4 房间')
  96. await wrapper.get('.solid-button').trigger('click')
  97. expect(wrapper.emitted('create')?.[0]?.[0]).not.toHaveProperty('hostProfile')
  98. expect(wrapper.emitted('create')?.[0]?.[0]).not.toHaveProperty('orchestrationMode')
  99. expect(wrapper.emitted('create')?.[0]?.[0]).not.toHaveProperty('instructions')
  100. wrapper.unmount()
  101. })
  102. it('offers host flow only when the server advertises the capability', async () => {
  103. const wrapper = mount(CreateGroupDialog, {
  104. attachTo: document.body,
  105. props: {
  106. open: true,
  107. profiles: profiles('yaoyao', 'yaoer'),
  108. hostEnabled: true,
  109. hostFlowEnabled: true,
  110. roomInstructionsEnabled: true,
  111. },
  112. global: { stubs: { teleport: true } },
  113. })
  114. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('顺序协作')
  115. await wrapper.get<HTMLTextAreaElement>('textarea').setValue('先核对事实,再输出中文结论。')
  116. await wrapper.get<HTMLSelectElement>('select[aria-label="协作模式"]').setValue('host')
  117. await wrapper.get('.solid-button').trigger('click')
  118. expect(wrapper.emitted('create')?.[0]?.[0]).toMatchObject({
  119. name: '顺序协作',
  120. instructions: '先核对事实,再输出中文结论。',
  121. orchestrationMode: 'host',
  122. })
  123. wrapper.unmount()
  124. })
  125. it('emits one host promotion and explains the independent auto-reply setting', async () => {
  126. const first = agent('agent-1', 'yaoyao', '夭夭', true, true)
  127. const second = agent('agent-2', 'yaoer', '瑶儿', false, false)
  128. const wrapper = mount(GroupManager, {
  129. attachTo: document.body,
  130. props: {
  131. room: { id: 'room-1', name: '群聊', memberIds: [first.id, second.id], replyRounds: 3 },
  132. agents: [first, second],
  133. hostEnabled: true,
  134. },
  135. })
  136. const selector = wrapper.get<HTMLSelectElement>('select[aria-label="主持人"]')
  137. expect(selector.element.value).toBe('agent-1')
  138. expect(wrapper.findAll('.host-badge')).toHaveLength(1)
  139. await selector.setValue('agent-2')
  140. expect(wrapper.emitted('updateAgent')?.[0]).toEqual(['agent-2', { isHost: true }])
  141. await wrapper.setProps({ agents: [{ ...first, isHost: false }, { ...second, isHost: true }] })
  142. await wrapper.get('button[aria-label="设置瑶儿"]').trigger('click')
  143. await nextTick()
  144. const dialog = document.querySelector('[role="dialog"][aria-label="瑶儿 Agent 设置"]')
  145. expect(dialog?.textContent).toContain('主持人始终处理用户无 @ 消息')
  146. expect(dialog?.querySelector('input[aria-label="无需 @ 也回复"]')).not.toBeNull()
  147. wrapper.unmount()
  148. })
  149. it('blocks removing a host when no enabled replacement exists and shows manager errors', () => {
  150. const first = agent('agent-1', 'yaoyao', '夭夭', true, true)
  151. const second = { ...agent('agent-2', 'yaoer', '瑶儿', false, false), enabled: false }
  152. const wrapper = mount(GroupManager, {
  153. props: {
  154. room: { id: 'room-1', name: '群聊', memberIds: [first.id, second.id], replyRounds: 3 },
  155. agents: [first, second],
  156. hostEnabled: true,
  157. managerError: '需要另一位已启用成员',
  158. },
  159. })
  160. const removeHost = wrapper.get<HTMLButtonElement>('button[aria-label="移除夭夭"]')
  161. expect(removeHost.element.disabled).toBe(true)
  162. expect(removeHost.attributes('title')).toContain('已启用成员')
  163. expect(wrapper.get('[role="alert"]').text()).toBe('需要另一位已启用成员')
  164. })
  165. it('emits a room update when switching collaboration mode', async () => {
  166. const first = agent('agent-1', 'yaoyao', '夭夭', true, false)
  167. const wrapper = mount(GroupManager, {
  168. props: {
  169. room: { id: 'room-1', name: '群聊', memberIds: [first.id], replyRounds: 3, orchestrationMode: 'free' },
  170. agents: [first],
  171. hostEnabled: true,
  172. hostFlowEnabled: true,
  173. },
  174. })
  175. await wrapper.get<HTMLSelectElement>('select[aria-label="协作模式"]').setValue('host')
  176. expect(wrapper.emitted('updateRoom')?.[0]?.[0]).toEqual({
  177. name: '群聊',
  178. replyRounds: 3,
  179. orchestrationMode: 'host',
  180. })
  181. })
  182. it('edits room instructions when the capability is available', async () => {
  183. const first = agent('agent-1', 'yaoyao', '夭夭', true, false)
  184. const wrapper = mount(GroupManager, {
  185. props: {
  186. room: {
  187. id: 'room-1', name: '群聊', memberIds: [first.id], replyRounds: 3,
  188. orchestrationMode: 'free', instructions: '旧规则',
  189. },
  190. agents: [first],
  191. hostEnabled: true,
  192. hostFlowEnabled: true,
  193. roomInstructionsEnabled: true,
  194. },
  195. })
  196. const instructions = wrapper.get<HTMLTextAreaElement>('textarea[aria-label="团队说明"]')
  197. await instructions.setValue('新规则\n输出中文')
  198. await instructions.trigger('change')
  199. expect(wrapper.emitted('updateRoom')?.at(-1)?.[0]).toMatchObject({
  200. instructions: '新规则\n输出中文',
  201. })
  202. })
  203. it('only allows remote Agents to rename themselves within the room', async () => {
  204. const local = agent('agent-1', 'yaoyao', '夭夭', false, false)
  205. const remote = {
  206. ...agent('agent-2', 'yaoer', '远端夭夭', false, false),
  207. nodeId: '11111111-1111-4111-8111-111111111111',
  208. nodeLabel: '工作室 Mac',
  209. }
  210. const wrapper = mount(GroupManager, {
  211. props: {
  212. room: { id: 'room-1', name: '群聊', memberIds: [local.id, remote.id], replyRounds: 3 },
  213. agents: [local, remote],
  214. remoteServerAddresses: { [remote.nodeId]: '192.168.1.20:9119' },
  215. },
  216. global: { stubs: { teleport: true } },
  217. })
  218. await wrapper.get('button[aria-label="设置远端夭夭"]').trigger('click')
  219. const remoteName = wrapper.get<HTMLInputElement>('input[aria-label="群内名称"]')
  220. await remoteName.setValue('群内审查员')
  221. await wrapper.get('.save-agent').trigger('click')
  222. expect(wrapper.emitted('updateAgent')?.at(-1)).toEqual([
  223. remote.id,
  224. { displayName: '群内审查员' },
  225. ])
  226. expect(wrapper.text()).toContain('192.168.1.20:9119')
  227. await wrapper.get('button[aria-label="设置夭夭"]').trigger('click')
  228. expect(wrapper.find('input[aria-label="群内名称"]').exists()).toBe(false)
  229. })
  230. it('uses the synced remote Agent name and marks its topic message', () => {
  231. const remote = {
  232. ...agent('agent-remote', 'reviewer', '群内审查员', false, false),
  233. nodeId: '11111111-1111-4111-8111-111111111111',
  234. nodeLabel: '工作室 Mac',
  235. }
  236. const message: GroupMessage = {
  237. seq: 1,
  238. id: 'message-1',
  239. roomId: 'room-1',
  240. senderKind: 'agent',
  241. senderId: remote.id,
  242. senderName: '旧名称',
  243. rootMessageId: 'message-1',
  244. content: '已完成',
  245. reasoning: '',
  246. toolState: [],
  247. status: 'completed',
  248. error: '',
  249. createdAt: 1,
  250. updatedAt: 1,
  251. }
  252. expect(groupMessageToUi(message, [remote])).toMatchObject({
  253. author: '群内审查员',
  254. isRemoteAgent: true,
  255. })
  256. })
  257. })