<commit_before>/* global describe, it, xit, before, beforeEach, after, afterEach */
/* jslint node: true, esnext: true */
"use strict";

const chai = require('chai'),
	assert = chai.assert,
	expect = chai.expect,
	should = chai.should(),
	ksm = require('kronos-service-manager'),
	testStep = require('kronos-test-step'),
	Flow = require('../lib/flow'),
	step = require('kronos-step');

const dummyFlow = {
	"myFlowName": {
		"type": "kronos-flow",
		"steps": {
			"slowInbound": {
				"type": "slow-start",
				"time": 10
			},
			"normal": {
				"type": "slow-start",
				"time": 5
			},
			"slowOutbound": {
				"type": "slow-start",
				"time": 70
			}
		}
	}
};

const autoStartFlow = {
	"myAutoStartFlow": {
		"type": "kronos-flow",
		"autostart": true,
		"steps": {
			"slowInbound": {
				"type": "slow-start",
				"time": 10
			}
		}
	}
};

describe('flow', () => {
	let manager;

	before('prepare manager', done => {
		ksm.manager({}, [require('../index')]).then(m => {
			manager = m;

			/**
			 * A dummy step for flow testing. The step will execute the stop
			 * and start command after the given amount of time
			 * @param stepName The name of this step
			 * @param time The time in millisecond it will last until the promise is fullfilled
			 */
			manager.registerStep(Object.assign({}, step.Step, {
				"name": "slow-start",

				initialize(manager, name, stepConfiguration, props) {
					props.time = {
						value: stepConfiguration.time
					};
					//console.log(`DummyStep: time: ${stepConfiguration.time}`);
				},

				_start() {
					return new Promise((fulfill, reject) =>
						setTimeout(() => fulfill(this), this.time));
				},

				_stop() {
					return new Promise((fulfill, reject) =>
						setTimeout(() => fulfill(this), this.time));
				}
			}));
			done();
		});
	});

	describe('autostart', () => {
		it("is false", () =>
			Flow.loadFlows(manager, dummyFlow).then(() => {
				const f = manager.flows.myFlowName;
				assert.equal(f.name, "myFlowName");
				assert.equal(f.autostart, false);
			})
		);

		it("is true", () =>
			Flow.loadFlows(manager, autoStartFlow).then(() => {
				const f = manager.flows.myAutoStartFlow;
				assert.equal(f.name, "myAutoStartFlow");
				assert.equal(f.autostart, true);
			})
		);
	});

	describe('livecycle', () => {
		before(done => {
			console.log(`A ${manager}`);
			Flow.loadFlows(manager, dummyFlow).then(() => {
				done();
			});
		});

		it("basic", done => {
			try {
				console.log(`B ${manager}`);
				testStep.checkStepLivecycle(manager, manager.flows.myFlowName, (step, state, livecycle, done) => {
					console.log('C');
					done();
				});
			} catch (e) {
				console.log(e);
			}
			setTimeout(() => done(), 500);
		});
	});<commit_msg>test: wip<commit_after>/* global describe, it, xit, before, beforeEach, after, afterEach */
/* jslint node: true, esnext: true */
"use strict";

const chai = require('chai'),
	assert = chai.assert,
	expect = chai.expect,
	should = chai.should(),
	ksm = require('kronos-service-manager'),
	testStep = require('kronos-test-step'),
	Flow = require('../lib/flow'),
	step = require('kronos-step');

describe('flow', () => {
	let manager;

	before('prepare manager', done => {
		ksm.manager({}, [require('../index')]).then(m => {
			manager = m;

			/**
			 * A dummy step for flow testing. The step will execute the stop
			 * and start command after the given amount of time
			 * @param stepName The name of this step
			 * @param time The time in millisecond it will last until the promise is fullfilled
			 */
			manager.registerStep(Object.assign({}, step.Step, {
				"name": "slow-start",

				initialize(manager, name, stepConfiguration, props) {
					props.time = {
						value: stepConfiguration.time
					};
					//console.log(`DummyStep: time: ${stepConfiguration.time}`);
				},

				_start() {
					return new Promise((fulfill, reject) =>
						setTimeout(() => fulfill(this), this.time));
				},

				_stop() {
					return new Promise((fulfill, reject) =>
						setTimeout(() => fulfill(this), this.time));
				}
			})).then(() => done());
		});
	});

	describe('autostart', () => {
		it("is false", () =>
			Flow.loadFlows(manager, {
				"myFlow": {
					"type": "kronos-flow",
					"steps": {
						"slowInbound": {
							"type": "slow-start",
							"time": 10
						}
					}
				}
			}).then(() => {
				const f = manager.flows.myFlow;
				assert.equal(f.name, "myFlow");
				assert.equal(f.autostart, false);
			})
		);

		it("is true", () =>
			Flow.loadFlows(manager, {
				"myAutoStartFlow": {
					"type": "kronos-flow",
					"autostart": true,
					"steps": {
						"slowInbound": {
							"type": "slow-start",
							"time": 10
						}
					}
				}
			}).then(() => {
				const f = manager.flows.myAutoStartFlow;
				assert.equal(f.name, "myAutoStartFlow");
				assert.equal(f.autostart, true);
			})
		);
	});

	describe('livecycle', () => {
		before(done => {
			Flow.loadFlows(manager, {
				"myFlowName": {
					"type": "kronos-flow",
					"steps": {
						"slowInbound": {
							"type": "slow-start",
							"time": 10
						},
						"normal": {
							"type": "slow-start",
							"time": 5
						},
						"slowOutbound": {
							"type": "slow-start",
							"time": 70
						},
						"optional-service": {
							"type": "slow-start",
							"endpoints": {
								/* TODO	"e1": {
										"target": "service:a1",
										"mandatory": false
									}*/
							}
						}
					}
				}
			}).then(() => {
				done();
			});
		});

		it("basic", done => {
			const s = manager.flows.myFlowName;

			s.start().then(() => s.stop().then(() => {
				done();
			}));

			// TODO how to call this without it()
			testStep.checkStepLivecycle(manager, s, (step, state, livecycle, done) => {
				console.log('C');
				done();
			});
		});
	});
{'fraction_non_alphanumeric': 0.19813760993274704, 'fraction_numerical': 0.0036213140196585617, 'mean_word_length': 2.625, 'pattern_counts': {'":': 52, '<': 3, '<?xml version=': 0, '>': 41, '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': '613215', 'n_tokens_mistral': 2621, 'n_tokens_neox': 1981, 'n_words': 417}