<commit_before>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 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 = 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);
  }

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

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

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

    it("should clean the output directory when specified", function(){
      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 }));
      assert.equal(true, (fs.existsSync(dir) || fs.existsSync(file))===false && result===false);
    });

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

    // environment dependent, also depends on inputFile and server files
    it("run sync, local robots file, local webserver", function(done){
      var counter = { count: 0 };
      var ourport = port; // first one
      server.start(path.join(__dirname, "./server"), ourport, (function(counter){
        return function() {
          counter.count++;
          if (counter.count===urls) {
            setTimeout(done, 10000);
          }
        };
      })(counter));
      var options = {
        source: inputFile,
        hostname: "localhost",
        port: ourport,
        selector: "#dynamic-content",
        outputDir: path.join(__dirname, "./tmp/sync/snapshots"),
        outputDirClean: true,
        timeout: 5000
      };
      var result = ss.run(optHelp.decorate(options));
      assert.equal(true, result);
    });

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

    it("run async, all snapshots should succeed, no output dir pre-exists", function(done){
      rimraf(path.join(__dirname, "./tmp/snapshots"));
      var ourport = ++port;
      server.start(path.join(__dirname, "./server"), ourport);
      var options = {
        source: inputFile,
        hostname: "localhost",
        port: ourport,
        selector: "#dynamic-content",
        outputDir: path.join(__dirname, "./tmp/snapshots"),<commit_msg>removed sync test<commit_after>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 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 = 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);
  }

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

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

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

    it("should clean the output directory when specified", function(){
      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 }));
      assert.equal(true, (fs.existsSync(dir) || fs.existsSync(file))===false && result===false);
    });

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

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

    it("run async, all snapshots should succeed, no output dir pre-exists", function(done){
      rimraf(path.join(__dirname, "./tmp/snapshots"));
      var ourport = ++port;
      server.start(path.join(__dirname, "./server"), ourport);
      var options = {
        source: inputFile,
        hostname: "localhost",
        port: ourport,
        selector: "#dynamic-content",
        outputDir: path.join(__dirname, "./tmp/snapshots"),
{'fraction_non_alphanumeric': 0.15667880145617474, 'fraction_numerical': 0.007280873704844581, 'mean_word_length': 3.035593220338983, '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': '3083891', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2080, 'n_words': 531}