"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const sinon_1 = __importDefault(require("sinon"));
const claimClassicTicker_1 = require("../claimClassicTicker");
const mocks_1 = require("../../../testUtils/mocks");
const constants_1 = require("../../../utils/constants");
const utilsConversionModule = __importStar(require("../../../utils/conversion"));
jest.mock('~/api/entities/TickerReservation', require("../../../testUtils/mocks/entities").mockTickerReservationModule('~/api/entities/TickerReservation'));
describe('claimClassicTicker procedure', () => {
    let mockContext;
    let ticker;
    let ethereumSignature;
    let ethereumAddress;
    let did;
    let rawTicker;
    let rawEthereumSignature;
    let addTransactionStub;
    let stringToTickerStub;
    let stringToEcdsaSignatureStub;
    beforeAll(() => {
        did = '0x0600000000000000000000000000000000000000000000000000000000000000';
        mocks_1.dsMockUtils.initMocks({ contextOptions: { did } });
        mocks_1.procedureMockUtils.initMocks();
        mocks_1.entityMockUtils.initMocks();
        ticker = 'SOME_TICKER';
        ethereumSignature =
            '0xce3f19a1aaaf04adfcd16fbc19ffd5d2d3117588a543dac2ff6befd2c6320b7c6ec586df0ec276775b6f53cb2dfc69f78efc2ef974e04cb269fe2e5d9546ea791c';
        ethereumAddress = '0xb710e549e2ac26ad77c3acc2bb7f6bb48d7e7c7f';
        stringToTickerStub = sinon_1.default.stub(utilsConversionModule, 'stringToTicker');
        stringToEcdsaSignatureStub = sinon_1.default.stub(utilsConversionModule, 'stringToEcdsaSignature');
    });
    beforeEach(() => {
        mockContext = mocks_1.dsMockUtils.getContextInstance();
        addTransactionStub = mocks_1.procedureMockUtils.getAddTransactionStub();
        rawTicker = mocks_1.dsMockUtils.createMockTicker(ticker);
        rawEthereumSignature = mocks_1.dsMockUtils.createMockEcdsaSignature(ethereumSignature);
        mocks_1.dsMockUtils.createQueryStub('asset', 'tickers', {
            returnValue: mocks_1.dsMockUtils.createMockTickerRegistration({
                owner: constants_1.CLASSIC_TICKER_OWNER_DID,
                expiry: null,
            }),
        });
        mocks_1.dsMockUtils.createQueryStub('asset', 'classicTickers', {
            returnValue: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockClassicTickerRegistration({
                /* eslint-disable @typescript-eslint/naming-convention */
                eth_owner: ethereumAddress,
                is_created: false,
                /* eslint-enable @typescript-eslint/naming-convention */
            })),
        });
        stringToTickerStub.withArgs(ticker, mockContext).returns(rawTicker);
        stringToEcdsaSignatureStub.withArgs(ethereumSignature).returns(rawEthereumSignature);
    });
    afterEach(() => {
        mocks_1.entityMockUtils.reset();
        mocks_1.procedureMockUtils.reset();
        mocks_1.dsMockUtils.reset();
    });
    afterAll(() => {
        mocks_1.entityMockUtils.cleanup();
        mocks_1.procedureMockUtils.cleanup();
        mocks_1.dsMockUtils.cleanup();
    });
    test('should throw an error if the ticker is not in the reserved classic ticker list', () => {
        mocks_1.dsMockUtils.createQueryStub('asset', 'classicTickers', {
            returnValue: mocks_1.dsMockUtils.createMockOption(),
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(claimClassicTicker_1.prepareClaimClassicTicker.call(proc, { ticker, ethereumSignature })).rejects.toThrow('The supplied ticker is not in the reserved classic ticker list');
    });
    test('should throw an error if the ticker reservation has expired', () => {
        mocks_1.dsMockUtils.createQueryStub('asset', 'tickers', {
            returnValue: mocks_1.dsMockUtils.createMockTickerRegistration({
                expiry: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockMoment(new Date('10/14/1987').getTime())),
                owner: constants_1.CLASSIC_TICKER_OWNER_DID,
            }),
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(claimClassicTicker_1.prepareClaimClassicTicker.call(proc, { ticker, ethereumSignature })).rejects.toThrow("The Ticker's claiming period has already expired");
    });
    test('should throw an error if the ticker has already been claimed', () => {
        mocks_1.dsMockUtils.createQueryStub('asset', 'tickers', {
            returnValue: mocks_1.dsMockUtils.createMockTickerRegistration({
                expiry: null,
                owner: 'someDid',
            }),
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        return expect(claimClassicTicker_1.prepareClaimClassicTicker.call(proc, { ticker, ethereumSignature })).rejects.toThrow('Ticker already claimed');
    });
    test('should throw an error if the ethereum signature is not valid', () => __awaiter(void 0, void 0, void 0, function* () {
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        let error;
        try {
            yield claimClassicTicker_1.prepareClaimClassicTicker.call(proc, { ticker, ethereumSignature: 'somethingInvalid' });
        }
        catch (err) {
            error = err;
        }
        expect(error.message).toBe('Ethereum signature not valid');
        expect(error.data).toEqual({
            error: 'Invalid signature length',
        });
    }));
    test('should throw an error if the ethereum address that owns the ticker is not the same one that signed', () => __awaiter(void 0, void 0, void 0, function* () {
        mocks_1.dsMockUtils.createQueryStub('asset', 'classicTickers', {
            returnValue: mocks_1.dsMockUtils.createMockOption(mocks_1.dsMockUtils.createMockClassicTickerRegistration({
                /* eslint-disable @typescript-eslint/naming-convention */
                eth_owner: 'notYou',
                is_created: false,
                /* eslint-enable @typescript-eslint/naming-convention */
            })),
        });
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        let error;
        try {
            yield claimClassicTicker_1.prepareClaimClassicTicker.call(proc, { ticker, ethereumSignature });
        }
        catch (err) {
            error = err;
        }
        expect(error.message).toBe('The Account that signed the message is not the classic Ticker owner');
        expect(error.data).toEqual({
            signerAddress: ethereumAddress,
        });
    }));
    test('should add a claim classic ticker transaction to the queue', () => __awaiter(void 0, void 0, void 0, function* () {
        const transaction = mocks_1.dsMockUtils.createTxStub('asset', 'claimClassicTicker');
        const proc = mocks_1.procedureMockUtils.getInstance(mockContext);
        yield claimClassicTicker_1.prepareClaimClassicTicker.call(proc, { ticker, ethereumSignature });
        sinon_1.default.assert.calledWith(addTransactionStub, {
            transaction,
            args: [rawTicker, rawEthereumSignature],
        });
    }));
});
//# sourceMappingURL=claimClassicTicker.js.map