groupHost.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import { mount } from '@vue/test-utils'
  2. import { nextTick } from 'vue'
  3. import { describe, expect, it } from 'vitest'
  4. import type { GroupAgent, GroupMessage, GroupRoomSummary } from '@shared/types'
  5. import CreateGroupDialog from '@/components/groups/CreateGroupDialog.vue'
  6. import GroupManager from '@/components/groups/GroupManager.vue'
  7. import { TEAM_PRESETS } from '@/components/groups/teamPresets'
  8. import { groupMessageToUi, roomSidebarItem } from '@/components/workspace/viewModels'
  9. const profiles = (...names: string[]) => names.map(name => ({
  10. id: `local|${name}`, profile: name, displayName: name, nodeId: 'local', nodeLabel: '当前 Hermes',
  11. }))
  12. function agent(id: string, profile: string, displayName: string, isHost: boolean, replyWithoutMention: boolean): GroupAgent {
  13. return {
  14. id, roomId: 'room-1', profile, nodeId: 'local', nodeLabel: '', displayName, description: `${displayName} 的职责`, storedSessionId: null,
  15. lastContextMessageSeq: 0, enabled: true, replyWithoutMention, isHost,
  16. model: null, provider: null, reasoningEffort: null, fastMode: null,
  17. createdAt: 1, updatedAt: 1, status: 'idle',
  18. }
  19. }
  20. describe('group host controls', () => {
  21. it('provides office and development presets with unique roles within the Agent limit', () => {
  22. expect(TEAM_PRESETS.map(preset => preset.name)).toEqual([
  23. '信息收集团队', '产品设计团队', '软件开发团队', '文案书写团队',
  24. '项目管理团队', '数据分析团队', '会议协作团队', '运维保障团队',
  25. ])
  26. for (const preset of TEAM_PRESETS) {
  27. expect(preset.roles.length).toBeGreaterThanOrEqual(4)
  28. expect(preset.roles.length).toBeLessThanOrEqual(8)
  29. expect(new Set(preset.roles.map(role => role.name)).size).toBe(preset.roles.length)
  30. expect(preset.roles.filter(role => role.host)).toHaveLength(1)
  31. }
  32. })
  33. it('maps preset roles onto existing Agents and submits their role descriptions', async () => {
  34. const choices = profiles('yaoyao', 'yaoer', 'reviewer', 'writer')
  35. const wrapper = mount(CreateGroupDialog, {
  36. attachTo: document.body,
  37. props: {
  38. open: true,
  39. profiles: choices,
  40. hostEnabled: true,
  41. hostFlowEnabled: true,
  42. roomInstructionsEnabled: true,
  43. },
  44. global: { stubs: { teleport: true } },
  45. })
  46. await wrapper.get('button[aria-label^="信息收集团队"]').trigger('click')
  47. expect(wrapper.get<HTMLInputElement>('input[placeholder="例如:产品评审"]').element.value).toBe('信息收集团队')
  48. expect(wrapper.findAll('.role-mapping select')).toHaveLength(4)
  49. await wrapper.get<HTMLSelectElement>('select[aria-label="信息检索对应的 Agent"]').setValue('local|yaoyao')
  50. await wrapper.get<HTMLSelectElement>('select[aria-label="信息检索对应的 Agent"]').setValue('local|reviewer')
  51. await wrapper.get('.solid-button').trigger('click')
  52. const payload = wrapper.emitted('create')?.[0]?.[0] as { members: Array<{ profile: string; displayName: string; description: string }>; hostProfile: string; autoReply: boolean; orchestrationMode: string; instructions: string }
  53. expect(payload.members.map(member => [member.profile, member.displayName])).toEqual([
  54. ['yaoer', '调研负责人'],
  55. ['reviewer', '信息检索'],
  56. ['yaoyao', '事实核验'],
  57. ['writer', '资料整理'],
  58. ])
  59. expect(payload.members.every(member => member.description.length > 0)).toBe(true)
  60. expect(payload.hostProfile).toBe('local|yaoer')
  61. expect(payload.autoReply).toBe(false)
  62. expect(payload.orchestrationMode).toBe('host')
  63. expect(payload.instructions).toContain('交叉核验')
  64. wrapper.unmount()
  65. })
  66. it('disables presets when the current Agent count is insufficient', async () => {
  67. const wrapper = mount(CreateGroupDialog, {
  68. attachTo: document.body,
  69. props: { open: true, profiles: profiles('yaoyao', 'yaoer', 'reviewer', 'writer') },
  70. global: { stubs: { teleport: true } },
  71. })
  72. const softwarePreset = wrapper.get<HTMLButtonElement>('button[aria-label^="软件开发团队"]')
  73. expect(softwarePreset.element.disabled).toBe(true)
  74. expect(softwarePreset.text()).toContain('还缺 1 人')
  75. await softwarePreset.trigger('click')
  76. expect(wrapper.find('.role-mapping').exists()).toBe(false)
  77. wrapper.unmount()
  78. })
  79. it('resolves a local team avatar by display name when an old room stored an internal Profile alias', () => {
  80. const room = {
  81. id: 'room-1', name: '头像团队', cwd: '', instructions: '', avatar: '', createdAt: 1, updatedAt: 1,
  82. archived: false, agentCount: 2, lastMessage: null, unreadCount: 0, activeRunCount: 0, maxReplyRounds: 3, orchestrationMode: 'free',
  83. avatarMembers: [
  84. { profile: 'legacy:default', nodeId: 'local', displayName: '丫头' },
  85. { profile: 'default', nodeId: 'remote-node', displayName: '远端丫头' },
  86. ],
  87. } satisfies GroupRoomSummary
  88. const item = roomSidebarItem(room, { default: 'data:image/png;base64,LOCAL==' }, { 丫头: 'data:image/png;base64,LOCAL==' })
  89. expect(item.avatarMembers).toEqual([
  90. { name: '丫头', avatar: 'data:image/png;base64,LOCAL==' },
  91. { name: '远端丫头', avatar: undefined },
  92. ])
  93. })
  94. it('keeps same-name Profiles distinct across paired Hermes nodes', async () => {
  95. const local = profiles('default')[0]!
  96. const remote = {
  97. id: '11111111-1111-4111-8111-111111111111|default',
  98. profile: 'default',
  99. displayName: '远端夭夭',
  100. nodeId: '11111111-1111-4111-8111-111111111111',
  101. nodeLabel: 'MacBook Pro',
  102. }
  103. const wrapper = mount(CreateGroupDialog, {
  104. attachTo: document.body,
  105. props: { open: true, profiles: [local, remote], hostEnabled: true },
  106. global: { stubs: { teleport: true } },
  107. })
  108. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('跨节点群')
  109. await wrapper.findAll('.agent-picker > button')[1]!.trigger('click')
  110. await wrapper.get('.solid-button').trigger('click')
  111. expect(wrapper.emitted('create')?.[0]?.[0]).toMatchObject({
  112. members: [local, remote],
  113. hostProfile: local.id,
  114. })
  115. wrapper.unmount()
  116. })
  117. it('adds an Agent from a paired node to an existing group', async () => {
  118. const local = agent('agent-local', 'default', '本机 Agent', true, true)
  119. const remote = {
  120. id: '11111111-1111-4111-8111-111111111111|reviewer',
  121. profile: 'reviewer',
  122. displayName: '远端评审',
  123. nodeId: '11111111-1111-4111-8111-111111111111',
  124. nodeLabel: '工作室 Mac',
  125. }
  126. const wrapper = mount(GroupManager, {
  127. props: {
  128. room: { id: 'room-1', name: '群聊', memberIds: [local.id], replyRounds: 3 },
  129. agents: [local],
  130. availableProfiles: [remote],
  131. },
  132. })
  133. await wrapper.get<HTMLSelectElement>('select[aria-label="选择要添加的 Agent"]').setValue(remote.id)
  134. expect(wrapper.emitted('addAgent')?.at(-1)).toEqual([remote.id])
  135. wrapper.unmount()
  136. })
  137. it('keeps one independent host selection when creating a v5 room', async () => {
  138. const wrapper = mount(CreateGroupDialog, {
  139. attachTo: document.body,
  140. props: { open: true, profiles: profiles('yaoyao', 'yaoer'), hostEnabled: true },
  141. global: { stubs: { teleport: true } },
  142. })
  143. await nextTick()
  144. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('主持人验收')
  145. const profileButtons = wrapper.findAll('.agent-picker > button')
  146. await profileButtons[1]!.trigger('click')
  147. const host = wrapper.get<HTMLSelectElement>('select[aria-label="主持人"]')
  148. await host.setValue('local|yaoer')
  149. await profileButtons[1]!.trigger('click')
  150. expect(host.element.value).toBe('local|yaoyao')
  151. await profileButtons[1]!.trigger('click')
  152. await host.setValue('local|yaoer')
  153. expect(wrapper.get<HTMLInputElement>('input[aria-label="所有成员无需 @ 也回复"]').element.checked).toBe(true)
  154. await wrapper.get('.solid-button').trigger('click')
  155. expect(wrapper.emitted('create')?.[0]?.[0]).toEqual({
  156. name: '主持人验收', members: profiles('yaoyao', 'yaoer'), hostProfile: 'local|yaoer', autoReply: true, replyRounds: 3,
  157. })
  158. wrapper.unmount()
  159. })
  160. it('keeps the legacy v4 create UI and payload free of host fields', async () => {
  161. const wrapper = mount(CreateGroupDialog, {
  162. attachTo: document.body,
  163. props: { open: true, profiles: profiles('yaoyao'), hostEnabled: false },
  164. global: { stubs: { teleport: true } },
  165. })
  166. await nextTick()
  167. expect(wrapper.find('select[aria-label="主持人"]').exists()).toBe(false)
  168. expect(wrapper.find('input[aria-label="启用自动回复"]').exists()).toBe(true)
  169. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('v4 房间')
  170. await wrapper.get('.solid-button').trigger('click')
  171. expect(wrapper.emitted('create')?.[0]?.[0]).not.toHaveProperty('hostProfile')
  172. expect(wrapper.emitted('create')?.[0]?.[0]).not.toHaveProperty('orchestrationMode')
  173. expect(wrapper.emitted('create')?.[0]?.[0]).not.toHaveProperty('instructions')
  174. wrapper.unmount()
  175. })
  176. it('offers host flow only when the server advertises the capability', async () => {
  177. const wrapper = mount(CreateGroupDialog, {
  178. attachTo: document.body,
  179. props: {
  180. open: true,
  181. profiles: profiles('yaoyao', 'yaoer'),
  182. hostEnabled: true,
  183. hostFlowEnabled: true,
  184. roomInstructionsEnabled: true,
  185. },
  186. global: { stubs: { teleport: true } },
  187. })
  188. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('顺序协作')
  189. await wrapper.get<HTMLTextAreaElement>('textarea').setValue('先核对事实,再输出中文结论。')
  190. await wrapper.get<HTMLSelectElement>('select[aria-label="协作模式"]').setValue('host')
  191. await wrapper.get('.solid-button').trigger('click')
  192. expect(wrapper.emitted('create')?.[0]?.[0]).toMatchObject({
  193. name: '顺序协作',
  194. instructions: '先核对事实,再输出中文结论。',
  195. orchestrationMode: 'host',
  196. })
  197. wrapper.unmount()
  198. })
  199. it('shows and submits an automatic team avatar from selected Agents', async () => {
  200. const choices = profiles('yaoyao', 'yaoer').map((profile, index) => ({
  201. ...profile,
  202. ...(index === 0 ? { avatar: 'data:image/png;base64,AA==' } : {}),
  203. }))
  204. const wrapper = mount(CreateGroupDialog, {
  205. attachTo: document.body,
  206. props: { open: true, profiles: choices, avatarEnabled: true },
  207. global: { stubs: { teleport: true } },
  208. })
  209. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('头像团队')
  210. await wrapper.findAll('.agent-picker > button')[1]!.trigger('click')
  211. expect(wrapper.findAll('.avatar-picker .team-avatar__member')).toHaveLength(2)
  212. expect(wrapper.get('.avatar-picker').text()).toContain('自动组合已选 Agent 的头像')
  213. await wrapper.get('.solid-button').trigger('click')
  214. expect(wrapper.emitted('create')?.[0]?.[0]).toMatchObject({
  215. name: '头像团队',
  216. avatar: '',
  217. members: choices,
  218. })
  219. wrapper.unmount()
  220. })
  221. it('emits one host promotion and explains the independent auto-reply setting', async () => {
  222. const first = agent('agent-1', 'yaoyao', '夭夭', true, true)
  223. const second = agent('agent-2', 'yaoer', '瑶儿', false, false)
  224. const wrapper = mount(GroupManager, {
  225. attachTo: document.body,
  226. props: {
  227. room: { id: 'room-1', name: '群聊', memberIds: [first.id, second.id], replyRounds: 3 },
  228. agents: [first, second],
  229. hostEnabled: true,
  230. },
  231. })
  232. const selector = wrapper.get<HTMLSelectElement>('select[aria-label="主持人"]')
  233. expect(selector.element.value).toBe('agent-1')
  234. expect(wrapper.findAll('.host-badge')).toHaveLength(1)
  235. await selector.setValue('agent-2')
  236. expect(wrapper.emitted('updateAgent')?.[0]).toEqual(['agent-2', { isHost: true }])
  237. await wrapper.setProps({ agents: [{ ...first, isHost: false }, { ...second, isHost: true }] })
  238. await wrapper.get('button[aria-label="设置瑶儿"]').trigger('click')
  239. await nextTick()
  240. const dialog = document.querySelector('[role="dialog"][aria-label="瑶儿 Agent 设置"]')
  241. expect(dialog?.textContent).toContain('主持人始终处理用户无 @ 消息')
  242. expect(dialog?.querySelector('input[aria-label="无需 @ 也回复"]')).not.toBeNull()
  243. wrapper.unmount()
  244. })
  245. it('blocks removing a host when no enabled replacement exists and shows manager errors', () => {
  246. const first = agent('agent-1', 'yaoyao', '夭夭', true, true)
  247. const second = { ...agent('agent-2', 'yaoer', '瑶儿', false, false), enabled: false }
  248. const wrapper = mount(GroupManager, {
  249. props: {
  250. room: { id: 'room-1', name: '群聊', memberIds: [first.id, second.id], replyRounds: 3 },
  251. agents: [first, second],
  252. hostEnabled: true,
  253. managerError: '需要另一位已启用成员',
  254. },
  255. })
  256. const removeHost = wrapper.get<HTMLButtonElement>('button[aria-label="移除夭夭"]')
  257. expect(removeHost.element.disabled).toBe(true)
  258. expect(removeHost.attributes('title')).toContain('已启用成员')
  259. expect(wrapper.get('[role="alert"]').text()).toBe('需要另一位已启用成员')
  260. })
  261. it('emits a room update when switching collaboration mode', async () => {
  262. const first = agent('agent-1', 'yaoyao', '夭夭', true, false)
  263. const wrapper = mount(GroupManager, {
  264. props: {
  265. room: { id: 'room-1', name: '群聊', memberIds: [first.id], replyRounds: 3, orchestrationMode: 'free' },
  266. agents: [first],
  267. hostEnabled: true,
  268. hostFlowEnabled: true,
  269. },
  270. })
  271. await wrapper.get<HTMLSelectElement>('select[aria-label="协作模式"]').setValue('host')
  272. expect(wrapper.emitted('updateRoom')?.[0]?.[0]).toEqual({
  273. name: '群聊',
  274. replyRounds: 3,
  275. orchestrationMode: 'host',
  276. })
  277. })
  278. it('edits room instructions when the capability is available', async () => {
  279. const first = agent('agent-1', 'yaoyao', '夭夭', true, false)
  280. const wrapper = mount(GroupManager, {
  281. props: {
  282. room: {
  283. id: 'room-1', name: '群聊', memberIds: [first.id], replyRounds: 3,
  284. orchestrationMode: 'free', instructions: '旧规则',
  285. },
  286. agents: [first],
  287. hostEnabled: true,
  288. hostFlowEnabled: true,
  289. roomInstructionsEnabled: true,
  290. },
  291. })
  292. const instructions = wrapper.get<HTMLTextAreaElement>('textarea[aria-label="团队说明"]')
  293. await instructions.setValue('新规则\n输出中文')
  294. await instructions.trigger('change')
  295. expect(wrapper.emitted('updateRoom')?.at(-1)?.[0]).toMatchObject({
  296. instructions: '新规则\n输出中文',
  297. })
  298. })
  299. it('only allows remote Agents to rename themselves within the room', async () => {
  300. const local = agent('agent-1', 'yaoyao', '夭夭', false, false)
  301. const remote = {
  302. ...agent('agent-2', 'yaoer', '远端夭夭', false, false),
  303. nodeId: '11111111-1111-4111-8111-111111111111',
  304. nodeLabel: '工作室 Mac',
  305. }
  306. const wrapper = mount(GroupManager, {
  307. props: {
  308. room: { id: 'room-1', name: '群聊', memberIds: [local.id, remote.id], replyRounds: 3 },
  309. agents: [local, remote],
  310. remoteServerAddresses: { [remote.nodeId]: '192.168.1.20:9119' },
  311. },
  312. global: { stubs: { teleport: true } },
  313. })
  314. await wrapper.get('button[aria-label="设置远端夭夭"]').trigger('click')
  315. const remoteName = wrapper.get<HTMLInputElement>('input[aria-label="群内名称"]')
  316. await remoteName.setValue('群内审查员')
  317. await wrapper.get('.save-agent').trigger('click')
  318. expect(wrapper.emitted('updateAgent')?.at(-1)).toEqual([
  319. remote.id,
  320. { displayName: '群内审查员' },
  321. ])
  322. expect(wrapper.text()).toContain('192.168.1.20:9119')
  323. await wrapper.get('button[aria-label="设置夭夭"]').trigger('click')
  324. expect(wrapper.find('input[aria-label="群内名称"]').exists()).toBe(false)
  325. })
  326. it('uses the synced remote Agent name and marks its topic message', () => {
  327. const remote = {
  328. ...agent('agent-remote', 'reviewer', '群内审查员', false, false),
  329. nodeId: '11111111-1111-4111-8111-111111111111',
  330. nodeLabel: '工作室 Mac',
  331. }
  332. const message: GroupMessage = {
  333. seq: 1,
  334. id: 'message-1',
  335. roomId: 'room-1',
  336. senderKind: 'agent',
  337. senderId: remote.id,
  338. senderName: '旧名称',
  339. rootMessageId: 'message-1',
  340. content: '已完成',
  341. reasoning: '',
  342. toolState: [],
  343. status: 'completed',
  344. error: '',
  345. createdAt: 1,
  346. updatedAt: 1,
  347. }
  348. expect(groupMessageToUi(message, [remote])).toMatchObject({
  349. author: '群内审查员',
  350. isRemoteAgent: true,
  351. })
  352. })
  353. })