<commit_before>var chai = require('chai');
var expect = require('chai').expect;
var AssertionError = require('chai').AssertionError;
var chaiAsPromised = require('chai-as-promised');
var chaiMugshot = require('../index.js');
var Mugshot = require('mugshot');
var WdioAdapter = Mugshot.adapters.WebdriverIO;
var wdio = require('webdriverio');
var fs = require('fs');
var path = require('path');

chai.use(chaiAsPromised);

var name = 'great';
var ext = '.png';
var dir = path.join(__dirname, '..', 'visual-tests');

function cleanUp() {
  var paths = [path.join(dir, name + ext), path.join(dir, name + '.new' + ext),
               path.join(dir, name + '.diff' + ext)];

  for (var i = 0; i < paths.length; i++) {
    try {
      fs.unlinkSync(paths[i]);
    } catch(error) {
      if (error.code !== 'ENOENT') {
        throw error;
      }
    }
  }

  try {
    fs.rmdirSync(dir);
  } catch(error) {
    if (error.code !== 'ENOENT') {
      throw error;
    }
  }
}

describe('Chai-Mugshot Plugin', function() {
  this.timeout(0);

  var url = 'file://' + path.join(__dirname, 'test.html'),
      brokenSelector = {
        name: name,
        selector: 'anything'
      },
      noSelector = {
        name: name
      },
      withSelector = {
        name: name,
        selector: '#rectangle'
      },
      wdioInstance, mugshot;

  before(function() {
    var options = {
      desiredCapabilities: {
        browserName: 'phantomjs'
      }
    };

    return wdioInstance = wdio.remote(options).init().url(url)
      .then(function() {
        var browser = new WdioAdapter(this);
        mugshot = new Mugshot(browser);

        chai.use(chaiMugshot(mugshot));
      });
  });

  beforeEach(function(done) {
    cleanUp();

    mugshot.test(withSelector, function(error) {
      if (error) {
        throw error;
      }

      done();
    });
  });

  it('should be rejected if mugshot fails', function() {
    return expect(expect(brokenSelector).to.be.identical).to.be
      .rejectedWith(Error);
  });

  describe('No baseline', function() {
    it('should not throw if there is no previous baseline', function() {
      fs.unlinkSync(path.join(dir, name + ext));

      return expect(expect(withSelector).to.be.identical).to.be.fulfilled;
    });
  });

  describe('No differences', function() {
    it('should not throw if there are no differences', function() {
      return expect(expect(withSelector).to.be.identical).to.be.fulfilled;
    });

    it('should throw error if there are differences', function() {
      return expect(expect(withSelector).to.not.be.identical).to.be
        .rejectedWith(AssertionError);
    });
  });

  describe('With differences', function() {
    it('should not throw if there are differences', function() {
      return expect(expect(noSelector).to.not.be.identical).to.be.fulfilled;
    });

    it('should throw if there are no differences', function() {
      return expect(expect(noSelector).to.be.identical).to.be
        .rejectedWith(AssertionError);
    });
  });

  describe('Test Runner Context', function() {
    var testRunnerCtx = {};

    before(function() {
      chai.use(chaiMugshot(mugshot, testRunnerCtx));
    });<commit_msg>add test for no baseline suite<commit_after>var chai = require('chai');
var expect = require('chai').expect;
var AssertionError = require('chai').AssertionError;
var chaiAsPromised = require('chai-as-promised');
var chaiMugshot = require('../index.js');
var Mugshot = require('mugshot');
var WdioAdapter = Mugshot.adapters.WebdriverIO;
var wdio = require('webdriverio');
var fs = require('fs');
var path = require('path');

chai.use(chaiAsPromised);

var name = 'great';
var ext = '.png';
var dir = path.join(__dirname, '..', 'visual-tests');

function cleanUp() {
  var paths = [path.join(dir, name + ext), path.join(dir, name + '.new' + ext),
               path.join(dir, name + '.diff' + ext)];

  for (var i = 0; i < paths.length; i++) {
    try {
      fs.unlinkSync(paths[i]);
    } catch(error) {
      if (error.code !== 'ENOENT') {
        throw error;
      }
    }
  }

  try {
    fs.rmdirSync(dir);
  } catch(error) {
    if (error.code !== 'ENOENT') {
      throw error;
    }
  }
}

describe('Chai-Mugshot Plugin', function() {
  this.timeout(0);

  var url = 'file://' + path.join(__dirname, 'test.html'),
      brokenSelector = {
        name: name,
        selector: 'anything'
      },
      noSelector = {
        name: name
      },
      withSelector = {
        name: name,
        selector: '#rectangle'
      },
      wdioInstance, mugshot;

  before(function() {
    var options = {
      desiredCapabilities: {
        browserName: 'phantomjs'
      }
    };

    return wdioInstance = wdio.remote(options).init().url(url)
      .then(function() {
        var browser = new WdioAdapter(this);
        mugshot = new Mugshot(browser);

        chai.use(chaiMugshot(mugshot));
      });
  });

  beforeEach(function(done) {
    cleanUp();

    mugshot.test(withSelector, function(error) {
      if (error) {
        throw error;
      }

      done();
    });
  });

  it('should be rejected with Error if Mugshot fails', function() {
    return expect(expect(brokenSelector).to.be.identical).to.be
      .rejectedWith(Error);
  });

  describe('No baseline', function() {
    beforeEach(function() {
      cleanUp();
    });

    it('should be fulfilled if expected to be identical', function() {
      return expect(expect(withSelector).to.be.identical).to.be.fulfilled;
    });

    it('should be rejected with AssertionError if expected to not be identical',
       function() {
      return expect(expect(withSelector).to.not.be.identical).to.be
        .rejectedWith(AssertionError);
    });
  });

  describe('No differences', function() {
    it('should be fullfilled if expected to be identical', function() {
      return expect(expect(withSelector).to.be.identical).to.be.fulfilled;
    });

    it('should be rejected with AssertionError if expected to not be identical',
       function() {
      return expect(expect(withSelector).to.not.be.identical).to.be
        .rejectedWith(AssertionError);
    });
  });

  describe('With differences', function() {
    it('should be fulfilled if expected to not be identical', function() {
      return expect(expect(noSelector).to.not.be.identical).to.be.fulfilled;
    });

    it('should be rejected with AssertionError if expected to be identical',
       function() {
      return expect(expect(noSelector).to.be.identical).to.be
        .rejectedWith(AssertionError);
    });
  });

  describe('Test Runner Context', function() {
    var testRunnerCtx = {};

    before(function() {
      chai.use(chaiMugshot(mugshot, testRunnerCtx));
    });
{'fraction_non_alphanumeric': 0.15557206537890045, 'fraction_numerical': 0.0005943536404160475, 'mean_word_length': 2.8418949771689497, 'pattern_counts': {'":': 0, '<': 5, '<?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': '449863', 'n_tokens_mistral': 2183, 'n_tokens_neox': 2117, 'n_words': 461}