<commit_before>/* global describe, it */
/* jslint node: true, esnext: true */

"use strict";

const fs = require('fs'),
	path = require('path'),
	uti = require('uti'),
	chai = require('chai'),
	assert = chai.assert,
	expect = chai.expect,
	should = chai.should(),
	streamEqual = require('stream-equal'),
	tmp = require('tmp'),
	testStep = require('kronos-test-step'),
	BaseStep = require('kronos-step'),
	file = require('../lib/steps/file');

const inFileName = path.join(__dirname, 'fixtures', 'file1.txt');

uti.initialize();

const manager = testStep.managerMock;
require('../index').registerWithManager(manager);

function makeEqualizer(done) {
	return function equalizer(err, equal) {
		assert(equal, "stream is equal to file content");
		done();
	};
}

describe('file', function () {
	describe('in', function () {
		describe('single', function () {
			const fileStep = file.createInstance(manager, undefined, {
				name: "myStep",
				type: "kronos-file",
				fileName: inFileName,
				endpoints: {
					"inout": {
						"in": false,
						"out": true,
						"active": true
					}
				}
			});

			const testEndpoint = BaseStep.createEndpoint('test', {
				"in": true,
				"passive": true
			});

			describe('start', function () {
				it("should produce a request", function (done) {
					let request;
					testEndpoint.receive(function* () {
						while (true) {
							request = yield;
						};
					});
					fileStep.endpoints.inout.connect(testEndpoint);

					fileStep.start().then(function (step) {
						try {
							assert.equal(fileStep.state, 'running');
							setTimeout(function () {
								assert.equal(request.info.name, inFileName);
								streamEqual(request.stream, fs.createReadStream(inFileName), makeEqualizer(done));
							}, 50);
						} catch (e) {
							done(e);
						}
					}, function () {
						done(`start failed`);
					});
				});
			});
		});
	});

	describe('out', function (done) {
		tmp.file(function (err, outFileName) {
			const fileStep = file.createInstance(manager, undefined, {
				name: "myStep",
				type: "kronos-file",
				fileName: outFileName,
				endpoints: {
					"inout": {
						"in": true,
						"passive": true
					}
				}
			});
			const testEndpoint = BaseStep.createEndpoint('test', {
				"out": true,
				"active": true
			});

			testEndpoint.connect(fileStep.endpoints.inout);

			describe('start', function () {
				it(`should create a file ${outFileName}`, function () {
					fileStep.start().then(function (step) {
						try {<commit_msg>test: some more std tests added<commit_after>/* global describe, it */
/* jslint node: true, esnext: true */

"use strict";

const fs = require('fs'),
	path = require('path'),
	uti = require('uti'),
	chai = require('chai'),
	assert = chai.assert,
	expect = chai.expect,
	should = chai.should(),
	streamEqual = require('stream-equal'),
	tmp = require('tmp'),
	testStep = require('kronos-test-step'),
	BaseStep = require('kronos-step'),
	file = require('../lib/steps/file');

const inFileName = path.join(__dirname, 'fixtures', 'file1.txt');

uti.initialize();

const manager = testStep.managerMock;
require('../index').registerWithManager(manager);

function makeEqualizer(done) {
	return function equalizer(err, equal) {
		assert(equal, "stream is equal to file content");
		done();
	};
}

describe('file', function () {
	describe('in', function () {
		describe('single', function () {
			const fileStep = file.createInstance(manager, undefined, {
				name: "myStep",
				type: "kronos-file",
				fileName: inFileName,
				endpoints: {
					"inout": {
						"in": false,
						"out": true,
						"active": true
					}
				}
			});

			const testEndpoint = BaseStep.createEndpoint('test', {
				"in": true,
				"passive": true
			});

			let request;
			testEndpoint.receive(function* () {
				while (true) {
					request = yield;
				};
			});
			fileStep.endpoints.inout.connect(testEndpoint);

			describe('static', function () {
				testStep.checkStepStatic(manager, fileStep);
			});

			describe('live-cycle', function () {
				testStep.checkStepLivecycle(manager, fileStep, function (step, state, livecycle, done) {
					done();
				});
			});

			describe('start', function () {
				it("should produce a request", function (done) {
					fileStep.endpoints.inout.connect(testEndpoint);

					fileStep.start().then(function (step) {
						try {
							assert.equal(fileStep.state, 'running');
							setTimeout(function () {
								assert.equal(request.info.name, inFileName);
								streamEqual(request.stream, fs.createReadStream(inFileName), makeEqualizer(done));
							}, 50);
						} catch (e) {
							done(e);
						}
					}, function () {
						done(`start failed`);
					});
				});
			});
		});
	});

	describe('out', function (done) {
		tmp.file(function (err, outFileName) {
			const fileStep = file.createInstance(manager, undefined, {
				name: "myStep",
				type: "kronos-file",
				fileName: outFileName,
				endpoints: {
					"inout": {
						"in": true,
						"passive": true
					}
				}
			});
			const testEndpoint = BaseStep.createEndpoint('test', {
				"out": true,
				"active": true
			});

			testEndpoint.connect(fileStep.endpoints.inout);


			describe('static', function () {
				testStep.checkStepStatic(manager, fileStep);
			});

			/* TODO why we get  Error: test:out:active: There was no 'inPassiveGenerator' set, so it could not return any
						describe('live-cycle', function () {
							testStep.checkStepLivecycle(manager, fileStep, function (step, state, livecycle, done) {
								done();
							});
						});
			*/

			describe('start', function () {
				it(`should create a file ${outFileName}`, function () {
					fileStep.start().then(function (step) {
						try {
{'fraction_non_alphanumeric': 0.1671610539172919, 'fraction_numerical': 0.0010469377072064212, 'mean_word_length': 3.100143061516452, 'pattern_counts': {'":': 22, '<': 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': '565748', 'n_tokens_mistral': 2344, 'n_tokens_neox': 1795, 'n_words': 383}