settingsCenterDialog.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { mount, type VueWrapper } from '@vue/test-utils'
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. import { defineComponent } from 'vue'
  4. import type { Profile } from '@shared/types'
  5. import SettingsCenterDialog from '@/components/app/SettingsCenterDialog.vue'
  6. const profiles: Profile[] = [
  7. { name: 'ops:blue/team', agentName: '运维机器人', isDefault: false },
  8. { name: 'default', agentName: '丫头', isDefault: true },
  9. ]
  10. function simpleStub(name: string, testId: string) {
  11. return defineComponent({ name, template: `<div data-testid="${testId}"></div>` })
  12. }
  13. const ModelServicesPanelStub = defineComponent({
  14. name: 'ModelServicesPanel',
  15. props: { profile: { type: String, required: true } },
  16. emits: ['dirty-change'],
  17. template: '<div data-testid="model-services" :data-profile="profile"><button data-testid="dirty-model" type="button" @click="$emit(\'dirty-change\', true)">dirty</button></div>',
  18. })
  19. const DuplexVoicePanelStub = defineComponent({
  20. name: 'DuplexVoicePanel',
  21. emits: ['dirty-change'],
  22. template: '<div data-testid="duplex-voice"><button data-testid="dirty-voice" type="button" @click="$emit(\'dirty-change\', true)">dirty</button></div>',
  23. })
  24. const SystemUpdatePanelStub = defineComponent({
  25. name: 'SystemUpdatePanel',
  26. emits: ['lock-change'],
  27. template: `
  28. <div data-testid="system-update">
  29. <button data-testid="lock-update" type="button" @click="$emit('lock-change', true)">lock</button>
  30. <button data-testid="unlock-update" type="button" @click="$emit('lock-change', false)">unlock</button>
  31. </div>
  32. `,
  33. })
  34. const childStubs = {
  35. Teleport: true,
  36. AgentAvatar: true,
  37. AppIcon: true,
  38. BotPluginsPanel: simpleStub('BotPluginsPanel', 'bot-plugins'),
  39. BotAutomationPanel: simpleStub('BotAutomationPanel', 'bot-automations'),
  40. AgentIdentityPanel: simpleStub('AgentIdentityPanel', 'agent-identity'),
  41. ModelServicesPanel: ModelServicesPanelStub,
  42. AccountSecurityPanel: simpleStub('AccountSecurityPanel', 'account-security'),
  43. NodePairingPanel: simpleStub('NodePairingPanel', 'node-pairing'),
  44. SystemOverviewPanel: simpleStub('SystemOverviewPanel', 'system-overview'),
  45. SystemManagementPanel: simpleStub('SystemManagementPanel', 'system-management'),
  46. DuplexVoicePanel: DuplexVoicePanelStub,
  47. SystemUpdatePanel: SystemUpdatePanelStub,
  48. }
  49. function mountSettings(overrides: Record<string, unknown> = {}) {
  50. return mount(SettingsCenterDialog, {
  51. attachTo: document.body,
  52. props: {
  53. open: true,
  54. activeProfile: profiles[0],
  55. profiles,
  56. userName: '管理员',
  57. pairingUserName: 'owner',
  58. isAdmin: true,
  59. ...overrides,
  60. } as never,
  61. global: { stubs: childStubs },
  62. })
  63. }
  64. function navigationButton(wrapper: VueWrapper, label: string) {
  65. const button = wrapper.findAll<HTMLButtonElement>('.settings-sidebar nav button')
  66. .find(candidate => candidate.text().includes(label))
  67. expect(button, `missing navigation button: ${label}`).toBeDefined()
  68. return button!
  69. }
  70. afterEach(() => {
  71. document.body.innerHTML = ''
  72. vi.restoreAllMocks()
  73. Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1024 })
  74. })
  75. describe('Settings center dialog', () => {
  76. it('keeps plugin, connected-app and automation settings scoped to Bot mode', async () => {
  77. const normal = mountSettings({ initialPage: 'bot-plugins' })
  78. expect(normal.find('[data-testid="bot-plugins"]').exists()).toBe(false)
  79. expect(normal.findAll('.settings-sidebar nav button').some(b => b.text() === '插件')).toBe(false)
  80. normal.unmount()
  81. const bot = mountSettings({ botMode: true, initialPage: 'bot-plugins' })
  82. await vi.waitFor(() => expect(bot.find('[data-testid="bot-plugins"]').exists()).toBe(true))
  83. await navigationButton(bot, '自动化').trigger('click')
  84. await vi.waitFor(() => expect(bot.find('[data-testid="bot-automations"]').exists()).toBe(true))
  85. await navigationButton(bot, '关于').trigger('click')
  86. expect(bot.get('.bot-about').text()).toContain('夭夭 AI')
  87. expect(bot.get('.bot-about a').attributes('href')).toBe('https://yaoyao.samien.cn')
  88. bot.unmount()
  89. })
  90. it('groups administrator pages and routes Agent, voice, and system content to the right scope', async () => {
  91. const wrapper = mountSettings()
  92. expect(wrapper.findAll('.settings-sidebar nav h3').map(heading => heading.text())).toEqual([
  93. '当前机器人',
  94. '账号',
  95. '系统 · 仅管理员',
  96. ])
  97. await navigationButton(wrapper, '模型与 Provider').trigger('click')
  98. expect(navigationButton(wrapper, '模型与 Provider').attributes('aria-current')).toBe('page')
  99. const modelPanel = wrapper.get('[data-testid="model-services"]')
  100. expect(modelPanel.attributes('data-profile')).toBe('ops:blue/team')
  101. expect(wrapper.get('.settings-content__header').text()).toContain('正在设置:运维机器人 / ops:blue/team')
  102. await navigationButton(wrapper, '登录与安全').trigger('click')
  103. expect(wrapper.find('.settings-agent-selector').exists()).toBe(false)
  104. expect(wrapper.get('.settings-account-summary').text()).toContain('owner')
  105. expect(wrapper.get('.settings-account-summary').text()).toContain('当前账号')
  106. expect(wrapper.get('.settings-account-summary .account-initial-avatar').text()).toBe('O')
  107. const voiceButton = navigationButton(wrapper, '双流语音')
  108. expect(voiceButton.text()).toContain('全局')
  109. expect(voiceButton.element.closest('section')?.textContent).toContain('系统 · 仅管理员')
  110. await voiceButton.trigger('click')
  111. expect(wrapper.find('[data-testid="duplex-voice"]').exists()).toBe(true)
  112. expect(wrapper.get('.settings-content__header').text()).toContain('全局设置 · 仅管理员')
  113. })
  114. it('hides model management and the complete system group from non-admin users', () => {
  115. const wrapper = mountSettings({ isAdmin: false, initialPage: 'system-update' })
  116. const navigation = wrapper.get('.settings-sidebar nav')
  117. expect(navigation.findAll('h3').map(heading => heading.text())).toEqual(['当前机器人', '账号'])
  118. expect(navigation.text()).not.toContain('模型与 Provider')
  119. expect(navigation.text()).not.toContain('系统 · 仅管理员')
  120. expect(navigation.text()).not.toContain('系统概览')
  121. expect(navigation.text()).not.toContain('双流语音')
  122. expect(navigation.text()).not.toContain('更新与回滚')
  123. expect(wrapper.find('[data-testid="system-update"]').exists()).toBe(false)
  124. expect(wrapper.get('.settings-content__header').text()).toContain('身份与头像')
  125. })
  126. it('blocks closing and page changes while the update panel reports a lock', async () => {
  127. const wrapper = mountSettings({ initialPage: 'system-update' })
  128. expect(wrapper.find('[data-testid="system-update"]').exists()).toBe(true)
  129. await wrapper.get('[data-testid="lock-update"]').trigger('click')
  130. expect(wrapper.get<HTMLButtonElement>('.settings-center__close').element.disabled).toBe(true)
  131. await navigationButton(wrapper, '登录与安全').trigger('click')
  132. await wrapper.get('.settings-center-layer').trigger('mousedown')
  133. expect(wrapper.emitted('close')).toBeUndefined()
  134. expect(wrapper.get('.settings-content__header').text()).toContain('更新与回滚')
  135. expect(wrapper.find('[data-testid="account-security"]').exists()).toBe(false)
  136. await wrapper.get('[data-testid="unlock-update"]').trigger('click')
  137. await navigationButton(wrapper, '登录与安全').trigger('click')
  138. expect(wrapper.find('[data-testid="account-security"]').exists()).toBe(true)
  139. })
  140. it('asks before discarding an edited page and keeps the current page when declined', async () => {
  141. const confirm = vi.spyOn(window, 'confirm').mockReturnValueOnce(false).mockReturnValueOnce(true)
  142. const wrapper = mountSettings()
  143. wrapper.findComponent({ name: 'AgentIdentityPanel' }).vm.$emit('dirty-change', true)
  144. await wrapper.vm.$nextTick()
  145. await navigationButton(wrapper, '登录与安全').trigger('click')
  146. expect(confirm).toHaveBeenCalledWith('放弃当前页面未保存的更改?')
  147. expect(wrapper.get('.settings-content__header').text()).toContain('身份与头像')
  148. await navigationButton(wrapper, '登录与安全').trigger('click')
  149. expect(wrapper.find('[data-testid="account-security"]').exists()).toBe(true)
  150. })
  151. it('protects Provider and global voice drafts from silent navigation or close', async () => {
  152. const confirm = vi.spyOn(window, 'confirm').mockReturnValue(false)
  153. const modelWrapper = mountSettings({ initialPage: 'agent-models' })
  154. await modelWrapper.get('[data-testid="dirty-model"]').trigger('click')
  155. await navigationButton(modelWrapper, '登录与安全').trigger('click')
  156. expect(confirm).toHaveBeenCalledWith('放弃当前页面未保存的更改?')
  157. expect(modelWrapper.find('[data-testid="model-services"]').exists()).toBe(true)
  158. modelWrapper.unmount()
  159. confirm.mockClear()
  160. const voiceWrapper = mountSettings({ initialPage: 'system-voice' })
  161. await voiceWrapper.get('[data-testid="dirty-voice"]').trigger('click')
  162. await voiceWrapper.get('.settings-center__close').trigger('click')
  163. expect(confirm).toHaveBeenCalledWith('放弃当前页面未保存的更改?')
  164. expect(voiceWrapper.emitted('close')).toBeUndefined()
  165. })
  166. it('moves mobile focus into details and back to the active category', async () => {
  167. Object.defineProperty(window, 'innerWidth', { configurable: true, value: 390 })
  168. const wrapper = mountSettings()
  169. const identity = navigationButton(wrapper, '身份与头像')
  170. await identity.trigger('click')
  171. await wrapper.vm.$nextTick()
  172. expect(document.activeElement).toBe(wrapper.get('.settings-content__header h3').element)
  173. await wrapper.get('.mobile-back').trigger('click')
  174. await wrapper.vm.$nextTick()
  175. expect(document.activeElement).toBe(identity.element)
  176. const security = navigationButton(wrapper, '登录与安全')
  177. await security.trigger('click')
  178. await wrapper.vm.$nextTick()
  179. expect(document.activeElement).toBe(wrapper.get('.settings-content__header h3').element)
  180. await wrapper.get('.mobile-back').trigger('click')
  181. await wrapper.vm.$nextTick()
  182. expect(document.activeElement).toBe(security.element)
  183. })
  184. it('supports listbox keyboard navigation for the Agent selector', async () => {
  185. const wrapper = mountSettings()
  186. const trigger = wrapper.get('.settings-agent-selector > button')
  187. expect(trigger.attributes('aria-haspopup')).toBe('listbox')
  188. await trigger.trigger('click')
  189. await wrapper.vm.$nextTick()
  190. const menu = wrapper.get('.settings-agent-menu')
  191. const options = menu.findAll<HTMLButtonElement>('[role="option"]')
  192. expect(document.activeElement).toBe(options[0]!.element)
  193. await menu.trigger('keydown', { key: 'ArrowDown' })
  194. expect(document.activeElement).toBe(options[1]!.element)
  195. })
  196. })