Skip to content

Commit

Permalink
✅ (core): Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jdabbech-ledger committed Oct 18, 2024
1 parent 82460bf commit dd61a4e
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DeviceModelId } from "@api/device/DeviceModel";
import { BleDeviceInfos } from "@internal/transport/ble/model/BleDeviceInfos";

import { StaticDeviceModelDataSource } from "./StaticDeviceModelDataSource";

Expand Down Expand Up @@ -149,4 +150,50 @@ describe("StaticDeviceModelDataSource", () => {
expect(deviceModels3.length).toEqual(0);
});
});
describe("getBluetoothServicesInfos", () => {
it("should return the ble service infos record", () => {
// given
const dataSource = new StaticDeviceModelDataSource();
// when
const bleServiceInfos = dataSource.getBluetoothServicesInfos();
// then
expect(bleServiceInfos).toStrictEqual({
"13d63400-2c97-0004-0000-4c6564676572": new BleDeviceInfos(
dataSource.getDeviceModel({ id: DeviceModelId.NANO_X }),
"13d63400-2c97-0004-0000-4c6564676572",
"13d63400-2c97-0004-0002-4c6564676572",
"13d63400-2c97-0004-0003-4c6564676572",
"13d63400-2c97-0004-0001-4c6564676572",
),
"13d63400-2c97-6004-0000-4c6564676572": new BleDeviceInfos(
dataSource.getDeviceModel({ id: DeviceModelId.STAX }),
"13d63400-2c97-6004-0000-4c6564676572",
"13d63400-2c97-6004-0002-4c6564676572",
"13d63400-2c97-6004-0003-4c6564676572",
"13d63400-2c97-6004-0001-4c6564676572",
),
"13d63400-2c97-3004-0000-4c6564676572": new BleDeviceInfos(
dataSource.getDeviceModel({ id: DeviceModelId.FLEX }),
"13d63400-2c97-3004-0000-4c6564676572",
"13d63400-2c97-3004-0002-4c6564676572",
"13d63400-2c97-3004-0003-4c6564676572",
"13d63400-2c97-3004-0001-4c6564676572",
),
});
});
});
describe("getBluetoothServices", () => {
it("should return the bluetooth services", () => {
// given
const dataSource = new StaticDeviceModelDataSource();
// when
const bleServices = dataSource.getBluetoothServices();
// then
expect(bleServices).toStrictEqual([
"13d63400-2c97-0004-0000-4c6564676572",
"13d63400-2c97-6004-0000-4c6564676572",
"13d63400-2c97-3004-0000-4c6564676572",
]);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,11 @@ export class StaticDeviceModelDataSource implements DeviceModelDataSource {
}

getBluetoothServices(): string[] {
return Object.values(StaticDeviceModelDataSource.deviceModelByIds).reduce<
string[]
>((acc, deviceModel) => {
const { bluetoothSpec } = deviceModel;

if (bluetoothSpec) {
return acc.concat(bluetoothSpec.map((spec) => spec.serviceUuid));
}
return acc;
}, []);
return Object.values(StaticDeviceModelDataSource.deviceModelByIds)
.map((deviceModel) =>
(deviceModel.bluetoothSpec || []).map((spec) => spec.serviceUuid),
)
.flat()
.filter((uuid) => !!uuid);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { deviceSessionStubBuilder } from "@internal/device-session/model/DeviceSession.stub";
import { DefaultDeviceSessionService } from "@internal/device-session/service/DefaultDeviceSessionService";
import { DeviceSessionService } from "@internal/device-session/service/DeviceSessionService";
import { CloseSessionsUseCase } from "@internal/device-session/use-case/CloseSessionsUseCase";
import { DefaultLoggerPublisherService } from "@internal/logger-publisher/service/DefaultLoggerPublisherService";
import { LoggerPublisherService } from "@internal/logger-publisher/service/LoggerPublisherService";
import { AxiosManagerApiDataSource } from "@internal/manager-api/data/AxiosManagerApiDataSource";
import { ManagerApiDataSource } from "@internal/manager-api/data/ManagerApiDataSource";
import { DefaultManagerApiService } from "@internal/manager-api/service/DefaultManagerApiService";
import { ManagerApiService } from "@internal/manager-api/service/ManagerApiService";

let logger: LoggerPublisherService;
let managerApiDataSource: ManagerApiDataSource;
let managerApi: ManagerApiService;
let sessionService: DeviceSessionService;

describe("CloseSessionsUseState", () => {
beforeEach(() => {
logger = new DefaultLoggerPublisherService(
[],
"close-sessions-use-case-test",
);
managerApiDataSource = new AxiosManagerApiDataSource({
managerApiUrl: "http://fake.url",
mockUrl: "http://fake-mock.url",
});
managerApi = new DefaultManagerApiService(managerApiDataSource);
sessionService = new DefaultDeviceSessionService(() => logger);
});

it("should be able to close every session", () => {
//given
const sessions = [...Array(10).keys()].map((id) => {
const session = deviceSessionStubBuilder(
{ id: id.toString() },
() => logger,
managerApi,
);
jest.spyOn(session, "close");
return session;
});
sessions.forEach((session) => sessionService.addDeviceSession(session));
const useCase = new CloseSessionsUseCase(sessionService);
//when
useCase.execute();
//then
sessions.forEach((session) => {
expect(session.close).toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ describe("BleDeviceConnection", () => {
const response = connection.sendApdu(new Uint8Array([]));
receiveApdu(connection, EMPTY_APDU_RESPONSE);
// then
expect(
writeCharacteristic.writeValueWithoutResponse,
).toHaveBeenCalledTimes(1);
expect(await response).toStrictEqual(
Right(
new ApduResponse({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* istanbul ignore file */
// pragma to ignore this file from coverage
import {
CommandResponse,
Device,
Expand Down Expand Up @@ -94,9 +96,6 @@ export class MockTransport implements Transport {
const sessionId: string = params.deviceId;
try {
const session: Session = await this.mockClient.connect(sessionId);
this.logger.debug("connected device model id::", {
data: { session, sessionId },
});
const connectedDevice = {
sendApdu: (apdu) => {
return this.sendApdu(
Expand Down

0 comments on commit dd61a4e

Please sign in to comment.