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. 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('shows theme previews and filters settings without changing account scope', async () => {
  75. const wrapper = mountSettings({initialPage:'appearance',themePreference:'light'})
  76. expect(wrapper.get('[role="dialog"]').attributes('aria-label')).toBe('我的设置')
  77. expect(wrapper.findAll('.theme-options img')).toHaveLength(3)
  78. await wrapper.findAll('[role="radio"]').find(button=>button.text()==='深色')!.trigger('click')
  79. expect(wrapper.emitted('set-theme')).toEqual([['dark']])
  80. await wrapper.get('input[aria-label="搜索设置"]').setValue('Hermes')
  81. expect(wrapper.findAll('.settings-sidebar nav button').map(button=>button.text())).toEqual(['Hermes 连接'])
  82. wrapper.unmount()
  83. })
  84. it.each([false, true])('keeps independent tools and About outside settings (Bot mode=%s)', botMode => {
  85. const wrapper = mountSettings({ botMode, initialPage: 'bot-plugins' })
  86. const labels = wrapper.findAll('.settings-sidebar nav button').map(button => button.text())
  87. for (const label of ['插件', '已连接应用', '自动化', '关于', '本地虚拟机']) expect(labels).not.toContain(label)
  88. expect(wrapper.find('[data-testid="bot-plugins"]').exists()).toBe(false)
  89. expect(wrapper.find('.bot-about').exists()).toBe(false)
  90. wrapper.unmount()
  91. })
  92. it('groups administrator pages and routes Agent, voice, and system content to the right scope', async () => {
  93. const wrapper = mountSettings()
  94. expect(wrapper.findAll('.settings-sidebar nav h3').map(heading => heading.text())).toEqual([
  95. '个人',
  96. '管理',
  97. '基础机器人',
  98. ])
  99. await navigationButton(wrapper, '模型与 Provider').trigger('click')
  100. expect(navigationButton(wrapper, '模型与 Provider').attributes('aria-current')).toBe('page')
  101. const modelPanel = wrapper.get('[data-testid="model-services"]')
  102. expect(modelPanel.attributes('data-profile')).toBe('ops:blue/team')
  103. expect(wrapper.get('.settings-content__header').text()).toContain('正在设置:运维机器人 / ops:blue/team')
  104. await navigationButton(wrapper, '登录与安全').trigger('click')
  105. expect(wrapper.find('.settings-agent-selector').exists()).toBe(false)
  106. expect(wrapper.get('.settings-content__header').text()).toContain('当前账号:owner')
  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. })