var buster = require('buster-node'),
  fs = require('fs'),
  hid = require('node-hid'),
  referee = require('referee'),
  UsbHid = require('../../lib/drivers/usbhid'),
  assert = referee.assert;

buster.testCase('usbhid - usbhid', {
  setUp: function () {
    this.mockHid = this.mock(hid);
  },
  'should set device when found': function () {
    var devices = [
      { vendorId: 0x0fc5, productId: 0xb080, path: 'somepath' }
    ];
    this.mockHid.expects('HID').once().withExactArgs('somepath').returns(devices[0]);
    this.mockHid.expects('devices').once().withExactArgs().returns(devices);
    var usbhid = new UsbHid();
    assert.equals(usbhid.device.vendorId, 0x0fc5);
    assert.equals(usbhid.device.productId, 0xb080);
    assert.equals(usbhid.device.path, 'somepath');
    assert.equals(usbhid.dataMap.off, '\x00');
    assert.equals(usbhid.dataMap.green, '\x01');
    assert.equals(usbhid.dataMap.red, '\x02');
    assert.equals(usbhid.dataMap.blue, '\x04');
    assert.equals(usbhid.dataMap.yellow, '\x04');
  },
  'should throw error when no device exists': function () {
    var devices = [
    ];
    this.mockHid.expects('devices').once().withExactArgs().returns(devices);
    try {
      new UsbHid();
    } catch (e) {
      assert.equals(e.message, 'Unable to find build light device via USB HID');
    }
  },
  'should throw error when no device matches': function () {
    var devices = [
      { vendorId: 0x0fc6, productId: 0xb081, path: 'somepath' }
    ];
    this.mockHid.expects('devices').once().withExactArgs().returns(devices);
    try {
      new UsbHid();
    } catch (e) {
      assert.equals(e.message, 'Unable to find build light device via USB HID');
    }
  }
});

buster.testCase('usbhid - on off', {
  setUp: function () {
    function mockWrite(data) {
    }
    var devices = [
      { vendorId: 0x0fc5, productId: 0xb080, path: 'somepath', write: mockWrite }
    ];

    this.mockHid = this.mock(hid);
    this.mockHid.expects('HID').once().withExactArgs('somepath').returns(devices[0]);
    this.mockHid.expects('devices').once().withExactArgs().returns(devices);

    this.usbHid = new UsbHid('/some/path');
  },
  'on should write correct data based on colour': function (done) {
    this.usbHid._write = function(data) {
      assert.equals(data, '\x02');
      done();
    };
    this.usbHid.on('red');
  },
  'off should write correct data based for switching off colours': function (done) {
    this.usbHid._write = function(data) {
      assert.equals(data, '\x00');
      done();
    };
    this.usbHid.off('red');
  },
  'write should write data to device': function () {
    this.usbHid._write('\x02');
  }
});

@@ -420,32 +420,65 @@
 epath' %7D%0A    %5D;%0A
+    this.stub(this.mockHid.HID);%0A
     this.mockHid
@@ -1833,37 +1833,33 @@
   %7D%0A    
-var d
+this.mockD
 evice
-s
  =
- %5B%0A     
  %7B vendo
@@ -1930,225 +1930,224 @@
 te %7D
+;
 %0A  
-  %5D;%0A%0A    this.mockHid = this.mock(hid);%0A    this.mockHid.expects('HID').once().withExactArgs('somepath').returns(devices%5B0%5D);%0A    this.mockHid.expects('devices').once().withExactArgs().returns(devices
+%7D,%0A  'on should write correct data based on colour': function (done) %7B%0A    var self = this;%0A    this.stub(UsbHid.prototype, '_find', function (vendorId, productId) %7B%0A      return self.mockDevice;%0A    %7D
 );%0A
-%0A
     
-this.
+var 
 usbH
@@ -2183,88 +2183,10 @@
 ;%0A  
-%7D,%0A  'on should write correct data based on colour': function (done) %7B%0A    this.
+  
 usbH
@@ -2266,37 +2266,32 @@
 e();%0A    %7D;%0A    
-this.
 usbHid.on('red')
@@ -2378,37 +2378,208 @@
 on (done) %7B%0A    
-this.
+var self = this;%0A    this.stub(UsbHid.prototype, '_find', function (vendorId, productId) %7B%0A      return self.mockDevice;%0A    %7D);%0A    var usbHid = new UsbHid('/some/path');%0A    
 usbHid._write = 
@@ -2651,29 +2651,24 @@
 %0A    %7D;%0A    
-this.
 usbHid.off('
@@ -2724,32 +2724,36 @@
 : function (
+done
 ) %7B%0A    
 this.usbHid.
@@ -2740,21 +2740,343 @@
 ) %7B%0A    
-this.
+function mockWrite(data) %7B%0A      assert.isArray(data);%0A      done();%0A    %7D%0A    var mockDevice = %7B vendorId: 0x0fc5, productId: 0xb080, path: 'somepath', write: mockWrite %7D;%0A    this.stub(UsbHid.prototype, '_find', function (vendorId, productId) %7B%0A      return mockDevice;%0A    %7D);%0A    var usbHid = new UsbHid('/some/path');%0A    
 usbHid._

Refactor test to avoid calling hid api.