<commit_before>var path = require('path');
var assert = require('assert');
global.ProjRequire = function(module) {
	return require(path.join(__dirname, '/../' + module)); 
}

describe('modFlow', function() {
  this.timeout(15000);
  var modFlow = ProjRequire('app/lib/modBrowser/modFlow.js');
  var executeWebform = function(item,done) {
	    var ctx = {}; // context object
		ctx.item = item;
		ctx.flows = {};
		ctx.webforms = {};
		ctx.pages = {};
		ctx.vars = {};
		ctx.blobVars = {};
		ctx._logs = [];
		ctx.FLOW_ENGINE_CANCELED_notification_queues = [];
		ctx.createFlowEngine = function(flow) {
			if(typeof flow != 'undefined') {
				if(typeof flow == 'object') {
					// flow object
					return new modFlow.FlowEngine(flow).setContext(ctx);
				}
				else if(typeof flow == 'string') {
					// flow name
					if(typeof ctx.flows[flow] != 'undefined') {
						return new modFlow.FlowEngine(ctx.flows[flow]).setContext(ctx);
					}
				}
			}
			// return dummy function for silent execution
			return {
				execute : function(next) {
					if(next.length == 1) {
						setTimeout(function() {
							next({});
						}, 1);
					}
					else {
						setTimeout(next, 1);
					}
				}
				,
				setInputVars : function(_vars){
					return this;
				}
			};
		}
		ctx.showItemWebform = function() {}
		//ctx.showCategory = function() {}
		//ctx.showWebView = function() {}
		// start simulation
		ctx._testRanCodes = [];
		ctx.wv = {
			ios : {
				stringByEvaluatingJavaScriptFromString : function(code) {
					ctx._testRanCodes.push(code);
					if(typeof item._testValue != 'undefined') {
						return item._testValue;
					}
				}
			},
			runJS : function(code, next) {
				ctx._testRanCodes.push(code);
				
				if(next) {
					setTimeout(function() {
						if(next.length == 1) {
							if(typeof item._testValue != 'undefined') {
								next(item._testValue);
							}
						}
						else {
							next();
						}
					}, 1);
				}
			},
			runJSSync : function(code, next) {
				ctx._testRanCodes.push(code);
				if(typeof item._testValue != 'undefined') {
					return item._testValue;
				}
			}
		};
		ctx._urls = [];
		ctx.showWebView = function(url) {
			ctx._urls.push(url);
		}
		ctx.showCategory = function(url) {
			ctx._urls.push(url);
		}
		// end simulation

		// #47 iterate all webform level flows and put into context flow collection
		if(typeof item.flows != 'undefined') {
			for(var i in item.flows) {
				ctx.flows[i] = item.flows[i];
			}
		}

		var flow = item.flow;
		// #47 FlowEngine webform level
		ctx.createFlowEngine(flow).execute(function() {
			done(ctx);
		});
  }
  
	
  describe('#setVar', function() {
	it('should able to setVar', function(done) {
		var webform = {
			heading:'test form',
			params: [],
			flow : {
				steps: [
					{type:'setVar',name:'apple',value:'test'},
					{type:'setVar',name:'result',value:'test ##apple##'},
					{type:'setVar',name:'orange',value:'test2',local:1},
					{type:'setVar',name:'result2',value:'test2 ##orange##'},
				]
			}
		};
		executeWebform(webform, function(ctx) {
			assert.equal(ctx.vars["apple"],"test");
			assert.equal(ctx.vars["result"],"test test");
			assert.equal(ctx._vars["orange"],"test2");
			assert.equal(ctx.vars["result2"],"test2 test2");
			done();
		});
    });
  });
  describe('#dummy', function() {
	it('should able to perform dummy flow', function(done) {
		var webform = {
			heading:'test form',
			params: [],
			flow : {
				steps: [
					{type:'dummy'}
				]
			}
		};
		executeWebform(webform, function(ctx) {
			done();
		});
    });
  });
  describe('#blank flow', function() {
	it('should able to perform flow for zero steps', function(done) {
		var webform = {
			heading:'test form',
			params: [],
			flow : {
				steps: [
					
				]
			}
		};
		executeWebform(webform, function(ctx) {
			done();
		});
    });
  });
  describe('#subflow', function() {
	it('should able to call subflows', function(done) {
		var webform = {
			heading:'test form',
			params: [],
			flow : {
				steps: [
					{type:'setVar',name:'apple',value:'test'},
					{type:'setVar',name:'result',value:'test ##apple##'},
					{type:'setVar',name:'someInput',value:'test4'},<commit_msg>should able to bypass flow if steps not defined<commit_after>var path = require('path');
var assert = require('assert');
global.ProjRequire = function(module) {
	return require(path.join(__dirname, '/../' + module)); 
}

describe('modFlow', function() {
  this.timeout(15000);
  var modFlow = ProjRequire('app/lib/modBrowser/modFlow.js');
  var executeWebform = function(item,done) {
	    var ctx = {}; // context object
		ctx.item = item;
		ctx.flows = {};
		ctx.webforms = {};
		ctx.pages = {};
		ctx.vars = {};
		ctx.blobVars = {};
		ctx._logs = [];
		ctx.FLOW_ENGINE_CANCELED_notification_queues = [];
		ctx.createFlowEngine = function(flow) {
			if(typeof flow != 'undefined') {
				if(typeof flow == 'object') {
					// flow object
					return new modFlow.FlowEngine(flow).setContext(ctx);
				}
				else if(typeof flow == 'string') {
					// flow name
					if(typeof ctx.flows[flow] != 'undefined') {
						return new modFlow.FlowEngine(ctx.flows[flow]).setContext(ctx);
					}
				}
			}
			// return dummy function for silent execution
			return {
				execute : function(next) {
					if(next.length == 1) {
						setTimeout(function() {
							next({});
						}, 1);
					}
					else {
						setTimeout(next, 1);
					}
				}
				,
				setInputVars : function(_vars){
					return this;
				}
			};
		}
		ctx.showItemWebform = function() {}
		//ctx.showCategory = function() {}
		//ctx.showWebView = function() {}
		// start simulation
		ctx._testRanCodes = [];
		ctx.wv = {
			ios : {
				stringByEvaluatingJavaScriptFromString : function(code) {
					ctx._testRanCodes.push(code);
					if(typeof item._testValue != 'undefined') {
						return item._testValue;
					}
				}
			},
			runJS : function(code, next) {
				ctx._testRanCodes.push(code);
				
				if(next) {
					setTimeout(function() {
						if(next.length == 1) {
							if(typeof item._testValue != 'undefined') {
								next(item._testValue);
							}
						}
						else {
							next();
						}
					}, 1);
				}
			},
			runJSSync : function(code, next) {
				ctx._testRanCodes.push(code);
				if(typeof item._testValue != 'undefined') {
					return item._testValue;
				}
			}
		};
		ctx._urls = [];
		ctx.showWebView = function(url) {
			ctx._urls.push(url);
		}
		ctx.showCategory = function(url) {
			ctx._urls.push(url);
		}
		// end simulation

		// #47 iterate all webform level flows and put into context flow collection
		if(typeof item.flows != 'undefined') {
			for(var i in item.flows) {
				ctx.flows[i] = item.flows[i];
			}
		}

		var flow = item.flow;
		// #47 FlowEngine webform level
		ctx.createFlowEngine(flow).execute(function() {
			done(ctx);
		});
  }
  
	
  describe('#setVar', function() {
	it('should able to setVar', function(done) {
		var webform = {
			heading:'test form',
			params: [],
			flow : {
				steps: [
					{type:'setVar',name:'apple',value:'test'},
					{type:'setVar',name:'result',value:'test ##apple##'},
					{type:'setVar',name:'orange',value:'test2',local:1},
					{type:'setVar',name:'result2',value:'test2 ##orange##'},
				]
			}
		};
		executeWebform(webform, function(ctx) {
			assert.equal(ctx.vars["apple"],"test");
			assert.equal(ctx.vars["result"],"test test");
			assert.equal(ctx._vars["orange"],"test2");
			assert.equal(ctx.vars["result2"],"test2 test2");
			done();
		});
    });
  });
  describe('#dummy', function() {
	it('should able to perform dummy flow', function(done) {
		var webform = {
			heading:'test form',
			params: [],
			flow : {
				steps: [
					{type:'dummy'}
				]
			}
		};
		executeWebform(webform, function(ctx) {
			done();
		});
    });
  });
  describe('#blank flow', function() {
	it('should able to perform flow for zero steps', function(done) {
		var webform = {
			heading:'test form',
			params: [],
			flow : {
				steps: [
					
				]
			}
		};
		executeWebform(webform, function(ctx) {
			done();
		});
    });
	it('should able to bypass flow if steps not defined', function(done) {
		var webform = {
			heading:'test form',
			params: [],
			flow : {
				
			}
		};
		executeWebform(webform, function(ctx) {
			done();
		});
    });
  });
  describe('#subflow', function() {
	it('should able to call subflows', function(done) {
		var webform = {
			heading:'test form',
			params: [],
			flow : {
				steps: [
					{type:'setVar',name:'apple',value:'test'},
					{type:'setVar',name:'result',value:'test ##apple##'},
					{type:'setVar',name:'someInput',value:'test4'},
{'fraction_non_alphanumeric': 0.1800069500752925, 'fraction_numerical': 0.00532839105756979, 'mean_word_length': 2.8373333333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '876713', 'n_tokens_mistral': 3721, 'n_tokens_neox': 2922, 'n_words': 528}