var penthouse = require('../lib/'),
	chai = require('chai'),
	should = chai.should(),
	css = require('css'),
	fs = require('fs'),
	read = fs.readFileSync,
	path = require('path'),
	async = require('async'),
	glob = require('glob');

// becasuse dont want to fail tests on white space differences
function normalisedCssAst (cssString) {
	return css.parse(css.stringify(css.parse(cssString), { compress: true }));
}

describe('basic tests of penthouse functionality', function () {
  var page1cssPath = path.join(__dirname, 'static-server', 'page1.css'),
		sharedCssFilePath = path.join(__dirname, 'static-server', 'shared.css'),
    page1 = path.join(__dirname, 'static-server', 'page1.html'),
		originalCss = read(page1cssPath).toString();

	// phantomjs takes a while to start up
	this.timeout(5000);

	it('should return css', function (done) {
    penthouse({
      url: page1,
      css: page1cssPath
  	}, function (err, result) {
      if(err) { done(err); }
      try {
        css.parse(result);
        done();
      } catch (ex) {
        done(ex);
      }
    });
	});

	it('should return a css file whose parsed AST is equal to the the original\'s AST when the viewport is large', function (done) {
		var widthLargerThanTotalTestCSS = 1000,
			heightLargerThanTotalTestCSS = 1000;
		penthouse({
			url    : page1,
			css : page1cssPath,
			width   : widthLargerThanTotalTestCSS,
			height  : heightLargerThanTotalTestCSS
		}, function (err, result) {
			if (err) {
				done(err);
				return;
			}
			try {
				var resultAst = normalisedCssAst(result);
				var expectedAst = normalisedCssAst(originalCss);
				resultAst.should.eql(expectedAst);
				done();
			} catch (ex) {
				done(ex);
			}

		});
	});

	it('should return a subset of the original AST rules when the viewport is small', function (done) {
		var widthLargerThanTotalTestCSS = 1000,
			heightSmallerThanTotalTestCSS = 100;
		penthouse({
			url    : page1,
			css : page1cssPath,
			width   : widthLargerThanTotalTestCSS,
			height  : heightSmallerThanTotalTestCSS
		}, function (err, result) {
			if (err) { done(err); }
			try {
				var resultAst = css.parse(result);
				var orgAst = css.parse(originalCss);
				resultAst.stylesheet.rules.should.have.length.lessThan(orgAst.stylesheet.rules.length);
				// not be empty
				done();
			} catch (ex) {
				done(ex);
			}

		});
	});

	/* future improvements
		- return parsingErrors from core; requires css@ >= 2.1, which requires Phantom@2.x
		- fault tolerant css parsing via postcss - also requires Phantom@2.x (and Node > 12 (?)) */
	it('should not crash on invalid css', function (done) {
		penthouse({
			url: page1,
			css: path.join(__dirname, 'static-server', 'invalid.css')
		}, function (err, result) {
			if(err) { done(err); }
			try {
				css.parse(result);
				done();
			} catch (ex) {
				done(ex);
			}
		});
	});

	it('should not crash on special chars', function (done) {
		penthouse({
			url: page1,
			css: path.join(__dirname, 'static-server', 'special-chars.css')
		}, function (err, result) {
			if(err) { done(err); }
			try {
				css.parse(result);
				done();
			} catch (ex) {
				done(ex);
			}
		});
	});
});

@@ -3155,13 +3155,284 @@
 %7D);%0A%09%7D);
+%0A%0A%09it('should exit after timeout', function (done) %7B%0A%09%09penthouse(%7B%0A%09%09%09url    : page1,%0A%09%09%09css : page1cssPath,%0A%09%09%09timeout: 100%0A%09%09%7D, function (err) %7B%0A%09%09%09if (err && /Penthouse timed out/.test(err)) done();%0A%09%09%09else %7B done(new Error('Did not get timeout error'));%7D%0A%09%09%7D);%0A%09%7D);%0A%0A
 %0A%7D);%0A

test for penthouse timeout