-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(app-shell): Migrate desktop app notify connectivity to disco…
- Loading branch information
Showing
18 changed files
with
1,140 additions
and
477 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './config' | ||
export * from './robots' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { HEALTH_STATUS_NOT_OK, HEALTH_STATUS_OK } from '../constants' | ||
|
||
export const mockLegacyHealthResponse = { | ||
name: 'opentrons-dev', | ||
api_version: '1.2.3', | ||
fw_version: '4.5.6', | ||
system_version: '7.8.9', | ||
robot_model: 'OT-2 Standard', | ||
} | ||
|
||
export const mockLegacyServerHealthResponse = { | ||
name: 'opentrons-dev', | ||
apiServerVersion: '1.2.3', | ||
serialNumber: '12345', | ||
updateServerVersion: '1.2.3', | ||
smoothieVersion: '4.5.6', | ||
systemVersion: '7.8.9', | ||
} | ||
|
||
export const MOCK_DISCOVERY_ROBOTS = [ | ||
{ | ||
name: 'opentrons-dev', | ||
health: mockLegacyHealthResponse, | ||
serverHealth: mockLegacyServerHealthResponse, | ||
addresses: [ | ||
{ | ||
ip: '10.14.19.50', | ||
port: 31950, | ||
seen: true, | ||
healthStatus: HEALTH_STATUS_OK, | ||
serverHealthStatus: HEALTH_STATUS_OK, | ||
healthError: null, | ||
serverHealthError: null, | ||
advertisedModel: null, | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'opentrons-dev2', | ||
health: mockLegacyHealthResponse, | ||
serverHealth: mockLegacyServerHealthResponse, | ||
addresses: [ | ||
{ | ||
ip: '10.14.19.51', | ||
port: 31950, | ||
seen: true, | ||
healthStatus: HEALTH_STATUS_OK, | ||
serverHealthStatus: HEALTH_STATUS_OK, | ||
healthError: null, | ||
serverHealthError: null, | ||
advertisedModel: null, | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'opentrons-dev3', | ||
health: mockLegacyHealthResponse, | ||
serverHealth: mockLegacyServerHealthResponse, | ||
addresses: [ | ||
{ | ||
ip: '10.14.19.52', | ||
port: 31950, | ||
seen: true, | ||
healthStatus: HEALTH_STATUS_NOT_OK, | ||
serverHealthStatus: HEALTH_STATUS_NOT_OK, | ||
healthError: null, | ||
serverHealthError: null, | ||
advertisedModel: null, | ||
}, | ||
], | ||
}, | ||
{ | ||
name: 'opentrons-dev4', | ||
health: mockLegacyHealthResponse, | ||
serverHealth: mockLegacyServerHealthResponse, | ||
addresses: [ | ||
{ | ||
ip: '10.14.19.53', | ||
port: 31950, | ||
seen: true, | ||
healthStatus: HEALTH_STATUS_OK, | ||
serverHealthStatus: HEALTH_STATUS_OK, | ||
healthError: null, | ||
serverHealthError: null, | ||
advertisedModel: null, | ||
}, | ||
], | ||
}, | ||
] | ||
|
||
export const MOCK_STORE_ROBOTS = [ | ||
{ | ||
robotName: 'opentrons-dev', | ||
ip: '10.14.19.50', | ||
}, | ||
{ | ||
robotName: 'opentrons-dev2', | ||
ip: '10.14.19.51', | ||
}, | ||
{ | ||
robotName: 'opentrons-dev3', | ||
ip: '10.14.19.52', | ||
}, | ||
{ | ||
robotName: 'opentrons-dev4', | ||
ip: '10.14.19.53', | ||
}, | ||
] | ||
|
||
export const MOCK_HEALTHY_ROBOTS = [ | ||
{ | ||
robotName: 'opentrons-dev', | ||
ip: '10.14.19.50', | ||
}, | ||
{ | ||
robotName: 'opentrons-dev2', | ||
ip: '10.14.19.51', | ||
}, | ||
{ | ||
robotName: 'opentrons-dev4', | ||
ip: '10.14.19.53', | ||
}, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { vi, describe, expect, it } from 'vitest' | ||
|
||
import { | ||
getHealthyRobotDataForNotifyConnections, | ||
cleanUpUnreachableRobots, | ||
establishConnections, | ||
closeConnectionsForcefullyFor, | ||
} from '../connect' | ||
import { connectionStore } from '../store' | ||
import { FAILURE_STATUSES } from '../../constants' | ||
import { | ||
MOCK_DISCOVERY_ROBOTS, | ||
MOCK_HEALTHY_ROBOTS, | ||
MOCK_STORE_ROBOTS, | ||
} from '../../__fixtures__' | ||
|
||
vi.mock('electron-store') | ||
vi.mock('../notifyLog', () => { | ||
return { | ||
createLogger: () => { | ||
return { debug: () => null } | ||
}, | ||
notifyLog: { debug: vi.fn(), warn: vi.fn() }, | ||
} | ||
}) | ||
|
||
describe('getHealthyRobotDataForNotifyConnections', () => { | ||
it('should filter a list of discovery robots, only returning robots that have a health status of ok', () => { | ||
const healthyRobots = getHealthyRobotDataForNotifyConnections( | ||
MOCK_DISCOVERY_ROBOTS | ||
) | ||
expect(healthyRobots).toEqual(MOCK_HEALTHY_ROBOTS) | ||
}) | ||
}) | ||
|
||
describe('cleanUpUnreachableRobots', () => { | ||
it('should close connections forcefully for unreachable robots and resolve them', async () => { | ||
MOCK_STORE_ROBOTS.forEach(robot => { | ||
void connectionStore | ||
.setPendingConnection(robot.robotName) | ||
.then(() => | ||
connectionStore.setConnected(robot.robotName, vi.fn() as any) | ||
) | ||
}) | ||
const unreachableRobots = await cleanUpUnreachableRobots( | ||
MOCK_HEALTHY_ROBOTS | ||
) | ||
expect(unreachableRobots).toEqual(['opentrons-dev3']) | ||
}) | ||
}) | ||
|
||
describe('establishConnections', () => { | ||
it('should not resolve any new connections if all reported robots are already in the connection store and connected', async () => { | ||
connectionStore.clearStore() | ||
MOCK_STORE_ROBOTS.forEach(robot => { | ||
void connectionStore | ||
.setPendingConnection(robot.robotName) | ||
.then(() => | ||
connectionStore.setConnected(robot.robotName, vi.fn() as any) | ||
) | ||
}) | ||
|
||
const newRobots = await establishConnections(MOCK_HEALTHY_ROBOTS) | ||
expect(newRobots).toEqual([]) | ||
}) | ||
|
||
it('should not attempt to connect to a robot if it a known notification port blocked robot', async () => { | ||
await connectionStore.setErrorStatus( | ||
'10.14.19.51', | ||
FAILURE_STATUSES.ECONNREFUSED | ||
) | ||
connectionStore.clearStore() | ||
|
||
const newRobots = await establishConnections(MOCK_HEALTHY_ROBOTS) | ||
expect(newRobots).toEqual([ | ||
{ ip: '10.14.19.50', robotName: 'opentrons-dev' }, | ||
{ ip: '10.14.19.53', robotName: 'opentrons-dev4' }, | ||
]) | ||
}) | ||
|
||
it('should not report a robot as new if it is connecting', async () => { | ||
connectionStore.clearStore() | ||
MOCK_STORE_ROBOTS.forEach(robot => { | ||
void connectionStore.setPendingConnection(robot.robotName) | ||
}) | ||
|
||
const newRobots = await establishConnections(MOCK_HEALTHY_ROBOTS) | ||
expect(newRobots).toEqual([]) | ||
}) | ||
|
||
it('should create a new entry in the connection store for a new robot', async () => { | ||
connectionStore.clearStore() | ||
await establishConnections(MOCK_HEALTHY_ROBOTS) | ||
console.log(connectionStore) | ||
expect(connectionStore.getRobotNameByIP('10.14.19.50')).not.toBeNull() | ||
}) | ||
}) | ||
|
||
describe('closeConnectionsForcefullyFor', () => { | ||
it('should return an array of promises for each closing connection and resolve after closing connections', async () => { | ||
connectionStore.clearStore() | ||
MOCK_STORE_ROBOTS.forEach(robot => { | ||
void connectionStore | ||
.setPendingConnection(robot.robotName) | ||
.then(() => | ||
connectionStore.setConnected(robot.robotName, vi.fn() as any) | ||
) | ||
}) | ||
const closingRobots = closeConnectionsForcefullyFor([ | ||
'opentrons-dev', | ||
'opentrons-dev2', | ||
]) | ||
closingRobots.forEach(robot => expect(robot).toBeInstanceOf(Promise)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { deserializeExpectedMessages } from '../deserialize' | ||
|
||
import type { NotifyResponseData } from '@opentrons/app/src/redux/shell/types' | ||
|
||
const MOCK_VALID_RESPONSE: NotifyResponseData = { refetchUsingHTTP: true } | ||
const MOCK_VALID_STRING_RESPONSE = JSON.stringify(MOCK_VALID_RESPONSE) | ||
const MOCK_INVALID_OBJECT = JSON.stringify({ test: 'MOCK_RESPONSE' }) | ||
const MOCK_INVALID_STRING = 'MOCK_STRING' | ||
|
||
describe('closeConnectionsForcefullyFor', () => { | ||
it('should resolve with the deserialized message if it is a valid notify response', async () => { | ||
const response = await deserializeExpectedMessages( | ||
MOCK_VALID_STRING_RESPONSE | ||
) | ||
expect(response).toEqual(MOCK_VALID_RESPONSE) | ||
}) | ||
|
||
it('should reject with an error if the deserialized message is not a valid notify response', async () => { | ||
const responsePromise = deserializeExpectedMessages(MOCK_INVALID_OBJECT) | ||
await expect(responsePromise).rejects.toThrowError( | ||
'Unexpected data received from notify broker: {"test":"MOCK_RESPONSE"}' | ||
) | ||
}) | ||
|
||
it('should reject with an error if the message cannot be deserialized', async () => { | ||
const responsePromise = deserializeExpectedMessages(MOCK_INVALID_STRING) | ||
await expect(responsePromise).rejects.toThrowError( | ||
'Unexpected data received from notify broker: MOCK_STRING' | ||
) | ||
}) | ||
}) |
Oops, something went wrong.