138 lines
5.1 KiB
TypeScript
138 lines
5.1 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { getTestFilesList, loadTestFile, filterSupportedFiles, type TestFileInfo } from '../src/services/testFilesApi'
|
|
|
|
// Mock fetch
|
|
global.fetch = vi.fn()
|
|
|
|
describe('testFilesApi', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
describe('getTestFilesList', () => {
|
|
it('devrait retourner une liste de fichiers de test', async () => {
|
|
// Mock des réponses fetch pour les fichiers connus
|
|
const mockResponses = [
|
|
{ ok: true, headers: new Map([['content-length', '1024'], ['content-type', 'image/jpeg']]) },
|
|
{ ok: true, headers: new Map([['content-length', '2048'], ['content-type', 'application/pdf']]) },
|
|
{ ok: false }, // Fichier non trouvé
|
|
{ ok: true, headers: new Map([['content-length', '512'], ['content-type', 'text/plain']]) },
|
|
{ ok: true, headers: new Map([['content-length', '256'], ['content-type', 'text/markdown']]) }
|
|
]
|
|
|
|
// Mock fetch pour chaque fichier
|
|
;(global.fetch as any).mockImplementation((url: string) => {
|
|
const fileName = url.split('/').pop()
|
|
const fileIndex = ['IMG_20250902_162159.jpg', 'IMG_20250902_162210.jpg', 'sample.md', 'sample.pdf', 'sample.txt'].indexOf(fileName!)
|
|
return Promise.resolve(mockResponses[fileIndex] || { ok: false })
|
|
})
|
|
|
|
const result = await getTestFilesList()
|
|
|
|
expect(result).toHaveLength(4) // 4 fichiers trouvés (1 non trouvé)
|
|
expect(result[0]).toMatchObject({
|
|
name: 'IMG_20250902_162159.jpg',
|
|
size: 1024,
|
|
type: 'image/jpeg'
|
|
})
|
|
})
|
|
|
|
it('devrait gérer les erreurs de fetch', async () => {
|
|
;(global.fetch as any).mockRejectedValue(new Error('Network error'))
|
|
|
|
const result = await getTestFilesList()
|
|
|
|
expect(result).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('loadTestFile', () => {
|
|
it('devrait charger un fichier de test avec succès', async () => {
|
|
const mockBlob = new Blob(['test content'], { type: 'text/plain' })
|
|
const mockResponse = {
|
|
ok: true,
|
|
blob: () => Promise.resolve(mockBlob)
|
|
}
|
|
|
|
;(global.fetch as any).mockResolvedValue(mockResponse)
|
|
|
|
const result = await loadTestFile('test.txt')
|
|
|
|
expect(result).toBeInstanceOf(File)
|
|
expect(result?.name).toBe('test.txt')
|
|
expect(result?.type).toBe('text/plain')
|
|
})
|
|
|
|
it('devrait retourner null si le fichier n\'existe pas', async () => {
|
|
const mockResponse = {
|
|
ok: false,
|
|
status: 404
|
|
}
|
|
|
|
;(global.fetch as any).mockResolvedValue(mockResponse)
|
|
|
|
const result = await loadTestFile('nonexistent.txt')
|
|
|
|
expect(result).toBeNull()
|
|
})
|
|
|
|
it('devrait gérer les erreurs de chargement', async () => {
|
|
;(global.fetch as any).mockRejectedValue(new Error('Network error'))
|
|
|
|
const result = await loadTestFile('test.txt')
|
|
|
|
expect(result).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('filterSupportedFiles', () => {
|
|
const testFiles: TestFileInfo[] = [
|
|
{ name: 'document.pdf', size: 1024, type: 'application/pdf', lastModified: Date.now() },
|
|
{ name: 'image.jpg', size: 2048, type: 'image/jpeg', lastModified: Date.now() },
|
|
{ name: 'text.txt', size: 512, type: 'text/plain', lastModified: Date.now() },
|
|
{ name: 'markdown.md', size: 256, type: 'text/markdown', lastModified: Date.now() },
|
|
{ name: 'document.docx', size: 4096, type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', lastModified: Date.now() },
|
|
{ name: 'unsupported.xyz', size: 128, type: 'application/unknown', lastModified: Date.now() },
|
|
{ name: 'image.png', size: 1536, type: 'image/png', lastModified: Date.now() }
|
|
]
|
|
|
|
it('devrait filtrer les fichiers supportés par type MIME', () => {
|
|
const result = filterSupportedFiles(testFiles)
|
|
|
|
expect(result).toHaveLength(6) // 6 fichiers supportés
|
|
expect(result.map(f => f.name)).toEqual([
|
|
'document.pdf',
|
|
'image.jpg',
|
|
'text.txt',
|
|
'markdown.md',
|
|
'document.docx',
|
|
'image.png'
|
|
])
|
|
})
|
|
|
|
it('devrait filtrer les fichiers supportés par extension', () => {
|
|
const filesWithUnknownMime: TestFileInfo[] = [
|
|
{ name: 'document.pdf', size: 1024, type: 'application/octet-stream', lastModified: Date.now() },
|
|
{ name: 'image.jpg', size: 2048, type: 'application/octet-stream', lastModified: Date.now() },
|
|
{ name: 'unsupported.xyz', size: 128, type: 'application/octet-stream', lastModified: Date.now() }
|
|
]
|
|
|
|
const result = filterSupportedFiles(filesWithUnknownMime)
|
|
|
|
expect(result).toHaveLength(2) // 2 fichiers supportés par extension
|
|
expect(result.map(f => f.name)).toEqual(['document.pdf', 'image.jpg'])
|
|
})
|
|
|
|
it('devrait retourner un tableau vide si aucun fichier supporté', () => {
|
|
const unsupportedFiles: TestFileInfo[] = [
|
|
{ name: 'file1.xyz', size: 128, type: 'application/unknown', lastModified: Date.now() },
|
|
{ name: 'file2.abc', size: 256, type: 'application/unknown', lastModified: Date.now() }
|
|
]
|
|
|
|
const result = filterSupportedFiles(unsupportedFiles)
|
|
|
|
expect(result).toEqual([])
|
|
})
|
|
})
|
|
})
|