forked from qodo-benchmark/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.spec.tsx
More file actions
799 lines (683 loc) · 23.3 KB
/
index.spec.tsx
File metadata and controls
799 lines (683 loc) · 23.3 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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
import type { DocumentIndexingStatus } from '@/models/datasets'
import type { InitialDocumentDetail } from '@/models/pipeline'
import { render, screen } from '@testing-library/react'
import * as React from 'react'
import { DatasourceType } from '@/models/pipeline'
import Processing from './index'
// ==========================================
// Mock External Dependencies
// ==========================================
// Mock useDocLink - returns a function that generates doc URLs
// Strips leading slash from path to match actual implementation behavior
vi.mock('@/context/i18n', () => ({
useDocLink: () => (path?: string) => {
const normalizedPath = path?.startsWith('/') ? path.slice(1) : (path || '')
return `https://docs.dify.ai/en-US/${normalizedPath}`
},
}))
// Mock dataset detail context
let mockDataset: {
id?: string
indexing_technique?: string
retrieval_model_dict?: { search_method?: string }
} | undefined
vi.mock('@/context/dataset-detail', () => ({
useDatasetDetailContextWithSelector: <T,>(selector: (state: { dataset?: typeof mockDataset }) => T): T => {
return selector({ dataset: mockDataset })
},
}))
// Mock the EmbeddingProcess component to track props
let embeddingProcessProps: Record<string, unknown> = {}
vi.mock('./embedding-process', () => ({
default: (props: Record<string, unknown>) => {
embeddingProcessProps = props
return (
<div data-testid="embedding-process">
<span data-testid="ep-dataset-id">{props.datasetId as string}</span>
<span data-testid="ep-batch-id">{props.batchId as string}</span>
<span data-testid="ep-documents-count">{(props.documents as unknown[])?.length ?? 0}</span>
<span data-testid="ep-indexing-type">{props.indexingType as string || 'undefined'}</span>
<span data-testid="ep-retrieval-method">{props.retrievalMethod as string || 'undefined'}</span>
</div>
)
},
}))
// ==========================================
// Test Data Factory Functions
// ==========================================
/**
* Creates a mock InitialDocumentDetail for testing
* Uses deterministic counter-based IDs to avoid flaky tests
*/
let documentIdCounter = 0
const createMockDocument = (overrides: Partial<InitialDocumentDetail> = {}): InitialDocumentDetail => ({
id: overrides.id ?? `doc-${++documentIdCounter}`,
name: 'test-document.txt',
data_source_type: DatasourceType.localFile,
data_source_info: {},
enable: true,
error: '',
indexing_status: 'waiting' as DocumentIndexingStatus,
position: 0,
...overrides,
})
/**
* Creates a list of mock documents
*/
const createMockDocuments = (count: number): InitialDocumentDetail[] =>
Array.from({ length: count }, (_, index) =>
createMockDocument({
id: `doc-${index + 1}`,
name: `document-${index + 1}.txt`,
position: index,
}))
// ==========================================
// Test Suite
// ==========================================
describe('Processing', () => {
beforeEach(() => {
vi.clearAllMocks()
embeddingProcessProps = {}
// Reset deterministic ID counter for reproducible tests
documentIdCounter = 0
// Reset mock dataset with default values
mockDataset = {
id: 'dataset-123',
indexing_technique: 'high_quality',
retrieval_model_dict: { search_method: 'semantic_search' },
}
})
// ==========================================
// Rendering Tests
// ==========================================
describe('Rendering', () => {
// Tests basic rendering functionality
it('should render without crashing', () => {
// Arrange
const props = {
batchId: 'batch-123',
documents: createMockDocuments(2),
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('embedding-process')).toBeInTheDocument()
})
it('should render the EmbeddingProcess component', () => {
// Arrange
const props = {
batchId: 'batch-456',
documents: createMockDocuments(3),
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('embedding-process')).toBeInTheDocument()
})
it('should render the side tip section with correct content', () => {
// Arrange
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert - verify translation keys are rendered
expect(screen.getByText('datasetCreation.stepThree.sideTipTitle')).toBeInTheDocument()
expect(screen.getByText('datasetCreation.stepThree.sideTipContent')).toBeInTheDocument()
expect(screen.getByText('datasetPipeline.addDocuments.stepThree.learnMore')).toBeInTheDocument()
})
it('should render the documentation link with correct attributes', () => {
// Arrange
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
const link = screen.getByRole('link', { name: 'datasetPipeline.addDocuments.stepThree.learnMore' })
expect(link).toHaveAttribute('href', 'https://docs.dify.ai/en-US/guides/knowledge-base/integrate-knowledge-within-application')
expect(link).toHaveAttribute('target', '_blank')
expect(link).toHaveAttribute('rel', 'noreferrer noopener')
})
it('should render the book icon in the side tip', () => {
// Arrange
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
const { container } = render(<Processing {...props} />)
// Assert - check for icon container with shadow styling
const iconContainer = container.querySelector('.shadow-lg.shadow-shadow-shadow-5')
expect(iconContainer).toBeInTheDocument()
})
})
// ==========================================
// Props Testing
// ==========================================
describe('Props', () => {
// Tests that props are correctly passed to child components
it('should pass batchId to EmbeddingProcess', () => {
// Arrange
const testBatchId = 'test-batch-id-789'
const props = {
batchId: testBatchId,
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('ep-batch-id')).toHaveTextContent(testBatchId)
expect(embeddingProcessProps.batchId).toBe(testBatchId)
})
it('should pass documents to EmbeddingProcess', () => {
// Arrange
const documents = createMockDocuments(5)
const props = {
batchId: 'batch-123',
documents,
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('ep-documents-count')).toHaveTextContent('5')
expect(embeddingProcessProps.documents).toEqual(documents)
})
it('should pass datasetId from context to EmbeddingProcess', () => {
// Arrange
mockDataset = {
id: 'context-dataset-id',
indexing_technique: 'high_quality',
retrieval_model_dict: { search_method: 'semantic_search' },
}
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('ep-dataset-id')).toHaveTextContent('context-dataset-id')
expect(embeddingProcessProps.datasetId).toBe('context-dataset-id')
})
it('should pass indexingType from context to EmbeddingProcess', () => {
// Arrange
mockDataset = {
id: 'dataset-123',
indexing_technique: 'economy',
retrieval_model_dict: { search_method: 'semantic_search' },
}
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('ep-indexing-type')).toHaveTextContent('economy')
expect(embeddingProcessProps.indexingType).toBe('economy')
})
it('should pass retrievalMethod from context to EmbeddingProcess', () => {
// Arrange
mockDataset = {
id: 'dataset-123',
indexing_technique: 'high_quality',
retrieval_model_dict: { search_method: 'keyword_search' },
}
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('ep-retrieval-method')).toHaveTextContent('keyword_search')
expect(embeddingProcessProps.retrievalMethod).toBe('keyword_search')
})
it('should handle different document types', () => {
// Arrange
const documents = [
createMockDocument({
id: 'doc-local',
name: 'local-file.pdf',
data_source_type: DatasourceType.localFile,
}),
createMockDocument({
id: 'doc-online',
name: 'online-doc',
data_source_type: DatasourceType.onlineDocument,
}),
createMockDocument({
id: 'doc-website',
name: 'website-page',
data_source_type: DatasourceType.websiteCrawl,
}),
]
const props = {
batchId: 'batch-123',
documents,
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('ep-documents-count')).toHaveTextContent('3')
expect(embeddingProcessProps.documents).toEqual(documents)
})
})
// ==========================================
// Edge Cases
// ==========================================
describe('Edge Cases', () => {
// Tests for boundary conditions and unusual inputs
it('should handle empty documents array', () => {
// Arrange
const props = {
batchId: 'batch-123',
documents: [],
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('embedding-process')).toBeInTheDocument()
expect(screen.getByTestId('ep-documents-count')).toHaveTextContent('0')
expect(embeddingProcessProps.documents).toEqual([])
})
it('should handle empty batchId', () => {
// Arrange
const props = {
batchId: '',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('embedding-process')).toBeInTheDocument()
expect(screen.getByTestId('ep-batch-id')).toHaveTextContent('')
})
it('should handle undefined dataset from context', () => {
// Arrange
mockDataset = undefined
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('embedding-process')).toBeInTheDocument()
expect(embeddingProcessProps.datasetId).toBeUndefined()
expect(embeddingProcessProps.indexingType).toBeUndefined()
expect(embeddingProcessProps.retrievalMethod).toBeUndefined()
})
it('should handle dataset with undefined id', () => {
// Arrange
mockDataset = {
id: undefined,
indexing_technique: 'high_quality',
retrieval_model_dict: { search_method: 'semantic_search' },
}
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('embedding-process')).toBeInTheDocument()
expect(embeddingProcessProps.datasetId).toBeUndefined()
})
it('should handle dataset with undefined indexing_technique', () => {
// Arrange
mockDataset = {
id: 'dataset-123',
indexing_technique: undefined,
retrieval_model_dict: { search_method: 'semantic_search' },
}
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('embedding-process')).toBeInTheDocument()
expect(embeddingProcessProps.indexingType).toBeUndefined()
})
it('should handle dataset with undefined retrieval_model_dict', () => {
// Arrange
mockDataset = {
id: 'dataset-123',
indexing_technique: 'high_quality',
retrieval_model_dict: undefined,
}
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('embedding-process')).toBeInTheDocument()
expect(embeddingProcessProps.retrievalMethod).toBeUndefined()
})
it('should handle dataset with empty retrieval_model_dict', () => {
// Arrange
mockDataset = {
id: 'dataset-123',
indexing_technique: 'high_quality',
retrieval_model_dict: {},
}
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('embedding-process')).toBeInTheDocument()
expect(embeddingProcessProps.retrievalMethod).toBeUndefined()
})
it('should handle large number of documents', () => {
// Arrange
const props = {
batchId: 'batch-123',
documents: createMockDocuments(100),
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('embedding-process')).toBeInTheDocument()
expect(screen.getByTestId('ep-documents-count')).toHaveTextContent('100')
})
it('should handle documents with error status', () => {
// Arrange
const documents = [
createMockDocument({
id: 'doc-error',
name: 'error-doc.txt',
error: 'Processing failed',
indexing_status: 'error' as DocumentIndexingStatus,
}),
]
const props = {
batchId: 'batch-123',
documents,
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('embedding-process')).toBeInTheDocument()
expect(embeddingProcessProps.documents).toEqual(documents)
})
it('should handle documents with special characters in names', () => {
// Arrange
const documents = [
createMockDocument({
id: 'doc-special',
name: 'document with spaces & special-chars_测试.pdf',
}),
]
const props = {
batchId: 'batch-123',
documents,
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('embedding-process')).toBeInTheDocument()
expect(embeddingProcessProps.documents).toEqual(documents)
})
it('should handle batchId with special characters', () => {
// Arrange
const props = {
batchId: 'batch-123-abc_xyz:456',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('ep-batch-id')).toHaveTextContent('batch-123-abc_xyz:456')
})
})
// ==========================================
// Context Integration Tests
// ==========================================
describe('Context Integration', () => {
// Tests for proper context usage
it('should correctly use context selectors for all dataset properties', () => {
// Arrange
mockDataset = {
id: 'full-dataset-id',
indexing_technique: 'high_quality',
retrieval_model_dict: { search_method: 'hybrid_search' },
}
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
expect(embeddingProcessProps.datasetId).toBe('full-dataset-id')
expect(embeddingProcessProps.indexingType).toBe('high_quality')
expect(embeddingProcessProps.retrievalMethod).toBe('hybrid_search')
})
it('should handle context changes with different indexing techniques', () => {
// Arrange - Test with economy indexing
mockDataset = {
id: 'dataset-economy',
indexing_technique: 'economy',
retrieval_model_dict: { search_method: 'keyword_search' },
}
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
const { rerender } = render(<Processing {...props} />)
// Assert economy indexing
expect(embeddingProcessProps.indexingType).toBe('economy')
// Arrange - Update to high_quality
mockDataset = {
id: 'dataset-hq',
indexing_technique: 'high_quality',
retrieval_model_dict: { search_method: 'semantic_search' },
}
// Act - Rerender with new context
rerender(<Processing {...props} />)
// Assert high_quality indexing
expect(embeddingProcessProps.indexingType).toBe('high_quality')
})
})
// ==========================================
// Layout Tests
// ==========================================
describe('Layout', () => {
// Tests for proper layout and structure
it('should render with correct layout structure', () => {
// Arrange
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
const { container } = render(<Processing {...props} />)
// Assert - Check for flex layout with proper widths
const mainContainer = container.querySelector('.flex.h-full.w-full.justify-center')
expect(mainContainer).toBeInTheDocument()
// Check for left panel (3/5 width)
const leftPanel = container.querySelector('.w-3\\/5')
expect(leftPanel).toBeInTheDocument()
// Check for right panel (2/5 width)
const rightPanel = container.querySelector('.w-2\\/5')
expect(rightPanel).toBeInTheDocument()
})
it('should render side tip card with correct styling', () => {
// Arrange
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
const { container } = render(<Processing {...props} />)
// Assert - Check for card container with rounded corners and background
const sideTipCard = container.querySelector('.rounded-xl.bg-background-section')
expect(sideTipCard).toBeInTheDocument()
})
it('should constrain max-width for EmbeddingProcess container', () => {
// Arrange
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
const { container } = render(<Processing {...props} />)
// Assert
const maxWidthContainer = container.querySelector('.max-w-\\[640px\\]')
expect(maxWidthContainer).toBeInTheDocument()
})
})
// ==========================================
// Document Variations Tests
// ==========================================
describe('Document Variations', () => {
// Tests for different document configurations
it('should handle documents with all indexing statuses', () => {
// Arrange
const statuses: DocumentIndexingStatus[] = [
'waiting',
'parsing',
'cleaning',
'splitting',
'indexing',
'paused',
'error',
'completed',
]
const documents = statuses.map((status, index) =>
createMockDocument({
id: `doc-${status}`,
name: `${status}-doc.txt`,
indexing_status: status,
position: index,
}),
)
const props = {
batchId: 'batch-123',
documents,
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('ep-documents-count')).toHaveTextContent(String(statuses.length))
expect(embeddingProcessProps.documents).toEqual(documents)
})
it('should handle documents with enabled and disabled states', () => {
// Arrange
const documents = [
createMockDocument({ id: 'doc-enabled', enable: true }),
createMockDocument({ id: 'doc-disabled', enable: false }),
]
const props = {
batchId: 'batch-123',
documents,
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('ep-documents-count')).toHaveTextContent('2')
expect(embeddingProcessProps.documents).toEqual(documents)
})
it('should handle documents from online drive source', () => {
// Arrange
const documents = [
createMockDocument({
id: 'doc-drive',
name: 'google-drive-doc',
data_source_type: DatasourceType.onlineDrive,
data_source_info: { provider: 'google_drive' },
}),
]
const props = {
batchId: 'batch-123',
documents,
}
// Act
render(<Processing {...props} />)
// Assert
expect(screen.getByTestId('embedding-process')).toBeInTheDocument()
expect(embeddingProcessProps.documents).toEqual(documents)
})
it('should handle documents with complex data_source_info', () => {
// Arrange
const documents = [
createMockDocument({
id: 'doc-notion',
name: 'Notion Page',
data_source_type: DatasourceType.onlineDocument,
data_source_info: {
notion_page_icon: { type: 'emoji', emoji: '📄' },
notion_workspace_id: 'ws-123',
notion_page_id: 'page-456',
},
}),
]
const props = {
batchId: 'batch-123',
documents,
}
// Act
render(<Processing {...props} />)
// Assert
expect(embeddingProcessProps.documents).toEqual(documents)
})
})
// ==========================================
// Retrieval Method Variations
// ==========================================
describe('Retrieval Method Variations', () => {
// Tests for different retrieval methods
const retrievalMethods = ['semantic_search', 'keyword_search', 'hybrid_search', 'full_text_search']
it.each(retrievalMethods)('should handle %s retrieval method', (method) => {
// Arrange
mockDataset = {
id: 'dataset-123',
indexing_technique: 'high_quality',
retrieval_model_dict: { search_method: method },
}
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
expect(embeddingProcessProps.retrievalMethod).toBe(method)
})
})
// ==========================================
// Indexing Technique Variations
// ==========================================
describe('Indexing Technique Variations', () => {
// Tests for different indexing techniques
const indexingTechniques = ['high_quality', 'economy']
it.each(indexingTechniques)('should handle %s indexing technique', (technique) => {
// Arrange
mockDataset = {
id: 'dataset-123',
indexing_technique: technique,
retrieval_model_dict: { search_method: 'semantic_search' },
}
const props = {
batchId: 'batch-123',
documents: createMockDocuments(1),
}
// Act
render(<Processing {...props} />)
// Assert
expect(embeddingProcessProps.indexingType).toBe(technique)
})
})
})