diff --git a/arduino-ide-extension/src/node/core-service-impl.ts b/arduino-ide-extension/src/node/core-service-impl.ts index 5ab4506fa..e5e3d05ac 100644 --- a/arduino-ide-extension/src/node/core-service-impl.ts +++ b/arduino-ide-extension/src/node/core-service-impl.ts @@ -411,17 +411,21 @@ export class CoreServiceImpl extends CoreClientAware implements CoreService { } } - private createPort(port: Port | undefined): RpcPort { + private createPort(port: Port | undefined): RpcPort | undefined { + if (!port) { + return undefined; + } const rpcPort = new RpcPort(); - if (port) { - rpcPort.setAddress(port.address); - rpcPort.setLabel(port.addressLabel); - rpcPort.setProtocol(port.protocol); - rpcPort.setProtocolLabel(port.protocolLabel); - if (port.properties) { - for (const [key, value] of Object.entries(port.properties)) { - rpcPort.getPropertiesMap().set(key, value); - } + rpcPort.setAddress(port.address); + rpcPort.setLabel(port.addressLabel); + rpcPort.setProtocol(port.protocol); + rpcPort.setProtocolLabel(port.protocolLabel); + if (port.hardwareId !== undefined) { + rpcPort.setHardwareId(port.hardwareId); + } + if (port.properties) { + for (const [key, value] of Object.entries(port.properties)) { + rpcPort.getPropertiesMap().set(key, value); } } return rpcPort; diff --git a/arduino-ide-extension/src/test/node/core-service-impl.test.ts b/arduino-ide-extension/src/test/node/core-service-impl.test.ts new file mode 100644 index 000000000..abba88357 --- /dev/null +++ b/arduino-ide-extension/src/test/node/core-service-impl.test.ts @@ -0,0 +1,41 @@ +import { expect } from 'chai'; +import { Port } from '../../node/cli-protocol/cc/arduino/cli/commands/v1/port_pb'; +import { CoreServiceImpl } from '../../node/core-service-impl'; + +describe('core-service-impl', () => { + describe('createPort', () => { + it("should map the 'undefined' port object to an 'undefined' gRPC port value", () => { + const actual = new CoreServiceImpl()['createPort'](undefined); + expect(actual).to.be.undefined; + }); + + it('should map a port object to the appropriate gRPC port object', () => { + const properties = { + alma: 'false', + korte: '36', + }; + const port = { + address: 'address', + addressLabel: 'address label', + hardwareId: '1730323', + protocol: 'serial', + protocolLabel: 'serial port', + properties, + } as const; + const actual = new CoreServiceImpl()['createPort'](port); + expect(actual).to.be.not.undefined; + const expected = new Port() + .setAddress(port.address) + .setHardwareId(port.hardwareId) + .setLabel(port.addressLabel) + .setProtocol(port.protocol) + .setProtocolLabel(port.protocolLabel); + Object.entries(properties).forEach(([key, value]) => + expected.getPropertiesMap().set(key, value) + ); + expect((actual).toObject(false)).to.be.deep.equal( + expected.toObject(false) + ); + }); + }); +});