<commit_before>const { spawn } = require("child_process");
const puppeteer = require("puppeteer");
const killPort = require("kill-port");
const request = require("request");

const backendPort = 12100;
const proxyPort = backendPort + 1;
const webServerPort = proxyPort + 1;

function setTimeoutPromise(timeout) {
  return new Promise(resolve => {
    setTimeout(resolve, timeout);
  });
}

function waitForProxyReady(command) {
  return new Promise(resolve => {
    command.stdout.on("data", function(data) {
      // not very explicit, but currently this message means the proxy is ready
      if (data.toString().includes("Root certificate")) {
        resolve();
      }
    });
  });
}

function startWebServer() {
  return new Promise(resolve => {
    let command = spawn(__dirname + "/node_modules/.bin/http-server", [
      "-p",
      webServerPort.toString(),
      __dirname
    ]);
    command.stdout.on("data", function(data) {
      if (data.toString().includes("Hit CTRL-C to stop the server")) {
        resolve();
      }
    });
    command.stderr.on("data", function(data) {
      console.log("err", data.toString());
    });
  });
}

function inspectDomChar(charIndex) {
  return new Promise(resolve => {
    request.post(
      {
        url: "http://localhost:" + backendPort + "/inspectDomChar",
        json: {
          charIndex
        }
      },
      function(err, resp, body) {
        resolve(body);
      }
    );
  });
}

function traverse(firstStep) {
  return new Promise(resolve => {
    request.post(
      {
        url: "http://localhost:" + backendPort + "/traverse",
        json: firstStep
      },
      function(err, resp, body) {
        resolve(body);
      }
    );
  });
}

async function inspectDomCharAndTraverse(charIndex) {
  const firstStep = await inspectDomChar(charIndex);
  const steps = (await traverse(firstStep))["steps"];
  return steps[steps.length - 1];
}

describe("E2E", () => {
  let browser;

  beforeAll(async () => {
    await killPort(backendPort);
    await killPort(proxyPort);
    await killPort(webServerPort);

    command = spawn(__dirname + "/bin/fromjs", [
      "--port",
      backendPort.toString(),
      "--shouldOpenBrowser",
      "no",
      "--sessionDirectory",
      "/tmp/fromjs-e2e"
    ]);

    command.stderr.on("data", function(data) {
      console.log("err", data.toString());
    });

    await waitForProxyReady(command);

    browser = await puppeteer.launch({<commit_msg>Console.log for debugging in CircleCI<commit_after>const { spawn } = require("child_process");
const puppeteer = require("puppeteer");
const killPort = require("kill-port");
const request = require("request");

const backendPort = 12100;
const proxyPort = backendPort + 1;
const webServerPort = proxyPort + 1;

function setTimeoutPromise(timeout) {
  return new Promise(resolve => {
    setTimeout(resolve, timeout);
  });
}

function waitForProxyReady(command) {
  return new Promise(resolve => {
    command.stdout.on("data", function(data) {
      // not very explicit, but currently this message means the proxy is ready
      if (data.toString().includes("Root certificate")) {
        resolve();
      }
    });
  });
}

function startWebServer() {
  return new Promise(resolve => {
    let command = spawn(__dirname + "/node_modules/.bin/http-server", [
      "-p",
      webServerPort.toString(),
      __dirname
    ]);
    command.stdout.on("data", function(data) {
      if (data.toString().includes("Hit CTRL-C to stop the server")) {
        resolve();
      }
    });
    command.stderr.on("data", function(data) {
      console.log("err", data.toString());
    });
  });
}

function inspectDomChar(charIndex) {
  return new Promise(resolve => {
    request.post(
      {
        url: "http://localhost:" + backendPort + "/inspectDomChar",
        json: {
          charIndex
        }
      },
      function(err, resp, body) {
        resolve(body);
      }
    );
  });
}

function traverse(firstStep) {
  return new Promise(resolve => {
    request.post(
      {
        url: "http://localhost:" + backendPort + "/traverse",
        json: firstStep
      },
      function(err, resp, body) {
        resolve(body);
      }
    );
  });
}

async function inspectDomCharAndTraverse(charIndex) {
  const firstStep = await inspectDomChar(charIndex);
  const steps = (await traverse(firstStep))["steps"];
  return steps[steps.length - 1];
}

describe("E2E", () => {
  let browser;

  beforeAll(async () => {
    await killPort(backendPort);
    await killPort(proxyPort);
    await killPort(webServerPort);

    command = spawn(__dirname + "/bin/fromjs", [
      "--port",
      backendPort.toString(),
      "--shouldOpenBrowser",
      "no",
      "--sessionDirectory",
      "/tmp/fromjs-e2e"
    ]);

    command.stdout.on("data", function(data) {
      console.log("CLI out", data.toString());
    });

    command.stderr.on("data", function(data) {
      console.log("CLI err", data.toString());
    });

    console.log("waiting for proxy");
    await waitForProxyReady(command);
    console.log("proxy ready!");

    browser = await puppeteer.launch({
{'fraction_non_alphanumeric': 0.15402567094515754, 'fraction_numerical': 0.0038895371450797353, 'mean_word_length': 2.8495508982035926, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1040380', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1523, 'n_words': 320}