import { describe, expect, it, vi } from 'vitest' import { localFilesAdapter } from '../../composables/filePicker/localFiles' describe('localFilesAdapter', () => { it('preserves order, duplicate files, nested statistics and scoped search', async () => { const files = [ new File(['abc'], 'run.raw'), new File(['12'], 'run.raw'), new File(['1234'], 'sample.mzML.gz'), new File(['x'], 'other.raw'), ] const adapter = localFilesAdapter(files, [ 'study/batch/run.raw', 'study/batch/run.raw', 'study/sample.mzML.gz', 'study2/other.raw', ]) expect(await adapter.listRoot()).toMatchObject([ { path: 'study', fileCount: 3, bytes: 9, formats: { raw: 2, 'mzML.gz': 1 } }, { path: 'study2', fileCount: 1, bytes: 1, formats: { raw: 1 } }, ]) expect(await adapter.listChildren('study')).toMatchObject([ { path: 'study/batch', fileCount: 2, bytes: 5, formats: { raw: 2 } }, { path: 'study/sample.mzML.gz', file: files[2] }, ]) expect(await adapter.listChildren('study/batch')).toMatchObject([ { path: 'study/batch/run.raw', file: files[0] }, { path: 'study/batch/run.raw (2)', file: files[1] }, ]) expect(await adapter.search('RUN', { path: 'study', recursive: true })).toHaveLength(2) expect(await adapter.search('RUN', { path: 'study', recursive: false })).toEqual([]) expect(await adapter.listChildren('missing')).toEqual([]) const roots = await adapter.listRoot() roots.pop() expect(await adapter.listRoot()).toHaveLength(2) }) it('builds and lists many sibling folders without repeatedly scanning all nodes', async () => { const files = Array.from({ length: 200 }, (_, i) => new File(['x'], `${i}.raw`)) const paths = files.map((file, i) => `folder-${i}/${file.name}`) const originalValues = Map.prototype.values let visited = 0 const values = vi.spyOn(Map.prototype, 'values').mockImplementation(function* (this: Map) { for (const value of originalValues.call(this)) { visited++ yield value } } as typeof Map.prototype.values) try { const adapter = localFilesAdapter(files, paths) await adapter.listRoot() for (let i = 0; i < files.length; i++) { expect(await adapter.listChildren(`folder-${i}`)).toHaveLength(1) } expect(visited).toBeLessThanOrEqual(files.length * 8) } finally { values.mockRestore() } }) })