groupHost.test.ts 17 KB

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