"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const customLockScreenLoad_1 = __importDefault(require("./customLockScreenLoad"));
const devices_1 = require("@ledgerhq/devices");
const rxjs_1 = require("rxjs");
const errors_1 = require("@ledgerhq/errors");
const errors_2 = require("../errors");
const mockTransport = {
    send: jest.fn(),
    getTraceContext: jest.fn(),
};
jest.mock("../deviceSDK/transports/core", () => ({
    withTransport: () => callback => callback({ transportRef: { current: mockTransport } }),
}));
jest.mock("./getDeviceInfo", () => jest.fn(() => (0, rxjs_1.of)([])));
describe("customLockScreenLoad", () => {
    it("should load image on device", async () => {
        // given
        const request = {
            deviceModelId: devices_1.DeviceModelId.stax,
            hexImage: "hello_world",
        };
        mockTransport.send.mockResolvedValue(Buffer.from([0x42, 0x42, 0x43, 0x90, 0x00]));
        // when
        const ret = await (0, rxjs_1.lastValueFrom)(await (0, customLockScreenLoad_1.default)({ deviceId: "deviceId", request }));
        // then
        expect(mockTransport.send).toHaveBeenNthCalledWith(1, 0xe0, 0x60, 0x00, 0x00, Buffer.from([0x00, 0x00, 0x00, 0x08]), [errors_1.StatusCodes.NOT_ENOUGH_SPACE, errors_1.StatusCodes.USER_REFUSED_ON_DEVICE, errors_1.StatusCodes.OK]);
        expect(mockTransport.send).toHaveBeenNthCalledWith(2, 0xe0, 0x61, 0x00, 0x00, Buffer.from([0x00, 0x00, 0x00, 0x00, 0x90, 0x01, 0xa0, 0x02, 0x21, 0x00, 0x00, 0x00]));
        expect(mockTransport.send).toHaveBeenNthCalledWith(3, 0xe0, 0x62, 0x00, 0x00, Buffer.from([]), [0x9000, 0x5501]);
        expect(ret).toStrictEqual({
            type: "imageLoaded",
            imageSize: 1111638928,
            imageHash: "424243",
        });
    });
    it.each([
        [
            "user refused on device",
            [0x55, 0x01],
            new errors_2.ImageLoadRefusedOnDevice("5501", { productName: "Ledger Stax" }),
        ],
        ["not enough space", [0x51, 0x02], new errors_1.ManagerNotEnoughSpaceError()],
        ["unexpected error", [0x42, 0x32], new errors_1.TransportError("Unexpected device response", "4232")],
    ])("should return an error if %s", async (_errorStr, statusCode, error) => {
        // given
        const request = {
            deviceModelId: devices_1.DeviceModelId.stax,
            hexImage: "hello_world",
        };
        mockTransport.send.mockResolvedValue(Buffer.from(statusCode));
        // when
        try {
            await (0, rxjs_1.lastValueFrom)(await (0, customLockScreenLoad_1.default)({ deviceId: "nanoX", request }));
        }
        catch (err) {
            expect(err).toStrictEqual(error);
        }
    });
});
//# sourceMappingURL=customLockScreenLoad.test.js.map