Skip to content

Commit

Permalink
♻️ (dmk): TransportService interface + DefaultTransportService implem
Browse files Browse the repository at this point in the history
  • Loading branch information
valpinkman committed Nov 15, 2024
1 parent b2e0e60 commit 0859764
Show file tree
Hide file tree
Showing 22 changed files with 163 additions and 161 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -53,7 +54,7 @@ describe("ConnectUseCase", () => {
});
managerApi = new DefaultManagerApiService(managerApiDataSource);
// @ts-expect-error mock
transportService = new TransportService();
transportService = new DefaultTransportService();
});

afterAll(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -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");

Expand Down Expand Up @@ -54,7 +54,7 @@ describe("ListenToConnectedDevice", () => {
// then
expect(emittedConnectedDevice).toEqual(
new ConnectedDevice({
internalConnectedDevice: connectedDevice,
transportConnectedDevice: connectedDevice,
sessionId: fakeSessionId,
}),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand All @@ -29,7 +29,7 @@ export class ListenToConnectedDeviceUseCase {
map(
(deviceSession) =>
new ConnectedDevice({
internalConnectedDevice: deviceSession.connectedDevice,
transportConnectedDevice: deviceSession.connectedDevice,
sessionId: deviceSession.id,
}),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>): Transport {
Expand Down Expand Up @@ -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", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +33,7 @@ describe("StartDiscoveringUseCase", () => {
beforeEach(() => {
transport = new TransportMock();
// @ts-expect-error mock
transportService = new TransportService();
transportService = new DefaultTransportService();
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,7 +17,7 @@ describe("StopDiscoveringUseCase", () => {
transport = new TransportMock();
transports = [transport];
// @ts-expect-error mock
transportService = new TransportService(transports);
transportService = new DefaultTransportService(transports);
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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", () => {
Expand Down Expand Up @@ -66,7 +66,7 @@ describe("TransportService", () => {

it("should throw an error", () => {
try {
transportService = new TransportService(
transportService = new DefaultTransportService(
[],
config,
loggerFactory,
Expand All @@ -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,
Expand All @@ -98,7 +98,7 @@ describe("TransportService", () => {
});

it("should add multiple transports", () => {
transportService = new TransportService(
transportService = new DefaultTransportService(
[transportFactory, transportFactory2],
config,
loggerFactory,
Expand All @@ -113,7 +113,7 @@ describe("TransportService", () => {

it("should not add duplicate transports", () => {
try {
new TransportService(
new DefaultTransportService(
[transportFactory, transportFactory],
config,
loggerFactory,
Expand All @@ -130,7 +130,7 @@ describe("TransportService", () => {

describe("addTransport", () => {
beforeEach(() => {
transportService = new TransportService(
transportService = new DefaultTransportService(
[transportFactory],
config,
loggerFactory,
Expand Down Expand Up @@ -170,7 +170,7 @@ describe("TransportService", () => {

describe("getTransport", () => {
beforeEach(() => {
transportService = new TransportService(
transportService = new DefaultTransportService(
[transportFactory],
config,
loggerFactory,
Expand All @@ -193,7 +193,7 @@ describe("TransportService", () => {

describe("getAllTransports", () => {
it("returns all transports (1 transport)", () => {
transportService = new TransportService(
transportService = new DefaultTransportService(
[transportFactory],
config,
loggerFactory,
Expand All @@ -206,7 +206,7 @@ describe("TransportService", () => {
});

it("returns all transports (2 transports)", () => {
transportService = new TransportService(
transportService = new DefaultTransportService(
[transportFactory, transportFactory2],
config,
loggerFactory,
Expand Down
Loading

0 comments on commit 0859764

Please sign in to comment.