settingsCenterDialog.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import { flushPromises, 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. delete window.yaoyaoDesktop
  70. document.body.innerHTML = ''
  71. vi.restoreAllMocks()
  72. Object.defineProperty(window, 'innerWidth', { configurable: true, value: 1024 })
  73. })
  74. describe('Settings center dialog', () => {
  75. it('keeps desktop runtime controls out of a browser', () => {
  76. const wrapper = mountSettings({ initialPage: 'desktop-mode' })
  77. expect(wrapper.findAll('.settings-sidebar nav button').map(button => button.text())).not.toContain('运行模式')
  78. expect(wrapper.find('[aria-label="服务器与客户端模式"]').exists()).toBe(false)
  79. wrapper.unmount()
  80. })
  81. it('lets a signed-in member switch this desktop from client to server without logging out', async () => {
  82. let mode: 'client' | 'server' = 'client'
  83. const switchMode = vi.fn(async (next: 'client' | 'server') => { mode = next; return { ok: true } })
  84. Object.defineProperty(window, 'yaoyaoDesktop', { configurable: true, value: {
  85. modeState: vi.fn(async () => ({ mode, serverURL: 'http://fixture:15300', switching: false })),
  86. switchMode,
  87. openRemoteLogin: vi.fn(async () => {}),
  88. } })
  89. const wrapper = mountSettings({ isAdmin: false, botMode: true, initialPage: 'desktop-mode' })
  90. await flushPromises()
  91. expect(navigationButton(wrapper, '运行模式').attributes('aria-current')).toBe('page')
  92. expect(wrapper.text()).toContain('当前:客户端模式')
  93. await wrapper.get('input[value="server"]').setValue()
  94. expect(switchMode).not.toHaveBeenCalled()
  95. await wrapper.get('.mode-primary').trigger('click')
  96. await flushPromises()
  97. expect(switchMode).toHaveBeenCalledWith('server')
  98. expect(wrapper.text()).toContain('当前:服务器模式')
  99. expect(wrapper.emitted('logout')).toBeUndefined()
  100. wrapper.unmount()
  101. })
  102. it('preserves the current mode on a failed switch and can reopen server login', async () => {
  103. const openRemoteLogin = vi.fn(async () => {})
  104. Object.defineProperty(window, 'yaoyaoDesktop', { configurable: true, value: {
  105. modeState: vi.fn(async () => ({ mode: 'server', serverURL: 'http://127.0.0.1:15300', switching: false })),
  106. switchMode: vi.fn(async () => ({ ok: false, error: '连接失败,请重试。' })), openRemoteLogin,
  107. } })
  108. const wrapper = mountSettings({ initialPage: 'desktop-mode' })
  109. await flushPromises()
  110. await wrapper.get('input[value="client"]').setValue()
  111. await wrapper.get('.mode-primary').trigger('click'); await flushPromises()
  112. expect(wrapper.get('[role="alert"]').text()).toContain('连接失败')
  113. expect(wrapper.text()).toContain('当前:服务器模式')
  114. await wrapper.findAll('.mode-actions button').find(button => button.text() === '更换服务器…')!.trigger('click')
  115. await flushPromises()
  116. expect(openRemoteLogin).toHaveBeenCalledOnce()
  117. expect(wrapper.get('[role="status"]').text()).toContain('服务器地址并登录')
  118. wrapper.unmount()
  119. })
  120. it.each(['agent-identity', 'agent-models'])('keeps %s and the Profile selector out of Bot-mode personal settings', initialPage => {
  121. const wrapper = mountSettings({ botMode: true, initialPage })
  122. expect(wrapper.findAll('.settings-sidebar nav h3').map(heading => heading.text())).toEqual(['个人', '管理'])
  123. expect(wrapper.find('.settings-agent-selector').exists()).toBe(false)
  124. expect(wrapper.find('[data-testid="agent-identity"]').exists()).toBe(false)
  125. expect(wrapper.find('[data-testid="model-services"]').exists()).toBe(false)
  126. expect(wrapper.get('.settings-content__header').text()).toContain('账号资料')
  127. wrapper.unmount()
  128. })
  129. it('shows theme previews and filters settings without changing account scope', async () => {
  130. const wrapper = mountSettings({initialPage:'appearance',themePreference:'light'})
  131. expect(wrapper.get('[role="dialog"]').attributes('aria-label')).toBe('我的设置')
  132. expect(wrapper.findAll('.theme-options img')).toHaveLength(3)
  133. await wrapper.findAll('[role="radio"]').find(button=>button.text()==='深色')!.trigger('click')
  134. expect(wrapper.emitted('set-theme')).toEqual([['dark']])
  135. await wrapper.get('input[aria-label="搜索设置"]').setValue('Hermes')
  136. expect(wrapper.findAll('.settings-sidebar nav button').map(button=>button.text())).toEqual(['Hermes 连接'])
  137. wrapper.unmount()
  138. })
  139. it.each([false, true])('keeps independent tools and About outside settings (Bot mode=%s)', botMode => {
  140. const wrapper = mountSettings({ botMode, initialPage: 'bot-plugins' })
  141. const labels = wrapper.findAll('.settings-sidebar nav button').map(button => button.text())
  142. for (const label of ['插件', '已连接应用', '自动化', '关于', '本地虚拟机']) expect(labels).not.toContain(label)
  143. expect(wrapper.find('[data-testid="bot-plugins"]').exists()).toBe(false)
  144. expect(wrapper.find('.bot-about').exists()).toBe(false)
  145. wrapper.unmount()
  146. })
  147. it('groups administrator pages and routes Agent, voice, and system content to the right scope', async () => {
  148. const wrapper = mountSettings()
  149. expect(wrapper.findAll('.settings-sidebar nav h3').map(heading => heading.text())).toEqual([
  150. '个人',
  151. '管理',
  152. '基础机器人',
  153. ])
  154. await navigationButton(wrapper, '模型与 Provider').trigger('click')
  155. expect(navigationButton(wrapper, '模型与 Provider').attributes('aria-current')).toBe('page')
  156. const modelPanel = wrapper.get('[data-testid="model-services"]')
  157. expect(modelPanel.attributes('data-profile')).toBe('ops:blue/team')
  158. expect(wrapper.get('.settings-content__header').text()).toContain('正在设置:运维机器人 / ops:blue/team')
  159. await navigationButton(wrapper, '登录与安全').trigger('click')
  160. expect(wrapper.find('.settings-agent-selector').exists()).toBe(false)
  161. expect(wrapper.get('.settings-content__header').text()).toContain('当前账号:owner')
  162. const voiceButton = navigationButton(wrapper, '双流语音')
  163. expect(voiceButton.text()).toContain('全局')
  164. expect(voiceButton.element.closest('section')?.textContent).toContain('管理')
  165. await voiceButton.trigger('click')
  166. expect(wrapper.find('[data-testid="duplex-voice"]').exists()).toBe(true)
  167. expect(wrapper.get('.settings-content__header').text()).toContain('全局设置 · 仅管理员')
  168. })
  169. it('hides model management and the complete system group from non-admin users', () => {
  170. const wrapper = mountSettings({ isAdmin: false, initialPage: 'system-update' })
  171. const navigation = wrapper.get('.settings-sidebar nav')
  172. expect(navigation.findAll('h3').map(heading => heading.text())).toEqual(['个人'])
  173. expect(navigation.text()).not.toContain('模型与 Provider')
  174. expect(navigation.text()).not.toContain('管理')
  175. expect(navigation.text()).not.toContain('系统概览')
  176. expect(navigation.text()).not.toContain('双流语音')
  177. expect(navigation.text()).not.toContain('更新与回滚')
  178. expect(wrapper.find('[data-testid="system-update"]').exists()).toBe(false)
  179. expect(wrapper.get('.settings-content__header').text()).toContain('账号资料')
  180. })
  181. it('blocks closing and page changes while the update panel reports a lock', async () => {
  182. const wrapper = mountSettings({ initialPage: 'system-update' })
  183. expect(wrapper.find('[data-testid="system-update"]').exists()).toBe(true)
  184. await wrapper.get('[data-testid="lock-update"]').trigger('click')
  185. expect(wrapper.get<HTMLButtonElement>('.settings-center__close').element.disabled).toBe(true)
  186. await navigationButton(wrapper, '登录与安全').trigger('click')
  187. await wrapper.get('.settings-center-layer').trigger('mousedown')
  188. expect(wrapper.emitted('close')).toBeUndefined()
  189. expect(wrapper.get('.settings-content__header').text()).toContain('更新与回滚')
  190. expect(wrapper.find('[data-testid="account-security"]').exists()).toBe(false)
  191. await wrapper.get('[data-testid="unlock-update"]').trigger('click')
  192. await navigationButton(wrapper, '登录与安全').trigger('click')
  193. expect(wrapper.find('[data-testid="account-security"]').exists()).toBe(true)
  194. })
  195. it('asks before discarding an edited page and keeps the current page when declined', async () => {
  196. const confirm = vi.spyOn(window, 'confirm').mockReturnValueOnce(false).mockReturnValueOnce(true)
  197. const wrapper = mountSettings()
  198. wrapper.findComponent({ name: 'AgentIdentityPanel' }).vm.$emit('dirty-change', true)
  199. await wrapper.vm.$nextTick()
  200. await navigationButton(wrapper, '登录与安全').trigger('click')
  201. expect(confirm).toHaveBeenCalledWith('放弃当前页面未保存的更改?')
  202. expect(wrapper.get('.settings-content__header').text()).toContain('身份与头像')
  203. await navigationButton(wrapper, '登录与安全').trigger('click')
  204. expect(wrapper.find('[data-testid="account-security"]').exists()).toBe(true)
  205. })
  206. it('protects Provider and global voice drafts from silent navigation or close', async () => {
  207. const confirm = vi.spyOn(window, 'confirm').mockReturnValue(false)
  208. const modelWrapper = mountSettings({ initialPage: 'agent-models' })
  209. await modelWrapper.get('[data-testid="dirty-model"]').trigger('click')
  210. await navigationButton(modelWrapper, '登录与安全').trigger('click')
  211. expect(confirm).toHaveBeenCalledWith('放弃当前页面未保存的更改?')
  212. expect(modelWrapper.find('[data-testid="model-services"]').exists()).toBe(true)
  213. modelWrapper.unmount()
  214. confirm.mockClear()
  215. const voiceWrapper = mountSettings({ initialPage: 'system-voice' })
  216. await voiceWrapper.get('[data-testid="dirty-voice"]').trigger('click')
  217. await voiceWrapper.get('.settings-center__close').trigger('click')
  218. expect(confirm).toHaveBeenCalledWith('放弃当前页面未保存的更改?')
  219. expect(voiceWrapper.emitted('close')).toBeUndefined()
  220. })
  221. it('moves mobile focus into details and back to the active category', async () => {
  222. Object.defineProperty(window, 'innerWidth', { configurable: true, value: 390 })
  223. const wrapper = mountSettings()
  224. const identity = navigationButton(wrapper, '身份与头像')
  225. await identity.trigger('click')
  226. await wrapper.vm.$nextTick()
  227. expect(document.activeElement).toBe(wrapper.get('.settings-content__header h3').element)
  228. await wrapper.get('.mobile-back').trigger('click')
  229. await wrapper.vm.$nextTick()
  230. expect(document.activeElement).toBe(identity.element)
  231. const security = navigationButton(wrapper, '登录与安全')
  232. await security.trigger('click')
  233. await wrapper.vm.$nextTick()
  234. expect(document.activeElement).toBe(wrapper.get('.settings-content__header h3').element)
  235. await wrapper.get('.mobile-back').trigger('click')
  236. await wrapper.vm.$nextTick()
  237. expect(document.activeElement).toBe(security.element)
  238. })
  239. it('supports listbox keyboard navigation for the Agent selector', async () => {
  240. const wrapper = mountSettings()
  241. const trigger = wrapper.get('.settings-agent-selector > button')
  242. expect(trigger.attributes('aria-haspopup')).toBe('listbox')
  243. await trigger.trigger('click')
  244. await wrapper.vm.$nextTick()
  245. const menu = wrapper.get('.settings-agent-menu')
  246. const options = menu.findAll<HTMLButtonElement>('[role="option"]')
  247. expect(document.activeElement).toBe(options[0]!.element)
  248. await menu.trigger('keydown', { key: 'ArrowDown' })
  249. expect(document.activeElement).toBe(options[1]!.element)
  250. })
  251. })