<commit_before>var assert = require('assert');
var fs = require('fs');
var spawn = require('child_process').spawn;
var getPixels = require('get-pixels');
var ndarray = require('ndarray');
var request = require('request');
var savePixels = require('save-pixels');

function makeRequest(params) {
  before(function (done) {
    // Stringify our argument for phantomjs
    var arg = JSON.stringify(params);
    var encodedArg = encodeURIComponent(arg);

    // Request to our server
    var that = this;
    request({
      url: 'http://localhost:9090/',
      method: 'POST',
      headers: {
        // DEV: PhantomJS looks for Proper-Case headers, request is lower-case =(
        // TODO: Note this in the README
        'Content-Length': encodedArg.length
      },
      body: encodedArg,
    }, function handlePhantomResponse (err, res, body) {
      that.res = res;
      that.body = body;
      done(err);
    });
  });
}
function debugActual(filename) {
  if (process.env.DEBUG_TEST) {
    before(function (done) {
      var png = savePixels(ndarray(this.actualPixels, [10, 10, 4], [4 * 10, 4, 1], 0), 'png');
      try { fs.mkdirSync(__dirname + '/actual-files'); } catch (e) {}
      png.pipe(fs.createWriteStream(__dirname + '/actual-files/' + filename));
      png.on('end', done);
    });
  }
}
function loadExpected(filename) {
  before(function (done) {
    var that = this;
    getPixels(__dirname + '/expected-files/' + filename, function (err, expectedPixels) {
      that.expectedPixels = expectedPixels;
      done(err);
    });
  });
}

describe('phantomjs-pixel-server', function () {
  before(function (done) {
    this.child = spawn('phantomjs', [__dirname + '/../lib/phantomjs-pixel-server.js'], {stdio: [0, 1, 2]});
    setTimeout(function () {
      // TODO: We could wait for 200 from server
      done();
    }, 1000);
  });
  after(function (done) {
    this.child.kill();
    this.child.on('exit', function (code, signal) {
      done();
    });
  });

  describe('given a set of commands on a canvas', function () {
    makeRequest({
      width: 10,
      height: 10,
      // TODO: Move to function over vanilla JS so the callback makes sense
      js: [
        // Draw a white on black checkerboard
        'context.fillStyle = "#000000";',
        'context.fillRect(0, 0, 10, 10);',
        'context.fillStyle = "#FFFFFF";',
        'context.fillRect(0, 0, 5, 5);',
        'context.fillRect(5, 5, 5, 5);',
        'cb();'
      ].join('\n')
    });
    before(function () {
      try {<commit_msg>Adding TODO for self<commit_after>var assert = require('assert');
var fs = require('fs');
var spawn = require('child_process').spawn;
var getPixels = require('get-pixels');
var ndarray = require('ndarray');
var request = require('request');
var savePixels = require('save-pixels');

function makeRequest(params) {
  before(function (done) {
    // Stringify our argument for phantomjs
    var arg = JSON.stringify(params);
    var encodedArg = encodeURIComponent(arg);

    // Request to our server
    var that = this;
    request({
      url: 'http://localhost:9090/',
      method: 'POST',
      headers: {
        // DEV: PhantomJS looks for Proper-Case headers, request is lower-case =(
        // TODO: Note this in the README
        'Content-Length': encodedArg.length
      },
      body: encodedArg,
    }, function handlePhantomResponse (err, res, body) {
      that.res = res;
      that.body = body;
      done(err);
    });
  });
}
function debugActual(filename) {
  if (process.env.DEBUG_TEST) {
    before(function (done) {
      var png = savePixels(ndarray(this.actualPixels, [10, 10, 4], [4 * 10, 4, 1], 0), 'png');
      try { fs.mkdirSync(__dirname + '/actual-files'); } catch (e) {}
      png.pipe(fs.createWriteStream(__dirname + '/actual-files/' + filename));
      png.on('end', done);
    });
  }
}
function loadExpected(filename) {
  before(function (done) {
    var that = this;
    getPixels(__dirname + '/expected-files/' + filename, function (err, expectedPixels) {
      that.expectedPixels = expectedPixels;
      done(err);
    });
  });
}

describe('phantomjs-pixel-server', function () {
  before(function (done) {
    this.child = spawn('phantomjs', [__dirname + '/../lib/phantomjs-pixel-server.js'], {stdio: [0, 1, 2]});
    setTimeout(function () {
      // TODO: We could wait for 200 from server
      done();
    }, 1000);
  });
  after(function (done) {
    this.child.kill();
    this.child.on('exit', function (code, signal) {
      done();
    });
  });

  describe('given a set of commands on a canvas', function () {
    makeRequest({
      width: 10,
      height: 10,
      // TODO: Move to function over vanilla JS so the callback makes sense
      // This seems promising https://github.com/gyuwon/function-signature
      js: [
        // Draw a white on black checkerboard
        'context.fillStyle = "#000000";',
        'context.fillRect(0, 0, 10, 10);',
        'context.fillStyle = "#FFFFFF";',
        'context.fillRect(0, 0, 5, 5);',
        'context.fillRect(5, 5, 5, 5);',
        'cb();'
      ].join('\n')
    });
    before(function () {
      try {
{'fraction_non_alphanumeric': 0.16297591297591296, 'fraction_numerical': 0.019036519036519036, 'mean_word_length': 2.739288307915759, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '348252', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1604, 'n_words': 419}