'use strict';
var sinon = require('sinon'),
    q = require('q'),
    path = require('path'),
    fs = require('fs'),
    Calibrator = require('../lib/calibrator'),
    Browser = require('../lib/browser'),
    Image = require('../lib/image'),
    GeminiError = require('../lib/errors/gemini-error');

describe('calibrator', function() {
    var browser, calibrator;

    function setScreenshot(imageName) {
        var imgPath = path.join(__dirname, 'functional', 'data', 'image', imageName),
            imgData = fs.readFileSync(imgPath);

        browser.captureFullscreenImage.returns(q(new Image(imgData)));
    }

    beforeEach(function() {
        var config = {
            browsers: {
                id: {
                    browserName: 'id'
                }
            }
        };
        browser = new Browser(config, 'id');
        sinon.stub(browser);
        browser.inject.returns(q());
        browser.open.returns(q());
        calibrator = new Calibrator();
    });

    it('should calculate correct crop area', function() {
        setScreenshot('calibrate.png');
        return calibrator.calibrate(browser)
            .then(function(rs) {
                rs.must.eql({top: 24, left: 6, right: 2, bottom: 0});
            });
    });

    it('should not perform the calibration process two times', function() {
        setScreenshot('calibrate.png');
        return calibrator.calibrate(browser)
            .then(function() {
                return calibrator.calibrate(browser);
            })
            .then(function() {
                sinon.assert.calledOnce(browser.open);
                sinon.assert.calledOnce(browser.inject);
                sinon.assert.calledOnce(browser.captureFullscreenImage);
            });
    });

    it('should return cached result second time', function() {
        setScreenshot('calibrate.png');
        return calibrator.calibrate(browser)
            .then(function() {
                return calibrator.calibrate(browser);
            })
            .then(function(rs) {
                rs.must.eql({top: 24, left: 6, right: 2, bottom: 0});
            });
    });

    it('should fail on broken calibration page', function(done) {
        setScreenshot('calibrate-broken.png');
        calibrator.calibrate(browser)
            .then(function(rs) {
                done(new Error('Promise must be rejected'));
            })
            .fail(function(err) {
                if (err instanceof GeminiError) {
                    return done();
                }
                done(new Error('Promise must be rejected with GeminiError'));
            });
    });
});

@@ -33,24 +33,61 @@
 e('sinon'),%0A
+    assert = require('chai').assert,%0A
     q = requ
@@ -1120,38 +1120,44 @@
 .png');%0A        
-return
+var result =
  calibrator.cali
@@ -1162,32 +1162,33 @@
 librate(browser)
+;
 %0A            .th
@@ -1184,61 +1184,51 @@
     
-    .then(function(rs) %7B%0A                rs.must.eql(
+return assert.eventually.deepEqual(result, 
 %7Btop
@@ -1265,32 +1265,16 @@
 m: 0%7D);%0A
-            %7D);%0A
     %7D);%0A
@@ -1574,38 +1574,32 @@
                 
-sinon.
 assert.calledOnc
@@ -1623,38 +1623,32 @@
                 
-sinon.
 assert.calledOnc
@@ -1682,22 +1682,16 @@
         
-sinon.
 assert.c
@@ -1861,38 +1861,44 @@
 .png');%0A        
-return
+var result =
  calibrator.cali
@@ -1916,32 +1916,44 @@
 er)%0A            
+ 
+           
 .then(function()
@@ -1947,32 +1947,44 @@
 en(function() %7B%0A
+            
                 
@@ -2025,35 +2025,32 @@
 r);%0A            
-%7D)%0A
             .the
@@ -2049,57 +2049,63 @@
     
-.then(function(rs) %7B%0A                rs.must.eql(
+%7D);%0A        return assert.eventually.deepEqual(result, 
 %7Btop
@@ -2142,32 +2142,16 @@
 m: 0%7D);%0A
-            %7D);%0A
     %7D);%0A
@@ -2209,20 +2209,16 @@
 unction(
-done
 ) %7B%0A    
@@ -2272,375 +2272,75 @@
     
-calibrator.calibrate(browser)%0A            .then(function(rs) %7B%0A                done(new Error('Promise must be rejected'));%0A            %7D)%0A            .fail(function(err) %7B%0A                if (err instanceof GeminiError) %7B%0A                    return done();%0A                %7D%0A                done(new Error('Promise must be rejected with GeminiError'));%0A            %7D
+return assert.isRejected(calibrator.calibrate(browser), GeminiError
 );%0A 

Migrate calibrator tests to chai.assert