{"size": 2940, "ext": "js", "max_stars_repo_path": "packages/truffle-debugger/test/context.js", "max_stars_repo_name": "sgryphon/truffle", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/truffle-debugger/test/context.js", "max_issues_repo_name": "sgryphon/truffle", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/truffle-debugger/test/context.js", "max_forks_repo_name": "sgryphon/truffle", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.3043478261, "max_line_length": 79, "alphanum_fraction": 0.6755102041}
import debugModule from "debug";
const debug = debugModule("test:context");

import { assert } from "chai";

import Ganache from "ganache-cli";

import { prepareContracts } from "./helpers";
import Debugger from "lib/debugger";

import sessionSelector from "lib/session/selectors";

const __OUTER = `
pragma solidity ~0.5;

import "./InnerContract.sol";

contract OuterContract {
  event Outer();

  InnerContract inner;

  constructor(address _inner) public {
    inner = InnerContract(_inner);
  }

  function run() public {
    inner.run();

    emit Outer();
  }
}
`;

const __INNER = `
pragma solidity ~0.5;

contract InnerContract {
  event Inner();

  function run() public {
    emit Inner();
  }
}
`;

const __MIGRATION = `
let OuterContract = artifacts.require("OuterContract");
let InnerContract = artifacts.require("InnerContract");

module.exports = function(deployer) {
  return deployer
    .then(function() {
      return deployer.deploy(InnerContract);
    })
    .then(function() {
      return InnerContract.deployed();
    })
    .then(function(inner) {
      return deployer.deploy(OuterContract, inner.address);
    });
};
`;

let migrations = {
  "2_deploy_contracts.js": __MIGRATION
};

let sources = {
  "OuterLibrary.sol": __OUTER,
  "InnerContract.sol": __INNER
};

describe("Contexts", function() {
  var provider;

  var abstractions;
  var artifacts;
  var files;

  before("Create Provider", async function() {
    provider = Ganache.provider({ seed: "debugger", gasLimit: 7000000 });
  });

  before("Prepare contracts and artifacts", async function() {
    this.timeout(30000);

    let prepared = await prepareContracts(provider, sources, migrations);
    abstractions = prepared.abstractions;
    artifacts = prepared.artifacts;
    files = prepared.files;
  });

  it("returns view of addresses affected", async function() {
    let outer = await abstractions.OuterContract.deployed();
    let inner = await abstractions.InnerContract.deployed();

    // run outer contract method
    let result = await outer.run();

    assert.equal(2, result.receipt.rawLogs.length, "There should be two logs");

    let txHash = result.tx;

    let bugger = await Debugger.forTx(txHash, {
      provider,
      files,
      contracts: artifacts
    });
    debug("debugger ready");

    let session = bugger.connect();

    let affectedInstances = session.view(
      sessionSelector.info.affectedInstances
    );
    debug("affectedInstances: %o", affectedInstances);

    let affectedAddresses = Object.keys(affectedInstances).map(address =>
      address.toLowerCase()
    );

    assert.equal(2, affectedAddresses.length);

    assert.include(
      affectedAddresses,
      outer.address.toLowerCase(),
      "OuterContract should be an affected address"
    );

    assert.include(
      affectedAddresses,
      inner.address.toLowerCase(),
      "InnerContract should be an affected address"
    );
  });
});
