"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// SPDX-License-Identifier: Apache-2.0
const ts_jest_1 = require("@golevelup/ts-jest");
const _1 = require("@faker-js/faker/.");
const TakeSnapshotCommand_1 = require("./TakeSnapshotCommand");
const TakeSnapshotCommandHandler_1 = require("./TakeSnapshotCommandHandler");
const DataFixture_1 = require("../../../../../../../../__tests__/fixtures/shared/DataFixture");
const EvmAddress_1 = __importDefault(require("../../../../../../../domain/context/contract/EvmAddress"));
const AccountFixture_1 = require("../../../../../../../../__tests__/fixtures/account/AccountFixture");
const Account_1 = __importDefault(require("../../../../../../../domain/context/account/Account"));
const SnapshotFixture_1 = require("../../../../../../../../__tests__/fixtures/snapshot/SnapshotFixture");
const TakeSnapshotCommandError_1 = require("./error/TakeSnapshotCommandError");
const BaseError_1 = require("../../../../../../../core/error/BaseError");
const SecurityRole_1 = require("../../../../../../../domain/context/security/SecurityRole");
describe("TakeSnapshotCommandHandler", () => {
    let handler;
    let command;
    const transactionServiceMock = (0, ts_jest_1.createMock)();
    const validationServiceMock = (0, ts_jest_1.createMock)();
    const accountServiceMock = (0, ts_jest_1.createMock)();
    const contractServiceMock = (0, ts_jest_1.createMock)();
    const evmAddress = new EvmAddress_1.default(DataFixture_1.EvmAddressPropsFixture.create().value);
    const transactionId = DataFixture_1.TransactionIdFixture.create().id;
    const errorMsg = DataFixture_1.ErrorMsgFixture.create().msg;
    const account = new Account_1.default(AccountFixture_1.AccountPropsFixture.create());
    const snapshotId = _1.faker.string.hexadecimal({
        length: 64,
        prefix: "0x",
    });
    beforeEach(() => {
        handler = new TakeSnapshotCommandHandler_1.TakeSnapshotCommandHandler(accountServiceMock, contractServiceMock, transactionServiceMock, validationServiceMock);
        command = SnapshotFixture_1.TakeSnapshotCommandFixture.create();
    });
    afterEach(() => {
        jest.resetAllMocks();
    });
    describe("execute", () => {
        describe("error cases", () => {
            it("throws TakeSnapshotCommandError when command fails with uncaught error", async () => {
                const fakeError = new Error(errorMsg);
                contractServiceMock.getContractEvmAddress.mockRejectedValue(fakeError);
                const resultPromise = handler.execute(command);
                await expect(resultPromise).rejects.toBeInstanceOf(TakeSnapshotCommandError_1.TakeSnapshotCommandError);
                await expect(resultPromise).rejects.toMatchObject({
                    message: expect.stringContaining(`An error occurred while taking snapshot: ${errorMsg}`),
                    errorCode: BaseError_1.ErrorCode.UncaughtCommandError,
                });
            });
        });
        describe("success cases", () => {
            it("should successfully take snapshot", async () => {
                contractServiceMock.getContractEvmAddress.mockResolvedValue(evmAddress);
                validationServiceMock.checkPause.mockResolvedValue(undefined);
                validationServiceMock.checkRole.mockResolvedValue(undefined);
                accountServiceMock.getCurrentAccount.mockReturnValue(account);
                transactionServiceMock.getHandler().takeSnapshot.mockResolvedValue({
                    id: transactionId,
                });
                transactionServiceMock.getTransactionResult.mockResolvedValue(snapshotId);
                const result = await handler.execute(command);
                expect(result).toBeInstanceOf(TakeSnapshotCommand_1.TakeSnapshotCommandResponse);
                expect(result.payload).toBe(parseInt(snapshotId, 16));
                expect(result.transactionId).toBe(transactionId);
                expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledTimes(1);
                expect(contractServiceMock.getContractEvmAddress).toHaveBeenCalledWith(command.securityId);
                expect(validationServiceMock.checkPause).toHaveBeenCalledTimes(1);
                expect(validationServiceMock.checkPause).toHaveBeenCalledWith(command.securityId);
                expect(validationServiceMock.checkRole).toHaveBeenCalledTimes(1);
                expect(validationServiceMock.checkRole).toHaveBeenCalledWith(SecurityRole_1.SecurityRole._SNAPSHOT_ROLE, account.id.toString(), command.securityId);
                expect(transactionServiceMock.getHandler().takeSnapshot).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getHandler().takeSnapshot).toHaveBeenCalledWith(evmAddress, command.securityId);
                expect(transactionServiceMock.getTransactionResult).toHaveBeenCalledTimes(1);
                expect(transactionServiceMock.getTransactionResult).toHaveBeenCalledWith({
                    res: { id: transactionId },
                    className: TakeSnapshotCommandHandler_1.TakeSnapshotCommandHandler.name,
                    position: 0,
                    numberOfResultsItems: 1,
                });
            });
        });
    });
});
