Skip to content

Commit

Permalink
✨ (dmk): Get mananger API service from internal API
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyuzhuang committed Dec 20, 2024
1 parent d978fdc commit 01bdd5b
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-jobs-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/device-management-kit": patch
---

Get manager API service from internal API
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type InternalApi = {
readonly setDeviceSessionState: (
state: DeviceSessionState,
) => DeviceSessionState;
getMetadataForAppHashes: ManagerApiService["getAppsByHash"];
readonly getManagerApiService: () => ManagerApiService;
};

export type DeviceActionIntermediateValue = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ const sendCommandMock = jest.fn();
const apiGetDeviceSessionStateMock = jest.fn();
const apiGetDeviceSessionStateObservableMock = jest.fn();
const setDeviceSessionStateMock = jest.fn();
const getMetadataForAppHashesMock = jest.fn();
const getManagerApiServiceMock = jest.fn();

export function makeDeviceActionInternalApiMock(): jest.Mocked<InternalApi> {
return {
sendCommand: sendCommandMock,
getDeviceSessionState: apiGetDeviceSessionStateMock,
getDeviceSessionStateObservable: apiGetDeviceSessionStateObservableMock,
setDeviceSessionState: setDeviceSessionStateMock,
getMetadataForAppHashes: getMetadataForAppHashesMock,
getManagerApiService: getManagerApiServiceMock,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ import { UserInteractionRequired } from "@api/device-action/model/UserInteractio
import { UnknownDAError } from "@api/device-action/os/Errors";
import { DeviceSessionStateType } from "@api/device-session/DeviceSessionState";
import { HttpFetchApiError } from "@internal/manager-api/model/Errors";
import { type ManagerApiService } from "@internal/manager-api/service/ManagerApiService";

import { ListAppsWithMetadataDeviceAction } from "./ListAppsWithMetadataDeviceAction";
import { type ListAppsWithMetadataDAState } from "./types";

jest.mock("@api/device-action/os/ListApps/ListAppsDeviceAction");

describe("ListAppsWithMetadataDeviceAction", () => {
const {
getMetadataForAppHashes: getMetadataForAppHashesMock,
// getDeviceSessionState: apiGetDeviceSessionStateMock,
// setDeviceSessionState: apiSetDeviceSessionStateMock,
} = makeDeviceActionInternalApiMock();
const { getManagerApiService: getManagerApiServiceMock } =
makeDeviceActionInternalApiMock();

const saveSessionStateMock = jest.fn();
const getDeviceSessionStateMock = jest.fn();
Expand All @@ -54,7 +52,9 @@ describe("ListAppsWithMetadataDeviceAction", () => {
input: {},
});

getMetadataForAppHashesMock.mockResolvedValue(Right([]));
getManagerApiServiceMock.mockReturnValue({
getAppsByHash: jest.fn().mockResolvedValue(Right([])),
} as unknown as ManagerApiService);

const expectedStates: Array<ListAppsWithMetadataDAState> = [
{
Expand Down Expand Up @@ -90,7 +90,9 @@ describe("ListAppsWithMetadataDeviceAction", () => {
input: {},
});

getMetadataForAppHashesMock.mockResolvedValue(Right([BTC_APP_METADATA]));
getManagerApiServiceMock.mockReturnValue({
getAppsByHash: jest.fn().mockResolvedValue(Right([BTC_APP_METADATA])),
} as unknown as ManagerApiService);

const expectedStates: Array<ListAppsWithMetadataDAState> = [
{
Expand Down Expand Up @@ -138,9 +140,11 @@ describe("ListAppsWithMetadataDeviceAction", () => {
input: {},
});

getMetadataForAppHashesMock.mockResolvedValue(
Right([BTC_APP_METADATA, ETH_APP_METADATA]),
);
getManagerApiServiceMock.mockReturnValue({
getAppsByHash: jest
.fn()
.mockResolvedValue(Right([BTC_APP_METADATA, ETH_APP_METADATA])),
} as unknown as ManagerApiService);

const expectedStates: Array<ListAppsWithMetadataDAState> = [
{
Expand Down Expand Up @@ -188,9 +192,16 @@ describe("ListAppsWithMetadataDeviceAction", () => {
input: {},
});

getMetadataForAppHashesMock.mockResolvedValue(
Right([BTC_APP_METADATA, CUSTOM_LOCK_SCREEN_APP_METADATA]),
);
// getMetadataForAppHashesMock.mockResolvedValue(
// Right([BTC_APP_METADATA, CUSTOM_LOCK_SCREEN_APP_METADATA]),
// );
getManagerApiServiceMock.mockReturnValue({
getAppsByHash: jest
.fn()
.mockResolvedValue(
Right([BTC_APP_METADATA, CUSTOM_LOCK_SCREEN_APP_METADATA]),
),
} as unknown as ManagerApiService);

const expectedStates: Array<ListAppsWithMetadataDAState> = [
{
Expand Down Expand Up @@ -274,9 +285,14 @@ describe("ListAppsWithMetadataDeviceAction", () => {
input: {},
});

getMetadataForAppHashesMock.mockRejectedValue(
new UnknownDAError("getAppsByHash failed"),
);
// getMetadataForAppHashesMock.mockRejectedValue(
// new UnknownDAError("getAppsByHash failed"),
// );
getManagerApiServiceMock.mockReturnValue({
getAppsByHash: jest
.fn()
.mockRejectedValue(new UnknownDAError("getAppsByHash failed")),
} as unknown as ManagerApiService);

const expectedStates: Array<ListAppsWithMetadataDAState> = [
{
Expand Down Expand Up @@ -320,7 +336,10 @@ describe("ListAppsWithMetadataDeviceAction", () => {

const error = new HttpFetchApiError(new Error("Failed to fetch data"));

getMetadataForAppHashesMock.mockResolvedValue(Left(error));
// getMetadataForAppHashesMock.mockResolvedValue(Left(error));
getManagerApiServiceMock.mockReturnValue({
getAppsByHash: jest.fn().mockResolvedValue(Left(error)),
} as unknown as ManagerApiService);

const expectedStates: Array<ListAppsWithMetadataDAState> = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ export class ListAppsWithMetadataDeviceAction extends XStateDeviceAction<

extractDependencies(internalApi: InternalApi): MachineDependencies {
return {
getAppsByHash: ({ input }) => internalApi.getMetadataForAppHashes(input),
getAppsByHash: ({ input }) =>
internalApi.getManagerApiService().getAppsByHash(input),
getDeviceSessionState: () => internalApi.getDeviceSessionState(),
saveSessionState: (state: DeviceSessionState) =>
internalApi.setDeviceSessionState(state),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { v4 as uuidv4 } from "uuid";

import { type Command } from "@api/command/Command";
import { type CommandResult } from "@api/command/model/CommandResult";
import { type ListAppsResponse } from "@api/command/os/ListAppsCommand";
import { CommandUtils } from "@api/command/utils/CommandUtils";
import { DeviceStatus } from "@api/device/DeviceStatus";
import {
Expand Down Expand Up @@ -165,8 +164,7 @@ export class DeviceSession {
this.setDeviceSessionState(state);
return this._deviceState.getValue();
},
getMetadataForAppHashes: (apps: ListAppsResponse) =>
this._managerApiService.getAppsByHash(apps),
getManagerApiService: () => this._managerApiService,
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import { type FinalFirmware } from "@internal/manager-api/model/Firmware";
import { ManagerApiDataSource } from "./ManagerApiDataSource";
import { ApplicationDto, AppTypeDto } from "./ManagerApiDto";

export type GenuineCheckParams = {
targetId: string;
perso: string;
};

@injectable()
export class AxiosManagerApiDataSource implements ManagerApiDataSource {
private readonly baseUrl: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,12 @@ export class HttpFetchApiError implements DmkError {
this.originalError = error;
}
}

export class WebSocketError implements DmkError {
_tag = "WebSocketError";
originalError?: unknown;

constructor(public readonly error: unknown) {
this.originalError = error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ const sendCommandMock = jest.fn();
const apiGetDeviceSessionStateMock = jest.fn();
const apiGetDeviceSessionStateObservableMock = jest.fn();
const setDeviceSessionStateMock = jest.fn();
const getMetadataForAppHashesMock = jest.fn();
const getManagerApiServiceMock = jest.fn();

export function makeDeviceActionInternalApiMock(): jest.Mocked<InternalApi> {
return {
sendCommand: sendCommandMock,
getDeviceSessionState: apiGetDeviceSessionStateMock,
getDeviceSessionStateObservable: apiGetDeviceSessionStateObservableMock,
setDeviceSessionState: setDeviceSessionStateMock,
getMetadataForAppHashes: getMetadataForAppHashesMock,
getManagerApiService: getManagerApiServiceMock,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ const sendCommandMock = jest.fn();
const apiGetDeviceSessionStateMock = jest.fn();
const apiGetDeviceSessionStateObservableMock = jest.fn();
const setDeviceSessionStateMock = jest.fn();
const getMetadataForAppHashesMock = jest.fn();
const getManagerApiServiceMock = jest.fn();

export function makeDeviceActionInternalApiMock(): jest.Mocked<InternalApi> {
return {
sendCommand: sendCommandMock,
getDeviceSessionState: apiGetDeviceSessionStateMock,
getDeviceSessionStateObservable: apiGetDeviceSessionStateObservableMock,
setDeviceSessionState: setDeviceSessionStateMock,
getMetadataForAppHashes: getMetadataForAppHashesMock,
getManagerApiService: getManagerApiServiceMock,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ const sendCommandMock = jest.fn();
const apiGetDeviceSessionStateMock = jest.fn();
const apiGetDeviceSessionStateObservableMock = jest.fn();
const setDeviceSessionStateMock = jest.fn();
const getMetadataForAppHashesMock = jest.fn();
const getManagerApiServiceMock = jest.fn();

export function makeDeviceActionInternalApiMock(): jest.Mocked<InternalApi> {
return {
sendCommand: sendCommandMock,
getDeviceSessionState: apiGetDeviceSessionStateMock,
getDeviceSessionStateObservable: apiGetDeviceSessionStateObservableMock,
setDeviceSessionState: setDeviceSessionStateMock,
getMetadataForAppHashes: getMetadataForAppHashesMock,
getManagerApiService: getManagerApiServiceMock,
};
}

0 comments on commit 01bdd5b

Please sign in to comment.