Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(app): Support "resume from recovery assuming false positive" action #16601

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useResumeRunFromRecoveryMutation,
useStopRunMutation,
useUpdateErrorRecoveryPolicy,
useResumeRunFromRecoveryAssumingFalsePositiveMutation,
} from '@opentrons/react-api-client'

import { useChainRunCommands } from '/app/resources/runs'
Expand All @@ -14,13 +15,16 @@ import {
RELEASE_GRIPPER_JAW,
buildPickUpTips,
buildIgnorePolicyRules,
isAssumeFalsePositiveResumeKind,
UPDATE_ESTIMATORS_EXCEPT_PLUNGERS,
HOME_GRIPPER_Z,
} from '../useRecoveryCommands'
import { RECOVERY_MAP } from '../../constants'
import { RECOVERY_MAP, ERROR_KINDS } from '../../constants'
import { getErrorKind } from '/app/organisms/ErrorRecoveryFlows/utils'

vi.mock('@opentrons/react-api-client')
vi.mock('/app/resources/runs')
vi.mock('/app/organisms/ErrorRecoveryFlows/utils')

describe('useRecoveryCommands', () => {
const mockFailedCommand = {
Expand All @@ -41,6 +45,9 @@ describe('useRecoveryCommands', () => {
const mockResumeRunFromRecovery = vi.fn(() =>
Promise.resolve(mockMakeSuccessToast())
)
const mockResumeRunFromRecoveryAssumingFalsePositive = vi.fn(() =>
Promise.resolve(mockMakeSuccessToast())
)
const mockStopRun = vi.fn()
const mockChainRunCommands = vi.fn().mockResolvedValue([])
const mockReportActionSelectedResult = vi.fn()
Expand Down Expand Up @@ -73,6 +80,11 @@ describe('useRecoveryCommands', () => {
vi.mocked(useUpdateErrorRecoveryPolicy).mockReturnValue({
mutateAsync: mockUpdateErrorRecoveryPolicy,
} as any)
vi.mocked(
useResumeRunFromRecoveryAssumingFalsePositiveMutation
).mockReturnValue({
mutateAsync: mockResumeRunFromRecoveryAssumingFalsePositive,
} as any)
})

it('should call chainRunRecoveryCommands with continuePastCommandFailure set to false', async () => {
Expand Down Expand Up @@ -317,7 +329,8 @@ describe('useRecoveryCommands', () => {

const expectedPolicyRules = buildIgnorePolicyRules(
'aspirateInPlace',
'mockErrorType'
'mockErrorType',
'ignoreAndContinue'
)

expect(mockUpdateErrorRecoveryPolicy).toHaveBeenCalledWith(
Expand Down Expand Up @@ -354,4 +367,54 @@ describe('useRecoveryCommands', () => {
RECOVERY_MAP.ERROR_WHILE_RECOVERING.ROUTE
)
})

describe('skipFailedCommand with false positive handling', () => {
it('should call resumeRunFromRecoveryAssumingFalsePositive for tip-related errors', async () => {
vi.mocked(getErrorKind).mockReturnValue(ERROR_KINDS.TIP_NOT_DETECTED)

const { result } = renderHook(() => useRecoveryCommands(props))

await act(async () => {
await result.current.skipFailedCommand()
})

expect(
mockResumeRunFromRecoveryAssumingFalsePositive
).toHaveBeenCalledWith(mockRunId)
expect(mockMakeSuccessToast).toHaveBeenCalled()
})

it('should call regular resumeRunFromRecovery for non-tip-related errors', async () => {
vi.mocked(getErrorKind).mockReturnValue(ERROR_KINDS.GRIPPER_ERROR)

const { result } = renderHook(() => useRecoveryCommands(props))

await act(async () => {
await result.current.skipFailedCommand()
})

expect(mockResumeRunFromRecovery).toHaveBeenCalledWith(mockRunId)
expect(mockMakeSuccessToast).toHaveBeenCalled()
})
})
})

describe('isAssumeFalsePositiveResumeKind', () => {
it(`should return true for ${ERROR_KINDS.TIP_NOT_DETECTED} error kind`, () => {
vi.mocked(getErrorKind).mockReturnValue(ERROR_KINDS.TIP_NOT_DETECTED)

expect(isAssumeFalsePositiveResumeKind({} as any)).toBe(true)
})

it(`should return true for ${ERROR_KINDS.TIP_DROP_FAILED} error kind`, () => {
vi.mocked(getErrorKind).mockReturnValue(ERROR_KINDS.TIP_DROP_FAILED)

expect(isAssumeFalsePositiveResumeKind({} as any)).toBe(true)
})

it('should return false for other error kinds', () => {
vi.mocked(getErrorKind).mockReturnValue(ERROR_KINDS.GRIPPER_ERROR)

expect(isAssumeFalsePositiveResumeKind({} as any)).toBe(false)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {
useResumeRunFromRecoveryMutation,
useStopRunMutation,
useUpdateErrorRecoveryPolicy,
useResumeRunFromRecoveryAssumingFalsePositiveMutation,
} from '@opentrons/react-api-client'

import { useChainRunCommands } from '/app/resources/runs'
import { RECOVERY_MAP } from '../constants'
import { ERROR_KINDS, RECOVERY_MAP } from '../constants'
import { getErrorKind } from '/app/organisms/ErrorRecoveryFlows/utils'

import type {
CreateCommand,
Expand All @@ -23,7 +25,9 @@ import type {
} from '@opentrons/shared-data'
import type {
CommandData,
IfMatchType,
RecoveryPolicyRulesParams,
RunAction,
} from '@opentrons/api-client'
import type { WellGroup } from '@opentrons/components'
import type { FailedCommand, RecoveryRoute, RouteStep } from '../types'
Expand Down Expand Up @@ -89,6 +93,9 @@ export function useRecoveryCommands({
const {
mutateAsync: resumeRunFromRecovery,
} = useResumeRunFromRecoveryMutation()
const {
mutateAsync: resumeRunFromRecoveryAssumingFalsePositive,
} = useResumeRunFromRecoveryAssumingFalsePositiveMutation()
const { stopRun } = useStopRunMutation()
const {
mutateAsync: updateErrorRecoveryPolicy,
Expand Down Expand Up @@ -198,9 +205,16 @@ export function useRecoveryCommands({
const handleIgnoringErrorKind = useCallback((): Promise<void> => {
if (ignoreErrors) {
if (failedCommandByRunRecord?.error != null) {
const ifMatch: IfMatchType = isAssumeFalsePositiveResumeKind(
failedCommandByRunRecord
)
? 'assumeFalsePositiveAndContinue'
: 'ignoreAndContinue'

const ignorePolicyRules = buildIgnorePolicyRules(
failedCommandByRunRecord.commandType,
failedCommandByRunRecord.error.errorType
failedCommandByRunRecord.error.errorType,
ifMatch
)

return updateErrorRecoveryPolicy(ignorePolicyRules)
Expand Down Expand Up @@ -247,9 +261,17 @@ export function useRecoveryCommands({
stopRun(runId)
}, [runId])

const handleResumeAction = (): Promise<RunAction> => {
if (isAssumeFalsePositiveResumeKind(failedCommandByRunRecord)) {
return resumeRunFromRecoveryAssumingFalsePositive(runId)
} else {
return resumeRunFromRecovery(runId)
}
}

const skipFailedCommand = useCallback((): void => {
void handleIgnoringErrorKind().then(() =>
resumeRunFromRecovery(runId).then(() => {
handleResumeAction().then(() => {
analytics.reportActionSelectedResult(
selectedRecoveryOption,
'succeeded'
Expand Down Expand Up @@ -303,6 +325,20 @@ export function useRecoveryCommands({
}
}

export function isAssumeFalsePositiveResumeKind(
failedCommandByRunRecord: UseRecoveryCommandsParams['failedCommandByRunRecord']
): boolean {
const errorKind = getErrorKind(failedCommandByRunRecord)

switch (errorKind) {
case ERROR_KINDS.TIP_NOT_DETECTED:
case ERROR_KINDS.TIP_DROP_FAILED:
return true
default:
return false
}
}

export const HOME_PIPETTE_Z_AXES: CreateCommand = {
commandType: 'home',
params: { axes: ['leftZ', 'rightZ'] },
Expand Down Expand Up @@ -372,13 +408,14 @@ export const buildPickUpTips = (

export const buildIgnorePolicyRules = (
commandType: FailedCommand['commandType'],
errorType: string
errorType: string,
ifMatch: IfMatchType
): RecoveryPolicyRulesParams => {
return [
{
commandType,
errorType,
ifMatch: 'ignoreAndContinue',
ifMatch,
},
]
}
3 changes: 3 additions & 0 deletions app/src/organisms/RunTimeControl/__tests__/hooks.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@ describe('useRunControls hook', () => {
const mockStopRun = vi.fn()
const mockCloneRun = vi.fn()
const mockResumeRunFromRecovery = vi.fn()
const mockResumeRunFromRecoveryAssumingFalsePositive = vi.fn()

when(useRunActionMutations).calledWith(mockPausedRun.id).thenReturn({
playRun: mockPlayRun,
pauseRun: mockPauseRun,
stopRun: mockStopRun,
resumeRunFromRecovery: mockResumeRunFromRecovery,
resumeRunFromRecoveryAssumingFalsePositive: mockResumeRunFromRecoveryAssumingFalsePositive,
isPlayRunActionLoading: false,
isPauseRunActionLoading: false,
isStopRunActionLoading: false,
isResumeRunFromRecoveryActionLoading: false,
isResumeRunFromRecoveryAssumingFalsePositiveActionLoading: false,
})
when(useCloneRun).calledWith(mockPausedRun.id, undefined, true).thenReturn({
cloneRun: mockCloneRun,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const mockPlayRun = vi.fn()
const mockPauseRun = vi.fn()
const mockStopRun = vi.fn()
const mockResumeRunFromRecovery = vi.fn()
const mockResumeRunFromRecoveryAssumingFalsePositive = vi.fn()

const render = (path = '/') => {
return renderWithProviders(
Expand Down Expand Up @@ -133,10 +134,12 @@ describe('RunningProtocol', () => {
pauseRun: mockPauseRun,
stopRun: mockStopRun,
resumeRunFromRecovery: mockResumeRunFromRecovery,
resumeRunFromRecoveryAssumingFalsePositive: mockResumeRunFromRecoveryAssumingFalsePositive,
isPlayRunActionLoading: false,
isPauseRunActionLoading: false,
isStopRunActionLoading: false,
isResumeRunFromRecoveryActionLoading: false,
isResumeRunFromRecoveryAssumingFalsePositiveActionLoading: false,
})
when(vi.mocked(useMostRecentCompletedAnalysis))
.calledWith(RUN_ID)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { QueryClient, QueryClientProvider } from 'react-query'
import { act, renderHook, waitFor } from '@testing-library/react'

import { createRunAction } from '@opentrons/api-client'

import { useHost } from '../../api'
import { useResumeRunFromRecoveryAssumingFalsePositiveMutation } from '..'

import { RUN_ID_1, mockResumeFromRecoveryAction } from '../__fixtures__'

import type * as React from 'react'
import type { HostConfig, Response, RunAction } from '@opentrons/api-client'
import type { UseResumeRunFromRecoveryAssumingFalsePositiveMutationOptions } from '../useResumeFromRecoveryAssumingFalsePositiveMutation'

vi.mock('@opentrons/api-client')
vi.mock('../../api/useHost')

const HOST_CONFIG: HostConfig = { hostname: 'localhost' }

describe('useResumeRunFromRecoveryAssumingFalsePositiveMutation hook', () => {
let wrapper: React.FunctionComponent<
{
children: React.ReactNode
} & UseResumeRunFromRecoveryAssumingFalsePositiveMutationOptions
>

beforeEach(() => {
const queryClient = new QueryClient()
const clientProvider: React.FunctionComponent<
{
children: React.ReactNode
} & UseResumeRunFromRecoveryAssumingFalsePositiveMutationOptions
> = ({ children }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)
wrapper = clientProvider
})

it('should return no data when calling resumeRunFromRecoveryAssumingFalsePositive if the request fails', async () => {
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
vi.mocked(createRunAction).mockRejectedValue('oh no')

const { result } = renderHook(
useResumeRunFromRecoveryAssumingFalsePositiveMutation,
{
wrapper,
}
)

expect(result.current.data).toBeUndefined()
act(() =>
result.current.resumeRunFromRecoveryAssumingFalsePositive(RUN_ID_1)
)
await waitFor(() => {
expect(result.current.data).toBeUndefined()
})
})

it('should create a resumeFromRecoveryAssumingFalsePositive run action when calling the resumeRunFromRecoveryAssumingFalsePositive callback', async () => {
vi.mocked(useHost).mockReturnValue(HOST_CONFIG)
vi.mocked(createRunAction).mockResolvedValue({
data: mockResumeFromRecoveryAction,
} as Response<RunAction>)

const { result } = renderHook(
useResumeRunFromRecoveryAssumingFalsePositiveMutation,
{
wrapper,
}
)
act(() =>
result.current.resumeRunFromRecoveryAssumingFalsePositive(RUN_ID_1)
)

await waitFor(() => {
expect(result.current.data).toEqual(mockResumeFromRecoveryAction)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import { useResumeRunFromRecoveryMutation } from '..'
import { RUN_ID_1, mockResumeFromRecoveryAction } from '../__fixtures__'

import type { HostConfig, Response, RunAction } from '@opentrons/api-client'
import type { UsePlayRunMutationOptions } from '../usePlayRunMutation'
import type { UseResumeRunFromRecoveryMutationOptions } from '../useResumeRunFromRecoveryMutation'

vi.mock('@opentrons/api-client')
vi.mock('../../api/useHost')

const HOST_CONFIG: HostConfig = { hostname: 'localhost' }

describe('usePlayRunMutation hook', () => {
describe('useResumeRunFromRecoveryMutation hook', () => {
let wrapper: React.FunctionComponent<
{ children: React.ReactNode } & UsePlayRunMutationOptions
{ children: React.ReactNode } & UseResumeRunFromRecoveryMutationOptions
>

beforeEach(() => {
const queryClient = new QueryClient()
const clientProvider: React.FunctionComponent<
{ children: React.ReactNode } & UsePlayRunMutationOptions
{ children: React.ReactNode } & UseResumeRunFromRecoveryMutationOptions
> = ({ children }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)
Expand Down
Loading
Loading