import { DebuggerClient } from "../debugger-client"
import request from 'sync-request';
import { InitArguments } from "../types";
jest.setTimeout(50000);

describe('Truffle debugger', () => {
  let client: DebuggerClient;
  beforeAll(async () => {
    client = new DebuggerClient();
    const resp = request('GET', "https://test.sentio.xyz/api/v1/solidity/fetch_and_compile?txHash=0x857c1331d430048a16b71515464c6d68e7cc17337de82afe0151652435862b4a", {json: true})
    const body = JSON.parse(resp.body as string);
    const shimmedCompilations = [];
    for (var key in body.result) {
      var value = body.result[key];
      // @ts-ignore
      shimmedCompilations.push(...value);
    }
    const init_args: InitArguments = {
      providerURL: 'https://test.sentio.xyz/api/v1/solidity',
      txnHash: "0x857c1331d430048a16b71515464c6d68e7cc17337de82afe0151652435862b4a",
      shimmedCompilations: shimmedCompilations,
      chainId: "0x1",
    }
    await client.initialize(init_args);
  }),

  afterEach(async () => {
    await client.reset();
  }),

  it('advance multiple steps', async () => {
    await client.advance(100000);
    expect(client.finished).toBe(true);
  }),

  it('breakpoint and evaluate variables', async () => {
    await client.addBreakpoint({
      compilationId: "externalFor(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48)Via(etherscan)Number(0)",
      sourcePath: "FiatTokenProxy.sol",
      line: 21
    });

    expect((await client.continueUntilBreakpoint()).lines.start.line).toBe(21);
    expect(await client.evaluateExpression("now + 2")).toBe(1670734885);
    expect(await client.evaluateExpression("now + 1")).toBe(1670734884);
  }),

  it('require stacktrace and varaibles', async () => {
    await client.stepNext();
    await client.stepInto();
    await client.stepInto();
    await client.stepInto();
    await client.stepInto();
    await client.stepInto();
    await client.stepInto();
    await client.stepInto();
    await client.stepInto();
    await client.stepInto();
    expect(await client.stepOver()).not.toBeNull;
    expect(await client.requireStacktrace()).not.toBeNull;
    expect(await client.requireVariables()).not.toBeNull;
  }),

  it('Trace', async () => {
    expect(await client.getCallTrace()).not.toBeNull;
  }),

  it('Get code', async () => {
    let private_client = new DebuggerClient();
    const resp = request('GET', "https://test.sentio.xyz/api/v1/solidity/fetch_and_compile?txHash=0xbeb67f776a16c8a01f4c4af9f1031fa86fba7fc08fd2a152b5214cfe14a14c04", {json: true})
    const body = JSON.parse(resp.body as string);
    const shimmedCompilations = [];
    for (var key in body.result) {
      var value = body.result[key];
      // @ts-ignore
      shimmedCompilations.push(...value);
    }
    const init_args: InitArguments = {
      providerURL: 'https://test.sentio.xyz/api/v1/solidity',
      txnHash: "0xbeb67f776a16c8a01f4c4af9f1031fa86fba7fc08fd2a152b5214cfe14a14c04",
      shimmedCompilations: shimmedCompilations,
      chainId: "0x1",
    }
    await private_client.initialize(init_args);
    await private_client.stepInto();
    await private_client.stepInto();
    await private_client.stepInto();
    await private_client.stepInto();
    await private_client.stepInto();
    expect(await client.requireVariables()).not.toBeNull;
    expect(await private_client.getCallTrace()).not.toBeNull;
  })
})