import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { createPinia, setActivePinia } from 'pinia' import axios, { type AxiosRequestConfig } from 'axios' import { useExperimentSamples } from '../../composables/useExperimentSamples' let fakeResponse: unknown = null let fakeError: Error | null = null let recordedUrls: string[] = [] describe('useExperimentSamples', () => { beforeEach(() => { setActivePinia(createPinia()) fakeResponse = {} fakeError = null recordedUrls = [] vi.spyOn(axios.Axios.prototype, 'get').mockImplementation(async function ( this: unknown, url: string, _config?: AxiosRequestConfig, ) { recordedUrls.push(url) if (fakeError) throw fakeError return { data: fakeResponse } }) }) afterEach(() => { vi.restoreAllMocks() }) it('derives samples from pre-fetched MS Designer design_data', () => { const hook = useExperimentSamples({ syncAppExperiment: false, designData: { samples: [ { sample_name: 'S1', sample_type: 'sample', conditions: { group: 'A' } }, { sample_name: 'QC1', sample_type: 'qc', conditions: { group: 'QC' } }, ], }, }) expect(hook.samples.value).toEqual(['S1']) expect(hook.sampleOptions.value).toEqual([{ value: 'S1', label: 'S1' }]) expect(recordedUrls).toEqual([]) }) it('loads and unwraps platform design_data for an experiment id', async () => { fakeResponse = { experiment_id: 7, plugin_id: 'ms-designer', data: { samples: [ { sample_name: 'S1', sample_type: 'sample' }, { sample_name: 'S2', sample_type: 'sample' }, ], }, schema_version: '1.0', } const hook = useExperimentSamples({ syncAppExperiment: false, immediate: false }) await hook.fetch(7) expect(recordedUrls).toEqual(['/experiments/7/data']) expect(hook.samples.value).toEqual(['S1', 'S2']) }) it('extracts options from template collection sample sheets', () => { const hook = useExperimentSamples({ syncAppExperiment: false, designData: { templates: { 'sample-sheet': { data: { samples: [ { sampleId: 'ctrl', name: 'Control', group: 'baseline' }, { sampleId: 'drug', name: 'Drug', group: 'treated' }, ], }, }, }, }, }) expect(hook.samples.value).toEqual(['ctrl', 'drug']) expect(hook.sampleOptions.value).toEqual([ { value: 'ctrl', label: 'Control', description: 'baseline' }, { value: 'drug', label: 'Drug', description: 'treated' }, ]) }) })