|
| 1 | +import { vi, describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { |
| 4 | + getHealthyRobotDataForNotifyConnections, |
| 5 | + cleanUpUnreachableRobots, |
| 6 | + establishConnections, |
| 7 | + closeConnectionsForcefullyFor, |
| 8 | +} from '../connect' |
| 9 | +import { connectionStore } from '../store' |
| 10 | +import { FAILURE_STATUSES } from '../../constants' |
| 11 | +import { |
| 12 | + MOCK_DISCOVERY_ROBOTS, |
| 13 | + MOCK_HEALTHY_ROBOTS, |
| 14 | + MOCK_STORE_ROBOTS, |
| 15 | +} from '../../__fixtures__' |
| 16 | + |
| 17 | +vi.mock('electron-store') |
| 18 | +vi.mock('../notifyLog', () => { |
| 19 | + return { |
| 20 | + createLogger: () => { |
| 21 | + return { debug: () => null } |
| 22 | + }, |
| 23 | + notifyLog: { debug: vi.fn(), warn: vi.fn() }, |
| 24 | + } |
| 25 | +}) |
| 26 | + |
| 27 | +describe('getHealthyRobotDataForNotifyConnections', () => { |
| 28 | + it('should filter a list of discovery robots, only returning robots that have a health status of ok', () => { |
| 29 | + const healthyRobots = getHealthyRobotDataForNotifyConnections( |
| 30 | + MOCK_DISCOVERY_ROBOTS |
| 31 | + ) |
| 32 | + expect(healthyRobots).toEqual(MOCK_HEALTHY_ROBOTS) |
| 33 | + }) |
| 34 | +}) |
| 35 | + |
| 36 | +describe('cleanUpUnreachableRobots', () => { |
| 37 | + it('should close connections forcefully for unreachable robots and resolve them', async () => { |
| 38 | + MOCK_STORE_ROBOTS.forEach(robot => { |
| 39 | + void connectionStore |
| 40 | + .setPendingConnection(robot.robotName) |
| 41 | + .then(() => |
| 42 | + connectionStore.setConnected(robot.robotName, vi.fn() as any) |
| 43 | + ) |
| 44 | + }) |
| 45 | + const unreachableRobots = await cleanUpUnreachableRobots( |
| 46 | + MOCK_HEALTHY_ROBOTS |
| 47 | + ) |
| 48 | + expect(unreachableRobots).toEqual(['opentrons-dev3']) |
| 49 | + }) |
| 50 | +}) |
| 51 | + |
| 52 | +describe('establishConnections', () => { |
| 53 | + it('should not resolve any new connections if all reported robots are already in the connection store and connected', async () => { |
| 54 | + connectionStore.clearStore() |
| 55 | + MOCK_STORE_ROBOTS.forEach(robot => { |
| 56 | + void connectionStore |
| 57 | + .setPendingConnection(robot.robotName) |
| 58 | + .then(() => |
| 59 | + connectionStore.setConnected(robot.robotName, vi.fn() as any) |
| 60 | + ) |
| 61 | + }) |
| 62 | + |
| 63 | + const newRobots = await establishConnections(MOCK_HEALTHY_ROBOTS) |
| 64 | + expect(newRobots).toEqual([]) |
| 65 | + }) |
| 66 | + |
| 67 | + it('should not attempt to connect to a robot if it a known notification port blocked robot', async () => { |
| 68 | + await connectionStore.setErrorStatus( |
| 69 | + '10.14.19.51', |
| 70 | + FAILURE_STATUSES.ECONNREFUSED |
| 71 | + ) |
| 72 | + connectionStore.clearStore() |
| 73 | + |
| 74 | + const newRobots = await establishConnections(MOCK_HEALTHY_ROBOTS) |
| 75 | + expect(newRobots).toEqual([ |
| 76 | + { ip: '10.14.19.50', robotName: 'opentrons-dev' }, |
| 77 | + { ip: '10.14.19.53', robotName: 'opentrons-dev4' }, |
| 78 | + ]) |
| 79 | + }) |
| 80 | + |
| 81 | + it('should not report a robot as new if it is connecting', async () => { |
| 82 | + connectionStore.clearStore() |
| 83 | + MOCK_STORE_ROBOTS.forEach(robot => { |
| 84 | + void connectionStore.setPendingConnection(robot.robotName) |
| 85 | + }) |
| 86 | + |
| 87 | + const newRobots = await establishConnections(MOCK_HEALTHY_ROBOTS) |
| 88 | + expect(newRobots).toEqual([]) |
| 89 | + }) |
| 90 | + |
| 91 | + it('should create a new entry in the connection store for a new robot', async () => { |
| 92 | + connectionStore.clearStore() |
| 93 | + await establishConnections(MOCK_HEALTHY_ROBOTS) |
| 94 | + console.log(connectionStore) |
| 95 | + expect(connectionStore.getRobotNameByIP('10.14.19.50')).not.toBeNull() |
| 96 | + }) |
| 97 | +}) |
| 98 | + |
| 99 | +describe('closeConnectionsForcefullyFor', () => { |
| 100 | + it('should return an array of promises for each closing connection and resolve after closing connections', async () => { |
| 101 | + connectionStore.clearStore() |
| 102 | + MOCK_STORE_ROBOTS.forEach(robot => { |
| 103 | + void connectionStore |
| 104 | + .setPendingConnection(robot.robotName) |
| 105 | + .then(() => |
| 106 | + connectionStore.setConnected(robot.robotName, vi.fn() as any) |
| 107 | + ) |
| 108 | + }) |
| 109 | + const closingRobots = closeConnectionsForcefullyFor([ |
| 110 | + 'opentrons-dev', |
| 111 | + 'opentrons-dev2', |
| 112 | + ]) |
| 113 | + closingRobots.forEach(robot => expect(robot).toBeInstanceOf(Promise)) |
| 114 | + }) |
| 115 | +}) |
0 commit comments