var fs = require('fs');
var path = require('path');
var tty = require('tty');
var expect = require('chai').expect;

var ttycolor = require('../..');
var outlog = path.join(__dirname, '..', '..', 'log', 'out.log');
var errlog = path.join(__dirname, '..', '..', 'log', 'err.log');

var stdout = {reader: null, writer: null},
  stderr = {reader: null, writer: null};

describe('ttycolor:', function() {
  beforeEach(function(done) {
    stdout.writer = fs.createWriteStream(outlog, {flags: 'w'});
    stdout.writer.on('open', function(fd) {
      done();
    });
  });
  it('should export properties', function(done) {
    expect(ttycolor.ansi).to.be.a('function');
    expect(ttycolor.attributes).to.be.an('object');
    expect(ttycolor.background).to.be.an('object');
    expect(ttycolor.colors).to.be.an('array');
    expect(ttycolor.console).to.be.an('object');
    expect(ttycolor.debug).to.be.a('function');
    expect(ttycolor.foreground).to.be.an('object');
    expect(ttycolor.stringify).to.be.a('function');
    done();
  });
  it('should write to stream', function(done) {
    var input = 'value';
    function cb(value) {
      expect(value).to.be.a('string').that.equals(input);
      var contents = fs.readFileSync(outlog);
      expect(contents.toString()).to.be.a('string').that.equals(input);
      stdout.writer.end();
      done();
    }
    console.write({stream: stdout.writer, callback: cb}, input);
  });
  it('should return empty string with no arguments', function(done) {
    var result = ttycolor.debug();
    expect(result).to.be.a('string').that.equals('');
    done();
  });
  it('should return vanilla string', function(done) {
    var input = 'value';
    var result = ttycolor.debug('%s', input);
    expect(result).to.be.a('string').that.equals(input);
    done();
  });
  it('should return vanilla number', function(done) {
    var input = 3.14;
    var result = ttycolor.debug('%s', input);
    expect(result).to.be.a('string').that.equals('' + input);
    done();
  });
  it('should return vanilla json', function(done) {
    var input = {message: 'json'};
    var result = ttycolor.debug('%j', input);
    expect(result).to.be.a('string').that.equals(JSON.stringify(input));
    done();
  });
  it('should handle extra format arguments', function(done) {
    var expected = 'foo:bar baz';
    var result = ttycolor.debug('%s:%s', 'foo', 'bar', 'baz');
    expect(result).to.be.a('string').that.equals(expected);
    done();
  });
  it('should handle too few format arguments', function(done) {
    var expected = 'foo:%s';
    var result = ttycolor.debug('%s:%s', 'foo');
    expect(result).to.be.a('string').that.equals(expected);
    done();
  });
  it('should handle non-string format', function(done) {
    var expected = '1 2 3';
    var result = ttycolor.debug(1, 2, 3);
    expect(result).to.be.a('string').that.equals(expected);
    done();
  });
  it('should handle undefined format', function(done) {
    var expected = 'undefined';
    var result = ttycolor.debug(undefined);
    expect(result).to.be.a('string').that.equals(expected);
    done();
  });
  it('should handle null format', function(done) {
    var expected = 'null';
    var result = ttycolor.debug(null);
    expect(result).to.be.a('string').that.equals(expected);
    done();
  });
  it('should handle true format', function(done) {
    var expected = 'true';
    var result = ttycolor.debug(true);
    expect(result).to.be.a('string').that.equals(expected);
    done();
  });
  it('should handle false format', function(done) {
    var expected = 'false';
    var result = ttycolor.debug(false);
    expect(result).to.be.a('string').that.equals(expected);
    done();
  });
  it('should handle object format', function(done) {
    var expected = '{}';
    var result = ttycolor.debug({});
    expect(result).to.be.a('string').that.equals(expected);
    done();
  });
  it('should handle array format', function(done) {
    var expected = '[]';
    var result = ttycolor.debug([]);
    expect(result).to.be.a('string').that.equals(expected);
    done();
  });
  it('should return escape sequence', function(done) {
    var input = 'value';
    var expected = '\u001b[1;37m' + input + '\u001b[0m';
    var result = ttycolor.stringify(
      input,
      ttycolor.foreground.white,
      ttycolor.attributes.bright);
      expect(result).to.be.a('string').that.equals(expected);
    done();
  });

})

@@ -49,34 +49,8 @@
 ');%0A
-var tty = require('tty');%0A
 var 
@@ -120,19 +120,16 @@
 ');%0Avar 
-out
 log = pa
@@ -186,152 +186,19 @@
 var 
-errlog = path.join(__dirname, '..', '..', 'log', 'err.log');%0A%0Avar stdout = %7Breader: null, writer: null%7D,%0A  stderr = %7Breader: null, writer: null%7D
+file = null
 ;%0A%0Ad
@@ -265,29 +265,20 @@
 ) %7B%0A    
-stdout.writer
+file
  = fs.cr
@@ -293,19 +293,16 @@
 eStream(
-out
 log, %7Bfl
@@ -317,29 +317,20 @@
 %7D);%0A    
-stdout.writer
+file
 .on('ope
@@ -1036,19 +1036,16 @@
 ileSync(
-out
 log);%0A  
@@ -1120,29 +1120,20 @@
 ;%0A      
-stdout.writer
+file
 .end();%0A
@@ -1183,21 +1183,12 @@
 am: 
-stdout.writer
+file
 , ca

Tidy test code.