Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ (dmk) [NO-ISSUE]: Get manager API service from internal API #574

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,13 @@ describe("ListAppsWithMetadataDeviceAction", () => {
input: {},
});

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 +282,11 @@ describe("ListAppsWithMetadataDeviceAction", () => {
input: {},
});

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 +330,9 @@ describe("ListAppsWithMetadataDeviceAction", () => {

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

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 @@ -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,
};
}