chatAppearance.test.ts 5.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { describe, expect, it, afterEach, beforeEach, vi } from 'vitest'
  2. import { nextTick } from 'vue'
  3. import WorkspaceMessageTimeline from '@/components/workspace/WorkspaceMessageTimeline.vue'
  4. import { mount } from '@vue/test-utils'
  5. import { appearancePreset, bubbleColors, bubbleVariables, contrast, normalizeAppearance } from '@/utils/chatAppearance'
  6. import { useChatAppearance, CHAT_APPEARANCE_KEY } from '@/stores/chatAppearance'
  7. import ChatAppearancePanel from '@/components/app/ChatAppearancePanel.vue'
  8. beforeEach(() => {
  9. const values = new Map<string, string>()
  10. vi.stubGlobal('localStorage', {
  11. getItem: (key: string) => values.get(key) ?? null,
  12. setItem: (key: string, value: string) => { values.set(key, value) },
  13. clear: () => values.clear(),
  14. })
  15. vi.stubGlobal('ResizeObserver', class { observe() {} disconnect() {} })
  16. HTMLElement.prototype.scrollTo = () => {}
  17. useChatAppearance().selectPreset('current')
  18. })
  19. afterEach(() => { vi.restoreAllMocks(); useChatAppearance().selectPreset('current') })
  20. describe('chat bubble appearance', () => {
  21. it.each([WorkspaceMessageTimeline])('updates rendered bubbles when a preset changes without replacing messages', async (component) => {
  22. const store = useChatAppearance()
  23. const wrapper = mount(component as typeof WorkspaceMessageTimeline, {
  24. props: {identity: 'appearance', messages: [{id:'u',role:'user',content:'问题'}, {id:'a',role:'assistant',content:'长回复'}, {id:'s',role:'system',content:'系统信息'}]},
  25. global: {stubs: {AgentAvatar:true, AppIcon:true}},
  26. })
  27. store.selectPreset('codex'); await nextTick()
  28. expect(wrapper.findAll('.message__content .chat-bubble')).toHaveLength(2)
  29. expect(wrapper.get('.message--user .message__content').attributes('style')).toContain('#E4F3FC')
  30. store.selectPreset('grok'); await nextTick()
  31. expect(wrapper.get('.message--user .message__content').attributes('style')).toContain('#090909')
  32. expect(wrapper.find('.message--system .chat-bubble').exists()).toBe(false)
  33. wrapper.unmount()
  34. })
  35. it('keeps both sides filled in Codex and Grok Bot presets with readable text', () => {
  36. expect(appearancePreset('grok').user.light).toBe('#090909')
  37. expect(appearancePreset('codex').user.light).toBe('#E4F3FC')
  38. for (const id of ['current', 'grok', 'codex'] as const) for (const role of ['user', 'assistant'] as const) {
  39. const style = appearancePreset(id)[role]
  40. for (const dark of [false, true]) {
  41. const colors = bubbleColors(style, dark)
  42. expect(colors.background).not.toContain('transparent')
  43. expect(contrast(colors.background, colors.text)).toBeGreaterThanOrEqual(4.5)
  44. }
  45. }
  46. })
  47. it('rejects corrupt persisted settings and clamps custom shapes', () => {
  48. for (const data of [null, {version: 2, preset: 'grok'}, {version: 1, preset: 'custom', user: {}}]) expect(normalizeAppearance(data)).toEqual(appearancePreset('current'))
  49. const custom = {...appearancePreset('codex'), preset: 'custom', user: {...appearancePreset('codex').user, radius: 300}}
  50. expect(normalizeAppearance(custom).user.radius).toBe(28)
  51. })
  52. it('protects text against extreme custom gradients', () => {
  53. const style = {...appearancePreset('codex').assistant, gradient: true, light: '#FFFFFF', lightEnd: '#000000'}
  54. expect(bubbleColors(style, false).background).toContain('rgba')
  55. expect(bubbleVariables({...style, tail:true}, 'assistant')['--bubble-radius']).toMatch(/4px$/)
  56. })
  57. it('persists preset selection and undo across store consumers', () => {
  58. const a = useChatAppearance(), b = useChatAppearance()
  59. a.selectPreset('grok'); a.selectPreset('codex')
  60. expect(b.appearance.value.preset).toBe('codex')
  61. expect(JSON.parse(window.localStorage.getItem(CHAT_APPEARANCE_KEY)! ).preset).toBe('codex')
  62. b.selectPreset('codex') // Selecting the active preset must preserve the undo target.
  63. b.undo()
  64. expect(a.appearance.value.preset).toBe('grok')
  65. expect(JSON.parse(window.localStorage.getItem(CHAT_APPEARANCE_KEY)!).preset).toBe('grok')
  66. })
  67. it('does not claim success when storage is unavailable', () => {
  68. const store = useChatAppearance()
  69. store.selectPreset('grok')
  70. vi.spyOn(window.localStorage, 'setItem').mockImplementation(() => {throw new Error('quota')})
  71. store.selectPreset('codex')
  72. expect(store.appearance.value.preset).toBe('grok')
  73. expect(store.saveError.value).toContain('无法保存')
  74. })
  75. it('edits the selected role and theme without changing the other role', async () => {
  76. const store = useChatAppearance(); store.selectPreset('codex')
  77. const wrapper = mount(ChatAppearancePanel, { global: {stubs: {AgentAvatar:true, AppIcon:true}} })
  78. await wrapper.get('[aria-label="调整哪一方"] button:nth-child(2)').trigger('click')
  79. await wrapper.get('[aria-label="预览深色气泡"]').trigger('click')
  80. await wrapper.get('[aria-label="颜色十六进制值"]').setValue('#090909')
  81. expect(store.appearance.value.assistant.dark).toBe('#090909')
  82. expect(store.appearance.value.assistant.light).toBe('#F1F1F1')
  83. expect(store.appearance.value.user.light).toBe('#E4F3FC')
  84. expect(store.appearance.value.preset).toBe('custom')
  85. await wrapper.get('[aria-label="颜色十六进制值"]').setValue('oops')
  86. expect(wrapper.get('[role="alert"]').text()).toContain('六位颜色')
  87. expect(store.appearance.value.assistant.dark).toBe('#090909')
  88. wrapper.unmount()
  89. })
  90. })