settingsCenterDialog.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. AgentIdentityPanel: simpleStub('AgentIdentityPanel', 'agent-identity'),
  39. ModelServicesPanel: ModelServicesPanelStub,
  40. AccountSecurityPanel: simpleStub('AccountSecurityPanel', 'account-security'),
  41. NodePairingPanel: simpleStub('NodePairingPanel', 'node-pairing'),
  42. SystemOverviewPanel: simpleStub('SystemOverviewPanel', 'system-overview'),
  43. SystemManagementPanel: simpleStub('SystemManagementPanel', 'system-management'),
  44. DuplexVoicePanel: DuplexVoicePanelStub,
  45. SystemUpdatePanel: SystemUpdatePanelStub,
  46. }
  47. function mountSettings(overrides: Record<string, unknown> = {}) {
  48. return mount(SettingsCenterDialog, {
  49. attachTo: document.body,
  50. props: {
  51. open: true,
  52. activeProfile: profiles[0],
  53. profiles,
  54. userName: '管理员',
  55. pairingUserName: 'owner',
  56. isAdmin: true,
  57. ...overrides,
  58. } as never,
  59. global: { stubs: childStubs },
  60. })
  61. }
  62. function navigationButton(wrapper: VueWrapper, label: string) {
  63. const button = wrapper.findAll<HTMLButtonElement>('.settings-sidebar nav button')
  64. .find(candidate => candidate.text().includes(label))
  65. expect(button, `missing navigation button: ${label}`).toBeDefined()
  66. return button!
  67. }
  68. afterEach(() => {
  69. document.body.innerHTML = ''
  70. vi.restoreAllMocks()
  71. Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1024 })
  72. })
  73. describe('Settings center dialog', () => {
  74. it.each(['agent-identity', 'agent-models'])('keeps %s and the Profile selector out of Bot-mode personal settings', initialPage => {
  75. const wrapper = mountSettings({ botMode: true, initialPage })
  76. expect(wrapper.findAll('.settings-sidebar nav h3').map(heading => heading.text())).toEqual(['个人', '管理'])
  77. expect(wrapper.find('.settings-agent-selector').exists()).toBe(false)
  78. expect(wrapper.find('[data-testid="agent-identity"]').exists()).toBe(false)
  79. expect(wrapper.find('[data-testid="model-services"]').exists()).toBe(false)
  80. expect(wrapper.get('.settings-content__header').text()).toContain('账号资料')
  81. wrapper.unmount()
  82. })
  83. it('shows theme previews and filters settings without changing account scope', async () => {
  84. const wrapper = mountSettings({initialPage:'appearance',themePreference:'light'})
  85. expect(wrapper.get('[role="dialog"]').attributes('aria-label')).toBe('我的设置')
  86. expect(wrapper.findAll('.theme-options img')).toHaveLength(3)
  87. await wrapper.findAll('[role="radio"]').find(button=>button.text()==='深色')!.trigger('click')
  88. expect(wrapper.emitted('set-theme')).toEqual([['dark']])
  89. await wrapper.get('input[aria-label="搜索设置"]').setValue('Hermes')
  90. expect(wrapper.findAll('.settings-sidebar nav button').map(button=>button.text())).toEqual(['Hermes 连接'])
  91. wrapper.unmount()
  92. })
  93. it.each([false, true])('keeps independent tools and About outside settings (Bot mode=%s)', botMode => {
  94. const wrapper = mountSettings({ botMode, initialPage: 'bot-plugins' })
  95. const labels = wrapper.findAll('.settings-sidebar nav button').map(button => button.text())
  96. for (const label of ['插件', '已连接应用', '自动化', '关于', '本地虚拟机']) expect(labels).not.toContain(label)
  97. expect(wrapper.find('[data-testid="bot-plugins"]').exists()).toBe(false)
  98. expect(wrapper.find('.bot-about').exists()).toBe(false)
  99. wrapper.unmount()
  100. })
  101. it('groups administrator pages and routes Agent, voice, and system content to the right scope', async () => {
  102. const wrapper = mountSettings()
  103. expect(wrapper.findAll('.settings-sidebar nav h3').map(heading => heading.text())).toEqual([
  104. '个人',
  105. '管理',
  106. '基础机器人',
  107. ])
  108. await navigationButton(wrapper, '模型与 Provider').trigger('click')
  109. expect(navigationButton(wrapper, '模型与 Provider').attributes('aria-current')).toBe('page')
  110. const modelPanel = wrapper.get('[data-testid="model-services"]')
  111. expect(modelPanel.attributes('data-profile')).toBe('ops:blue/team')
  112. expect(wrapper.get('.settings-content__header').text()).toContain('正在设置:运维机器人 / ops:blue/team')
  113. await navigationButton(wrapper, '登录与安全').trigger('click')
  114. expect(wrapper.find('.settings-agent-selector').exists()).toBe(false)
  115. expect(wrapper.get('.settings-content__header').text()).toContain('当前账号:owner')
  116. const voiceButton = navigationButton(wrapper, '双流语音')
  117. expect(voiceButton.text()).toContain('全局')
  118. expect(voiceButton.element.closest('section')?.textContent).toContain('管理')
  119. await voiceButton.trigger('click')
  120. expect(wrapper.find('[data-testid="duplex-voice"]').exists()).toBe(true)
  121. expect(wrapper.get('.settings-content__header').text()).toContain('全局设置 · 仅管理员')
  122. })
  123. it('hides model management and the complete system group from non-admin users', () => {
  124. const wrapper = mountSettings({ isAdmin: false, initialPage: 'system-update' })
  125. const navigation = wrapper.get('.settings-sidebar nav')
  126. expect(navigation.findAll('h3').map(heading => heading.text())).toEqual(['个人'])
  127. expect(navigation.text()).not.toContain('模型与 Provider')
  128. expect(navigation.text()).not.toContain('管理')
  129. expect(navigation.text()).not.toContain('系统概览')
  130. expect(navigation.text()).not.toContain('双流语音')
  131. expect(navigation.text()).not.toContain('更新与回滚')
  132. expect(wrapper.find('[data-testid="system-update"]').exists()).toBe(false)
  133. expect(wrapper.get('.settings-content__header').text()).toContain('账号资料')
  134. })
  135. it('blocks closing and page changes while the update panel reports a lock', async () => {
  136. const wrapper = mountSettings({ initialPage: 'system-update' })
  137. expect(wrapper.find('[data-testid="system-update"]').exists()).toBe(true)
  138. await wrapper.get('[data-testid="lock-update"]').trigger('click')
  139. expect(wrapper.get<HTMLButtonElement>('.settings-center__close').element.disabled).toBe(true)
  140. await navigationButton(wrapper, '登录与安全').trigger('click')
  141. await wrapper.get('.settings-center-layer').trigger('mousedown')
  142. expect(wrapper.emitted('close')).toBeUndefined()
  143. expect(wrapper.get('.settings-content__header').text()).toContain('更新与回滚')
  144. expect(wrapper.find('[data-testid="account-security"]').exists()).toBe(false)
  145. await wrapper.get('[data-testid="unlock-update"]').trigger('click')
  146. await navigationButton(wrapper, '登录与安全').trigger('click')
  147. expect(wrapper.find('[data-testid="account-security"]').exists()).toBe(true)
  148. })
  149. it('asks before discarding an edited page and keeps the current page when declined', async () => {
  150. const confirm = vi.spyOn(window, 'confirm').mockReturnValueOnce(false).mockReturnValueOnce(true)
  151. const wrapper = mountSettings()
  152. wrapper.findComponent({ name: 'AgentIdentityPanel' }).vm.$emit('dirty-change', true)
  153. await wrapper.vm.$nextTick()
  154. await navigationButton(wrapper, '登录与安全').trigger('click')
  155. expect(confirm).toHaveBeenCalledWith('放弃当前页面未保存的更改?')
  156. expect(wrapper.get('.settings-content__header').text()).toContain('身份与头像')
  157. await navigationButton(wrapper, '登录与安全').trigger('click')
  158. expect(wrapper.find('[data-testid="account-security"]').exists()).toBe(true)
  159. })
  160. it('protects Provider and global voice drafts from silent navigation or close', async () => {
  161. const confirm = vi.spyOn(window, 'confirm').mockReturnValue(false)
  162. const modelWrapper = mountSettings({ initialPage: 'agent-models' })
  163. await modelWrapper.get('[data-testid="dirty-model"]').trigger('click')
  164. await navigationButton(modelWrapper, '登录与安全').trigger('click')
  165. expect(confirm).toHaveBeenCalledWith('放弃当前页面未保存的更改?')
  166. expect(modelWrapper.find('[data-testid="model-services"]').exists()).toBe(true)
  167. modelWrapper.unmount()
  168. confirm.mockClear()
  169. const voiceWrapper = mountSettings({ initialPage: 'system-voice' })
  170. await voiceWrapper.get('[data-testid="dirty-voice"]').trigger('click')
  171. await voiceWrapper.get('.settings-center__close').trigger('click')
  172. expect(confirm).toHaveBeenCalledWith('放弃当前页面未保存的更改?')
  173. expect(voiceWrapper.emitted('close')).toBeUndefined()
  174. })
  175. it('moves mobile focus into details and back to the active category', async () => {
  176. Object.defineProperty(window, 'innerWidth', { configurable: true, value: 390 })
  177. const wrapper = mountSettings()
  178. const identity = navigationButton(wrapper, '身份与头像')
  179. await identity.trigger('click')
  180. await wrapper.vm.$nextTick()
  181. expect(document.activeElement).toBe(wrapper.get('.settings-content__header h3').element)
  182. await wrapper.get('.mobile-back').trigger('click')
  183. await wrapper.vm.$nextTick()
  184. expect(document.activeElement).toBe(identity.element)
  185. const security = navigationButton(wrapper, '登录与安全')
  186. await security.trigger('click')
  187. await wrapper.vm.$nextTick()
  188. expect(document.activeElement).toBe(wrapper.get('.settings-content__header h3').element)
  189. await wrapper.get('.mobile-back').trigger('click')
  190. await wrapper.vm.$nextTick()
  191. expect(document.activeElement).toBe(security.element)
  192. })
  193. it('supports listbox keyboard navigation for the Agent selector', async () => {
  194. const wrapper = mountSettings()
  195. const trigger = wrapper.get('.settings-agent-selector > button')
  196. expect(trigger.attributes('aria-haspopup')).toBe('listbox')
  197. await trigger.trigger('click')
  198. await wrapper.vm.$nextTick()
  199. const menu = wrapper.get('.settings-agent-menu')
  200. const options = menu.findAll<HTMLButtonElement>('[role="option"]')
  201. expect(document.activeElement).toBe(options[0]!.element)
  202. await menu.trigger('keydown', { key: 'ArrowDown' })
  203. expect(document.activeElement).toBe(options[1]!.element)
  204. })
  205. })