forked from qodo-benchmark/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.spec.tsx
More file actions
620 lines (496 loc) · 19.6 KB
/
install.spec.tsx
File metadata and controls
620 lines (496 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
import type { PluginDeclaration } from '../../../types'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { PluginCategoryEnum, TaskStatus } from '../../../types'
import Install from './install'
// Factory function for test data
const createMockManifest = (overrides: Partial<PluginDeclaration> = {}): PluginDeclaration => ({
plugin_unique_identifier: 'test-plugin-uid',
version: '1.0.0',
author: 'test-author',
icon: 'test-icon.png',
name: 'Test Plugin',
category: PluginCategoryEnum.tool,
label: { 'en-US': 'Test Plugin' } as PluginDeclaration['label'],
description: { 'en-US': 'A test plugin' } as PluginDeclaration['description'],
created_at: '2024-01-01T00:00:00Z',
resource: {},
plugins: [],
verified: true,
endpoint: { settings: [], endpoints: [] },
model: null,
tags: [],
agent_strategy: null,
meta: { version: '1.0.0', minimum_dify_version: '0.8.0' },
trigger: {} as PluginDeclaration['trigger'],
...overrides,
})
// Mock external dependencies
const mockUseCheckInstalled = vi.fn()
vi.mock('@/app/components/plugins/install-plugin/hooks/use-check-installed', () => ({
default: () => mockUseCheckInstalled(),
}))
const mockInstallPackageFromLocal = vi.fn()
vi.mock('@/service/use-plugins', () => ({
useInstallPackageFromLocal: () => ({
mutateAsync: mockInstallPackageFromLocal,
}),
usePluginTaskList: () => ({
handleRefetch: vi.fn(),
}),
}))
const mockUninstallPlugin = vi.fn()
vi.mock('@/service/plugins', () => ({
uninstallPlugin: (...args: unknown[]) => mockUninstallPlugin(...args),
}))
const mockCheck = vi.fn()
const mockStop = vi.fn()
vi.mock('../../base/check-task-status', () => ({
default: () => ({
check: mockCheck,
stop: mockStop,
}),
}))
const mockLangGeniusVersionInfo = { current_version: '1.0.0' }
vi.mock('@/context/app-context', () => ({
useAppContext: () => ({
langGeniusVersionInfo: mockLangGeniusVersionInfo,
}),
}))
vi.mock('react-i18next', async (importOriginal) => {
const actual = await importOriginal<typeof import('react-i18next')>()
const { createReactI18nextMock } = await import('@/test/i18n-mock')
return {
...actual,
...createReactI18nextMock(),
Trans: ({ i18nKey, components }: { i18nKey: string, components?: Record<string, React.ReactNode> }) => (
<span data-testid="trans">
{i18nKey}
{components?.trustSource}
</span>
),
}
})
vi.mock('../../../card', () => ({
default: ({ payload, titleLeft }: {
payload: Record<string, unknown>
titleLeft?: React.ReactNode
}) => (
<div data-testid="card">
<span data-testid="card-name">{payload?.name as string}</span>
<div data-testid="card-title-left">{titleLeft}</div>
</div>
),
}))
vi.mock('../../base/version', () => ({
default: ({ hasInstalled, installedVersion, toInstallVersion }: {
hasInstalled: boolean
installedVersion?: string
toInstallVersion: string
}) => (
<div data-testid="version">
<span data-testid="version-has-installed">{hasInstalled ? 'true' : 'false'}</span>
<span data-testid="version-installed">{installedVersion || 'null'}</span>
<span data-testid="version-to-install">{toInstallVersion}</span>
</div>
),
}))
vi.mock('../../utils', () => ({
pluginManifestToCardPluginProps: (manifest: PluginDeclaration) => ({
name: manifest.name,
author: manifest.author,
version: manifest.version,
}),
}))
describe('Install', () => {
const defaultProps = {
uniqueIdentifier: 'test-unique-identifier',
payload: createMockManifest(),
onCancel: vi.fn(),
onStartToInstall: vi.fn(),
onInstalled: vi.fn(),
onFailed: vi.fn(),
}
beforeEach(() => {
vi.clearAllMocks()
mockUseCheckInstalled.mockReturnValue({
installedInfo: null,
isLoading: false,
})
mockInstallPackageFromLocal.mockReset()
mockUninstallPlugin.mockReset()
mockCheck.mockReset()
mockStop.mockReset()
})
// ================================
// Rendering Tests
// ================================
describe('Rendering', () => {
it('should render ready to install message', () => {
render(<Install {...defaultProps} />)
expect(screen.getByText('plugin.installModal.readyToInstall')).toBeInTheDocument()
})
it('should render trust source message', () => {
render(<Install {...defaultProps} />)
expect(screen.getByTestId('trans')).toBeInTheDocument()
})
it('should render plugin card', () => {
render(<Install {...defaultProps} />)
expect(screen.getByTestId('card')).toBeInTheDocument()
expect(screen.getByTestId('card-name')).toHaveTextContent('Test Plugin')
})
it('should render cancel button', () => {
render(<Install {...defaultProps} />)
expect(screen.getByRole('button', { name: 'common.operation.cancel' })).toBeInTheDocument()
})
it('should render install button', () => {
render(<Install {...defaultProps} />)
expect(screen.getByRole('button', { name: 'plugin.installModal.install' })).toBeInTheDocument()
})
it('should show version component when not loading', () => {
mockUseCheckInstalled.mockReturnValue({
installedInfo: null,
isLoading: false,
})
render(<Install {...defaultProps} />)
expect(screen.getByTestId('version')).toBeInTheDocument()
})
it('should not show version component when loading', () => {
mockUseCheckInstalled.mockReturnValue({
installedInfo: null,
isLoading: true,
})
render(<Install {...defaultProps} />)
expect(screen.queryByTestId('version')).not.toBeInTheDocument()
})
})
// ================================
// Version Display Tests
// ================================
describe('Version Display', () => {
it('should display toInstallVersion from payload', () => {
const payload = createMockManifest({ version: '2.0.0' })
mockUseCheckInstalled.mockReturnValue({
installedInfo: null,
isLoading: false,
})
render(<Install {...defaultProps} payload={payload} />)
expect(screen.getByTestId('version-to-install')).toHaveTextContent('2.0.0')
})
it('should display hasInstalled=false when not installed', () => {
mockUseCheckInstalled.mockReturnValue({
installedInfo: null,
isLoading: false,
})
render(<Install {...defaultProps} />)
expect(screen.getByTestId('version-has-installed')).toHaveTextContent('false')
})
it('should display hasInstalled=true when already installed', () => {
mockUseCheckInstalled.mockReturnValue({
installedInfo: {
'test-author/Test Plugin': {
installedVersion: '0.9.0',
installedId: 'installed-id',
uniqueIdentifier: 'old-uid',
},
},
isLoading: false,
})
render(<Install {...defaultProps} />)
expect(screen.getByTestId('version-has-installed')).toHaveTextContent('true')
expect(screen.getByTestId('version-installed')).toHaveTextContent('0.9.0')
})
})
// ================================
// Install Button State Tests
// ================================
describe('Install Button State', () => {
it('should disable install button when loading', () => {
mockUseCheckInstalled.mockReturnValue({
installedInfo: null,
isLoading: true,
})
render(<Install {...defaultProps} />)
expect(screen.getByRole('button', { name: 'plugin.installModal.install' })).toBeDisabled()
})
it('should enable install button when not loading', () => {
mockUseCheckInstalled.mockReturnValue({
installedInfo: null,
isLoading: false,
})
render(<Install {...defaultProps} />)
expect(screen.getByRole('button', { name: 'plugin.installModal.install' })).not.toBeDisabled()
})
})
// ================================
// Cancel Button Tests
// ================================
describe('Cancel Button', () => {
it('should call onCancel and stop when cancel button is clicked', () => {
const onCancel = vi.fn()
render(<Install {...defaultProps} onCancel={onCancel} />)
fireEvent.click(screen.getByRole('button', { name: 'common.operation.cancel' }))
expect(mockStop).toHaveBeenCalled()
expect(onCancel).toHaveBeenCalledTimes(1)
})
it('should hide cancel button when installing', async () => {
mockInstallPackageFromLocal.mockImplementation(() => new Promise(() => {}))
render(<Install {...defaultProps} />)
fireEvent.click(screen.getByRole('button', { name: 'plugin.installModal.install' }))
await waitFor(() => {
expect(screen.queryByRole('button', { name: 'common.operation.cancel' })).not.toBeInTheDocument()
})
})
})
// ================================
// Installation Flow Tests
// ================================
describe('Installation Flow', () => {
it('should call onStartToInstall when install button is clicked', async () => {
mockInstallPackageFromLocal.mockResolvedValue({
all_installed: true,
task_id: 'task-123',
})
const onStartToInstall = vi.fn()
render(<Install {...defaultProps} onStartToInstall={onStartToInstall} />)
fireEvent.click(screen.getByRole('button', { name: 'plugin.installModal.install' }))
await waitFor(() => {
expect(onStartToInstall).toHaveBeenCalledTimes(1)
})
})
it('should call onInstalled when all_installed is true', async () => {
mockInstallPackageFromLocal.mockResolvedValue({
all_installed: true,
task_id: 'task-123',
})
const onInstalled = vi.fn()
render(<Install {...defaultProps} onInstalled={onInstalled} />)
fireEvent.click(screen.getByRole('button', { name: 'plugin.installModal.install' }))
await waitFor(() => {
expect(onInstalled).toHaveBeenCalled()
})
})
it('should check task status when all_installed is false', async () => {
mockInstallPackageFromLocal.mockResolvedValue({
all_installed: false,
task_id: 'task-123',
})
mockCheck.mockResolvedValue({ status: TaskStatus.success, error: null })
const onInstalled = vi.fn()
render(<Install {...defaultProps} onInstalled={onInstalled} />)
fireEvent.click(screen.getByRole('button', { name: 'plugin.installModal.install' }))
await waitFor(() => {
expect(mockCheck).toHaveBeenCalledWith({
taskId: 'task-123',
pluginUniqueIdentifier: 'test-unique-identifier',
})
})
await waitFor(() => {
expect(onInstalled).toHaveBeenCalledWith(true)
})
})
it('should call onFailed when task status is failed', async () => {
mockInstallPackageFromLocal.mockResolvedValue({
all_installed: false,
task_id: 'task-123',
})
mockCheck.mockResolvedValue({ status: TaskStatus.failed, error: 'Task failed error' })
const onFailed = vi.fn()
render(<Install {...defaultProps} onFailed={onFailed} />)
fireEvent.click(screen.getByRole('button', { name: 'plugin.installModal.install' }))
await waitFor(() => {
expect(onFailed).toHaveBeenCalledWith('Task failed error')
})
})
it('should uninstall existing plugin before installing new version', async () => {
mockUseCheckInstalled.mockReturnValue({
installedInfo: {
'test-author/Test Plugin': {
installedVersion: '0.9.0',
installedId: 'installed-id-to-uninstall',
uniqueIdentifier: 'old-uid',
},
},
isLoading: false,
})
mockUninstallPlugin.mockResolvedValue({})
mockInstallPackageFromLocal.mockResolvedValue({
all_installed: true,
task_id: 'task-123',
})
render(<Install {...defaultProps} />)
fireEvent.click(screen.getByRole('button', { name: 'plugin.installModal.install' }))
await waitFor(() => {
expect(mockUninstallPlugin).toHaveBeenCalledWith('installed-id-to-uninstall')
})
await waitFor(() => {
expect(mockInstallPackageFromLocal).toHaveBeenCalled()
})
})
})
// ================================
// Error Handling Tests
// ================================
describe('Error Handling', () => {
it('should call onFailed with error string', async () => {
mockInstallPackageFromLocal.mockRejectedValue('Installation error string')
const onFailed = vi.fn()
render(<Install {...defaultProps} onFailed={onFailed} />)
fireEvent.click(screen.getByRole('button', { name: 'plugin.installModal.install' }))
await waitFor(() => {
expect(onFailed).toHaveBeenCalledWith('Installation error string')
})
})
it('should call onFailed without message when error is not string', async () => {
mockInstallPackageFromLocal.mockRejectedValue({ code: 'ERROR' })
const onFailed = vi.fn()
render(<Install {...defaultProps} onFailed={onFailed} />)
fireEvent.click(screen.getByRole('button', { name: 'plugin.installModal.install' }))
await waitFor(() => {
expect(onFailed).toHaveBeenCalledWith()
})
})
})
// ================================
// Auto Install Behavior Tests
// ================================
describe('Auto Install Behavior', () => {
it('should call onInstalled when already installed with same uniqueIdentifier', async () => {
mockUseCheckInstalled.mockReturnValue({
installedInfo: {
'test-author/Test Plugin': {
installedVersion: '1.0.0',
installedId: 'installed-id',
uniqueIdentifier: 'test-unique-identifier',
},
},
isLoading: false,
})
const onInstalled = vi.fn()
render(<Install {...defaultProps} onInstalled={onInstalled} />)
await waitFor(() => {
expect(onInstalled).toHaveBeenCalled()
})
})
it('should not auto-call onInstalled when uniqueIdentifier differs', () => {
mockUseCheckInstalled.mockReturnValue({
installedInfo: {
'test-author/Test Plugin': {
installedVersion: '1.0.0',
installedId: 'installed-id',
uniqueIdentifier: 'different-uid',
},
},
isLoading: false,
})
const onInstalled = vi.fn()
render(<Install {...defaultProps} onInstalled={onInstalled} />)
// Should not be called immediately
expect(onInstalled).not.toHaveBeenCalled()
})
})
// ================================
// Dify Version Compatibility Tests
// ================================
describe('Dify Version Compatibility', () => {
it('should not show warning when dify version is compatible', () => {
mockLangGeniusVersionInfo.current_version = '1.0.0'
const payload = createMockManifest({ meta: { version: '1.0.0', minimum_dify_version: '0.8.0' } })
render(<Install {...defaultProps} payload={payload} />)
expect(screen.queryByText(/plugin.difyVersionNotCompatible/)).not.toBeInTheDocument()
})
it('should show warning when dify version is incompatible', () => {
mockLangGeniusVersionInfo.current_version = '1.0.0'
const payload = createMockManifest({ meta: { version: '1.0.0', minimum_dify_version: '2.0.0' } })
render(<Install {...defaultProps} payload={payload} />)
expect(screen.getByText(/plugin.difyVersionNotCompatible/)).toBeInTheDocument()
})
it('should be compatible when minimum_dify_version is undefined', () => {
mockLangGeniusVersionInfo.current_version = '1.0.0'
const payload = createMockManifest({ meta: { version: '1.0.0' } })
render(<Install {...defaultProps} payload={payload} />)
expect(screen.queryByText(/plugin.difyVersionNotCompatible/)).not.toBeInTheDocument()
})
it('should be compatible when current_version is empty', () => {
mockLangGeniusVersionInfo.current_version = ''
const payload = createMockManifest({ meta: { version: '1.0.0', minimum_dify_version: '2.0.0' } })
render(<Install {...defaultProps} payload={payload} />)
// When current_version is empty, should be compatible (no warning)
expect(screen.queryByText(/plugin.difyVersionNotCompatible/)).not.toBeInTheDocument()
})
it('should be compatible when current_version is undefined', () => {
mockLangGeniusVersionInfo.current_version = undefined as unknown as string
const payload = createMockManifest({ meta: { version: '1.0.0', minimum_dify_version: '2.0.0' } })
render(<Install {...defaultProps} payload={payload} />)
// When current_version is undefined, should be compatible (no warning)
expect(screen.queryByText(/plugin.difyVersionNotCompatible/)).not.toBeInTheDocument()
})
})
// ================================
// Installing State Tests
// ================================
describe('Installing State', () => {
it('should show installing text when installing', async () => {
mockInstallPackageFromLocal.mockImplementation(() => new Promise(() => {}))
render(<Install {...defaultProps} />)
fireEvent.click(screen.getByRole('button', { name: 'plugin.installModal.install' }))
await waitFor(() => {
expect(screen.getByText('plugin.installModal.installing')).toBeInTheDocument()
})
})
it('should disable install button when installing', async () => {
mockInstallPackageFromLocal.mockImplementation(() => new Promise(() => {}))
render(<Install {...defaultProps} />)
fireEvent.click(screen.getByRole('button', { name: 'plugin.installModal.install' }))
await waitFor(() => {
expect(screen.getByRole('button', { name: /plugin.installModal.installing/ })).toBeDisabled()
})
})
it('should show loading spinner when installing', async () => {
mockInstallPackageFromLocal.mockImplementation(() => new Promise(() => {}))
render(<Install {...defaultProps} />)
fireEvent.click(screen.getByRole('button', { name: 'plugin.installModal.install' }))
await waitFor(() => {
const spinner = document.querySelector('.animate-spin-slow')
expect(spinner).toBeInTheDocument()
})
})
it('should not trigger install twice when already installing', async () => {
mockInstallPackageFromLocal.mockImplementation(() => new Promise(() => {}))
render(<Install {...defaultProps} />)
const installButton = screen.getByRole('button', { name: 'plugin.installModal.install' })
// Click install
fireEvent.click(installButton)
await waitFor(() => {
expect(mockInstallPackageFromLocal).toHaveBeenCalledTimes(1)
})
// Try to click again (button should be disabled but let's verify the guard works)
fireEvent.click(screen.getByRole('button', { name: /plugin.installModal.installing/ }))
// Should still only be called once due to isInstalling guard
expect(mockInstallPackageFromLocal).toHaveBeenCalledTimes(1)
})
})
// ================================
// Callback Props Tests
// ================================
describe('Callback Props', () => {
it('should work without onStartToInstall callback', async () => {
mockInstallPackageFromLocal.mockResolvedValue({
all_installed: true,
task_id: 'task-123',
})
const onInstalled = vi.fn()
render(
<Install
{...defaultProps}
onStartToInstall={undefined}
onInstalled={onInstalled}
/>,
)
fireEvent.click(screen.getByRole('button', { name: 'plugin.installModal.install' }))
await waitFor(() => {
expect(onInstalled).toHaveBeenCalled()
})
})
})
})