<commit_before>var util = require('util');
var PubSub = require('../lib/pubsub_service');
var Logger = require('../lib/logger');
var Runtime = require('../zetta_runtime');
var Scientist = require('zetta-scientist');
var assert = require('assert');
var SensorDriver = require('./fixture/sensor_driver');
var TestDriver = require('./fixture/example_driver');
var MemRegistry = require('./fixture/mem_registry');

describe('Driver', function() {
  var machine = null;
  var pubsub = null;
  var log = null;
  var reg = null;

  beforeEach(function(){
    reg = new MemRegistry();
    pubsub = new PubSub();
    log = new Logger({pubsub: pubsub});
    log.pubsub = pubsub;
    // create machine
    machine = Scientist.create(TestDriver);
    machine._pubsub = pubsub; // setup pubsub, log, registry
    machine._log = log;
    machine._registry = reg;

    // init machine
    machine = Scientist.init(machine);
  });

  it('should be attached to the zetta runtime', function() {
    assert.ok(Runtime.Device);
  });

  describe('Configuration', function() {
    it('should be configured by Scientist#configure', function() {
      assert.ok(machine.call);
      assert.equal(machine.type, 'testdriver');
      assert.equal(machine.state, 'ready');
      assert.equal(machine.name, 'Matt\'s Test Device');
    });

    it('should have an id automatically generated for it', function(){
      assert.ok(machine.id);
    });

    it('should have properties function', function() {
      assert.equal(typeof machine.properties, 'function');
    });

    it('properties function should return filtered property list', function() {
      machine._foo = 123;
      var p = machine.properties();
      assert.equal(p._foo, undefined);
    });

  });

  describe('Logging', function() {
    it('should expose a .log method', function() {
      assert.equal(typeof machine.log, 'function');
    });

    it('should have log level functions', function() {
      assert.ok(machine.log);
      assert.ok(machine.info);
      assert.ok(machine.warn);
      assert.ok(machine.error);
    });
  });


  describe('Transitions', function() {

    it('shuld not throw when calling an invalid transition name.', function(done) {
      machine.call('not-a-transition', function(err) {
        assert(err);
        done();
      });
    });

    it('should not throw when calling a transition not allowed in invalid state.', function(done) {
      machine.state = 'not-a-state';
      machine.call('prepare', function(err) {
        assert(err);
        done();
      });
    });

    it('avialable transitions should not throw when in invalid state.', function(done) {
      machine.state = 'not-a-state';
      machine.transitionsAvailable();
      done();
    });

    it('should change the state from ready to changed when calling change.', function(done) {
      machine.call('change', function() {
        assert.equal(machine.state, 'changed');<commit_msg>Added a test for the specific error given to transition callback for when device is in destroyed state.<commit_after>var util = require('util');
var PubSub = require('../lib/pubsub_service');
var Logger = require('../lib/logger');
var Runtime = require('../zetta_runtime');
var Scientist = require('zetta-scientist');
var assert = require('assert');
var SensorDriver = require('./fixture/sensor_driver');
var TestDriver = require('./fixture/example_driver');
var MemRegistry = require('./fixture/mem_registry');

describe('Driver', function() {
  var machine = null;
  var pubsub = null;
  var log = null;
  var reg = null;

  beforeEach(function(){
    reg = new MemRegistry();
    pubsub = new PubSub();
    log = new Logger({pubsub: pubsub});
    log.pubsub = pubsub;
    // create machine
    machine = Scientist.create(TestDriver);
    machine._pubsub = pubsub; // setup pubsub, log, registry
    machine._log = log;
    machine._registry = reg;

    // init machine
    machine = Scientist.init(machine);
  });

  it('should be attached to the zetta runtime', function() {
    assert.ok(Runtime.Device);
  });

  describe('Configuration', function() {
    it('should be configured by Scientist#configure', function() {
      assert.ok(machine.call);
      assert.equal(machine.type, 'testdriver');
      assert.equal(machine.state, 'ready');
      assert.equal(machine.name, '<NAME>');
    });

    it('should have an id automatically generated for it', function(){
      assert.ok(machine.id);
    });

    it('should have properties function', function() {
      assert.equal(typeof machine.properties, 'function');
    });

    it('properties function should return filtered property list', function() {
      machine._foo = 123;
      var p = machine.properties();
      assert.equal(p._foo, undefined);
    });

  });

  describe('Logging', function() {
    it('should expose a .log method', function() {
      assert.equal(typeof machine.log, 'function');
    });

    it('should have log level functions', function() {
      assert.ok(machine.log);
      assert.ok(machine.info);
      assert.ok(machine.warn);
      assert.ok(machine.error);
    });
  });


  describe('Transitions', function() {

    it('should not throw when calling an invalid transition name.', function(done) {
      machine.call('not-a-transition', function(err) {
        assert(err);
        done();
      });
    });

    it('should not throw when calling a transition when destroyed.', function(done) {
      machine.state = 'zetta-device-destroy';
      machine.call('prepare', function(err) {
        assert(err);
        assert.equal(err.message, 'Machine destroyed. Cannot use transition prepare');
        done();
      });
    });

    it('should not throw when calling a transition not allowed in invalid state.', function(done) {
      machine.state = 'not-a-state';
      machine.call('prepare', function(err) {
        assert(err);
        done();
      });
    });

    it('avialable transitions should not throw when in invalid state.', function(done) {
      machine.state = 'not-a-state';
      machine.transitionsAvailable();
      done();
    });

    it('should change the state from ready to changed when calling change.', function(done) {
      machine.call('change', function() {
        assert.equal(machine.state, 'changed');
{'fraction_non_alphanumeric': 0.15023100207105305, 'fraction_numerical': 0.0009558706388402103, 'mean_word_length': 3.1603711066931743, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6232184', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1716, 'n_words': 472}