|
| 1 | +import { readFileSync } from 'node:fs'; |
| 2 | +import { fileURLToPath } from 'node:url'; |
| 3 | +import { mount } from '@vue/test-utils'; |
| 4 | +import { createI18n } from 'vue-i18n'; |
| 5 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 6 | +import { defineComponent, h, nextTick } from 'vue'; |
| 7 | + |
| 8 | +import ConversationPane from '../src/components/ConversationPane.vue'; |
| 9 | +import Composer from '../src/components/Composer.vue'; |
| 10 | +import { messages } from '../src/i18n/locales'; |
| 11 | +import type { ChatTurn, ConversationStatus } from '../src/types'; |
| 12 | + |
| 13 | +const sourcePath = (path: string) => fileURLToPath(new URL(path, import.meta.url)); |
| 14 | +const conversationPaneSource = readFileSync(sourcePath('../src/components/ConversationPane.vue'), 'utf8'); |
| 15 | +const touchedSources = { |
| 16 | + 'ConversationPane.vue': conversationPaneSource, |
| 17 | + 'i18n/locales/index.ts': readFileSync(sourcePath('../src/i18n/locales/index.ts'), 'utf8'), |
| 18 | + 'i18n/locales/en/suggestions.ts': readFileSync(sourcePath('../src/i18n/locales/en/suggestions.ts'), 'utf8'), |
| 19 | + 'test/empty-suggestions.test.ts': readFileSync(sourcePath('./empty-suggestions.test.ts'), 'utf8'), |
| 20 | +}; |
| 21 | + |
| 22 | +// These literals predate starter suggestions in ConversationPane.vue. |
| 23 | +const preExistingColorLiterals = new Set([ |
| 24 | + '#000', |
| 25 | + ['rgba', '(0, 0, 0, 0.28)'].join(''), |
| 26 | + ['rgba', '(0, 0, 0, 0.14)'].join(''), |
| 27 | + ['rgba', '(0, 0, 0, 0.12)'].join(''), |
| 28 | + ['rgba', '(0, 0, 0, 0.18)'].join(''), |
| 29 | +]); |
| 30 | +const rawColorPattern = /#[0-9a-f]{3,8}\b|rgba?\([^)]*\)/gi; |
| 31 | + |
| 32 | +const status: ConversationStatus = { |
| 33 | + model: 'pythinker-test', |
| 34 | + modelId: 'pythinker-test', |
| 35 | + ctxUsed: 0, |
| 36 | + ctxMax: 0, |
| 37 | + permission: 'manual', |
| 38 | + branch: 'main', |
| 39 | + cwd: '/repo', |
| 40 | + isGitRepo: true, |
| 41 | +}; |
| 42 | + |
| 43 | +const turn: ChatTurn = { |
| 44 | + id: 'turn_1', |
| 45 | + role: 'user', |
| 46 | + text: 'A message already exists', |
| 47 | +}; |
| 48 | + |
| 49 | +class MockResizeObserver { |
| 50 | + constructor(_callback: ResizeObserverCallback) {} |
| 51 | + observe(): void {} |
| 52 | + unobserve(): void {} |
| 53 | + disconnect(): void {} |
| 54 | +} |
| 55 | + |
| 56 | +function createTestI18n() { |
| 57 | + return createI18n({ |
| 58 | + legacy: false, |
| 59 | + locale: 'en', |
| 60 | + messages, |
| 61 | + missingWarn: false, |
| 62 | + fallbackWarn: false, |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +function createComposerStub(loadForEdit: (value: string) => void) { |
| 67 | + return defineComponent({ |
| 68 | + name: 'ComposerStub', |
| 69 | + setup(_, { expose }) { |
| 70 | + expose({ loadForEdit }); |
| 71 | + return () => h('div', { class: 'composer-stub' }); |
| 72 | + }, |
| 73 | + }); |
| 74 | +} |
| 75 | + |
| 76 | +function mountPane(extraProps: Record<string, unknown> = {}, composer = createComposerStub(() => {})) { |
| 77 | + vi.stubGlobal('ResizeObserver', MockResizeObserver); |
| 78 | + |
| 79 | + return mount(ConversationPane, { |
| 80 | + attachTo: document.body, |
| 81 | + props: { |
| 82 | + mobile: true, |
| 83 | + turns: [], |
| 84 | + tasks: [], |
| 85 | + status, |
| 86 | + sessionLoading: false, |
| 87 | + running: false, |
| 88 | + ...extraProps, |
| 89 | + }, |
| 90 | + global: { |
| 91 | + plugins: [createTestI18n()], |
| 92 | + stubs: { |
| 93 | + ChatHeader: true, |
| 94 | + ChatPane: true, |
| 95 | + ChatDock: true, |
| 96 | + DynamicWorkflowCard: true, |
| 97 | + Composer: composer, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +afterEach(() => { |
| 104 | + document.body.innerHTML = ''; |
| 105 | + vi.unstubAllGlobals(); |
| 106 | + vi.restoreAllMocks(); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('ConversationPane starter suggestions', () => { |
| 110 | + it('renders four suggestions only for an empty session', () => { |
| 111 | + const empty = mountPane(); |
| 112 | + expect(empty.findAll('.empty-suggestion')).toHaveLength(4); |
| 113 | + expect(empty.findAll('.empty-suggestion-title').map((node) => node.text())).toEqual([ |
| 114 | + 'Explain this repository', |
| 115 | + 'Find a good first task', |
| 116 | + 'Run the project checks', |
| 117 | + 'Review the working tree', |
| 118 | + ]); |
| 119 | + |
| 120 | + const existing = mountPane({ turns: [turn] }); |
| 121 | + expect(existing.findAll('.empty-suggestion')).toHaveLength(0); |
| 122 | + }); |
| 123 | + |
| 124 | + it('loads the clicked prompt through the empty composer', async () => { |
| 125 | + const loadForEdit = vi.fn(); |
| 126 | + const wrapper = mountPane({}, createComposerStub(loadForEdit)); |
| 127 | + |
| 128 | + await wrapper.get('.empty-suggestion').trigger('click'); |
| 129 | + |
| 130 | + expect(loadForEdit).toHaveBeenCalledWith( |
| 131 | + 'Explain this repository. Show the main packages and how they work together.', |
| 132 | + ); |
| 133 | + }); |
| 134 | + |
| 135 | + it('does not submit or send a message when a suggestion is clicked', async () => { |
| 136 | + const wrapper = mountPane({}, Composer); |
| 137 | + const suggestion = wrapper.get('.empty-suggestion'); |
| 138 | + |
| 139 | + await suggestion.trigger('click'); |
| 140 | + await nextTick(); |
| 141 | + |
| 142 | + expect(wrapper.emitted('submit')).toBeUndefined(); |
| 143 | + expect(wrapper.findComponent(Composer).emitted('submit')).toBeUndefined(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('focuses the real composer after loading a suggestion', async () => { |
| 147 | + const wrapper = mountPane({}, Composer); |
| 148 | + |
| 149 | + await wrapper.get('.empty-suggestion').trigger('click'); |
| 150 | + await nextTick(); |
| 151 | + |
| 152 | + const textarea = wrapper.get('textarea.ph').element as HTMLTextAreaElement; |
| 153 | + expect(textarea.value).toBe( |
| 154 | + 'Explain this repository. Show the main packages and how they work together.', |
| 155 | + ); |
| 156 | + expect(document.activeElement).toBe(textarea); |
| 157 | + }); |
| 158 | +}); |
| 159 | + |
| 160 | +describe('starter suggestion styling and source guards', () => { |
| 161 | + it('uses flat button styling with no base border, background, or shadow', () => { |
| 162 | + const gridRule = conversationPaneSource.match(/\.empty-suggestions\s*\{([\s\S]*?)\n\}/)?.[1] ?? ''; |
| 163 | + const baseRule = conversationPaneSource.match(/\.empty-suggestion\s*\{([\s\S]*?)\n\}/)?.[1] ?? ''; |
| 164 | + |
| 165 | + expect(gridRule).toMatch(/grid-template-columns:\s*repeat\(auto-fit,/); |
| 166 | + expect(gridRule).toMatch(/calc\(var\(--ui-font-size\) \* 20\)/); |
| 167 | + expect(baseRule).not.toBe(''); |
| 168 | + expect(baseRule).toMatch(/border:\s*0;/); |
| 169 | + expect(baseRule).toMatch(/background:\s*none;/); |
| 170 | + expect(baseRule).toMatch(/box-shadow:\s*none;/); |
| 171 | + expect(baseRule).not.toMatch(/border-radius/); |
| 172 | + expect(baseRule).toMatch(/animation:\s*suggestion-rise 200ms/); |
| 173 | + expect(baseRule).toMatch(/animation-delay:\s*calc\(var\(--suggestion-index\) \* 45ms\)/); |
| 174 | + }); |
| 175 | + |
| 176 | + it('disables suggestion entry animation in the existing reduced-motion block', () => { |
| 177 | + const reducedMotionStart = conversationPaneSource.lastIndexOf('@media (prefers-reduced-motion: reduce)'); |
| 178 | + const reducedMotionEnd = conversationPaneSource.indexOf('.empty-add-workspace', reducedMotionStart); |
| 179 | + const reducedMotionBlock = conversationPaneSource.slice(reducedMotionStart, reducedMotionEnd); |
| 180 | + |
| 181 | + expect(reducedMotionBlock).toContain('.empty-suggestion'); |
| 182 | + expect(reducedMotionBlock).toMatch(/\.empty-suggestion[^}]*animation:\s*none;/s); |
| 183 | + }); |
| 184 | + |
| 185 | + it('keeps touched files free of dark utilities and new raw color literals', () => { |
| 186 | + const darkUtility = ['dark', ':'].join(''); |
| 187 | + |
| 188 | + for (const [file, source] of Object.entries(touchedSources)) { |
| 189 | + expect(source, file).not.toContain(darkUtility); |
| 190 | + const literals = source.match(rawColorPattern) ?? []; |
| 191 | + expect(literals.filter((literal) => !preExistingColorLiterals.has(literal)), file).toEqual([]); |
| 192 | + } |
| 193 | + }); |
| 194 | +}); |
0 commit comments