import { ConnectorInfo } from '../api/api.types'; import { acceptedExtensions, connectionsFor, connectionsForFiles, kindForFile } from './connection-registry'; import { DocumentConnection } from './document-source'; import { EegConnection } from './eeg-source'; import { FileConnection } from './file-source'; import { PdfConnection } from './pdf-source'; const connector = (kind: ConnectorInfo['kind'], extensions: string[], category: ConnectorInfo['category'] = 'file'): ConnectorInfo => ({ kind, label: kind, description: '', category, icon: 'x', extensions, }); const CATALOG: ConnectorInfo[] = [ connector('csv', ['csv', 'txt']), connector('excel', ['xlsx']), connector('pdf', ['pdf'], 'document'), connector('document', ['pdf', 'png', 'tif'], 'document'), connector('edf', ['edf'], 'signal'), connector('sql', [], 'database'), ]; describe('connection registry', () => { it('picks the first connector that reads an extension, as the server does', () => { expect(kindForFile('a.CSV', CATALOG)).toBe('csv'); expect(kindForFile('reporte.pdf', CATALOG)).toBe('pdf'); expect(kindForFile('escaneo.tif', CATALOG)).toBe('document'); expect(kindForFile('notas.docx', CATALOG)).toBeNull(); expect(acceptedExtensions(CATALOG)).toEqual(['csv', 'txt', 'xlsx', 'pdf', 'png', 'tif', 'edf']); }); it('one connection per tabular file, one for all recordings or documents dropped together', () => { const files = ['a.csv', 'b.csv', 'r1.edf', 'r2.edf', 'p.png', 'q.tif', 'x.docx', 'informe.pdf'].map((name) => new File(['x'], name)); const { connections, rejected } = connectionsForFiles(files, CATALOG); expect(rejected.map((file) => file.name)).toEqual(['x.docx']); expect(connections.filter((c) => c instanceof FileConnection)).toHaveLength(2); const [eeg] = connections.filter((c) => c instanceof EegConnection) as EegConnection[]; expect(eeg.files).toHaveLength(2); expect(eeg.label).toBe('2 registros EEG'); const [documents] = connections.filter((c) => c instanceof DocumentConnection) as DocumentConnection[]; expect(documents.files.map((f) => f.name)).toEqual(['p.png', 'q.tif']); expect(connections.some((c) => c instanceof PdfConnection)).toBe(true); }); it('the connector picked in the gallery decides, e.g. PDFs read as documents', () => { const [connection] = connectionsFor('document', [new File(['x'], 'a.pdf'), new File(['y'], 'b.pdf')]); expect(connection).toBeInstanceOf(DocumentConnection); }); });