import { describe, expect, it } from 'vitest' import type { ProtocolStep } from '../../types' import type { StepTemplate } from '../../composables/useProtocolTemplates' import { buildCustomProtocolTemplate, buildProtocolPreviewParams, buildProtocolStepEditorState, buildProtocolStepPreview, buildProtocolTemplateDefaults, } from '../../components/ProtocolStepEditor.state' const template: StepTemplate = { id: 'builtin-incubation', type: 'incubation', name: 'Incubation', description: 'Hold samples under controlled conditions', defaultDuration: 60, parameters: [ { key: 'temperature', label: 'Temperature', type: 'temperature', unit: 'C', default: 37, required: true, }, { key: 'notes', label: 'Notes', type: 'text', }, ], isBuiltIn: true, } describe('ProtocolStepEditor state helpers', () => { it('builds editor defaults from template parameter defaults', () => { expect(buildProtocolTemplateDefaults(template)).toEqual({ name: 'Incubation', description: 'Hold samples under controlled conditions', duration: 60, parameters: { temperature: 37, }, }) }) it('initializes editor fields from an existing step', () => { const step: ProtocolStep = { id: 'step-1', type: 'incubation', name: 'Overnight incubation', description: 'Long run', duration: 720, status: 'in_progress', parameters: { temperature: 4, }, order: 3, } const state = buildProtocolStepEditorState(step, (type) => ( type === 'incubation' ? template : undefined )) expect(state).toEqual({ selectedTemplateId: 'builtin-incubation', fields: { name: 'Overnight incubation', description: 'Long run', duration: 720, parameters: { temperature: 4, }, }, }) }) it('preserves source step metadata when building a preview', () => { const preview = buildProtocolStepPreview( template, { name: '', description: 'Preview description', parameters: { temperature: 25, }, }, { id: 'step-2', type: 'incubation', name: 'Source', status: 'completed', order: 5, } ) expect(preview).toEqual({ id: 'step-2', type: 'incubation', name: 'Incubation', description: 'Preview description', duration: 60, status: 'completed', parameters: { temperature: 25, }, order: 5, }) }) it('formats only populated preview parameters', () => { const formatted = buildProtocolPreviewParams( template, { temperature: 37, notes: '', }, (value, parameter) => `${parameter.label}: ${value}` ) expect(formatted).toBe('Temperature: 37') }) it('creates a custom template from the current editor fields', () => { expect( buildCustomProtocolTemplate( template, { name: 'Cold incubation', description: 'Cold room workflow', duration: 120, parameters: { temperature: 4, notes: 'keep covered', }, }, 'custom-fixed' ) ).toEqual({ id: 'custom-fixed', type: 'incubation', name: 'Cold incubation', description: 'Cold room workflow', defaultDuration: 120, parameters: [ { key: 'temperature', label: 'Temperature', type: 'temperature', unit: 'C', default: 4, required: true, }, { key: 'notes', label: 'Notes', type: 'text', default: 'keep covered', }, ], isBuiltIn: false, }) }) })