groupHost.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { mount } from '@vue/test-utils'
  2. import { nextTick } from 'vue'
  3. import { describe, expect, it } from 'vitest'
  4. import type { GroupAgent } from '@shared/types'
  5. import CreateGroupDialog from '@/components/groups/CreateGroupDialog.vue'
  6. import GroupManager from '@/components/groups/GroupManager.vue'
  7. function agent(id: string, profile: string, displayName: string, isHost: boolean, replyWithoutMention: boolean): GroupAgent {
  8. return {
  9. id, roomId: 'room-1', profile, displayName, description: `${displayName} 的职责`, storedSessionId: null,
  10. lastContextMessageSeq: 0, enabled: true, replyWithoutMention, isHost,
  11. model: null, provider: null, reasoningEffort: null, fastMode: null,
  12. createdAt: 1, updatedAt: 1, status: 'idle',
  13. }
  14. }
  15. describe('group host controls', () => {
  16. it('keeps one independent host selection when creating a v5 room', async () => {
  17. const wrapper = mount(CreateGroupDialog, {
  18. attachTo: document.body,
  19. props: { open: true, profiles: ['yaoyao', 'yaoer'], hostEnabled: true },
  20. global: { stubs: { teleport: true } },
  21. })
  22. await nextTick()
  23. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('主持人验收')
  24. const profileButtons = wrapper.findAll('.agent-picker > button')
  25. await profileButtons[1]!.trigger('click')
  26. const host = wrapper.get<HTMLSelectElement>('select[aria-label="主持人"]')
  27. await host.setValue('yaoer')
  28. await profileButtons[1]!.trigger('click')
  29. expect(host.element.value).toBe('yaoyao')
  30. await profileButtons[1]!.trigger('click')
  31. await host.setValue('yaoer')
  32. expect(wrapper.get<HTMLInputElement>('input[aria-label="所有成员无需 @ 也回复"]').element.checked).toBe(true)
  33. await wrapper.get('.solid-button').trigger('click')
  34. expect(wrapper.emitted('create')?.[0]?.[0]).toEqual({
  35. name: '主持人验收', profiles: ['yaoyao', 'yaoer'], hostProfile: 'yaoer', autoReply: true, replyRounds: 3,
  36. })
  37. wrapper.unmount()
  38. })
  39. it('keeps the legacy v4 create UI and payload free of host fields', async () => {
  40. const wrapper = mount(CreateGroupDialog, {
  41. attachTo: document.body,
  42. props: { open: true, profiles: ['yaoyao'], hostEnabled: false },
  43. global: { stubs: { teleport: true } },
  44. })
  45. await nextTick()
  46. expect(wrapper.find('select[aria-label="主持人"]').exists()).toBe(false)
  47. expect(wrapper.find('input[aria-label="启用自动回复"]').exists()).toBe(true)
  48. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('v4 房间')
  49. await wrapper.get('.solid-button').trigger('click')
  50. expect(wrapper.emitted('create')?.[0]?.[0]).not.toHaveProperty('hostProfile')
  51. expect(wrapper.emitted('create')?.[0]?.[0]).not.toHaveProperty('orchestrationMode')
  52. expect(wrapper.emitted('create')?.[0]?.[0]).not.toHaveProperty('instructions')
  53. wrapper.unmount()
  54. })
  55. it('offers host flow only when the server advertises the capability', async () => {
  56. const wrapper = mount(CreateGroupDialog, {
  57. attachTo: document.body,
  58. props: {
  59. open: true,
  60. profiles: ['yaoyao', 'yaoer'],
  61. hostEnabled: true,
  62. hostFlowEnabled: true,
  63. roomInstructionsEnabled: true,
  64. },
  65. global: { stubs: { teleport: true } },
  66. })
  67. await wrapper.get('input[placeholder="例如:产品评审"]').setValue('顺序协作')
  68. await wrapper.get<HTMLTextAreaElement>('textarea').setValue('先核对事实,再输出中文结论。')
  69. await wrapper.get<HTMLSelectElement>('select[aria-label="协作模式"]').setValue('host')
  70. await wrapper.get('.solid-button').trigger('click')
  71. expect(wrapper.emitted('create')?.[0]?.[0]).toMatchObject({
  72. name: '顺序协作',
  73. instructions: '先核对事实,再输出中文结论。',
  74. orchestrationMode: 'host',
  75. })
  76. wrapper.unmount()
  77. })
  78. it('emits one host promotion and explains the independent auto-reply setting', async () => {
  79. const first = agent('agent-1', 'yaoyao', '夭夭', true, true)
  80. const second = agent('agent-2', 'yaoer', '瑶儿', false, false)
  81. const wrapper = mount(GroupManager, {
  82. attachTo: document.body,
  83. props: {
  84. room: { id: 'room-1', name: '群聊', memberIds: [first.id, second.id], replyRounds: 3 },
  85. agents: [first, second],
  86. hostEnabled: true,
  87. },
  88. })
  89. const selector = wrapper.get<HTMLSelectElement>('select[aria-label="主持人"]')
  90. expect(selector.element.value).toBe('agent-1')
  91. expect(wrapper.findAll('.host-badge')).toHaveLength(1)
  92. await selector.setValue('agent-2')
  93. expect(wrapper.emitted('updateAgent')?.[0]).toEqual(['agent-2', { isHost: true }])
  94. await wrapper.setProps({ agents: [{ ...first, isHost: false }, { ...second, isHost: true }] })
  95. await wrapper.get('button[aria-label="设置瑶儿"]').trigger('click')
  96. await nextTick()
  97. const dialog = document.querySelector('[role="dialog"][aria-label="瑶儿 Agent 设置"]')
  98. expect(dialog?.textContent).toContain('主持人始终处理用户无 @ 消息')
  99. expect(dialog?.querySelector('input[aria-label="无需 @ 也回复"]')).not.toBeNull()
  100. wrapper.unmount()
  101. })
  102. it('blocks removing a host when no enabled replacement exists and shows manager errors', () => {
  103. const first = agent('agent-1', 'yaoyao', '夭夭', true, true)
  104. const second = { ...agent('agent-2', 'yaoer', '瑶儿', false, false), enabled: false }
  105. const wrapper = mount(GroupManager, {
  106. props: {
  107. room: { id: 'room-1', name: '群聊', memberIds: [first.id, second.id], replyRounds: 3 },
  108. agents: [first, second],
  109. hostEnabled: true,
  110. managerError: '需要另一位已启用成员',
  111. },
  112. })
  113. const removeHost = wrapper.get<HTMLButtonElement>('button[aria-label="移除夭夭"]')
  114. expect(removeHost.element.disabled).toBe(true)
  115. expect(removeHost.attributes('title')).toContain('已启用成员')
  116. expect(wrapper.get('[role="alert"]').text()).toBe('需要另一位已启用成员')
  117. })
  118. it('emits a room update when switching collaboration mode', async () => {
  119. const first = agent('agent-1', 'yaoyao', '夭夭', true, false)
  120. const wrapper = mount(GroupManager, {
  121. props: {
  122. room: { id: 'room-1', name: '群聊', memberIds: [first.id], replyRounds: 3, orchestrationMode: 'free' },
  123. agents: [first],
  124. hostEnabled: true,
  125. hostFlowEnabled: true,
  126. },
  127. })
  128. await wrapper.get<HTMLSelectElement>('select[aria-label="协作模式"]').setValue('host')
  129. expect(wrapper.emitted('updateRoom')?.[0]?.[0]).toEqual({
  130. name: '群聊',
  131. replyRounds: 3,
  132. orchestrationMode: 'host',
  133. })
  134. })
  135. it('edits room instructions when the capability is available', async () => {
  136. const first = agent('agent-1', 'yaoyao', '夭夭', true, false)
  137. const wrapper = mount(GroupManager, {
  138. props: {
  139. room: {
  140. id: 'room-1', name: '群聊', memberIds: [first.id], replyRounds: 3,
  141. orchestrationMode: 'free', instructions: '旧规则',
  142. },
  143. agents: [first],
  144. hostEnabled: true,
  145. hostFlowEnabled: true,
  146. roomInstructionsEnabled: true,
  147. },
  148. })
  149. const instructions = wrapper.get<HTMLTextAreaElement>('textarea[aria-label="房间说明"]')
  150. await instructions.setValue('新规则\n输出中文')
  151. await instructions.trigger('change')
  152. expect(wrapper.emitted('updateRoom')?.at(-1)?.[0]).toMatchObject({
  153. instructions: '新规则\n输出中文',
  154. })
  155. })
  156. })