"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const hw_transport_mocker_1 = require("@ledgerhq/hw-transport-mocker");
const _1 = require(".");
const errors_1 = require("@ledgerhq/errors");
jest.useFakeTimers();
describe("open", () => {
    describe("When no transport working with the device has been registered", () => {
        it("should reject with a CantOpenDevice error", async () => {
            (0, _1.registerTransportModule)({
                id: "test_0",
                open: (_id, _timeoutMs) => {
                    // Handles no device
                    return null;
                },
                disconnect: (_id) => {
                    return Promise.resolve();
                },
            });
            const openPromise = (0, _1.open)("device_0");
            await expect(openPromise).rejects.toBeInstanceOf(errors_1.CantOpenDevice);
        });
    });
    describe("When a transport working with the device has been registered and no timeout is reached", () => {
        it("should return the associated opened Transport instance", async () => {
            (0, _1.registerTransportModule)({
                id: "test_1",
                open: (id, _timeoutMs) => {
                    // Filters on this current test
                    if (id !== "device_1")
                        return null;
                    return Promise.resolve((0, hw_transport_mocker_1.aTransportBuilder)());
                },
                disconnect: (_id) => {
                    return Promise.resolve();
                },
            });
            const openPromise = (0, _1.open)("device_1");
            await expect(openPromise).resolves.toBeTruthy();
        });
    });
    describe("When the open timeout is reached", () => {
        it("should reject with an error on timeout", async () => {
            (0, _1.registerTransportModule)({
                id: "test_2",
                open: (id, timeoutMs) => {
                    // Filters on this current test
                    if (id !== "device_2")
                        return null;
                    return new Promise(resolve => {
                        // The Transport is created (too late) 100ms after the timeout
                        setTimeout(() => resolve((0, hw_transport_mocker_1.aTransportBuilder)()), timeoutMs ? timeoutMs + 100 : 0);
                    });
                },
                disconnect: (_id) => {
                    return Promise.resolve();
                },
            });
            const timeoutMs = 1000;
            const openPromise = (0, _1.open)("device_2", timeoutMs);
            jest.advanceTimersByTime(timeoutMs);
            await expect(openPromise).rejects.toBeInstanceOf(errors_1.CantOpenDevice);
        });
        test("And the Transport/module implementation timeouts before open, it should still reject with an error", async () => {
            (0, _1.registerTransportModule)({
                id: "test_3",
                open: (id, timeoutMs) => {
                    // Filters on this current test
                    if (id !== "device_3")
                        return null;
                    return new Promise((_resolve, reject) => {
                        // Times out before `open`
                        // Rejects with another kind of error to differentiate with CantOpenDevice
                        setTimeout(() => reject(new errors_1.TransportError("", "")), timeoutMs ? timeoutMs - 200 : 0);
                    });
                },
                disconnect: (_id) => {
                    return Promise.resolve();
                },
            });
            const timeoutMs = 1000;
            const openPromise = (0, _1.open)("device_3", timeoutMs);
            // Advances time after the implementation timeout but before the `open` timeout
            jest.advanceTimersByTime(timeoutMs - 100);
            await expect(openPromise).rejects.toBeInstanceOf(errors_1.TransportError);
        });
    });
});
//# sourceMappingURL=index.test.js.map