import { describe, expect, it } from 'vitest' import { createTextDroppedSample, normalizeDroppedSample, parseDroppedSampleJson, parseDroppedSampleTransfer, } from '../../components/WellPlate.sampleDrop' function createTransfer(data: Record) { return { getData(format: string) { return data[format] ?? '' }, } } describe('WellPlate sample drop helpers', () => { it('normalizes JSON sample payloads', () => { const raw = { id: 'sample-1', sample_name: 'Sample 1', sample_type: 'qc', injection_volume: 5, injection_count: 2, custom_method: 'fast', metadata: { batch: 'A' }, } expect(normalizeDroppedSample(raw)).toEqual({ id: 'sample-1', sampleName: 'Sample 1', label: 'Sample 1', sampleType: 'qc', injectionVolume: 5, injectionCount: 2, customMethod: 'fast', metadata: { batch: 'A' }, raw, }) }) it('parses JSON payloads before text fallback', () => { const transfer = createTransfer({ 'application/json': JSON.stringify({ name: 'From JSON' }), 'text/plain': 'From Text', }) expect(parseDroppedSampleTransfer(transfer)).toMatchObject({ sampleName: 'From JSON', label: 'From JSON', }) }) it('creates a text sample when no JSON payload exists', () => { expect(createTextDroppedSample(' Sample A ')).toEqual({ sampleName: 'Sample A', label: 'Sample A', raw: 'Sample A', }) expect(parseDroppedSampleTransfer(createTransfer({ 'text/plain': 'Sample B' }))).toMatchObject({ sampleName: 'Sample B', label: 'Sample B', }) }) it('rejects invalid or empty payloads', () => { expect(normalizeDroppedSample({ metadata: { batch: 'A' } })).toBeNull() expect(createTextDroppedSample(' ')).toBeNull() expect(parseDroppedSampleTransfer(createTransfer({ 'application/json': '{' }))).toBeNull() }) }) describe('parseDroppedSampleJson', () => { it('should return null for a plain-text payload', () => { expect(parseDroppedSampleJson(createTransfer({ 'text/plain': 'Sample 1' }))).toBeNull() }) it('should parse a JSON sample payload', () => { expect(parseDroppedSampleJson(createTransfer({ 'application/json': '{"id":"s1"}' }))?.id).toBe('s1') }) it('should ignore an injection count below one', () => { expect(normalizeDroppedSample({ name: 'x', injectionCount: -3 })?.injectionCount).toBeUndefined() }) it('should ignore a zero injection volume', () => { expect(normalizeDroppedSample({ name: 'x', injectionVolume: 0 })?.injectionVolume).toBeUndefined() }) })