"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const getBatteryStatus_1 = __importStar(require("./getBatteryStatus"));
const types_devices_1 = require("@ledgerhq/types-devices");
const mockTransportGenerator = out => ({ send: () => out });
describe("getBatteryStatus", () => {
    test("battery percentage OK", async () => {
        const p2 = getBatteryStatus_1.BatteryStatusTypes.BATTERY_PERCENTAGE;
        const mockedTransport = mockTransportGenerator(Buffer.from("639000", "hex"));
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore next-line
        const response = await (0, getBatteryStatus_1.default)(mockedTransport, [p2]);
        expect(response).toEqual([99]);
    });
    test("battery percentage KO returns -1", async () => {
        const p2 = getBatteryStatus_1.BatteryStatusTypes.BATTERY_PERCENTAGE;
        const mockedTransport = mockTransportGenerator(Buffer.from("FF9000", "hex"));
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore next-line
        const response = await (0, getBatteryStatus_1.default)(mockedTransport, [p2]);
        expect(response).toEqual([-1]);
    });
    test("battery voltage resolves", async () => {
        const p2 = getBatteryStatus_1.BatteryStatusTypes.BATTERY_VOLTAGE;
        const mockedTransport = mockTransportGenerator(Buffer.from("0FFF9000", "hex"));
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore next-line
        const response = await (0, getBatteryStatus_1.default)(mockedTransport, [p2]);
        expect(response).toEqual([4095]);
    });
    test("battery temperature with positive values", async () => {
        const p2 = getBatteryStatus_1.BatteryStatusTypes.BATTERY_TEMPERATURE;
        const mockedTransport = mockTransportGenerator(Buffer.from("109000", "hex"));
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore next-line
        const response = await (0, getBatteryStatus_1.default)(mockedTransport, [p2]);
        expect(response).toEqual([16]);
    });
    test("battery temperature with negative values", async () => {
        const p2 = getBatteryStatus_1.BatteryStatusTypes.BATTERY_TEMPERATURE;
        const mockedTransport = mockTransportGenerator(Buffer.from("FD9000", "hex"));
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore next-line
        const response = await (0, getBatteryStatus_1.default)(mockedTransport, [p2]);
        expect(response).toEqual([-3]);
    });
    test("battery current with positive values", async () => {
        const p2 = getBatteryStatus_1.BatteryStatusTypes.BATTERY_CURRENT;
        const mockedTransport = mockTransportGenerator(Buffer.from("109000", "hex"));
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore next-line
        const response = await (0, getBatteryStatus_1.default)(mockedTransport, [p2]);
        expect(response).toEqual([16]);
    });
    test("battery current with negative values", async () => {
        const p2 = getBatteryStatus_1.BatteryStatusTypes.BATTERY_CURRENT;
        const mockedTransport = mockTransportGenerator(Buffer.from("FD9000", "hex"));
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore next-line
        const response = await (0, getBatteryStatus_1.default)(mockedTransport, [p2]);
        expect(response).toEqual([-3]);
    });
    test("battery flags for USB charging", async () => {
        const mockedTransport = mockTransportGenerator(Buffer.from("0000000F9000", "hex"));
        const p2 = getBatteryStatus_1.BatteryStatusTypes.BATTERY_FLAGS;
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore next-line
        const response = await (0, getBatteryStatus_1.default)(mockedTransport, [p2]);
        expect(response).toEqual([
            {
                charging: types_devices_1.ChargingModes.USB,
                issueCharging: false,
                issueTemperature: false,
                issueBattery: false,
            },
        ]);
    });
    test("battery flags for not charging", async () => {
        const mockedTransport = mockTransportGenerator(Buffer.from("000000069000", "hex"));
        const p2 = getBatteryStatus_1.BatteryStatusTypes.BATTERY_FLAGS;
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore next-line
        const response = await (0, getBatteryStatus_1.default)(mockedTransport, [p2]);
        expect(response).toEqual([
            {
                charging: types_devices_1.ChargingModes.NONE,
                issueCharging: false,
                issueTemperature: false,
                issueBattery: false,
            },
        ]);
    });
    test("battery flags for Qi charging without USB", async () => {
        const mockedTransport = mockTransportGenerator(Buffer.from("000000079000", "hex"));
        const p2 = getBatteryStatus_1.BatteryStatusTypes.BATTERY_FLAGS;
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore next-line
        const response = await (0, getBatteryStatus_1.default)(mockedTransport, [p2]);
        expect(response).toEqual([
            {
                charging: types_devices_1.ChargingModes.QI,
                issueCharging: false,
                issueTemperature: false,
                issueBattery: false,
            },
        ]);
    });
    test("battery flags for Qi charging with USB plugged in", async () => {
        const mockedTransport = mockTransportGenerator(Buffer.from("0000000F9000", "hex"));
        const p2 = getBatteryStatus_1.BatteryStatusTypes.BATTERY_FLAGS;
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore next-line
        const response = await (0, getBatteryStatus_1.default)(mockedTransport, [p2]);
        expect(response).toEqual([
            {
                charging: types_devices_1.ChargingModes.USB,
                issueCharging: false,
                issueTemperature: false,
                issueBattery: false,
            },
        ]);
    });
    test("multiple statuses: percentage + battery flags for Qi charging with USB plugged in", async () => {
        let calls = 0;
        const multipleStatusMockedTransport = {
            send: () => {
                if (calls === 0) {
                    calls++;
                    return Buffer.from("0000000F9000", "hex");
                }
                return Buffer.from("639000", "hex");
            },
        };
        const percentage = getBatteryStatus_1.BatteryStatusTypes.BATTERY_PERCENTAGE;
        const flags = getBatteryStatus_1.BatteryStatusTypes.BATTERY_FLAGS;
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore next-line
        const response = await (0, getBatteryStatus_1.default)(multipleStatusMockedTransport, [flags, percentage]);
        expect(response).toEqual([
            {
                charging: types_devices_1.ChargingModes.USB,
                issueCharging: false,
                issueTemperature: false,
                issueBattery: false,
            },
            99,
        ]);
    });
});
//# sourceMappingURL=getBatteryStatus.test.js.map