From 843c5bd50786a062f257ec75ba38b436dc8adc3c Mon Sep 17 00:00:00 2001 From: ncdiehl11 Date: Thu, 16 Jan 2025 14:11:58 -0500 Subject: [PATCH] add tests --- .../absorbanceReaderCloseLid.test.ts | 83 +++++++++++++++++++ .../__tests__/absorbanceReaderOpenLid.test.ts | 83 +++++++++++++++++++ .../atomic/absorbanceReaderCloseLid.ts | 7 +- .../atomic/absorbanceReaderOpenLid.ts | 12 +-- 4 files changed, 178 insertions(+), 7 deletions(-) create mode 100644 step-generation/src/__tests__/absorbanceReaderCloseLid.test.ts create mode 100644 step-generation/src/__tests__/absorbanceReaderOpenLid.test.ts diff --git a/step-generation/src/__tests__/absorbanceReaderCloseLid.test.ts b/step-generation/src/__tests__/absorbanceReaderCloseLid.test.ts new file mode 100644 index 00000000000..2ba6a433cb8 --- /dev/null +++ b/step-generation/src/__tests__/absorbanceReaderCloseLid.test.ts @@ -0,0 +1,83 @@ +import { beforeEach, describe, it, expect, vi } from 'vitest' +import { + ABSORBANCE_READER_TYPE, + ABSORBANCE_READER_V1, +} from '@opentrons/shared-data' +import { + getErrorResult, + makeContext, + getInitialRobotStateStandard, +} from '../fixtures' +import { absorbanceReaderCloseLid } from '../commandCreators/atomic/absorbanceReaderCloseLid' +import { absorbanceReaderStateGetter } from '../robotStateSelectors' +import type { + AbsorbanceReaderState, + InvariantContext, + RobotState, +} from '../types' + +const moduleId = 'absorbanceReaderId' +vi.mock('../robotStateSelectors') + +describe('absorbanceReaderCloseLid', () => { + let invariantContext: InvariantContext + let robotState: RobotState + beforeEach(() => { + invariantContext = makeContext() + invariantContext.moduleEntities[moduleId] = { + id: moduleId, + type: ABSORBANCE_READER_TYPE, + model: ABSORBANCE_READER_V1, + } + robotState = getInitialRobotStateStandard(invariantContext) + robotState.modules[moduleId] = { + slot: 'D3', + moduleState: { + type: ABSORBANCE_READER_TYPE, + initialization: null, + lidOpen: false, + }, + } + vi.mocked(absorbanceReaderStateGetter).mockReturnValue( + {} as AbsorbanceReaderState + ) + }) + it.only('creates absorbance reader close lid command', () => { + const module = moduleId + const result = absorbanceReaderCloseLid( + { + module, + commandCreatorFnName: 'absorbanceReaderCloseLid', + }, + invariantContext, + robotState + ) + expect(result).toEqual({ + commands: [ + { + commandType: 'absorbanceReader/closeLid', + key: expect.any(String), + params: { + moduleId: module, + }, + }, + ], + }) + }) + it('creates returns error if bad module state', () => { + const module = moduleId + vi.mocked(absorbanceReaderStateGetter).mockReturnValue(null) + const result = absorbanceReaderCloseLid( + { + module, + commandCreatorFnName: 'absorbanceReaderCloseLid', + }, + invariantContext, + robotState + ) + expect(getErrorResult(result).errors).toHaveLength(1) + expect(getErrorResult(result).errors[0]).toMatchObject({ + type: 'MISSING_MODULE', + }) + }) +}) diff --git a/step-generation/src/__tests__/absorbanceReaderOpenLid.test.ts b/step-generation/src/__tests__/absorbanceReaderOpenLid.test.ts new file mode 100644 index 00000000000..5f591211b8c --- /dev/null +++ b/step-generation/src/__tests__/absorbanceReaderOpenLid.test.ts @@ -0,0 +1,83 @@ +import { beforeEach, describe, it, expect, vi } from 'vitest' +import { + ABSORBANCE_READER_TYPE, + ABSORBANCE_READER_V1, +} from '@opentrons/shared-data' +import { + getErrorResult, + makeContext, + getInitialRobotStateStandard, +} from '../fixtures' +import { absorbanceReaderOpenLid } from '../commandCreators/atomic/absorbanceReaderOpenLid' +import { absorbanceReaderStateGetter } from '../robotStateSelectors' +import type { + AbsorbanceReaderState, + InvariantContext, + RobotState, +} from '../types' + +const moduleId = 'absorbanceReaderId' +vi.mock('../robotStateSelectors') + +describe('absorbanceReaderOpenLid', () => { + let invariantContext: InvariantContext + let robotState: RobotState + beforeEach(() => { + invariantContext = makeContext() + invariantContext.moduleEntities[moduleId] = { + id: moduleId, + type: ABSORBANCE_READER_TYPE, + model: ABSORBANCE_READER_V1, + } + robotState = getInitialRobotStateStandard(invariantContext) + robotState.modules[moduleId] = { + slot: 'D3', + moduleState: { + type: ABSORBANCE_READER_TYPE, + initialization: null, + lidOpen: false, + }, + } + vi.mocked(absorbanceReaderStateGetter).mockReturnValue( + {} as AbsorbanceReaderState + ) + }) + it('creates absorbance reader open lid command', () => { + const module = moduleId + const result = absorbanceReaderOpenLid( + { + module, + commandCreatorFnName: 'absorbanceReaderOpenLid', + }, + invariantContext, + robotState + ) + expect(result).toEqual({ + commands: [ + { + commandType: 'absorbanceReader/openLid', + key: expect.any(String), + params: { + moduleId: module, + }, + }, + ], + }) + }) + it('creates returns error if bad module state', () => { + const module = moduleId + vi.mocked(absorbanceReaderStateGetter).mockReturnValue(null) + const result = absorbanceReaderOpenLid( + { + module, + commandCreatorFnName: 'absorbanceReaderOpenLid', + }, + invariantContext, + robotState + ) + expect(getErrorResult(result).errors).toHaveLength(1) + expect(getErrorResult(result).errors[0]).toMatchObject({ + type: 'MISSING_MODULE', + }) + }) +}) diff --git a/step-generation/src/commandCreators/atomic/absorbanceReaderCloseLid.ts b/step-generation/src/commandCreators/atomic/absorbanceReaderCloseLid.ts index 03634a139ba..20d66a7b2de 100644 --- a/step-generation/src/commandCreators/atomic/absorbanceReaderCloseLid.ts +++ b/step-generation/src/commandCreators/atomic/absorbanceReaderCloseLid.ts @@ -1,6 +1,6 @@ import { uuid } from '../../utils' import { missingModuleError } from '../../errorCreators' -import { getModuleState } from '../../robotStateSelectors' +import { absorbanceReaderStateGetter } from '../../robotStateSelectors' import type { AbsorbanceReaderLidArgs, CommandCreator } from '../../types' export const absorbanceReaderCloseLid: CommandCreator = ( @@ -8,7 +8,10 @@ export const absorbanceReaderCloseLid: CommandCreator = invariantContext, prevRobotState ) => { - const absorbanceReaderState = getModuleState(prevRobotState, args.module) + const absorbanceReaderState = absorbanceReaderStateGetter( + prevRobotState, + args.module + ) if (args.module == null || absorbanceReaderState == null) { return { errors: [missingModuleError()], diff --git a/step-generation/src/commandCreators/atomic/absorbanceReaderOpenLid.ts b/step-generation/src/commandCreators/atomic/absorbanceReaderOpenLid.ts index f43bc1c2a23..35c8d125328 100644 --- a/step-generation/src/commandCreators/atomic/absorbanceReaderOpenLid.ts +++ b/step-generation/src/commandCreators/atomic/absorbanceReaderOpenLid.ts @@ -1,16 +1,18 @@ import { uuid } from '../../utils' import { missingModuleError } from '../../errorCreators' -import { getModuleState } from '../../robotStateSelectors' +import { absorbanceReaderStateGetter } from '../../robotStateSelectors' -import type { ModuleOnlyParams } from '@opentrons/shared-data/protocol/types/schemaV4' -import type { CommandCreator } from '../../types' +import type { AbsorbanceReaderLidArgs, CommandCreator } from '../../types' -export const absorbanceReaderOpenLid: CommandCreator = ( +export const absorbanceReaderOpenLid: CommandCreator = ( args, invariantContext, prevRobotState ) => { - const absorbanceReaderState = getModuleState(prevRobotState, args.module) + const absorbanceReaderState = absorbanceReaderStateGetter( + prevRobotState, + args.module + ) if (args.module == null || absorbanceReaderState == null) { return { errors: [missingModuleError()],