var Base58 = require('../lib/base58');
var should = require('chai').should();

describe('Base58', function() {
  var buf = new Buffer([0, 1, 2, 3, 253, 254, 255]);
  var enc = "1W7N4RuG";

  it('should make an instance with "new"', function() {
    var b58 = new Base58();
    should.exist(b58);
  });

  it('should make an instance without "new"', function() {
    var b58 = Base58();
    should.exist(b58);
  });

  describe('#set', function() {
    
    it('should set a blank buffer', function() {
      Base58().set({buf: new Buffer([])});
    });

  });

  describe('@encode', function() {

    it('should encode the buffer accurately', function() {
      Base58.encode(buf).should.equal(enc);
    });

    it('should throw an error when the Input is not a buffer', function() {
      (function() {
        Base58.encode("string")
      }).should.throw('Input should be a buffer');
    });

  });

  describe('@decode', function() {

    it('should decode this encoded value correctly', function() {
      Base58.decode(enc).toString('hex').should.equal(buf.toString('hex'));
    });

    it('should throw an error when Input is not a string', function() {
      (function() {
        Base58.decode(5);
      }).should.throw('Input should be a string');
    });

  });

  describe('#fromBuffer', function() {
    
    it('should not fail', function() {
      should.exist(Base58().fromBuffer(buf));
    });

    it('should set buffer', function() {
      var b58 = Base58().fromBuffer(buf);
      b58.buf.toString('hex').should.equal(buf.toString('hex'));
    });

  });

  describe('#fromString', function() {

    it('should convert this known string to a buffer', function() {
      Base58().fromString(enc).toBuffer().toString('hex').should.equal(buf.toString('hex'));
    });

  });

  describe('#toBuffer', function() {

    it('should return the buffer', function() {
      var b58 = Base58({buf: buf});
      b58.buf.toString('hex').should.equal(buf.toString('hex'));
    });

  });

  describe('#toString', function() {

    it('should return the buffer', function() {
      var b58 = Base58({buf: buf});
      b58.toString().should.equal(enc);
    });

  });

});