"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const types_devices_1 = require("@ledgerhq/types-devices");
const getBatteryStatus_1 = require("../../hw/getBatteryStatus");
const getBatteryStatus_2 = require("./getBatteryStatus");
const rxjs_1 = require("rxjs");
const mockTransportGenerator = out => ({
    send: () => new Promise(res => res(out)),
    on: () => { },
    off: () => { },
    setExchangeUnresponsiveTimeout: () => { },
});
describe("getBatteryStatus", () => {
    test("battery percentage OK", async () => {
        const statusType = getBatteryStatus_1.BatteryStatusTypes.BATTERY_PERCENTAGE;
        const transport = mockTransportGenerator(Buffer.from("639000", "hex"));
        const response = await (0, rxjs_1.firstValueFrom)((0, getBatteryStatus_2.getBatteryStatus)({
            transport,
            statusType,
        }));
        if (response.type === "data") {
            expect(response.batteryStatus).toEqual(99);
        }
        else {
            fail("Incorrect response event");
        }
    });
    test("battery percentage KO returns -1", async () => {
        const statusType = getBatteryStatus_1.BatteryStatusTypes.BATTERY_PERCENTAGE;
        const transport = mockTransportGenerator(Buffer.from("FF9000", "hex"));
        const response = await (0, rxjs_1.firstValueFrom)((0, getBatteryStatus_2.getBatteryStatus)({
            transport,
            statusType,
        }));
        if (response.type === "data") {
            expect(response.batteryStatus).toEqual(-1);
        }
        else {
            fail("Incorrect response event");
        }
    });
    test("battery voltage resolves", async () => {
        const statusType = getBatteryStatus_1.BatteryStatusTypes.BATTERY_VOLTAGE;
        const transport = mockTransportGenerator(Buffer.from("0FFF9000", "hex"));
        const response = await (0, rxjs_1.firstValueFrom)((0, getBatteryStatus_2.getBatteryStatus)({
            transport,
            statusType,
        }));
        if (response.type === "data") {
            expect(response.batteryStatus).toEqual(4095);
        }
        else {
            fail("Incorrect response event");
        }
    });
    test("battery temperature with positive values", async () => {
        const statusType = getBatteryStatus_1.BatteryStatusTypes.BATTERY_TEMPERATURE;
        const transport = mockTransportGenerator(Buffer.from("109000", "hex"));
        const response = await (0, rxjs_1.firstValueFrom)((0, getBatteryStatus_2.getBatteryStatus)({
            transport,
            statusType,
        }));
        if (response.type === "data") {
            expect(response.batteryStatus).toEqual(16);
        }
        else {
            fail("Incorrect response event");
        }
    });
    test("battery temperature with negative values", async () => {
        const statusType = getBatteryStatus_1.BatteryStatusTypes.BATTERY_TEMPERATURE;
        const transport = mockTransportGenerator(Buffer.from("FD9000", "hex"));
        const response = await (0, rxjs_1.firstValueFrom)((0, getBatteryStatus_2.getBatteryStatus)({
            transport,
            statusType,
        }));
        if (response.type === "data") {
            expect(response.batteryStatus).toEqual(-3);
        }
        else {
            fail("Incorrect response event");
        }
    });
    test("battery current with positive values", async () => {
        const statusType = getBatteryStatus_1.BatteryStatusTypes.BATTERY_CURRENT;
        const transport = mockTransportGenerator(Buffer.from("109000", "hex"));
        const response = await (0, rxjs_1.firstValueFrom)((0, getBatteryStatus_2.getBatteryStatus)({
            transport,
            statusType,
        }));
        if (response.type === "data") {
            expect(response.batteryStatus).toEqual(16);
        }
        else {
            fail("Incorrect response event");
        }
    });
    test("battery current with negative values", async () => {
        const statusType = getBatteryStatus_1.BatteryStatusTypes.BATTERY_CURRENT;
        const transport = mockTransportGenerator(Buffer.from("FD9000", "hex"));
        const response = await (0, rxjs_1.firstValueFrom)((0, getBatteryStatus_2.getBatteryStatus)({
            transport,
            statusType,
        }));
        if (response.type === "data") {
            expect(response.batteryStatus).toEqual(-3);
        }
        else {
            fail("Incorrect response event");
        }
    });
    test("battery flags for USB charging", async () => {
        const statusType = getBatteryStatus_1.BatteryStatusTypes.BATTERY_FLAGS;
        const transport = mockTransportGenerator(Buffer.from("0000000F9000", "hex"));
        const response = await (0, rxjs_1.firstValueFrom)((0, getBatteryStatus_2.getBatteryStatus)({
            transport,
            statusType,
        }));
        if (response.type === "data") {
            expect(response.batteryStatus).toEqual({
                charging: types_devices_1.ChargingModes.USB,
                issueCharging: false,
                issueTemperature: false,
                issueBattery: false,
            });
        }
        else {
            fail("Incorrect response event");
        }
    });
    test("battery flags for not charging", async () => {
        const statusType = getBatteryStatus_1.BatteryStatusTypes.BATTERY_FLAGS;
        const transport = mockTransportGenerator(Buffer.from("000000069000", "hex"));
        const response = await (0, rxjs_1.firstValueFrom)((0, getBatteryStatus_2.getBatteryStatus)({
            transport,
            statusType,
        }));
        if (response.type === "data") {
            expect(response.batteryStatus).toEqual({
                charging: types_devices_1.ChargingModes.NONE,
                issueCharging: false,
                issueTemperature: false,
                issueBattery: false,
            });
        }
        else {
            fail("Incorrect response event");
        }
    });
    test("battery flags for Qi charging without USB", async () => {
        const statusType = getBatteryStatus_1.BatteryStatusTypes.BATTERY_FLAGS;
        const transport = mockTransportGenerator(Buffer.from("000000079000", "hex"));
        const response = await (0, rxjs_1.firstValueFrom)((0, getBatteryStatus_2.getBatteryStatus)({
            transport,
            statusType,
        }));
        if (response.type === "data") {
            expect(response.batteryStatus).toEqual({
                charging: types_devices_1.ChargingModes.QI,
                issueCharging: false,
                issueTemperature: false,
                issueBattery: false,
            });
        }
        else {
            fail("Incorrect response event");
        }
    });
});
//# sourceMappingURL=getBatteryStatus.test.js.map