@veltra/desktop 1.10.0 → 1.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/dist/components/ai-button/ai-button.js +117 -0
  2. package/dist/components/ai-button/ai-button.js.map +1 -0
  3. package/dist/components/ai-button/style.d.ts +1 -0
  4. package/dist/components/ai-button/style.js +3 -0
  5. package/dist/components/ai-button/style2.css +29 -0
  6. package/dist/components/code-editor/basic-setup.js +5 -5
  7. package/dist/components/code-editor/code-editor.js +2 -2
  8. package/dist/components/code-editor/langs/bash/index.js +2 -2
  9. package/dist/components/code-editor/langs/powershell/index.js +2 -2
  10. package/dist/components/code-editor/langs/spel/index.js +1 -1
  11. package/dist/components/code-editor/langs/spel/language.js +1 -1
  12. package/dist/components/code-editor/shell-extension.js +2 -2
  13. package/dist/components/file-viewer/previewers/image-previewer.js +28 -124
  14. package/dist/components/file-viewer/previewers/image-previewer.js.map +1 -1
  15. package/dist/components/file-viewer/previewers/pdf-worker-wrapper.js.map +1 -1
  16. package/dist/components/file-viewer/previewers/sheet-previewer.js +5 -5
  17. package/dist/components/file-viewer/previewers/sheet-previewer.js.map +1 -1
  18. package/dist/components/file-viewer/style2.css +42 -203
  19. package/dist/components/image-previewer/image-previewer.js +286 -0
  20. package/dist/components/image-previewer/image-previewer.js.map +1 -0
  21. package/dist/components/image-previewer/style.d.ts +1 -0
  22. package/dist/components/image-previewer/style.js +2 -0
  23. package/dist/components/image-previewer/style2.css +174 -0
  24. package/dist/components/index.d.ts +21 -19
  25. package/dist/components/index.js +41 -37
  26. package/dist/components/index.js.map +1 -1
  27. package/dist/index.d.ts +110 -106
  28. package/dist/index.js +108 -106
  29. package/dist/node_modules/.bun/@codemirror_autocomplete@6.20.3/node_modules/@codemirror/autocomplete/dist/index.js +3 -3
  30. package/dist/node_modules/.bun/@codemirror_autocomplete@6.20.3/node_modules/@codemirror/autocomplete/dist/index.js.map +1 -1
  31. package/dist/node_modules/.bun/{@codemirror_commands@6.11.0 → @codemirror_commands@6.11.1}/node_modules/@codemirror/commands/dist/index.js +6 -6
  32. package/dist/node_modules/.bun/@codemirror_commands@6.11.1/node_modules/@codemirror/commands/dist/index.js.map +1 -0
  33. package/dist/node_modules/.bun/@codemirror_lang-css@6.3.1/node_modules/@codemirror/lang-css/dist/index.js +1 -1
  34. package/dist/node_modules/.bun/@codemirror_lang-css@6.3.1/node_modules/@codemirror/lang-css/dist/index.js.map +1 -1
  35. package/dist/node_modules/.bun/@codemirror_lang-html@6.4.12/node_modules/@codemirror/lang-html/dist/index.js +3 -3
  36. package/dist/node_modules/.bun/@codemirror_lang-html@6.4.12/node_modules/@codemirror/lang-html/dist/index.js.map +1 -1
  37. package/dist/node_modules/.bun/@codemirror_lang-java@6.0.2/node_modules/@codemirror/lang-java/dist/index.js +1 -1
  38. package/dist/node_modules/.bun/@codemirror_lang-java@6.0.2/node_modules/@codemirror/lang-java/dist/index.js.map +1 -1
  39. package/dist/node_modules/.bun/@codemirror_lang-javascript@6.2.5/node_modules/@codemirror/lang-javascript/dist/index.js +3 -3
  40. package/dist/node_modules/.bun/@codemirror_lang-javascript@6.2.5/node_modules/@codemirror/lang-javascript/dist/index.js.map +1 -1
  41. package/dist/node_modules/.bun/@codemirror_lang-json@6.0.2/node_modules/@codemirror/lang-json/dist/index.js +1 -1
  42. package/dist/node_modules/.bun/@codemirror_lang-json@6.0.2/node_modules/@codemirror/lang-json/dist/index.js.map +1 -1
  43. package/dist/node_modules/.bun/@codemirror_lang-markdown@6.5.2/node_modules/@codemirror/lang-markdown/dist/index.js +3 -3
  44. package/dist/node_modules/.bun/@codemirror_lang-markdown@6.5.2/node_modules/@codemirror/lang-markdown/dist/index.js.map +1 -1
  45. package/dist/node_modules/.bun/@codemirror_lang-sql@6.10.0/node_modules/@codemirror/lang-sql/dist/index.js +1 -1
  46. package/dist/node_modules/.bun/@codemirror_lang-sql@6.10.0/node_modules/@codemirror/lang-sql/dist/index.js.map +1 -1
  47. package/dist/node_modules/.bun/{@codemirror_language@6.12.4 → @codemirror_language@6.13.1}/node_modules/@codemirror/language/dist/index.js +5 -168
  48. package/dist/node_modules/.bun/@codemirror_language@6.13.1/node_modules/@codemirror/language/dist/index.js.map +1 -0
  49. package/dist/node_modules/.bun/{@codemirror_legacy-modes@6.5.4 → @codemirror_legacy-modes@6.5.5}/node_modules/@codemirror/legacy-modes/mode/powershell.js +1 -1
  50. package/dist/node_modules/.bun/{@codemirror_legacy-modes@6.5.4 → @codemirror_legacy-modes@6.5.5}/node_modules/@codemirror/legacy-modes/mode/powershell.js.map +1 -1
  51. package/dist/node_modules/.bun/{@codemirror_legacy-modes@6.5.4 → @codemirror_legacy-modes@6.5.5}/node_modules/@codemirror/legacy-modes/mode/shell.js +1 -1
  52. package/dist/node_modules/.bun/{@codemirror_legacy-modes@6.5.4 → @codemirror_legacy-modes@6.5.5}/node_modules/@codemirror/legacy-modes/mode/shell.js.map +1 -1
  53. package/dist/node_modules/.bun/@codemirror_lint@6.9.7/node_modules/@codemirror/lint/dist/index.js +2 -2
  54. package/dist/node_modules/.bun/@codemirror_lint@6.9.7/node_modules/@codemirror/lint/dist/index.js.map +1 -1
  55. package/dist/node_modules/.bun/{@codemirror_search@6.7.1 → @codemirror_search@6.7.2}/node_modules/@codemirror/search/dist/index.js +10 -3
  56. package/dist/node_modules/.bun/@codemirror_search@6.7.2/node_modules/@codemirror/search/dist/index.js.map +1 -0
  57. package/dist/node_modules/.bun/{@codemirror_state@6.7.1 → @codemirror_state@6.7.6}/node_modules/@codemirror/state/dist/index.js +50 -16
  58. package/dist/node_modules/.bun/@codemirror_state@6.7.6/node_modules/@codemirror/state/dist/index.js.map +1 -0
  59. package/dist/node_modules/.bun/@codemirror_streamparser@6.0.0/node_modules/@codemirror/streamparser/dist/index.js +169 -0
  60. package/dist/node_modules/.bun/@codemirror_streamparser@6.0.0/node_modules/@codemirror/streamparser/dist/index.js.map +1 -0
  61. package/dist/node_modules/.bun/@codemirror_theme-one-dark@6.1.3/node_modules/@codemirror/theme-one-dark/dist/index.js +2 -2
  62. package/dist/node_modules/.bun/@codemirror_theme-one-dark@6.1.3/node_modules/@codemirror/theme-one-dark/dist/index.js.map +1 -1
  63. package/dist/node_modules/.bun/{@codemirror_view@6.43.9 → @codemirror_view@6.43.14}/node_modules/@codemirror/view/dist/index.js +159 -93
  64. package/dist/node_modules/.bun/@codemirror_view@6.43.14/node_modules/@codemirror/view/dist/index.js.map +1 -0
  65. package/dist/node_modules/.bun/@lezer_common@1.5.3/node_modules/@lezer/common/dist/index.js.map +1 -1
  66. package/dist/node_modules/.bun/@lezer_css@1.3.9/node_modules/@lezer/css/dist/index.js.map +1 -1
  67. package/dist/node_modules/.bun/@lezer_highlight@1.2.5/node_modules/@lezer/highlight/dist/index.js.map +1 -1
  68. package/dist/node_modules/.bun/@lezer_html@1.3.13/node_modules/@lezer/html/dist/index.js.map +1 -1
  69. package/dist/node_modules/.bun/@lezer_java@1.1.5/node_modules/@lezer/java/dist/index.js.map +1 -1
  70. package/dist/node_modules/.bun/@lezer_javascript@1.5.6/node_modules/@lezer/javascript/dist/index.js.map +1 -1
  71. package/dist/node_modules/.bun/@lezer_json@1.0.3/node_modules/@lezer/json/dist/index.js.map +1 -1
  72. package/dist/node_modules/.bun/@lezer_lr@1.4.11/node_modules/@lezer/lr/dist/index.js.map +1 -1
  73. package/dist/node_modules/.bun/@lezer_markdown@1.8.0/node_modules/@lezer/markdown/dist/index.js.map +1 -1
  74. package/dist/node_modules/.bun/@marijn_find-cluster-break@1.0.4/node_modules/@marijn/find-cluster-break/src/index.js.map +1 -1
  75. package/dist/node_modules/.bun/crelt@1.0.7/node_modules/crelt/index.js.map +1 -1
  76. package/dist/node_modules/.bun/style-mod@4.1.4/node_modules/style-mod/src/style-mod.js.map +1 -1
  77. package/dist/node_modules/.bun/w3c-keyname@2.2.8/node_modules/w3c-keyname/index.js.map +1 -1
  78. package/dist/types/ai-button.d.ts +42 -0
  79. package/dist/types/image-previewer.d.ts +48 -0
  80. package/dist/types/index.d.ts +3 -1
  81. package/package.json +16 -16
  82. package/src/components/ai-button/__test__/ai-button.test.ts +375 -0
  83. package/src/components/ai-button/ai-button.vue +87 -0
  84. package/src/components/ai-button/index.ts +1 -0
  85. package/src/components/ai-button/style.scss +19 -0
  86. package/src/components/ai-button/style.ts +3 -0
  87. package/src/components/file-viewer/__test__/file-viewer.test.ts +58 -0
  88. package/src/components/file-viewer/previewers/image-previewer.vue +25 -138
  89. package/src/components/file-viewer/previewers/sheet-previewer.vue +1 -1
  90. package/src/components/file-viewer/style.scss +54 -266
  91. package/src/components/image-previewer/__test__/image-previewer.test.ts +292 -0
  92. package/src/components/image-previewer/image-previewer.vue +314 -0
  93. package/src/components/image-previewer/index.ts +1 -0
  94. package/src/components/image-previewer/style.scss +188 -0
  95. package/src/components/image-previewer/style.ts +2 -0
  96. package/src/components/index.ts +4 -0
  97. package/src/types/ai-button.ts +44 -0
  98. package/src/types/image-previewer.ts +50 -0
  99. package/src/types/index.ts +3 -0
  100. package/dist/node_modules/.bun/@codemirror_commands@6.11.0/node_modules/@codemirror/commands/dist/index.js.map +0 -1
  101. package/dist/node_modules/.bun/@codemirror_language@6.12.4/node_modules/@codemirror/language/dist/index.js.map +0 -1
  102. package/dist/node_modules/.bun/@codemirror_search@6.7.1/node_modules/@codemirror/search/dist/index.js.map +0 -1
  103. package/dist/node_modules/.bun/@codemirror_state@6.7.1/node_modules/@codemirror/state/dist/index.js.map +0 -1
  104. package/dist/node_modules/.bun/@codemirror_view@6.43.9/node_modules/@codemirror/view/dist/index.js.map +0 -1
@@ -0,0 +1,375 @@
1
+ import { useConfig } from '@veltra/compositions'
2
+ import type { AiProviderConfig } from '@veltra/utils'
3
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vite-plus/test'
4
+ import { createApp, h, nextTick } from 'vue'
5
+
6
+ import UAiButton from '../ai-button.vue'
7
+
8
+ const { setConfig } = useConfig()
9
+
10
+ /** useConfig 的 ai 是模块级全局状态,测后复位避免污染其它用例 */
11
+ function resetAiConfig() {
12
+ setConfig({
13
+ ai: {
14
+ url: undefined,
15
+ apiKey: undefined,
16
+ model: undefined,
17
+ reasoningLevel: undefined,
18
+ stream: undefined
19
+ }
20
+ })
21
+ }
22
+
23
+ function provider(overrides: Partial<AiProviderConfig> = {}): Partial<AiProviderConfig> {
24
+ return {
25
+ url: 'https://api.example.com/v1/chat/completions',
26
+ apiKey: 'sk-test',
27
+ model: 'gpt-x',
28
+ ...overrides
29
+ }
30
+ }
31
+
32
+ function jsonResponse(text: string): Response {
33
+ return new Response(
34
+ JSON.stringify({ choices: [{ message: { role: 'assistant', content: text } }] }),
35
+ { status: 200, headers: { 'Content-Type': 'application/json' } }
36
+ )
37
+ }
38
+
39
+ function sseResponse(sse: string): Response {
40
+ return new Response(sse, { status: 200, headers: { 'Content-Type': 'text/event-stream' } })
41
+ }
42
+
43
+ function deferred<T>() {
44
+ let resolve!: (value: T) => void
45
+ const promise = new Promise<T>((r) => {
46
+ resolve = r
47
+ })
48
+ return { promise, resolve }
49
+ }
50
+
51
+ function mountAiButton(
52
+ props: Record<string, unknown> = {},
53
+ slots: Record<string, () => unknown> = {}
54
+ ) {
55
+ const host = document.createElement('div')
56
+ document.body.appendChild(host)
57
+
58
+ const app = createApp({ render: () => h(UAiButton, props, slots) })
59
+ app.mount(host)
60
+
61
+ return {
62
+ host,
63
+ button: host.querySelector('button')!,
64
+ unmount() {
65
+ app.unmount()
66
+ host.remove()
67
+ }
68
+ }
69
+ }
70
+
71
+ function click(el: Element) {
72
+ el.dispatchEvent(new MouseEvent('click', { bubbles: true }))
73
+ }
74
+
75
+ function requestBody(fetchMock: ReturnType<typeof vi.fn>, nth = 1): Record<string, any> {
76
+ return JSON.parse(String(fetchMock.mock.calls[nth - 1]![1]!.body))
77
+ }
78
+
79
+ beforeEach(() => {
80
+ // happy-dom 中 offsetWidth/offsetHeight 恒为 0,模拟尺寸供 usePop 定位
81
+ vi.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockReturnValue(200)
82
+ vi.spyOn(HTMLElement.prototype, 'offsetHeight', 'get').mockReturnValue(200)
83
+
84
+ // usePop 的浮层容器是模块级单例,上一个用例清空 body 后需补回 teleport 目标
85
+ if (!document.getElementById('pop-container')) {
86
+ const popContainer = document.createElement('div')
87
+ popContainer.id = 'pop-container'
88
+ document.body.appendChild(popContainer)
89
+ }
90
+ })
91
+
92
+ afterEach(() => {
93
+ vi.unstubAllGlobals()
94
+ vi.restoreAllMocks()
95
+ resetAiConfig()
96
+ document.body.innerHTML = ''
97
+ })
98
+
99
+ describe('UAiButton 普通变体', () => {
100
+ it('点击按 OpenAI 兼容协议 POST 配置 url,请求体组装 model/stream/messages,成功触发 success 完整文本', async () => {
101
+ const fetchMock = vi.fn(async () => jsonResponse('完整答案'))
102
+ vi.stubGlobal('fetch', fetchMock)
103
+ const onSuccess = vi.fn()
104
+ const onError = vi.fn()
105
+
106
+ const { button, unmount } = mountAiButton({
107
+ provider: provider({ model: 'gpt-x' }),
108
+ systemPrompt: 'sys',
109
+ prompt: 'hi',
110
+ onSuccess,
111
+ onError
112
+ })
113
+
114
+ try {
115
+ click(button)
116
+ await vi.waitFor(() => expect(onSuccess).toHaveBeenCalledWith('完整答案'))
117
+ expect(onError).not.toHaveBeenCalled()
118
+
119
+ expect(fetchMock).toHaveBeenCalledTimes(1)
120
+ expect(fetchMock.mock.calls[0]![0]).toBe('https://api.example.com/v1/chat/completions')
121
+ expect(requestBody(fetchMock)).toMatchObject({
122
+ model: 'gpt-x',
123
+ stream: false,
124
+ messages: [
125
+ { role: 'system', content: 'sys' },
126
+ { role: 'user', content: 'hi' }
127
+ ]
128
+ })
129
+ } finally {
130
+ unmount()
131
+ }
132
+ })
133
+
134
+ it('流式:增量逐段触发 delta,结束后 success 为拼接完整文本', async () => {
135
+ const sse =
136
+ 'data: {"choices":[{"delta":{"content":"He"}}]}\n\n' +
137
+ 'data: {"choices":[{"delta":{"content":"llo"}}]}\n\n' +
138
+ 'data: [DONE]\n\n'
139
+ const fetchMock = vi.fn(async () => sseResponse(sse))
140
+ vi.stubGlobal('fetch', fetchMock)
141
+ const onDelta = vi.fn()
142
+ const onSuccess = vi.fn()
143
+
144
+ const { button, unmount } = mountAiButton({
145
+ provider: provider({ stream: true }),
146
+ prompt: 'hi',
147
+ onDelta,
148
+ onSuccess
149
+ })
150
+
151
+ try {
152
+ click(button)
153
+ await vi.waitFor(() => expect(onSuccess).toHaveBeenCalled())
154
+
155
+ expect(onDelta).toHaveBeenNthCalledWith(1, 'He')
156
+ expect(onDelta).toHaveBeenNthCalledWith(2, 'llo')
157
+ expect(onSuccess).toHaveBeenCalledWith('Hello')
158
+ expect(requestBody(fetchMock).stream).toBe(true)
159
+ } finally {
160
+ unmount()
161
+ }
162
+ })
163
+
164
+ it('失败触发 error(Error 载荷),不触发 success', async () => {
165
+ vi.stubGlobal(
166
+ 'fetch',
167
+ vi
168
+ .fn()
169
+ .mockRejectedValueOnce(new TypeError('network down'))
170
+ .mockResolvedValueOnce(new Response('boom', { status: 500 }))
171
+ )
172
+ const onSuccess = vi.fn()
173
+ const onError = vi.fn()
174
+
175
+ const { button, unmount } = mountAiButton({
176
+ provider: provider(),
177
+ prompt: 'hi',
178
+ onSuccess,
179
+ onError
180
+ })
181
+
182
+ try {
183
+ click(button)
184
+ await vi.waitFor(() => expect(onError).toHaveBeenCalledTimes(1))
185
+ expect(onError).toHaveBeenNthCalledWith(1, expect.any(Error))
186
+
187
+ click(button)
188
+ await vi.waitFor(() => expect(onError).toHaveBeenCalledTimes(2))
189
+ expect(onSuccess).not.toHaveBeenCalled()
190
+ } finally {
191
+ unmount()
192
+ }
193
+ })
194
+
195
+ it('生成中按钮 loading 并忽略重复点击,结束后恢复可点', async () => {
196
+ const pending = deferred<Response>()
197
+ const fetchMock = vi.fn(() => pending.promise)
198
+ vi.stubGlobal('fetch', fetchMock)
199
+ const onSuccess = vi.fn()
200
+
201
+ const { button, unmount } = mountAiButton({ provider: provider(), prompt: 'hi', onSuccess })
202
+
203
+ try {
204
+ click(button)
205
+ await nextTick()
206
+ expect(fetchMock).toHaveBeenCalledTimes(1)
207
+ expect(button.classList.contains('is-loading')).toBe(true)
208
+
209
+ click(button)
210
+ click(button)
211
+ expect(fetchMock).toHaveBeenCalledTimes(1)
212
+
213
+ pending.resolve(jsonResponse('ok'))
214
+ await vi.waitFor(() => expect(onSuccess).toHaveBeenCalledWith('ok'))
215
+ await nextTick()
216
+ expect(button.classList.contains('is-loading')).toBe(false)
217
+
218
+ click(button)
219
+ expect(fetchMock).toHaveBeenCalledTimes(2)
220
+ } finally {
221
+ unmount()
222
+ }
223
+ })
224
+ })
225
+
226
+ describe('UAiButton 配置优先级', () => {
227
+ it('实例 provider 字段 > 全局 ai 字段 > 默认值(stream 缺省 false)', async () => {
228
+ setConfig({
229
+ ai: {
230
+ url: 'https://global.example.com/v1/chat/completions',
231
+ apiKey: 'sk-global',
232
+ model: 'g-global'
233
+ }
234
+ })
235
+ const fetchMock = vi.fn(async () => jsonResponse('ok'))
236
+ vi.stubGlobal('fetch', fetchMock)
237
+ const onSuccess = vi.fn()
238
+
239
+ // 组件 A:无 provider,全量走全局配置
240
+ const a = mountAiButton({ prompt: 'a', onSuccess })
241
+ // 组件 B:共用全局配置,仅实例覆盖 model 并开流式
242
+ const sse = 'data: {"choices":[{"delta":{"content":"B"}}]}\n\ndata: [DONE]\n\n'
243
+ fetchMock.mockResolvedValueOnce(jsonResponse('ok')).mockResolvedValueOnce(sseResponse(sse))
244
+ const onDelta = vi.fn()
245
+ const b = mountAiButton({
246
+ prompt: 'b',
247
+ provider: { model: 'g-instance', stream: true },
248
+ onDelta,
249
+ onSuccess
250
+ })
251
+
252
+ try {
253
+ click(a.button)
254
+ await vi.waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1))
255
+ click(b.button)
256
+ await vi.waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(2))
257
+
258
+ const bodyA = requestBody(fetchMock, 1)
259
+ expect(fetchMock.mock.calls[0]![0]).toBe('https://global.example.com/v1/chat/completions')
260
+ expect(bodyA.model).toBe('g-global')
261
+ expect(bodyA.stream).toBe(false)
262
+ expect(fetchMock.mock.calls[0]![1]!.headers).toMatchObject({
263
+ Authorization: 'Bearer sk-global'
264
+ })
265
+
266
+ const bodyB = requestBody(fetchMock, 2)
267
+ expect(bodyB.model).toBe('g-instance')
268
+ expect(bodyB.stream).toBe(true)
269
+ expect(onDelta).toHaveBeenCalledWith('B')
270
+ } finally {
271
+ a.unmount()
272
+ b.unmount()
273
+ }
274
+ })
275
+
276
+ it('无全局配置时仅实例 provider 也可用,stream 默认 false', async () => {
277
+ const fetchMock = vi.fn(async () => jsonResponse('ok'))
278
+ vi.stubGlobal('fetch', fetchMock)
279
+ const onSuccess = vi.fn()
280
+
281
+ const { button, unmount } = mountAiButton({ prompt: 'hi', provider: provider(), onSuccess })
282
+
283
+ try {
284
+ click(button)
285
+ await vi.waitFor(() => expect(onSuccess).toHaveBeenCalled())
286
+ expect(requestBody(fetchMock).stream).toBe(false)
287
+ } finally {
288
+ unmount()
289
+ }
290
+ })
291
+ })
292
+
293
+ describe('UAiButton pre-input 变体', () => {
294
+ function findBubble() {
295
+ return document.body.querySelector<HTMLElement>('.u-ai-button__main')
296
+ }
297
+
298
+ function findActionButtons(): HTMLButtonElement[] {
299
+ return Array.from(
300
+ document.body.querySelectorAll<HTMLButtonElement>('.u-ai-button__action button')
301
+ )
302
+ }
303
+
304
+ function mountPreInput(extra: Record<string, unknown> = {}) {
305
+ return mountAiButton(
306
+ { variant: 'pre-input', prompt: 'hi', provider: provider(), ...extra },
307
+ { 'pre-input': () => h('div', { class: 'pre-slot' }, '补充输入') }
308
+ )
309
+ }
310
+
311
+ /** 等待 usePop 打开气泡时的异步定位(含 resize 触发的一轮)落地,避免关闭后回调访问已销毁元素 */
312
+ async function openBubble(button: HTMLButtonElement) {
313
+ click(button)
314
+ await nextTick()
315
+ await nextTick()
316
+ await new Promise((resolve) => setTimeout(resolve, 20))
317
+ }
318
+
319
+ it('点击展开气泡:#pre-input 插槽与内置确认/取消按钮', async () => {
320
+ const { button, unmount } = mountPreInput()
321
+
322
+ try {
323
+ await openBubble(button)
324
+
325
+ expect(findBubble()).not.toBeNull()
326
+ expect(document.body.querySelector('.pre-slot')?.textContent).toBe('补充输入')
327
+ expect(findActionButtons().map((b) => b.textContent?.trim())).toEqual(['取消', '确认'])
328
+ } finally {
329
+ unmount()
330
+ }
331
+ })
332
+
333
+ it('点击按钮本身不发请求,确认后关闭气泡并发起请求', async () => {
334
+ const fetchMock = vi.fn(async () => jsonResponse('ok'))
335
+ vi.stubGlobal('fetch', fetchMock)
336
+ const onConfirm = vi.fn()
337
+ const onSuccess = vi.fn()
338
+
339
+ const { button, unmount } = mountPreInput({ onConfirm, onSuccess })
340
+
341
+ try {
342
+ await openBubble(button)
343
+ expect(fetchMock).not.toHaveBeenCalled()
344
+
345
+ click(findActionButtons()[1]!)
346
+ expect(onConfirm).toHaveBeenCalledTimes(1)
347
+ await vi.waitFor(() => expect(findBubble()).toBeNull())
348
+ await vi.waitFor(() => expect(onSuccess).toHaveBeenCalledWith('ok'))
349
+ expect(fetchMock).toHaveBeenCalledTimes(1)
350
+ } finally {
351
+ unmount()
352
+ }
353
+ })
354
+
355
+ it('取消后关闭气泡且不发请求', async () => {
356
+ const fetchMock = vi.fn(async () => jsonResponse('ok'))
357
+ vi.stubGlobal('fetch', fetchMock)
358
+ const onCancel = vi.fn()
359
+ const onSuccess = vi.fn()
360
+
361
+ const { button, unmount } = mountPreInput({ onCancel, onSuccess })
362
+
363
+ try {
364
+ await openBubble(button)
365
+
366
+ click(findActionButtons()[0]!)
367
+ expect(onCancel).toHaveBeenCalledTimes(1)
368
+ await vi.waitFor(() => expect(findBubble()).toBeNull())
369
+ expect(fetchMock).not.toHaveBeenCalled()
370
+ expect(onSuccess).not.toHaveBeenCalled()
371
+ } finally {
372
+ unmount()
373
+ }
374
+ })
375
+ })
@@ -0,0 +1,87 @@
1
+ <template>
2
+ <!-- pre-input 变体:先经气泡收集补充输入,确认后再发请求 -->
3
+ <u-tip
4
+ v-if="variant === 'pre-input'"
5
+ trigger="click"
6
+ direction="bottom"
7
+ v-model:visible="visible"
8
+ >
9
+ <u-button :class="cls.b" :size="size" :icon="Sparkles" :loading="loading" />
10
+
11
+ <template #content>
12
+ <div :class="cls.e('main')">
13
+ <slot name="pre-input" />
14
+ </div>
15
+ <div :class="cls.e('action')">
16
+ <u-button size="small" type="primary" text @click="cancel"> 取消 </u-button>
17
+ <u-button size="small" type="primary" @click="confirm"> 确认 </u-button>
18
+ </div>
19
+ </template>
20
+ </u-tip>
21
+
22
+ <!-- 普通变体:点击即发 -->
23
+ <u-button v-else :class="cls.b" :size="size" :icon="Sparkles" :loading="loading" @click="send" />
24
+ </template>
25
+
26
+ <script lang="ts" setup>
27
+ import { useConfig } from '@veltra/compositions'
28
+ import { Sparkles } from '@veltra/icons'
29
+ import { bem, requestAiText } from '@veltra/utils'
30
+ import type { AiProviderConfig } from '@veltra/utils'
31
+ import { computed, ref } from 'vue'
32
+
33
+ import type { AiButtonEmits, AiButtonProps } from '../../types'
34
+ import { UButton } from '../button'
35
+ import { UTip } from '../tip'
36
+
37
+ defineOptions({ name: 'UAiButton' })
38
+
39
+ const { provider, systemPrompt, prompt, variant = 'default', size } = defineProps<AiButtonProps>()
40
+
41
+ const emit = defineEmits<AiButtonEmits>()
42
+
43
+ const cls = bem('ai-button')
44
+
45
+ const { config } = useConfig()
46
+
47
+ /** 配置优先级:实例 provider 字段 > 全局 ai 字段 > 默认值(stream 缺省 false) */
48
+ const requestConfig = computed<AiProviderConfig>(() => ({
49
+ stream: false,
50
+ ...config.ai,
51
+ ...provider
52
+ }))
53
+
54
+ /** 生成中:按钮 loading 转圈并禁用 */
55
+ const loading = ref(false)
56
+
57
+ /** 发起请求;生成中忽略重复触发 */
58
+ function send() {
59
+ if (loading.value) return
60
+ loading.value = true
61
+
62
+ requestAiText({
63
+ config: requestConfig.value,
64
+ prompt: prompt ?? '',
65
+ systemPrompt,
66
+ onDelta: (chunk) => emit('delta', chunk),
67
+ onSuccess: (text) => emit('success', text),
68
+ onError: (error) => emit('error', error)
69
+ }).finally(() => {
70
+ loading.value = false
71
+ })
72
+ }
73
+
74
+ /** pre-input 气泡可见性 */
75
+ const visible = ref(false)
76
+
77
+ function confirm() {
78
+ emit('confirm')
79
+ visible.value = false
80
+ send()
81
+ }
82
+
83
+ function cancel() {
84
+ emit('cancel')
85
+ visible.value = false
86
+ }
87
+ </script>
@@ -0,0 +1 @@
1
+ export { default as UAiButton } from './ai-button.vue'
@@ -0,0 +1,19 @@
1
+ @use 'pkg:@veltra/styles/mixins' as m;
2
+ @use 'pkg:@veltra/styles/functions' as fn;
3
+ @use 'pkg:@veltra/styles/vars';
4
+
5
+ // 方便拼接
6
+ $root-name: ai-button;
7
+
8
+ @include m.b($root-name) {
9
+ @include m.e(main) {
10
+ max-width: 280px;
11
+ width: max-content;
12
+ }
13
+
14
+ @include m.e(action) {
15
+ margin-top: fn.use-var(gap, default);
16
+ @include m.flex($justify: flex-end);
17
+ gap: fn.use-var(gap, small);
18
+ }
19
+ }
@@ -0,0 +1,3 @@
1
+ import '../tip/style'
2
+ import '../button/style'
3
+ import './style.scss'
@@ -0,0 +1,58 @@
1
+ import { describe, expect, it, vi } from 'vite-plus/test'
2
+ import { createApp, h, nextTick } from 'vue'
3
+
4
+ import UFileViewer from '../file-viewer.vue'
5
+
6
+ function mountViewer(files: { name: string; src: string }[]) {
7
+ const host = document.createElement('div')
8
+ document.body.appendChild(host)
9
+ const app = createApp({ render: () => h(UFileViewer, { files }) })
10
+ app.mount(host)
11
+ return {
12
+ host,
13
+ unmount() {
14
+ app.unmount()
15
+ host.remove()
16
+ }
17
+ }
18
+ }
19
+
20
+ describe('UFileViewer 图片预览回归(内嵌 UImagePreviewer)', () => {
21
+ it('传入图片项时渲染内嵌预览面与 img,无 lightbox 遮罩', async () => {
22
+ const { host, unmount } = mountViewer([{ name: 'a.png', src: 'https://example.com/a.png' }])
23
+ // previewer 为异步组件,等待 loader 解析并挂载
24
+ await vi.waitFor(() => {
25
+ expect(host.querySelector('.u-file-viewer__image')).toBeTruthy()
26
+ })
27
+
28
+ // 宿主覆盖类与组件自带预览面类落在同一元素上(file-viewer 模态覆盖依赖此前提)
29
+ const stage = host.querySelector<HTMLElement>('.u-file-viewer__image')
30
+ expect(stage).toBeTruthy()
31
+ expect(stage!.classList.contains('u-image-previewer__stage')).toBe(true)
32
+ expect(stage!.querySelector('img')!.getAttribute('src')).toBe('https://example.com/a.png')
33
+ expect(host.querySelector('.u-image-previewer.is-modal')).toBeNull()
34
+ expect(host.querySelector('.u-image-previewer__toolbar')).toBeNull()
35
+ unmount()
36
+ })
37
+
38
+ it('工具栏缩放按钮经 expose 驱动预览面并回显百分比', async () => {
39
+ const { host, unmount } = mountViewer([{ name: 'a.png', src: 'https://example.com/a.png' }])
40
+ await vi.waitFor(() => {
41
+ expect(host.querySelector('.u-file-viewer__image')).toBeTruthy()
42
+ })
43
+
44
+ host.querySelector<HTMLElement>('[aria-label="放大"]')!.click()
45
+ await nextTick()
46
+
47
+ const transform = host.querySelector<HTMLElement>('.u-file-viewer__image')!.style.transform
48
+ expect(transform).toContain('scale(1.1)')
49
+ expect(host.querySelector('.u-file-viewer__zoom-value')!.textContent).toBe('110%')
50
+
51
+ host.querySelector<HTMLElement>('[aria-label="重置视图"]')!.click()
52
+ await nextTick()
53
+ expect(host.querySelector<HTMLElement>('.u-file-viewer__image')!.style.transform).toContain(
54
+ 'scale(1)'
55
+ )
56
+ unmount()
57
+ })
58
+ })