settingsCenterDialog.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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: '运维 Agent', 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('groups administrator pages and routes Agent, voice, and system content to the right scope', async () => {
  75. const wrapper = mountSettings()
  76. expect(wrapper.findAll('.settings-sidebar nav h3').map(heading => heading.text())).toEqual([
  77. '当前 Agent',
  78. '账号',
  79. '系统 · 仅管理员',
  80. ])
  81. await navigationButton(wrapper, '模型与 Provider').trigger('click')
  82. expect(navigationButton(wrapper, '模型与 Provider').attributes('aria-current')).toBe('page')
  83. const modelPanel = wrapper.get('[data-testid="model-services"]')
  84. expect(modelPanel.attributes('data-profile')).toBe('ops:blue/team')
  85. expect(wrapper.get('.settings-content__header').text()).toContain('正在设置:运维 Agent / ops:blue/team')
  86. const voiceButton = navigationButton(wrapper, '双流语音')
  87. expect(voiceButton.text()).toContain('全局')
  88. expect(voiceButton.element.closest('section')?.textContent).toContain('系统 · 仅管理员')
  89. await voiceButton.trigger('click')
  90. expect(wrapper.find('[data-testid="duplex-voice"]').exists()).toBe(true)
  91. expect(wrapper.get('.settings-content__header').text()).toContain('全局设置 · 仅管理员')
  92. })
  93. it('hides model management and the complete system group from non-admin users', () => {
  94. const wrapper = mountSettings({ isAdmin: false, initialPage: 'system-update' })
  95. const navigation = wrapper.get('.settings-sidebar nav')
  96. expect(navigation.findAll('h3').map(heading => heading.text())).toEqual(['当前 Agent', '账号'])
  97. expect(navigation.text()).not.toContain('模型与 Provider')
  98. expect(navigation.text()).not.toContain('系统 · 仅管理员')
  99. expect(navigation.text()).not.toContain('系统概览')
  100. expect(navigation.text()).not.toContain('双流语音')
  101. expect(navigation.text()).not.toContain('更新与回滚')
  102. expect(wrapper.find('[data-testid="system-update"]').exists()).toBe(false)
  103. expect(wrapper.get('.settings-content__header').text()).toContain('身份与头像')
  104. })
  105. it('blocks closing and page changes while the update panel reports a lock', async () => {
  106. const wrapper = mountSettings({ initialPage: 'system-update' })
  107. expect(wrapper.find('[data-testid="system-update"]').exists()).toBe(true)
  108. await wrapper.get('[data-testid="lock-update"]').trigger('click')
  109. expect(wrapper.get<HTMLButtonElement>('.settings-center__close').element.disabled).toBe(true)
  110. await navigationButton(wrapper, '登录与安全').trigger('click')
  111. await wrapper.get('.settings-center-layer').trigger('mousedown')
  112. expect(wrapper.emitted('close')).toBeUndefined()
  113. expect(wrapper.get('.settings-content__header').text()).toContain('更新与回滚')
  114. expect(wrapper.find('[data-testid="account-security"]').exists()).toBe(false)
  115. await wrapper.get('[data-testid="unlock-update"]').trigger('click')
  116. await navigationButton(wrapper, '登录与安全').trigger('click')
  117. expect(wrapper.find('[data-testid="account-security"]').exists()).toBe(true)
  118. })
  119. it('asks before discarding an edited page and keeps the current page when declined', async () => {
  120. const confirm = vi.spyOn(window, 'confirm').mockReturnValueOnce(false).mockReturnValueOnce(true)
  121. const wrapper = mountSettings()
  122. wrapper.findComponent({ name: 'AgentIdentityPanel' }).vm.$emit('dirty-change', true)
  123. await wrapper.vm.$nextTick()
  124. await navigationButton(wrapper, '登录与安全').trigger('click')
  125. expect(confirm).toHaveBeenCalledWith('放弃当前页面未保存的更改?')
  126. expect(wrapper.get('.settings-content__header').text()).toContain('身份与头像')
  127. await navigationButton(wrapper, '登录与安全').trigger('click')
  128. expect(wrapper.find('[data-testid="account-security"]').exists()).toBe(true)
  129. })
  130. it('protects Provider and global voice drafts from silent navigation or close', async () => {
  131. const confirm = vi.spyOn(window, 'confirm').mockReturnValue(false)
  132. const modelWrapper = mountSettings({ initialPage: 'agent-models' })
  133. await modelWrapper.get('[data-testid="dirty-model"]').trigger('click')
  134. await navigationButton(modelWrapper, '登录与安全').trigger('click')
  135. expect(confirm).toHaveBeenCalledWith('放弃当前页面未保存的更改?')
  136. expect(modelWrapper.find('[data-testid="model-services"]').exists()).toBe(true)
  137. modelWrapper.unmount()
  138. confirm.mockClear()
  139. const voiceWrapper = mountSettings({ initialPage: 'system-voice' })
  140. await voiceWrapper.get('[data-testid="dirty-voice"]').trigger('click')
  141. await voiceWrapper.get('.settings-center__close').trigger('click')
  142. expect(confirm).toHaveBeenCalledWith('放弃当前页面未保存的更改?')
  143. expect(voiceWrapper.emitted('close')).toBeUndefined()
  144. })
  145. it('moves mobile focus into details and back to the active category', async () => {
  146. Object.defineProperty(window, 'innerWidth', { configurable: true, value: 390 })
  147. const wrapper = mountSettings()
  148. const identity = navigationButton(wrapper, '身份与头像')
  149. await identity.trigger('click')
  150. await wrapper.vm.$nextTick()
  151. expect(document.activeElement).toBe(wrapper.get('.settings-content__header h3').element)
  152. await wrapper.get('.mobile-back').trigger('click')
  153. await wrapper.vm.$nextTick()
  154. expect(document.activeElement).toBe(identity.element)
  155. const security = navigationButton(wrapper, '登录与安全')
  156. await security.trigger('click')
  157. await wrapper.vm.$nextTick()
  158. expect(document.activeElement).toBe(wrapper.get('.settings-content__header h3').element)
  159. await wrapper.get('.mobile-back').trigger('click')
  160. await wrapper.vm.$nextTick()
  161. expect(document.activeElement).toBe(security.element)
  162. })
  163. it('supports listbox keyboard navigation for the Agent selector', async () => {
  164. const wrapper = mountSettings()
  165. const trigger = wrapper.get('.settings-agent-selector > button')
  166. expect(trigger.attributes('aria-haspopup')).toBe('listbox')
  167. await trigger.trigger('click')
  168. await wrapper.vm.$nextTick()
  169. const menu = wrapper.get('.settings-agent-menu')
  170. const options = menu.findAll<HTMLButtonElement>('[role="option"]')
  171. expect(document.activeElement).toBe(options[0]!.element)
  172. await menu.trigger('keydown', { key: 'ArrowDown' })
  173. expect(document.activeElement).toBe(options[1]!.element)
  174. })
  175. })