"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("rxjs");
const errors_1 = require("@ledgerhq/errors");
const toggleOnboardingEarlyCheck_1 = require("./toggleOnboardingEarlyCheck");
const toggleOnboardingEarlyCheck_2 = require("../commands/toggleOnboardingEarlyCheck");
const core_1 = require("../transports/core");
const aTransportRef_1 = require("../mocks/aTransportRef");
jest.useFakeTimers();
jest.mock("../commands/toggleOnboardingEarlyCheck");
const mockedToggleOnboardingEarlyCheckCmd = jest.mocked(toggleOnboardingEarlyCheck_2.toggleOnboardingEarlyCheckCmd);
jest.mock("../transports/core");
const mockedWithTransport = jest.mocked(core_1.withTransport);
describe("@deviceSDK/tasks/toggleOnboardingEarlyCheckTask", () => {
    beforeAll(async () => {
        const transportRef = await (0, aTransportRef_1.aTransportRefBuilder)();
        mockedWithTransport.mockReturnValue(job => (0, rxjs_1.from)(job({ transportRef })));
    });
    describe("When the device is in the expected onboarding state", () => {
        it("should emit a success event after being able to enter or exit the onboarding early checks step successfully", done => {
            mockedToggleOnboardingEarlyCheckCmd.mockReturnValue((0, rxjs_1.of)({ type: "success" }));
            // `enter` and `exit` have the same behavior
            (0, toggleOnboardingEarlyCheck_1.toggleOnboardingEarlyCheckTask)({
                deviceId: "",
                toggleType: "exit",
            }).subscribe({
                next: event => {
                    expect(event.type).toBe("success");
                    done();
                },
                error: (error) => {
                    done(`No error should have been thrown: ${JSON.stringify(error)}`);
                },
            });
        });
    });
    describe("When the device is neither in the WELCOME and WELCOME_STEP2 onboarding state, and it returns 0x6982", () => {
        it("should emit a task error event with the correct error type", done => {
            mockedToggleOnboardingEarlyCheckCmd.mockReturnValue((0, rxjs_1.throwError)(() => new errors_1.TransportStatusError(errors_1.StatusCodes.SECURITY_STATUS_NOT_SATISFIED)));
            (0, toggleOnboardingEarlyCheck_1.toggleOnboardingEarlyCheckTask)({
                deviceId: "",
                toggleType: "enter",
            }).subscribe({
                next: event => {
                    try {
                        if (event.type === "taskError") {
                            expect(event.error).toBe("DeviceInInvalidState");
                            done();
                        }
                        else {
                            done(`The event is incorrect: ${JSON.stringify(event)}`);
                        }
                    }
                    catch (expectError) {
                        done(expectError);
                    }
                },
                error: (error) => {
                    done(`The error should have been mapped to an event, not thrown: ${JSON.stringify(error)}`);
                },
            });
        });
    });
    describe("When the command is not respected the expected APDU format", () => {
        it("should emit a task error event with the correct error type", done => {
            mockedToggleOnboardingEarlyCheckCmd.mockReturnValue((0, rxjs_1.throwError)(() => new errors_1.TransportStatusError(errors_1.StatusCodes.INCORRECT_LENGTH)));
            (0, toggleOnboardingEarlyCheck_1.toggleOnboardingEarlyCheckTask)({
                deviceId: "",
                toggleType: "enter",
            }).subscribe({
                next: event => {
                    try {
                        if (event.type === "taskError") {
                            expect(event.error).toBe("InternalError");
                            done();
                        }
                        else {
                            done(`The event is incorrect: ${JSON.stringify(event)}`);
                        }
                    }
                    catch (expectError) {
                        done(expectError);
                    }
                },
                error: (error) => {
                    done(`The error should have been mapped to an event, not thrown: ${JSON.stringify(error)}`);
                },
            });
        });
    });
});
//# sourceMappingURL=toggleOnboardingEarlyCheck.test.js.map