From 085976495f1e374ab2484d0e8e03f71aaf60e99a Mon Sep 17 00:00:00 2001 From: "Valentin D. Pinkman" Date: Fri, 8 Nov 2024 12:00:42 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20(dmk):=20TransportService?= =?UTF-8?q?=20interface=20+=20DefaultTransportService=20implem?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + .../model/TransportConnectedDevice.test.ts | 12 -- .../GetDeviceSessionStateUseCase.test.ts | 7 -- .../discovery/use-case/ConnectUseCase.test.ts | 7 +- .../discovery/use-case/ConnectUseCase.ts | 2 +- .../use-case/DisconnectUseCase.test.ts | 7 +- .../discovery/use-case/DisconnectUseCase.ts | 2 +- .../ListConnectedDevicesUseCase.test.ts | 4 +- .../ListenToConnectedDeviceUseCase.test.ts | 6 +- .../ListenToConnectedDeviceUseCase.ts | 4 +- .../ListenToKnownDevicesUseCase.test.ts | 7 +- .../use-case/ListenToKnownDevicesUseCase.ts | 2 +- .../use-case/StartDiscoveringUseCase.test.ts | 7 +- .../use-case/StartDiscoveringUseCase.ts | 2 +- .../use-case/StopDiscoveringUseCase.test.ts | 7 +- .../use-case/StopDiscoveringUseCase.ts | 2 +- .../internal/transport/di/transportModule.ts | 4 +- ...est.ts => DefaultTransportService.test.ts} | 20 ++-- .../service/DefaultTransportService.ts | 106 ++++++++++++++++++ .../transport/service/TransportService.ts | 106 ++---------------- ...tService.ts => DefaultTransportService.ts} | 3 +- turbo.json | 6 +- 22 files changed, 163 insertions(+), 161 deletions(-) rename packages/device-management-kit/src/internal/transport/service/{TransportService.test.ts => DefaultTransportService.test.ts} (92%) create mode 100644 packages/device-management-kit/src/internal/transport/service/DefaultTransportService.ts rename packages/device-management-kit/src/internal/transport/service/__mocks__/{TransportService.ts => DefaultTransportService.ts} (84%) diff --git a/package.json b/package.json index d9459a936..aeebc7bd3 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "test:coverage": "turbo run test:coverage", "typecheck": "turbo run typecheck", "health-check": "turbo run health-check --output-logs=errors-only --continue", + "clean": "rimraf -g **/.turbo **/.next **/coverage", "dmk": "pnpm --filter @ledgerhq/device-management-kit", "context-module": "pnpm --filter @ledgerhq/context-module", "signer-btc": "pnpm --filter @ledgerhq/device-signer-kit-btc", diff --git a/packages/device-management-kit/src/api/transport/model/TransportConnectedDevice.test.ts b/packages/device-management-kit/src/api/transport/model/TransportConnectedDevice.test.ts index ab10b0fdd..cd16085ca 100644 --- a/packages/device-management-kit/src/api/transport/model/TransportConnectedDevice.test.ts +++ b/packages/device-management-kit/src/api/transport/model/TransportConnectedDevice.test.ts @@ -32,21 +32,9 @@ describe("TransportConnectedDevice", () => { expect(connectedDevice.type).toEqual("MOCK"); }); -<<<<<<< HEAD it("should return the correct send apdu response", () => { expect(connectedDevice.sendApdu(new Uint8Array())).resolves.toMatchObject( Right(defaultApduResponseStubBuilder()), ); -||||||| parent of 61c06245 (✅ (dmk): Add tests for TransportService + fixes) - it("should return the correct send apdu response", () => { - expect(connectedDevice.sendApdu(new Uint8Array())).toMatchObject( - Promise.resolve(defaultApduResponseStubBuilder()), - ); -======= - it("should return the correct send apdu response", async () => { - const response = await connectedDevice.sendApdu(new Uint8Array()); - - expect(response).toMatchObject(Right(defaultApduResponseStubBuilder())); ->>>>>>> 61c06245 (✅ (dmk): Add tests for TransportService + fixes) }); }); diff --git a/packages/device-management-kit/src/internal/device-session/use-case/GetDeviceSessionStateUseCase.test.ts b/packages/device-management-kit/src/internal/device-session/use-case/GetDeviceSessionStateUseCase.test.ts index 908f319fd..d746c5832 100644 --- a/packages/device-management-kit/src/internal/device-session/use-case/GetDeviceSessionStateUseCase.test.ts +++ b/packages/device-management-kit/src/internal/device-session/use-case/GetDeviceSessionStateUseCase.test.ts @@ -52,17 +52,10 @@ describe("GetDeviceSessionStateUseCase", () => { const res = useCase.execute({ sessionId: fakeSessionId }); const expected = deviceSession.state; -<<<<<<< HEAD deviceSession.close(); // then - expect(response).toStrictEqual(deviceSession.state); -||||||| parent of 61c06245 (✅ (dmk): Add tests for TransportService + fixes) - // then - expect(response).toStrictEqual(deviceSession.state); -======= expect(res).toStrictEqual(expected); ->>>>>>> 61c06245 (✅ (dmk): Add tests for TransportService + fixes) }); it("should throw error when deviceSession is not found", (done) => { diff --git a/packages/device-management-kit/src/internal/discovery/use-case/ConnectUseCase.test.ts b/packages/device-management-kit/src/internal/discovery/use-case/ConnectUseCase.test.ts index 08c631a3c..18122959f 100644 --- a/packages/device-management-kit/src/internal/discovery/use-case/ConnectUseCase.test.ts +++ b/packages/device-management-kit/src/internal/discovery/use-case/ConnectUseCase.test.ts @@ -16,12 +16,13 @@ import { AxiosManagerApiDataSource } from "@internal/manager-api/data/AxiosManag import { type ManagerApiDataSource } from "@internal/manager-api/data/ManagerApiDataSource"; import { DefaultManagerApiService } from "@internal/manager-api/service/DefaultManagerApiService"; import { type ManagerApiService } from "@internal/manager-api/service/ManagerApiService"; -import { TransportService } from "@internal/transport/service/TransportService"; +import { DefaultTransportService } from "@internal/transport/service/DefaultTransportService"; +import { type TransportService } from "@internal/transport/service/TransportService"; import { ConnectUseCase } from "./ConnectUseCase"; jest.mock("@internal/manager-api/data/AxiosManagerApiDataSource"); -jest.mock("@internal/transport/service/TransportService"); +jest.mock("@internal/transport/service/DefaultTransportService"); // TODO test several transports // let transports: WebUsbHidTransport[]; @@ -53,7 +54,7 @@ describe("ConnectUseCase", () => { }); managerApi = new DefaultManagerApiService(managerApiDataSource); // @ts-expect-error mock - transportService = new TransportService(); + transportService = new DefaultTransportService(); }); afterAll(() => { diff --git a/packages/device-management-kit/src/internal/discovery/use-case/ConnectUseCase.ts b/packages/device-management-kit/src/internal/discovery/use-case/ConnectUseCase.ts index 45fb14ce4..f4aac7c31 100644 --- a/packages/device-management-kit/src/internal/discovery/use-case/ConnectUseCase.ts +++ b/packages/device-management-kit/src/internal/discovery/use-case/ConnectUseCase.ts @@ -13,7 +13,7 @@ import { loggerTypes } from "@internal/logger-publisher/di/loggerTypes"; import { managerApiTypes } from "@internal/manager-api/di/managerApiTypes"; import type { ManagerApiService } from "@internal/manager-api/service/ManagerApiService"; import { transportDiTypes } from "@internal/transport/di/transportDiTypes"; -import { TransportService } from "@internal/transport/service/TransportService"; +import { type TransportService } from "@internal/transport/service/TransportService"; /** * The arguments for the ConnectUseCase. diff --git a/packages/device-management-kit/src/internal/discovery/use-case/DisconnectUseCase.test.ts b/packages/device-management-kit/src/internal/discovery/use-case/DisconnectUseCase.test.ts index 7784eb980..d5b0aaeef 100644 --- a/packages/device-management-kit/src/internal/discovery/use-case/DisconnectUseCase.test.ts +++ b/packages/device-management-kit/src/internal/discovery/use-case/DisconnectUseCase.test.ts @@ -12,11 +12,12 @@ import { AxiosManagerApiDataSource } from "@internal/manager-api/data/AxiosManag import { type ManagerApiDataSource } from "@internal/manager-api/data/ManagerApiDataSource"; import { DefaultManagerApiService } from "@internal/manager-api/service/DefaultManagerApiService"; import { type ManagerApiService } from "@internal/manager-api/service/ManagerApiService"; -import { TransportService } from "@internal/transport/service/TransportService"; +import { DefaultTransportService } from "@internal/transport/service/DefaultTransportService"; +import { type TransportService } from "@internal/transport/service/TransportService"; import { DisconnectUseCase } from "./DisconnectUseCase"; -jest.mock("@internal/transport/service/TransportService"); +jest.mock("@internal/transport/service/DefaultTransportService"); let sessionService: DefaultDeviceSessionService; // TODO test several transports @@ -39,7 +40,7 @@ describe("DisconnectUseCase", () => { transports = [transport]; sessionService = new DefaultDeviceSessionService(loggerFactory); // @ts-expect-error mock - transportService = new TransportService(); + transportService = new DefaultTransportService(); jest .spyOn(transportService, "getTransport") .mockReturnValue(Maybe.of(transport)); diff --git a/packages/device-management-kit/src/internal/discovery/use-case/DisconnectUseCase.ts b/packages/device-management-kit/src/internal/discovery/use-case/DisconnectUseCase.ts index dc8936692..441e70af1 100644 --- a/packages/device-management-kit/src/internal/discovery/use-case/DisconnectUseCase.ts +++ b/packages/device-management-kit/src/internal/discovery/use-case/DisconnectUseCase.ts @@ -8,7 +8,7 @@ import { deviceSessionTypes } from "@internal/device-session/di/deviceSessionTyp import type { DeviceSessionService } from "@internal/device-session/service/DeviceSessionService"; import { loggerTypes } from "@internal/logger-publisher/di/loggerTypes"; import { transportDiTypes } from "@internal/transport/di/transportDiTypes"; -import { TransportService } from "@internal/transport/service/TransportService"; +import { type TransportService } from "@internal/transport/service/TransportService"; /** * The arguments for the DisconnectUseCase. diff --git a/packages/device-management-kit/src/internal/discovery/use-case/ListConnectedDevicesUseCase.test.ts b/packages/device-management-kit/src/internal/discovery/use-case/ListConnectedDevicesUseCase.test.ts index d9aeeb5f1..cbd8f99f3 100644 --- a/packages/device-management-kit/src/internal/discovery/use-case/ListConnectedDevicesUseCase.test.ts +++ b/packages/device-management-kit/src/internal/discovery/use-case/ListConnectedDevicesUseCase.test.ts @@ -57,11 +57,11 @@ describe("ListDeviceSessionsUseCase", () => { // then expect(response).toStrictEqual([ new ConnectedDevice({ - internalConnectedDevice: deviceSession1.connectedDevice, + transportConnectedDevice: deviceSession1.connectedDevice, sessionId: deviceSession1.id, }), new ConnectedDevice({ - internalConnectedDevice: deviceSession2.connectedDevice, + transportConnectedDevice: deviceSession2.connectedDevice, sessionId: deviceSession2.id, }), ]); diff --git a/packages/device-management-kit/src/internal/discovery/use-case/ListenToConnectedDeviceUseCase.test.ts b/packages/device-management-kit/src/internal/discovery/use-case/ListenToConnectedDeviceUseCase.test.ts index bf6cc2dcd..9881ecab2 100644 --- a/packages/device-management-kit/src/internal/discovery/use-case/ListenToConnectedDeviceUseCase.test.ts +++ b/packages/device-management-kit/src/internal/discovery/use-case/ListenToConnectedDeviceUseCase.test.ts @@ -1,15 +1,15 @@ +import { type LoggerPublisherService } from "@api/logger-publisher/service/LoggerPublisherService"; import { ConnectedDevice } from "@api/transport/model/ConnectedDevice"; +import { connectedDeviceStubBuilder } from "@api/transport/model/TransportConnectedDevice.stub"; import { deviceSessionStubBuilder } from "@internal/device-session/model/DeviceSession.stub"; import { DefaultDeviceSessionService } from "@internal/device-session/service/DefaultDeviceSessionService"; import { type DeviceSessionService } from "@internal/device-session/service/DeviceSessionService"; import { ListenToConnectedDeviceUseCase } from "@internal/discovery/use-case/ListenToConnectedDeviceUseCase"; import { DefaultLoggerPublisherService } from "@internal/logger-publisher/service/DefaultLoggerPublisherService"; -import { type LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService"; import { AxiosManagerApiDataSource } from "@internal/manager-api/data/AxiosManagerApiDataSource"; import { type ManagerApiDataSource } from "@internal/manager-api/data/ManagerApiDataSource"; import { DefaultManagerApiService } from "@internal/manager-api/service/DefaultManagerApiService"; import { type ManagerApiService } from "@internal/manager-api/service/ManagerApiService"; -import { connectedDeviceStubBuilder } from "@internal/transport/model/InternalConnectedDevice.stub"; jest.mock("@internal/manager-api/data/AxiosManagerApiDataSource"); @@ -54,7 +54,7 @@ describe("ListenToConnectedDevice", () => { // then expect(emittedConnectedDevice).toEqual( new ConnectedDevice({ - internalConnectedDevice: connectedDevice, + transportConnectedDevice: connectedDevice, sessionId: fakeSessionId, }), ); diff --git a/packages/device-management-kit/src/internal/discovery/use-case/ListenToConnectedDeviceUseCase.ts b/packages/device-management-kit/src/internal/discovery/use-case/ListenToConnectedDeviceUseCase.ts index 082f4e489..d8999065e 100644 --- a/packages/device-management-kit/src/internal/discovery/use-case/ListenToConnectedDeviceUseCase.ts +++ b/packages/device-management-kit/src/internal/discovery/use-case/ListenToConnectedDeviceUseCase.ts @@ -1,10 +1,10 @@ import { inject, injectable } from "inversify"; import { map, Observable } from "rxjs"; +import { LoggerPublisherService } from "@api/logger-publisher/service/LoggerPublisherService"; import { deviceSessionTypes } from "@internal/device-session/di/deviceSessionTypes"; import type { DeviceSessionService } from "@internal/device-session/service/DeviceSessionService"; import { loggerTypes } from "@internal/logger-publisher/di/loggerTypes"; -import { LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService"; import { ConnectedDevice } from "@root/src"; /** @@ -29,7 +29,7 @@ export class ListenToConnectedDeviceUseCase { map( (deviceSession) => new ConnectedDevice({ - internalConnectedDevice: deviceSession.connectedDevice, + transportConnectedDevice: deviceSession.connectedDevice, sessionId: deviceSession.id, }), ), diff --git a/packages/device-management-kit/src/internal/discovery/use-case/ListenToKnownDevicesUseCase.test.ts b/packages/device-management-kit/src/internal/discovery/use-case/ListenToKnownDevicesUseCase.test.ts index b2e688922..6092d0f24 100644 --- a/packages/device-management-kit/src/internal/discovery/use-case/ListenToKnownDevicesUseCase.test.ts +++ b/packages/device-management-kit/src/internal/discovery/use-case/ListenToKnownDevicesUseCase.test.ts @@ -4,11 +4,12 @@ import { type DeviceId, type DeviceModel } from "@api/device/DeviceModel"; import { deviceModelStubBuilder } from "@api/device-model/model/DeviceModel.stub"; import { type TransportDiscoveredDevice } from "@api/transport/model/TransportDiscoveredDevice"; import { type DiscoveredDevice, type Transport } from "@api/types"; -import { TransportService } from "@internal/transport/service/TransportService"; +import { DefaultTransportService } from "@internal/transport/service/DefaultTransportService"; +import { type TransportService } from "@internal/transport/service/TransportService"; import { ListenToKnownDevicesUseCase } from "./ListenToKnownDevicesUseCase"; -jest.mock("@internal/transport/service/TransportService"); +jest.mock("@internal/transport/service/DefaultTransportService"); let transportService: TransportService; function makeMockTransport(props: Partial): Transport { @@ -68,7 +69,7 @@ describe("ListenToKnownDevicesUseCase", () => { beforeEach(() => { jest.clearAllMocks(); // @ts-expect-error mock - transportService = new TransportService(); + transportService = new DefaultTransportService(); }); describe("when no transports are available", () => { diff --git a/packages/device-management-kit/src/internal/discovery/use-case/ListenToKnownDevicesUseCase.ts b/packages/device-management-kit/src/internal/discovery/use-case/ListenToKnownDevicesUseCase.ts index 7f3980140..e577ca8a7 100644 --- a/packages/device-management-kit/src/internal/discovery/use-case/ListenToKnownDevicesUseCase.ts +++ b/packages/device-management-kit/src/internal/discovery/use-case/ListenToKnownDevicesUseCase.ts @@ -6,7 +6,7 @@ import type { Transport } from "@api/transport/model/Transport"; import { TransportDiscoveredDevice } from "@api/transport/model/TransportDiscoveredDevice"; import { DiscoveredDevice } from "@api/types"; import { transportDiTypes } from "@internal/transport/di/transportDiTypes"; -import { TransportService } from "@internal/transport/service/TransportService"; +import { type TransportService } from "@internal/transport/service/TransportService"; /** * Listen to list of known discovered devices (and later BLE). diff --git a/packages/device-management-kit/src/internal/discovery/use-case/StartDiscoveringUseCase.test.ts b/packages/device-management-kit/src/internal/discovery/use-case/StartDiscoveringUseCase.test.ts index 882d52ec8..ac7bbfdd9 100644 --- a/packages/device-management-kit/src/internal/discovery/use-case/StartDiscoveringUseCase.test.ts +++ b/packages/device-management-kit/src/internal/discovery/use-case/StartDiscoveringUseCase.test.ts @@ -10,11 +10,12 @@ import { type DiscoveredDevice, type Transport, } from "@api/types"; -import { TransportService } from "@internal/transport/service/TransportService"; +import { DefaultTransportService } from "@internal/transport/service/DefaultTransportService"; +import { type TransportService } from "@internal/transport/service/TransportService"; import { StartDiscoveringUseCase } from "./StartDiscoveringUseCase"; -jest.mock("@internal/transport/service/TransportService"); +jest.mock("@internal/transport/service/DefaultTransportService"); let transport: Transport; let transportService: TransportService; @@ -32,7 +33,7 @@ describe("StartDiscoveringUseCase", () => { beforeEach(() => { transport = new TransportMock(); // @ts-expect-error mock - transportService = new TransportService(); + transportService = new DefaultTransportService(); }); afterEach(() => { diff --git a/packages/device-management-kit/src/internal/discovery/use-case/StartDiscoveringUseCase.ts b/packages/device-management-kit/src/internal/discovery/use-case/StartDiscoveringUseCase.ts index 892dbc7de..42e773628 100644 --- a/packages/device-management-kit/src/internal/discovery/use-case/StartDiscoveringUseCase.ts +++ b/packages/device-management-kit/src/internal/discovery/use-case/StartDiscoveringUseCase.ts @@ -7,7 +7,7 @@ import { TransportNotSupportedError } from "@api/transport/model/Errors"; import { TransportDiscoveredDevice } from "@api/transport/model/TransportDiscoveredDevice"; import { TransportIdentifier } from "@api/transport/model/TransportIdentifier"; import { transportDiTypes } from "@internal/transport/di/transportDiTypes"; -import { TransportService } from "@internal/transport/service/TransportService"; +import { type TransportService } from "@internal/transport/service/TransportService"; export type StartDiscoveringUseCaseArgs = { /** diff --git a/packages/device-management-kit/src/internal/discovery/use-case/StopDiscoveringUseCase.test.ts b/packages/device-management-kit/src/internal/discovery/use-case/StopDiscoveringUseCase.test.ts index 2b19abace..e1ea0d611 100644 --- a/packages/device-management-kit/src/internal/discovery/use-case/StopDiscoveringUseCase.test.ts +++ b/packages/device-management-kit/src/internal/discovery/use-case/StopDiscoveringUseCase.test.ts @@ -1,10 +1,11 @@ import { TransportMock } from "@api/transport/model/__mocks__/TransportMock"; import { type Transport } from "@api/types"; -import { TransportService } from "@internal/transport/service/TransportService"; +import { DefaultTransportService } from "@internal/transport/service/DefaultTransportService"; +import { type TransportService } from "@internal/transport/service/TransportService"; import { StopDiscoveringUseCase } from "./StopDiscoveringUseCase"; -jest.mock("@internal/transport/service/TransportService"); +jest.mock("@internal/transport/service/DefaultTransportService"); // TODO test several transports let transport: Transport; @@ -16,7 +17,7 @@ describe("StopDiscoveringUseCase", () => { transport = new TransportMock(); transports = [transport]; // @ts-expect-error mock - transportService = new TransportService(transports); + transportService = new DefaultTransportService(transports); }); afterEach(() => { diff --git a/packages/device-management-kit/src/internal/discovery/use-case/StopDiscoveringUseCase.ts b/packages/device-management-kit/src/internal/discovery/use-case/StopDiscoveringUseCase.ts index 31001e25a..383b3a274 100644 --- a/packages/device-management-kit/src/internal/discovery/use-case/StopDiscoveringUseCase.ts +++ b/packages/device-management-kit/src/internal/discovery/use-case/StopDiscoveringUseCase.ts @@ -1,7 +1,7 @@ import { inject, injectable } from "inversify"; import { transportDiTypes } from "@internal/transport/di/transportDiTypes"; -import { TransportService } from "@internal/transport/service/TransportService"; +import { type TransportService } from "@internal/transport/service/TransportService"; /** * Stops discovering devices connected. diff --git a/packages/device-management-kit/src/internal/transport/di/transportModule.ts b/packages/device-management-kit/src/internal/transport/di/transportModule.ts index 3d4a491ba..77b8f9ec1 100644 --- a/packages/device-management-kit/src/internal/transport/di/transportModule.ts +++ b/packages/device-management-kit/src/internal/transport/di/transportModule.ts @@ -2,7 +2,7 @@ import { ContainerModule } from "inversify"; import { type DmkConfig } from "@api/DmkConfig"; import { type TransportFactory } from "@api/transport/model/Transport"; -import { TransportService } from "@internal/transport/service/TransportService"; +import { DefaultTransportService } from "@internal/transport/service/DefaultTransportService"; import { transportDiTypes } from "./transportDiTypes"; @@ -20,7 +20,7 @@ export const transportModuleFactory = ({ new ContainerModule((bind, _unbind, _isBound, _rebind) => { bind(transportDiTypes.TransportsInput).toConstantValue(transports); bind(transportDiTypes.TransportService) - .to(TransportService) + .to(DefaultTransportService) .inSingletonScope(); bind(transportDiTypes.DmkConfig).toConstantValue(config); diff --git a/packages/device-management-kit/src/internal/transport/service/TransportService.test.ts b/packages/device-management-kit/src/internal/transport/service/DefaultTransportService.test.ts similarity index 92% rename from packages/device-management-kit/src/internal/transport/service/TransportService.test.ts rename to packages/device-management-kit/src/internal/transport/service/DefaultTransportService.test.ts index 87493ca16..3f64b2322 100644 --- a/packages/device-management-kit/src/internal/transport/service/TransportService.test.ts +++ b/packages/device-management-kit/src/internal/transport/service/DefaultTransportService.test.ts @@ -19,7 +19,7 @@ import { } from "@api/transport/model/Transport"; import { DefaultLoggerPublisherService } from "@internal/logger-publisher/service/DefaultLoggerPublisherService"; -import { TransportService } from "./TransportService"; +import { DefaultTransportService } from "./DefaultTransportService"; let apduSenderService: ApduSenderServiceFactory; let apduReceiverService: ApduReceiverServiceFactory; @@ -30,7 +30,7 @@ let transport: Transport; let transportFactory2: TransportFactory; let transport2: Transport; let deviceModelDataSource: DeviceModelDataSource; -let transportService: TransportService; +let transportService: DefaultTransportService; let loggerFactory: () => LoggerPublisherService; describe("TransportService", () => { @@ -66,7 +66,7 @@ describe("TransportService", () => { it("should throw an error", () => { try { - transportService = new TransportService( + transportService = new DefaultTransportService( [], config, loggerFactory, @@ -85,7 +85,7 @@ describe("TransportService", () => { describe("when transports are provided", () => { it("should add 1 transport", () => { - transportService = new TransportService( + transportService = new DefaultTransportService( [transportFactory], config, loggerFactory, @@ -98,7 +98,7 @@ describe("TransportService", () => { }); it("should add multiple transports", () => { - transportService = new TransportService( + transportService = new DefaultTransportService( [transportFactory, transportFactory2], config, loggerFactory, @@ -113,7 +113,7 @@ describe("TransportService", () => { it("should not add duplicate transports", () => { try { - new TransportService( + new DefaultTransportService( [transportFactory, transportFactory], config, loggerFactory, @@ -130,7 +130,7 @@ describe("TransportService", () => { describe("addTransport", () => { beforeEach(() => { - transportService = new TransportService( + transportService = new DefaultTransportService( [transportFactory], config, loggerFactory, @@ -170,7 +170,7 @@ describe("TransportService", () => { describe("getTransport", () => { beforeEach(() => { - transportService = new TransportService( + transportService = new DefaultTransportService( [transportFactory], config, loggerFactory, @@ -193,7 +193,7 @@ describe("TransportService", () => { describe("getAllTransports", () => { it("returns all transports (1 transport)", () => { - transportService = new TransportService( + transportService = new DefaultTransportService( [transportFactory], config, loggerFactory, @@ -206,7 +206,7 @@ describe("TransportService", () => { }); it("returns all transports (2 transports)", () => { - transportService = new TransportService( + transportService = new DefaultTransportService( [transportFactory, transportFactory2], config, loggerFactory, diff --git a/packages/device-management-kit/src/internal/transport/service/DefaultTransportService.ts b/packages/device-management-kit/src/internal/transport/service/DefaultTransportService.ts new file mode 100644 index 000000000..7109d957a --- /dev/null +++ b/packages/device-management-kit/src/internal/transport/service/DefaultTransportService.ts @@ -0,0 +1,106 @@ +import { inject, injectable } from "inversify"; +import { Either, Left, Maybe, Right } from "purify-ts"; + +import { type DeviceModelDataSource } from "@api/device-model/data/DeviceModelDataSource"; +import { type ApduReceiverServiceFactory } from "@api/device-session/service/ApduReceiverService"; +import { type ApduSenderServiceFactory } from "@api/device-session/service/ApduSenderService"; +import { type DmkConfig } from "@api/DmkConfig"; +import { LoggerPublisherService } from "@api/logger-publisher/service/LoggerPublisherService"; +import { + NoTransportProvidedError, + TransportAlreadyExistsError, +} from "@api/transport/model/Errors"; +import { TransportFactory } from "@api/transport/model/Transport"; +import { Transport } from "@api/types"; +import { deviceModelTypes } from "@internal/device-model/di/deviceModelTypes"; +import { deviceSessionTypes } from "@internal/device-session/di/deviceSessionTypes"; +import { loggerTypes } from "@internal/logger-publisher/di/loggerTypes"; +import { transportDiTypes } from "@internal/transport/di/transportDiTypes"; + +import { TransportService } from "./TransportService"; + +@injectable() +export class DefaultTransportService implements TransportService { + private _transports: Map = new Map(); + private _logger: LoggerPublisherService; + + constructor( + @inject(transportDiTypes.TransportsInput) + _transports: TransportFactory[], + @inject(transportDiTypes.DmkConfig) + private readonly _config: DmkConfig, + @inject(loggerTypes.LoggerPublisherServiceFactory) + private readonly _loggerModuleFactory: ( + tag: string, + ) => LoggerPublisherService, + @inject(deviceModelTypes.DeviceModelDataSource) + private readonly _deviceModelDataSource: DeviceModelDataSource, + @inject(deviceSessionTypes.ApduSenderServiceFactory) + private readonly _apduSenderServiceFactory: ApduSenderServiceFactory, + @inject(deviceSessionTypes.ApduReceiverServiceFactory) + private readonly _apduReceiverServiceFactory: ApduReceiverServiceFactory, + ) { + this._logger = _loggerModuleFactory("TransportService"); + + if (_transports.length === 0) { + this._logger.warn( + "No transports provided, please check your configuration", + ); + + throw new NoTransportProvidedError( + "No transports provided, please check your configuration", + ); + } + + for (const transport of _transports) { + const either = this.addTransport(transport); + + if (either.isLeft()) { + throw either.extract(); + } + } + } + + addTransport( + factory: TransportFactory, + ): Either { + const transport = factory({ + deviceModelDataSource: this._deviceModelDataSource, + loggerServiceFactory: this._loggerModuleFactory, + config: this._config, + apduSenderServiceFactory: this._apduSenderServiceFactory, + apduReceiverServiceFactory: this._apduReceiverServiceFactory, + }); + + return this.addTransportInternal(transport); + } + + private addTransportInternal( + transport: Transport, + ): Either { + const MaybeTransport = this.getTransport(transport.getIdentifier()); + + if (MaybeTransport.isJust()) { + this._logger.warn( + `Transport ${transport.getIdentifier()} already exists, please check your configuration`, + ); + + return Left( + new TransportAlreadyExistsError( + `Transport ${transport.getIdentifier()} already exists, please check your configuration`, + ), + ); + } + + this._transports.set(transport.getIdentifier(), transport); + return Right(undefined); + } + + getTransport(identifier: string): Maybe { + return Maybe.fromNullable(this._transports.get(identifier)); + } + + getAllTransports(): Transport[] { + return Array.from(this._transports.values()); + } +} diff --git a/packages/device-management-kit/src/internal/transport/service/TransportService.ts b/packages/device-management-kit/src/internal/transport/service/TransportService.ts index e4ef01595..d1d43f517 100644 --- a/packages/device-management-kit/src/internal/transport/service/TransportService.ts +++ b/packages/device-management-kit/src/internal/transport/service/TransportService.ts @@ -1,104 +1,12 @@ -import { inject, injectable } from "inversify"; -import { Either, Left, Maybe, Right } from "purify-ts"; +import { type Either, type Maybe } from "purify-ts"; -import { type DeviceModelDataSource } from "@api/device-model/data/DeviceModelDataSource"; -import { type ApduReceiverServiceFactory } from "@api/device-session/service/ApduReceiverService"; -import { type ApduSenderServiceFactory } from "@api/device-session/service/ApduSenderService"; -import { type DmkConfig } from "@api/DmkConfig"; -import { LoggerPublisherService } from "@api/logger-publisher/service/LoggerPublisherService"; -import { - NoTransportProvidedError, - TransportAlreadyExistsError, -} from "@api/transport/model/Errors"; -import { TransportFactory } from "@api/transport/model/Transport"; -import { Transport } from "@api/types"; -import { deviceModelTypes } from "@internal/device-model/di/deviceModelTypes"; -import { deviceSessionTypes } from "@internal/device-session/di/deviceSessionTypes"; -import { loggerTypes } from "@internal/logger-publisher/di/loggerTypes"; -import { transportDiTypes } from "@internal/transport/di/transportDiTypes"; - -@injectable() -export class TransportService { - private _transports: Map = new Map(); - private _logger: LoggerPublisherService; - - constructor( - @inject(transportDiTypes.TransportsInput) - _transports: TransportFactory[], - @inject(transportDiTypes.DmkConfig) - private readonly _config: DmkConfig, - @inject(loggerTypes.LoggerPublisherServiceFactory) - private readonly _loggerModuleFactory: ( - tag: string, - ) => LoggerPublisherService, - @inject(deviceModelTypes.DeviceModelDataSource) - private readonly _deviceModelDataSource: DeviceModelDataSource, - @inject(deviceSessionTypes.ApduSenderServiceFactory) - private readonly _apduSenderServiceFactory: ApduSenderServiceFactory, - @inject(deviceSessionTypes.ApduReceiverServiceFactory) - private readonly _apduReceiverServiceFactory: ApduReceiverServiceFactory, - ) { - this._logger = _loggerModuleFactory("TransportService"); - - if (_transports.length === 0) { - this._logger.warn( - "No transports provided, please check your configuration", - ); - - throw new NoTransportProvidedError( - "No transports provided, please check your configuration", - ); - } - - for (const transport of _transports) { - const either = this.addTransport(transport); - - if (either.isLeft()) { - throw either.extract(); - } - } - } +import { type TransportAlreadyExistsError } from "@api/index"; +import { type Transport, type TransportFactory } from "@api/types"; +export interface TransportService { + getTransport(transportId: string): Maybe; + getAllTransports(): Transport[]; addTransport( factory: TransportFactory, - ): Either { - const transport = factory({ - deviceModelDataSource: this._deviceModelDataSource, - loggerServiceFactory: this._loggerModuleFactory, - config: this._config, - apduSenderServiceFactory: this._apduSenderServiceFactory, - apduReceiverServiceFactory: this._apduReceiverServiceFactory, - }); - - return this.addTransportInternal(transport); - } - - private addTransportInternal( - transport: Transport, - ): Either { - const MaybeTransport = this.getTransport(transport.getIdentifier()); - - if (MaybeTransport.isJust()) { - this._logger.warn( - `Transport ${transport.getIdentifier()} already exists, please check your configuration`, - ); - - return Left( - new TransportAlreadyExistsError( - `Transport ${transport.getIdentifier()} already exists, please check your configuration`, - ), - ); - } - - this._transports.set(transport.getIdentifier(), transport); - return Right(undefined); - } - - getTransport(identifier: string): Maybe { - return Maybe.fromNullable(this._transports.get(identifier)); - } - - getAllTransports(): Transport[] { - return Array.from(this._transports.values()); - } + ): Either; } diff --git a/packages/device-management-kit/src/internal/transport/service/__mocks__/TransportService.ts b/packages/device-management-kit/src/internal/transport/service/__mocks__/DefaultTransportService.ts similarity index 84% rename from packages/device-management-kit/src/internal/transport/service/__mocks__/TransportService.ts rename to packages/device-management-kit/src/internal/transport/service/__mocks__/DefaultTransportService.ts index 4493ea093..62de2682a 100644 --- a/packages/device-management-kit/src/internal/transport/service/__mocks__/TransportService.ts +++ b/packages/device-management-kit/src/internal/transport/service/__mocks__/DefaultTransportService.ts @@ -2,10 +2,11 @@ import { type DeviceModelDataSource } from "@api/device-model/data/DeviceModelDa import { type DmkConfig } from "@api/DmkConfig"; import { type Transport } from "@api/types"; import { DefaultLoggerPublisherServiceStub } from "@internal/logger-publisher/service/DefaultLoggerPublisherService.stub"; +import { type TransportService } from "@internal/transport/service/TransportService"; const loggerFactory = (_arg: string) => new DefaultLoggerPublisherServiceStub(); -export class TransportService { +export class DefaultTransportService implements TransportService { _transports: Map = new Map(); _loggerModuleFactory = loggerFactory; _config = {} as DmkConfig; diff --git a/turbo.json b/turbo.json index 10ee890bb..0e318c110 100644 --- a/turbo.json +++ b/turbo.json @@ -8,11 +8,11 @@ "inputs": ["src/**/*.ts", "index.ts"] }, "lint": { - "dependsOn": ["build"], + "dependsOn": ["^build"], "outputs": [] }, "lint:fix": { - "dependsOn": ["build"], + "dependsOn": ["^build"], "outputs": [] }, "prettier": { @@ -42,7 +42,7 @@ "outputs": [] }, "health-check": { - "dependsOn": ["^build", "^prettier", "^lint", "^typecheck"] + "dependsOn": ["build", "prettier", "lint", "typecheck"] } } }