Skip to content

Commit

Permalink
fix(app, shared-data): app crashes when using python apiLevel<2.16 wi…
Browse files Browse the repository at this point in the history
…th fixed trash (#16451)
  • Loading branch information
TamarZanzouri authored Oct 10, 2024
1 parent fc53c0f commit 7fb38c9
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ describe('getRunCurrentModulesInfo', () => {
},
} as any
const mockDeckDef = {} as any
const mockProtocolAnalysis = {} as any

beforeEach(() => {
vi.mocked(getLoadedLabwareDefinitionsByUri).mockReturnValue({
Expand All @@ -185,7 +184,7 @@ describe('getRunCurrentModulesInfo', () => {
const result = getRunCurrentModulesInfo({
runRecord: null as any,
deckDef: mockDeckDef,
protocolAnalysis: mockProtocolAnalysis,
labwareDefinitionsByUri: {},
})

expect(result).toEqual([])
Expand All @@ -195,7 +194,7 @@ describe('getRunCurrentModulesInfo', () => {
const result = getRunCurrentModulesInfo({
runRecord: mockRunRecord,
deckDef: mockDeckDef,
protocolAnalysis: null,
labwareDefinitionsByUri: null,
})

expect(result).toEqual([])
Expand All @@ -205,7 +204,9 @@ describe('getRunCurrentModulesInfo', () => {
const result = getRunCurrentModulesInfo({
runRecord: mockRunRecord,
deckDef: mockDeckDef,
protocolAnalysis: mockProtocolAnalysis,
labwareDefinitionsByUri: {
'opentrons/opentrons_96_pcr_adapter/1': 'MOCK_LW_DEF',
} as any,
})

expect(result).toEqual([
Expand All @@ -226,7 +227,7 @@ describe('getRunCurrentModulesInfo', () => {
data: { modules: [mockModule], labware: [] },
},
deckDef: mockDeckDef,
protocolAnalysis: mockProtocolAnalysis,
labwareDefinitionsByUri: {},
})
expect(result).toEqual([
{
Expand All @@ -245,7 +246,7 @@ describe('getRunCurrentModulesInfo', () => {
const result = getRunCurrentModulesInfo({
runRecord: mockRunRecord,
deckDef: mockDeckDef,
protocolAnalysis: mockProtocolAnalysis,
labwareDefinitionsByUri: null,
})
expect(result).toEqual([])
})
Expand All @@ -270,7 +271,7 @@ describe('getRunCurrentLabwareInfo', () => {
it('should return an empty array if runRecord is null', () => {
const result = getRunCurrentLabwareInfo({
runRecord: undefined,
protocolAnalysis: {} as any,
labwareDefinitionsByUri: {} as any,
})

expect(result).toEqual([])
Expand All @@ -279,7 +280,7 @@ describe('getRunCurrentLabwareInfo', () => {
it('should return an empty array if protocolAnalysis is null', () => {
const result = getRunCurrentLabwareInfo({
runRecord: { data: { labware: [] } } as any,
protocolAnalysis: null,
labwareDefinitionsByUri: null,
})

expect(result).toEqual([])
Expand All @@ -293,7 +294,9 @@ describe('getRunCurrentLabwareInfo', () => {

const result = getRunCurrentLabwareInfo({
runRecord: { data: { labware: [mockPickUpTipLwSlotName] } } as any,
protocolAnalysis: { commands: [] } as any,
labwareDefinitionsByUri: {
[mockPickUpTipLabware.definitionUri]: mockLabwareDef,
},
})

expect(result).toEqual([
Expand Down
50 changes: 32 additions & 18 deletions app/src/organisms/ErrorRecoveryFlows/hooks/useDeckMapUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMemo } from 'react'
import {
getDeckDefFromRobotType,
getLoadedLabwareDefinitionsByUri,
getFixedTrashLabwareDefinition,
getModuleDef2,
getPositionFromSlotId,
getSimplestDeckConfigForProtocol,
Expand All @@ -20,6 +21,7 @@ import type {
CutoutConfigProtocolSpec,
LoadedLabware,
RobotType,
LabwareDefinitionsByUri,
} from '@opentrons/shared-data'
import type { ErrorRecoveryFlowsProps } from '..'
import type { UseFailedLabwareUtilsResult } from './useFailedLabwareUtils'
Expand Down Expand Up @@ -50,14 +52,22 @@ export function useDeckMapUtils({
const deckConfig = getSimplestDeckConfigForProtocol(protocolAnalysis)
const deckDef = getDeckDefFromRobotType(robotType)

const labwareDefinitionsByUri = useMemo(
() =>
protocolAnalysis != null
? getLoadedLabwareDefinitionsByUri(protocolAnalysis?.commands)
: null,
[protocolAnalysis]
)

const currentModulesInfo = useMemo(
() =>
getRunCurrentModulesInfo({
runRecord,
deckDef,
protocolAnalysis,
labwareDefinitionsByUri,
}),
[runRecord, deckDef, protocolAnalysis]
[runRecord, deckDef, labwareDefinitionsByUri]
)

const runCurrentModules = useMemo(
Expand All @@ -70,8 +80,8 @@ export function useDeckMapUtils({
)

const currentLabwareInfo = useMemo(
() => getRunCurrentLabwareInfo({ runRecord, protocolAnalysis }),
[runRecord, protocolAnalysis]
() => getRunCurrentLabwareInfo({ runRecord, labwareDefinitionsByUri }),
[runRecord, labwareDefinitionsByUri]
)

const runCurrentLabware = useMemo(
Expand Down Expand Up @@ -182,13 +192,13 @@ interface RunCurrentModuleInfo {
export const getRunCurrentModulesInfo = ({
runRecord,
deckDef,
protocolAnalysis,
labwareDefinitionsByUri,
}: {
protocolAnalysis: UseDeckMapUtilsProps['protocolAnalysis']
runRecord: UseDeckMapUtilsProps['runRecord']
deckDef: DeckDefinition
labwareDefinitionsByUri?: LabwareDefinitionsByUri | null
}): RunCurrentModuleInfo[] => {
if (runRecord == null || protocolAnalysis == null) {
if (runRecord == null || labwareDefinitionsByUri == null) {
return []
} else {
return runRecord.data.modules.reduce<RunCurrentModuleInfo[]>(
Expand All @@ -203,10 +213,6 @@ export const getRunCurrentModulesInfo = ({
lw.location.moduleId === module.id
)

const labwareDefinitionsByUri = getLoadedLabwareDefinitionsByUri(
protocolAnalysis.commands
)

const nestedLabwareDef =
nestedLabware != null
? labwareDefinitionsByUri[nestedLabware.definitionUri]
Expand Down Expand Up @@ -249,21 +255,18 @@ interface RunCurrentLabwareInfo {
// Derive the labware info necessary to render labware on the deck.
export function getRunCurrentLabwareInfo({
runRecord,
protocolAnalysis,
labwareDefinitionsByUri,
}: {
runRecord: UseDeckMapUtilsProps['runRecord']
protocolAnalysis: UseDeckMapUtilsProps['protocolAnalysis']
labwareDefinitionsByUri?: LabwareDefinitionsByUri | null
}): RunCurrentLabwareInfo[] {
if (runRecord == null || protocolAnalysis == null) {
if (runRecord == null || labwareDefinitionsByUri == null) {
return []
} else {
return runRecord.data.labware.reduce((acc: RunCurrentLabwareInfo[], lw) => {
const loc = lw.location
const [slotName, labwareLocation] = getSlotNameAndLwLocFrom(loc, true) // Exclude modules since handled separately.
const labwareDefinitionsByUri = getLoadedLabwareDefinitionsByUri(
protocolAnalysis.commands
)
const labwareDef = labwareDefinitionsByUri[lw.definitionUri]
const labwareDef = getLabwareDefinition(lw, labwareDefinitionsByUri)

if (slotName == null || labwareLocation == null) {
return acc
Expand All @@ -281,6 +284,17 @@ export function getRunCurrentLabwareInfo({
}
}

const getLabwareDefinition = (
labware: LoadedLabware,
protocolLabwareDefinitionsByUri: LabwareDefinitionsByUri
): LabwareDefinition2 => {
if (labware.id === 'fixedTrash') {
return getFixedTrashLabwareDefinition()
} else {
return protocolLabwareDefinitionsByUri[labware.definitionUri]
}
}

// Get the slotName for on deck labware.
export function getSlotNameAndLwLocFrom(
location: LabwareLocation | null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, it, expect } from 'vitest'
import { getFixedTrashLabwareDefinition } from '../index'
import type { LabwareDefinition2 } from '../..'
import fixedTrashUncasted from '../../../labware/definitions/2/opentrons_1_trash_3200ml_fixed/1.json'

describe('getFixedTrashLabwareDefinition', () => {
it(`should return the fixed trash labware defition`, () => {
expect(getFixedTrashLabwareDefinition()).toEqual(
(fixedTrashUncasted as unknown) as LabwareDefinition2
)
})
})
7 changes: 7 additions & 0 deletions shared-data/js/helpers/getFixedTrashLabwareDefinition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { LabwareDefinition2 } from '..'
import fixedTrashUncasted from '../../labware/definitions/2/opentrons_1_trash_3200ml_fixed/1.json'

export function getFixedTrashLabwareDefinition(): LabwareDefinition2 {
const LabwareDefinition2 = (fixedTrashUncasted as unknown) as LabwareDefinition2
return LabwareDefinition2
}
1 change: 1 addition & 0 deletions shared-data/js/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export * from './getModuleVizDims'
export * from './getVectorDifference'
export * from './getVectorSum'
export * from './getLoadedLabwareDefinitionsByUri'
export * from './getFixedTrashLabwareDefinition'
export * from './getOccludedSlotCountForModule'
export * from './labwareInference'
export * from './getAddressableAreasInProtocol'
Expand Down

0 comments on commit 7fb38c9

Please sign in to comment.