/**
 * Module dependencies
 */

var util = require('util');
var assert = require('assert');
var tmp = require('tmp');
var _ = require('@sailshq/lodash');
var async = require('async');

var Filesystem = require('machinepack-fs');

var Sails = require('../../lib').constructor;

tmp.setGracefulCleanup();


describe('helpers :: ', function() {

  describe('basic usage :: ', function() {

    var curDir, tmpDir, sailsApp;

    before(function(done) {
      // Cache the current working directory.
      curDir = process.cwd();
      // Create a temp directory.
      tmpDir = tmp.dirSync({gracefulCleanup: true, unsafeCleanup: true});
      // Switch to the temp directory.
      process.chdir(tmpDir.name);

      Filesystem.writeSync({
        force: true,
        destination: 'api/helpers/greet.js',
        string: 'module.exports = { inputs: { name: { example: \'bob\', required: true } }, exits: { success: { outputExample: \'Hi, Bob!\'} }, fn: function (inputs, exits) { return exits.success(\'Hi, \' + inputs.name + \'!\'); } }'
      }).execSync();

      (new Sails()).load({
        hooks: {
          grunt: false, views: false, pubsub: false
        },
        log: {level: 'silent'},
        helpers: {
          moduleDefinitions: {
            ucase: { sync: true, inputs: { string: { example: 'Hi, Bob!', required: true } }, exits: { success: { outputExample: 'HI, BOB!'} }, fn: function(inputs, exits) { return exits.success(inputs.string.toUpperCase()); } }
          },
        }
      }, function(err, _sailsApp) {
        if (err) { return done(err); }
        sailsApp = _sailsApp;
        return done();
      });
    });

    after(function(done) {
      process.chdir(curDir);
      if (sailsApp) {sailsApp.lower(done);}
      else {
        return done();
      }
    });

    it('should load helpers from disk and merge them with programmatically added helpers', function() {
      assert.equal(_.keys(sailsApp.helpers).length, 2);
    });

    it('should load asynchronously helpers correctly', function(done) {
      sailsApp.helpers.greet({ name: 'Glen' }).switch({
        error: done,
        success: function ( result ) {
          assert.equal(result, 'Hi, Glen!');
          return done();
        }
      });
    });

    it('should load synchronously helpers correctly', function() {
      var result = sailsApp.helpers.ucase({ string: 'Hi, Glen!' }).execSync();
      assert.equal(result, 'HI, GLEN!');
    });

  });

});

@@ -1986,34 +1986,32 @@
 oad asynchronous
-ly
  helpers correct
@@ -2065,24 +2065,14 @@
 eet(
-%7B name: 
 'Glen'
- %7D
 ).sw
@@ -2130,20 +2130,170 @@
 on (
- result ) %7B%0A
+result1) %7B%0A          sailsApp.helpers.greet.with(%7B name: 'Glen' %7D).exec(function(err, result2) %7B%0A            if (err) %7B return done(err); %7D%0A            try %7B%0A    
     
@@ -2313,24 +2313,25 @@
 equal(result
+1
 , 'Hi, Glen!
@@ -2336,32 +2336,96 @@
 n!');%0A          
+ 
+   assert.equal(result2, result1);%0A            %7D catch (err) %7B 
 return done();%0A 
@@ -2416,26 +2416,31 @@
 return done(
-);
+err); %7D
 %0A        %7D%0A 
@@ -2428,33 +2428,82 @@
 err); %7D%0A        
-%7D
+    return done();%0A          %7D);//_%E2%88%8F_%0A        %7D//%3E
 %0A      %7D);%0A    %7D
@@ -2488,32 +2488,37 @@
   %7D//%3E%0A      %7D);
+//_%E2%88%8F_
 %0A    %7D);%0A%0A    it
@@ -2542,18 +2542,16 @@
 chronous
-ly
  helpers
@@ -2592,16 +2592,17 @@
 r result
+1
  = sails
@@ -2623,45 +2623,680 @@
 ase(
-%7B string: 'Hi, Glen!' %7D).execSync();%0A
+'Hi, Glen!');%0A      var result2 = sailsApp.helpers.ucase.with(%7B string: 'Hi, Glen!' %7D);%0A      assert.equal(result1, 'HI, GLEN!');%0A      assert.equal(result2, result1);%0A    %7D);%0A%0A    it('should support customization', function(done) %7B%0A      sailsApp.helpers.greet.customize(%7B arginStyle: 'named', execStyle: 'natural' %7D)(%7B name: 'Glen' %7D).then(function(result1)%7B%0A        assert.equal(result1, 'Hi, Glen!');%0A        var result2 = sailsApp.helpers.ucase.customize(%7B arginStyle: 'serial', execStyle: 'deferred' %7D)('Hi, Glen!').now();%0A        var result3 = sailsApp.helpers.ucase.customize(%7B arginStyle: 'serial', execStyle: 'deferred' %7D).with(%7B string: 'Hi, Glen!' %7D).now();%0A  
     
@@ -3316,16 +3316,17 @@
 l(result
+2
 , 'HI, G
@@ -3329,24 +3329,145 @@
 I, GLEN!');%0A
+        assert.equal(result3, result2);%0A        return done();%0A      %7D).catch (function(err)%7B return done(err); %7D);//_%E2%88%8F_%0A
     %7D);%0A%0A  %7D

Update tests to demonstrate new default expectations for helpers.