"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const sinon_1 = __importDefault(require("sinon"));
const moralis_1 = require("../../../src/providers/chain-state/external/adapters/moralis");
const alchemy_1 = require("../../../src/providers/chain-state/external/adapters/alchemy");
const errors_1 = require("../../../src/providers/chain-state/external/adapters/errors");
const transaction_1 = require("../../../src/providers/chain-state/evm/models/transaction");
const config_1 = __importDefault(require("../../../src/config"));
const MORALIS_KEY = process.env.MORALIS_API_KEY;
const ALCHEMY_KEY = process.env.ALCHEMY_API_KEY;
// Known BASE mainnet transaction for cross-adapter verification
const KNOWN_TX_HASH = '0x6a4be6adf22988c7f4e92cb829b1d0e4e9a7f5bf3e2d456d63e51c9b287f1f4c';
const CHAIN_ID = '0x2105'; // BASE mainnet = 8453
describe('Multi-Provider Integration (BASE mainnet)', function () {
    this.timeout(30000);
    let sandbox;
    let moralis;
    let alchemy;
    const savedExternalProviders = config_1.default.externalProviders;
    before(function () {
        if (!MORALIS_KEY || !ALCHEMY_KEY) {
            this.skip();
        }
        config_1.default.externalProviders = {
            ...savedExternalProviders,
            moralis: { apiKey: MORALIS_KEY },
            alchemy: { apiKey: ALCHEMY_KEY }
        };
    });
    after(function () {
        config_1.default.externalProviders = savedExternalProviders;
    });
    beforeEach(function () {
        sandbox = sinon_1.default.createSandbox();
        sandbox.stub(transaction_1.EVMTransactionStorage, 'addEffectsToTxs').callsFake(() => { });
        sandbox.stub(transaction_1.EVMTransactionStorage, 'abiDecode').returns(undefined);
        sandbox.stub(transaction_1.EVMTransactionStorage, '_apiTransform').callsFake((tx) => tx);
        moralis = new moralis_1.MoralisAdapter({ name: 'moralis', priority: 1 });
        alchemy = new alchemy_1.AlchemyAdapter({ name: 'alchemy', priority: 2 });
    });
    afterEach(function () {
        sandbox.restore();
    });
    it('should get a known transaction via Moralis adapter', async function () {
        const tx = await moralis.getTransaction({
            chain: 'BASE',
            network: 'mainnet',
            chainId: CHAIN_ID,
            txId: KNOWN_TX_HASH
        });
        (0, chai_1.expect)(tx).to.exist;
        (0, chai_1.expect)(tx.txid).to.equal(KNOWN_TX_HASH);
        (0, chai_1.expect)(tx.blockHeight).to.be.a('number').and.to.be.greaterThan(0);
    });
    it('should get the same transaction via Alchemy adapter', async function () {
        const tx = await alchemy.getTransaction({
            chain: 'BASE',
            network: 'mainnet',
            chainId: CHAIN_ID,
            txId: KNOWN_TX_HASH
        });
        (0, chai_1.expect)(tx).to.exist;
        (0, chai_1.expect)(tx.txid).to.equal(KNOWN_TX_HASH);
        (0, chai_1.expect)(tx.blockHeight).to.be.a('number').and.to.be.greaterThan(0);
    });
    it('should stream address transactions', function (done) {
        const stream = moralis.streamAddressTransactions({
            chain: 'BASE',
            network: 'mainnet',
            chainId: CHAIN_ID,
            address: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', // vitalik.eth
            args: { pageSize: 3 }
        });
        const results = [];
        stream.on('data', (d) => results.push(d));
        stream.on('end', () => {
            (0, chai_1.expect)(results.length).to.be.greaterThan(0);
            done();
        });
        stream.on('error', done);
    });
    it('should failover when primary API key is invalid', async function () {
        const badConfig = config_1.default.externalProviders;
        config_1.default.externalProviders = {
            ...badConfig,
            moralis: { apiKey: 'invalid-key-12345' }
        };
        const badAdapter = new moralis_1.MoralisAdapter({ name: 'moralis', priority: 1 });
        config_1.default.externalProviders = badConfig;
        try {
            await badAdapter.getTransaction({
                chain: 'BASE',
                network: 'mainnet',
                chainId: CHAIN_ID,
                txId: KNOWN_TX_HASH
            });
            chai_1.expect.fail('Should have thrown');
        }
        catch (err) {
            (0, chai_1.expect)(err).to.be.instanceOf(errors_1.AdapterError);
            (0, chai_1.expect)(err.code).to.equal(errors_1.AdapterErrorCode.AUTH);
        }
    });
});
//# sourceMappingURL=csp.test.js.map