<commit_before>/* global describe, it, before, beforeEach */
var assert = require("assert");
var path = require("path");
var fs = require("fs");
var spawn = require('child_process').spawn;
var rimraf = require("rimraf").sync;
var ss = require("../../../lib/html-snapshots");
var optHelp = require("../../helpers/options");
var resHelp = require("../../helpers/result");
var server = require("../../server");
var port = 8034;

describe("html-snapshots", function() {

  var inputFile = path.join(__dirname, "./test_robots.txt");
  var spawnedProcessPattern = "^phantomjs$";
  var urls = 3; // must match test_robots.txt
  var bogusFile = "./bogus/file.txt";

  // Count actual phantomjs processes in play, requires pgrep
  function countSpawnedProcesses(cb) {
    var pgrep;
    // std mac pgrep doesn't have a count option. How stupid is that?
    if (process.platform === "darwin") {
      var wc = spawn("wc", ["-l"]);
      pgrep = spawn("pgrep", [spawnedProcessPattern]);
      pgrep.stdout.on("data", function (data) {
        wc.stdin.write(data);
      });
      wc.stdout.on("data", cb);
    } else {
      pgrep = spawn("pgrep", [spawnedProcessPattern, "-c"]);
      pgrep.stdout.on("data", cb);
    }
  }

  // Clear any lingering phantomjs processes in play
  function killSpawnedProcesses(cb) {
    var pkill = spawn("pkill", [spawnedProcessPattern]);
    pkill.on("exit", cb);
  }

  function cleanup(done, arg) {
    if (process.platform === "win32") {
      setTimeout(done, 3000, arg);
    } else {
      setImmediate(function () {
        killSpawnedProcesses(function() {
          done(arg);
        });
      });
    }
  }

  describe("library", function() {
    this.timeout(30000);

    before(function (done) {
      server.start(path.join(__dirname, "./server"), port, done);
    });

    describe("run basics", function() {

      it("no arguments should return false", function(done) {
        assert.equal(false, ss.run(optHelp.decorate({}), function (err) {
          resHelp.mustBeError(err);
          cleanup(done);
        }));
      });

      it("invalid source should return false", function(done) {
        assert.equal(false, ss.run(optHelp.decorate({ source: bogusFile }), function (err) {
          resHelp.mustBeError(err);
          cleanup(done);
        }));
      });

      it("should clean the output directory when specified", function(done) {
        var dir = path.join(__dirname, "./tmpdir");
        var file = path.join(dir, "somefile.txt");
        if (!fs.existsSync(dir)) {
          fs.mkdirSync(dir);
        }
        fs.writeFileSync(file, "some data");
        assert.equal(true, fs.existsSync(dir));
        var result = ss.run(
          optHelp.decorate({ source: bogusFile, outputDir: dir, outputDirClean: true }),
          function (err) {
            resHelp.mustBeError(err);
            cleanup(done);
          }
        );
        assert.equal(true, (fs.existsSync(dir) || fs.existsSync(file))===false && result===false);
      });

      it("default snapshot script should exist", function(done) {
        var options = { source: "./bogus/file.txt" };
        var result = ss.run(optHelp.decorate(options), function (err) {
          resHelp.mustBeError(err);
          cleanup(done);
        });
        assert.equal(true, fs.existsSync(options.snapshotScript) && result===false);
      });
    });

    describe("async runs", function() {

      it("should all succeed, no output dir pre-exists", function(done) {
        rimraf(path.join(__dirname, "./tmp/snapshots"));
        var options = {
          source: inputFile,
          hostname: "localhost",
          port: port,
          selector: "#dynamic-content",
          outputDir: path.join(__dirname, "./tmp/snapshots"),
          outputDirClean: true,
          timeout: 10000
        };
        var result = ss.run(optHelp.decorate(options), function(err, completed) {
          // this still fails occasionally.
          console.log('@@@ error: ' + err +', '+require('util').inspect(completed, {depth:null}));
          cleanup(done, err);
        });
        assert.equal(true, result);
      });

      it("should all succeed, output dir does pre-exist", function(done) {
        var options = {
          source: inputFile,
          hostname: "localhost",
          port: port,
          selector: "#dynamic-content",
          outputDir: path.join(__dirname, "./tmp/snapshots"),
          outputDirClean: true,
          timeout: 10000
        };
        var result = ss.run(optHelp.decorate(options), function(err, completed) {
          console.log('@@@ error: ' + err +', '+require('util').inspect(completed, {depth:null}));
          cleanup(done, err);
        });
        assert.equal(true, result);
      });

      it("should all fail with bad phantomjs process to spawn", function(done) {
        var options = {
          source: inputFile,
          hostname: "localhost",
          port: port,
          selector: "#dynamic-content",
          outputDir: path.join(__dirname, "./tmp/sync/snapshots"),
          outputDirClean: true,
          phantomjs: bogusFile,
          timeout: 1000
        };
        var result = ss.run(options, function(err, snapshots) {
          // here is where the error should be
          resHelp.mustBeError(err);
          cleanup(done);
        });
        assert.equal(true, result);
      });

      it("should all fail, bad remote sitemap", function(done) {
        var options = {
          input: "sitemap",
          source: "http://localhost:"+port+"/index.html",
          port: port,
          selector: "#dynamic-content",<commit_msg>comment update<commit_after>/* global describe, it, before, beforeEach */
var assert = require("assert");
var path = require("path");
var fs = require("fs");
var spawn = require('child_process').spawn;
var rimraf = require("rimraf").sync;
var ss = require("../../../lib/html-snapshots");
var optHelp = require("../../helpers/options");
var resHelp = require("../../helpers/result");
var server = require("../../server");
var port = 8034;

describe("html-snapshots", function() {

  var inputFile = path.join(__dirname, "./test_robots.txt");
  var spawnedProcessPattern = "^phantomjs$";
  var urls = 3; // must match test_robots.txt
  var bogusFile = "./bogus/file.txt";

  // Count actual phantomjs processes in play, requires pgrep
  function countSpawnedProcesses(cb) {
    var pgrep;
    // std mac pgrep doesn't have a count option. How stupid is that?
    if (process.platform === "darwin") {
      var wc = spawn("wc", ["-l"]);
      pgrep = spawn("pgrep", [spawnedProcessPattern]);
      pgrep.stdout.on("data", function (data) {
        wc.stdin.write(data);
      });
      wc.stdout.on("data", cb);
    } else {
      pgrep = spawn("pgrep", [spawnedProcessPattern, "-c"]);
      pgrep.stdout.on("data", cb);
    }
  }

  // Clear any lingering phantomjs processes in play
  function killSpawnedProcesses(cb) {
    var pkill = spawn("pkill", [spawnedProcessPattern]);
    pkill.on("exit", cb);
  }

  function cleanup(done, arg) {
    if (process.platform === "win32") {
      setTimeout(done, 3000, arg);
    } else {
      setImmediate(function () {
        killSpawnedProcesses(function() {
          done(arg);
        });
      });
    }
  }

  describe("library", function() {
    this.timeout(30000);

    before(function (done) {
      server.start(path.join(__dirname, "./server"), port, done);
    });

    describe("run basics", function() {

      it("no arguments should return false", function(done) {
        assert.equal(false, ss.run(optHelp.decorate({}), function (err) {
          resHelp.mustBeError(err);
          cleanup(done);
        }));
      });

      it("invalid source should return false", function(done) {
        assert.equal(false, ss.run(optHelp.decorate({ source: bogusFile }), function (err) {
          resHelp.mustBeError(err);
          cleanup(done);
        }));
      });

      it("should clean the output directory when specified", function(done) {
        var dir = path.join(__dirname, "./tmpdir");
        var file = path.join(dir, "somefile.txt");
        if (!fs.existsSync(dir)) {
          fs.mkdirSync(dir);
        }
        fs.writeFileSync(file, "some data");
        assert.equal(true, fs.existsSync(dir));
        var result = ss.run(
          optHelp.decorate({ source: bogusFile, outputDir: dir, outputDirClean: true }),
          function (err) {
            resHelp.mustBeError(err);
            cleanup(done);
          }
        );
        assert.equal(true, (fs.existsSync(dir) || fs.existsSync(file))===false && result===false);
      });

      it("default snapshot script should exist", function(done) {
        var options = { source: "./bogus/file.txt" };
        var result = ss.run(optHelp.decorate(options), function (err) {
          resHelp.mustBeError(err);
          cleanup(done);
        });
        assert.equal(true, fs.existsSync(options.snapshotScript) && result===false);
      });
    });

    describe("async runs", function() {

      it("should all succeed, no output dir pre-exists", function(done) {
        rimraf(path.join(__dirname, "./tmp/snapshots"));
        var options = {
          source: inputFile,
          hostname: "localhost",
          port: port,
          selector: "#dynamic-content",
          outputDir: path.join(__dirname, "./tmp/snapshots"),
          outputDirClean: true,
          timeout: 10000
        };
        var result = ss.run(optHelp.decorate(options), function(err, completed) {
          // this still fails on mac occasionally.
          console.log('@@@ error: ' + err +', '+require('util').inspect(completed, {depth:null}));
          cleanup(done, err);
        });
        assert.equal(true, result);
      });

      it("should all succeed, output dir does pre-exist", function(done) {
        var options = {
          source: inputFile,
          hostname: "localhost",
          port: port,
          selector: "#dynamic-content",
          outputDir: path.join(__dirname, "./tmp/snapshots"),
          outputDirClean: true,
          timeout: 10000
        };
        var result = ss.run(optHelp.decorate(options), function(err, completed) {
          // this still fails on mac occasionally.
          console.log('@@@ error: ' + err +', '+require('util').inspect(completed, {depth:null}));
          cleanup(done, err);
        });
        assert.equal(true, result);
      });

      it("should all fail with bad phantomjs process to spawn", function(done) {
        var options = {
          source: inputFile,
          hostname: "localhost",
          port: port,
          selector: "#dynamic-content",
          outputDir: path.join(__dirname, "./tmp/sync/snapshots"),
          outputDirClean: true,
          phantomjs: bogusFile,
          timeout: 1000
        };
        var result = ss.run(options, function(err, snapshots) {
          // here is where the error should be
          resHelp.mustBeError(err);
          cleanup(done);
        });
        assert.equal(true, result);
      });

      it("should all fail, bad remote sitemap", function(done) {
        var options = {
          input: "sitemap",
          source: "http://localhost:"+port+"/index.html",
          port: port,
          selector: "#dynamic-content",
{'fraction_non_alphanumeric': 0.15564580206586034, 'fraction_numerical': 0.005297077778758718, 'mean_word_length': 2.347517730496454, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2627581', 'n_tokens_mistral': 3358, 'n_tokens_neox': 3253, 'n_words': 764}