|
| 1 | +import { SerialServiceImpl } from './../../node/serial/serial-service-impl'; |
| 2 | +import { IMock, It, Mock } from 'typemoq'; |
| 3 | +import { createSandbox } from 'sinon'; |
| 4 | +import * as sinonChai from 'sinon-chai'; |
| 5 | +import { expect, use } from 'chai'; |
| 6 | +use(sinonChai); |
| 7 | + |
| 8 | +import { ILogger } from '@theia/core/lib/common/logger'; |
| 9 | +import { MonitorClientProvider } from '../../node/serial/monitor-client-provider'; |
| 10 | +import { WebSocketService } from '../../node/web-socket/web-socket-service'; |
| 11 | +import { MonitorServiceClient } from '../../node/cli-protocol/cc/arduino/cli/monitor/v1/monitor_grpc_pb'; |
| 12 | + |
| 13 | +describe.only('SerialServiceImpl', () => { |
| 14 | + let subject: SerialServiceImpl; |
| 15 | + |
| 16 | + let logger: IMock<ILogger>; |
| 17 | + let serialClientProvider: IMock<MonitorClientProvider>; |
| 18 | + let webSocketService: IMock<WebSocketService>; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + logger = Mock.ofType<ILogger>(); |
| 22 | + logger.setup((b) => b.info(It.isAnyString())); |
| 23 | + logger.setup((b) => b.warn(It.isAnyString())); |
| 24 | + logger.setup((b) => b.error(It.isAnyString())); |
| 25 | + |
| 26 | + serialClientProvider = Mock.ofType<MonitorClientProvider>(); |
| 27 | + webSocketService = Mock.ofType<WebSocketService>(); |
| 28 | + |
| 29 | + subject = new SerialServiceImpl( |
| 30 | + logger.object, |
| 31 | + serialClientProvider.object, |
| 32 | + webSocketService.object |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + // context('when a serial connection is requested', () => { |
| 37 | + // const sandbox = createSandbox(); |
| 38 | + // beforeEach(() => { |
| 39 | + // subject.uploadInProgress = false; |
| 40 | + // sandbox.spy(subject, 'disconnect'); |
| 41 | + // sandbox.spy(subject, 'updateWsConfigParam'); |
| 42 | + // }); |
| 43 | + |
| 44 | + // afterEach(function () { |
| 45 | + // sandbox.restore(); |
| 46 | + // }); |
| 47 | + |
| 48 | + // context('and an upload is in progress', () => { |
| 49 | + // beforeEach(async () => { |
| 50 | + // subject.uploadInProgress = true; |
| 51 | + // }); |
| 52 | + |
| 53 | + // it('should not change the connection status', async () => { |
| 54 | + // await subject.connectSerialIfRequired(); |
| 55 | + // expect(subject.disconnect).to.have.callCount(0); |
| 56 | + // }); |
| 57 | + // }); |
| 58 | + |
| 59 | + // context('and there is no upload in progress', () => { |
| 60 | + // beforeEach(async () => { |
| 61 | + // subject.uploadInProgress = false; |
| 62 | + // }); |
| 63 | + |
| 64 | + // context('and there are 0 attached ws clients', () => { |
| 65 | + // it('should disconnect', async () => { |
| 66 | + // await subject.connectSerialIfRequired(); |
| 67 | + // expect(subject.disconnect).to.have.been.calledOnce; |
| 68 | + // }); |
| 69 | + // }); |
| 70 | + |
| 71 | + // context('and there are > 0 attached ws clients', () => { |
| 72 | + // beforeEach(() => { |
| 73 | + // webSocketService |
| 74 | + // .setup((b) => b.getConnectedClientsNumber()) |
| 75 | + // .returns(() => 1); |
| 76 | + // }); |
| 77 | + |
| 78 | + // it('should not call the disconenct', async () => { |
| 79 | + // await subject.connectSerialIfRequired(); |
| 80 | + // expect(subject.disconnect).to.have.callCount(0); |
| 81 | + // }); |
| 82 | + // }); |
| 83 | + // }); |
| 84 | + // }); |
| 85 | + |
| 86 | + // context('when a disconnection is requested', () => { |
| 87 | + // const sandbox = createSandbox(); |
| 88 | + // beforeEach(() => {}); |
| 89 | + |
| 90 | + // afterEach(function () { |
| 91 | + // sandbox.restore(); |
| 92 | + // }); |
| 93 | + |
| 94 | + // context('and a serialConnection is not set', () => { |
| 95 | + // it('should return a NOT_CONNECTED status', async () => { |
| 96 | + // const status = await subject.disconnect(); |
| 97 | + // expect(status).to.be.equal(Status.NOT_CONNECTED); |
| 98 | + // }); |
| 99 | + // }); |
| 100 | + |
| 101 | + // context('and a serialConnection is set', async () => { |
| 102 | + // beforeEach(async () => { |
| 103 | + // sandbox.spy(subject, 'updateWsConfigParam'); |
| 104 | + // await subject.disconnect(); |
| 105 | + // }); |
| 106 | + |
| 107 | + // it('should dispose the serialConnection', async () => { |
| 108 | + // const serialConnectionOpen = await subject.isSerialPortOpen(); |
| 109 | + // expect(serialConnectionOpen).to.be.false; |
| 110 | + // }); |
| 111 | + |
| 112 | + // it('should call updateWsConfigParam with disconnected status', async () => { |
| 113 | + // expect(subject.updateWsConfigParam).to.be.calledWith({ |
| 114 | + // connected: false, |
| 115 | + // }); |
| 116 | + // }); |
| 117 | + // }); |
| 118 | + // }); |
| 119 | + |
| 120 | + context('when a new config is passed in', () => { |
| 121 | + const sandbox = createSandbox(); |
| 122 | + beforeEach(async () => { |
| 123 | + subject.uploadInProgress = false; |
| 124 | + webSocketService |
| 125 | + .setup((b) => b.getConnectedClientsNumber()) |
| 126 | + .returns(() => 1); |
| 127 | + |
| 128 | + serialClientProvider |
| 129 | + .setup((b) => b.client()) |
| 130 | + .returns(async () => { |
| 131 | + return { |
| 132 | + streamingOpen: () => { |
| 133 | + return { |
| 134 | + on: (str: string, cb: any) => {}, |
| 135 | + write: (chunk: any, cb: any) => { |
| 136 | + cb(); |
| 137 | + }, |
| 138 | + cancel: () => {}, |
| 139 | + }; |
| 140 | + }, |
| 141 | + } as MonitorServiceClient; |
| 142 | + }); |
| 143 | + |
| 144 | + sandbox.spy(subject, 'disconnect'); |
| 145 | + |
| 146 | + await subject.setSerialConfig({ |
| 147 | + board: { name: 'test' }, |
| 148 | + port: { address: 'test', protocol: 'test' }, |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + afterEach(function () { |
| 153 | + sandbox.restore(); |
| 154 | + subject.dispose(); |
| 155 | + }); |
| 156 | + |
| 157 | + // it('should disconnect from previous connection', async () => { |
| 158 | + // expect(subject.disconnect).to.be.called; |
| 159 | + // }); |
| 160 | + |
| 161 | + it('should create the serialConnection', async () => { |
| 162 | + const serialConnectionOpen = await subject.isSerialPortOpen(); |
| 163 | + expect(serialConnectionOpen).to.be.true; |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments