<commit_before>// promise-component-mocha.js

var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.should();
chai.use(chaiAsPromised);

var _ = require('underscore');
var noflo = require('noflo');
var promiseComponent = require('../components/promise-component.js');

describe('promise-component', function() {
    it("should reject undefined definition", function() {
        return Promise.resolve().then(promiseComponent).should.be.rejected;
    });
    it("should reject empty definition", function() {
        return Promise.resolve({}).then(promiseComponent).should.be.rejected;
    });
    it("should reject empty inPorts", function() {
        return Promise.resolve({inPorts:{}}).then(promiseComponent).should.be.rejected;
    });
    it("should reject empty resolvePort", function() {
        return Promise.resolve({inPorts:{in:{}},resolvePort:{}}).then(promiseComponent).should.be.rejected;
    });
    it("should reject empty rejectPort", function() {
        return Promise.resolve({inPorts:{in:{}},rejectPort:{}}).then(promiseComponent).should.be.rejected;
    });
    it("should trigger ondata function", function() {
        var handler;
        return Promise.resolve({
            inPorts:{
                'in':{
                    ondata: function(payload) {
                        handler(payload);
                    }
                }
            }
        }).then(promiseComponent).then(createComponent).then(function(component){
            // have the handler call a Promise resolve function to
            // check that the data sent on the in port is passed to the handler
            return new Promise(function(callback){
                handler = callback;
                sendData(component, 'in', "hello");
            });
        }).should.become("hello");
    });
    it("should resolve ondata function", function() {
        return Promise.resolve({
            inPorts:{
                'in':{
                    ondata: function(payload) {
                        return payload + " world";
                    }
                }
            }
        }).then(promiseComponent).then(createComponent).then(function(component){
            return onceData(component, 'out', 'error', 'in', "hello");
        }).should.become("hello world");
    });
    it("should reject ondata function", function() {
        return Promise.resolve({
            inPorts:{
                'in':{
                    ondata: function(payload) {
                        throw payload + " world";
                    }
                }
            }
        }).then(promiseComponent).then(createComponent).then(function(component){
            return onceData(component, 'out', 'error', 'in', "hello");
        }).should.be.rejectedWith("hello world");
    });
    it("should resolve ondata function on custom inPort name", function() {
        return Promise.resolve({
            inPorts:{
                incoming:{
                    ondata: function(payload) {
                        return payload + " world";
                    }
                }
            }
        }).then(promiseComponent).then(createComponent).then(function(component){
            return onceData(component, 'out', 'error', 'incoming', "hello");
        }).should.become("hello world");
    });
    it("should resolve ondata function on custom resolve port name", function() {
        return Promise.resolve({
            inPorts:{
                incoming:{
                    ondata: function(payload) {
                        return payload + " world";
                    }
                }
            },
            resolvePort: {
                name: 'outgoing'
            }
        }).then(promiseComponent).then(createComponent).then(function(component){
            return onceData(component, 'outgoing', 'error', 'incoming', "hello");
        }).should.become("hello world");
    });
    it("should reject ondata function on custom reject port name", function() {
        return Promise.resolve({
            inPorts:{
                incoming:{
                    ondata: function(payload) {
                        throw payload + " world";
                    }
                }
            },
            resolvePort: {
                name: 'outgoing'
            },
            rejectPort: {
                name: 'sentback'
            }
        }).then(promiseComponent).then(createComponent).then(function(component){
            return onceData(component, 'outgoing', 'sentback', 'incoming', "hello");
        }).should.be.rejectedWith("hello world");
    });
    function createComponent(getComponent) {
        var component = getComponent();
        _.forEach(component.inPorts, function(port, name) {
            port.nodeInstance = component;
            port.name = name;
        });
        _.forEach(component.outPorts, function(port, name) {
            port.nodeInstance = component;
            port.name = name;
        });
        return component;
    }
    function sendData(component, port, payload) {
        var socket = noflo.internalSocket.createSocket();
        component.inPorts[port].attach(socket);
        socket.send("hello");
        socket.disconnect();
        component.inPorts[port].detach(socket);
    }
    function onceData(component, resolvePort, rejectPort, sendPort, sendPayload) {
        return new Promise(function(resolve, reject) {
            var out = noflo.internalSocket.createSocket();
            var error = noflo.internalSocket.createSocket();
            component.outPorts[resolvePort].attach(out);
            component.outPorts[rejectPort].attach(error);
            out.once('data', resolve);<commit_msg>Use the provided payload in the unit test<commit_after>// promise-component-mocha.js

var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.should();
chai.use(chaiAsPromised);

var _ = require('underscore');
var noflo = require('noflo');
var promiseComponent = require('../components/promise-component.js');

describe('promise-component', function() {
    it("should reject undefined definition", function() {
        return Promise.resolve().then(promiseComponent).should.be.rejected;
    });
    it("should reject empty definition", function() {
        return Promise.resolve({}).then(promiseComponent).should.be.rejected;
    });
    it("should reject empty inPorts", function() {
        return Promise.resolve({inPorts:{}}).then(promiseComponent).should.be.rejected;
    });
    it("should reject empty resolvePort", function() {
        return Promise.resolve({inPorts:{in:{}},resolvePort:{}}).then(promiseComponent).should.be.rejected;
    });
    it("should reject empty rejectPort", function() {
        return Promise.resolve({inPorts:{in:{}},rejectPort:{}}).then(promiseComponent).should.be.rejected;
    });
    it("should trigger ondata function", function() {
        var handler;
        return Promise.resolve({
            inPorts:{
                'in':{
                    ondata: function(payload) {
                        handler(payload);
                    }
                }
            }
        }).then(promiseComponent).then(createComponent).then(function(component){
            // have the handler call a Promise resolve function to
            // check that the data sent on the in port is passed to the handler
            return new Promise(function(callback){
                handler = callback;
                sendData(component, 'in', "hello");
            });
        }).should.become("hello");
    });
    it("should resolve ondata function", function() {
        return Promise.resolve({
            inPorts:{
                'in':{
                    ondata: function(payload) {
                        return payload + " world";
                    }
                }
            }
        }).then(promiseComponent).then(createComponent).then(function(component){
            return onceData(component, 'out', 'error', 'in', "hello");
        }).should.become("hello world");
    });
    it("should reject ondata function", function() {
        return Promise.resolve({
            inPorts:{
                'in':{
                    ondata: function(payload) {
                        throw payload + " world";
                    }
                }
            }
        }).then(promiseComponent).then(createComponent).then(function(component){
            return onceData(component, 'out', 'error', 'in', "hello");
        }).should.be.rejectedWith("hello world");
    });
    it("should resolve ondata function on custom inPort name", function() {
        return Promise.resolve({
            inPorts:{
                incoming:{
                    ondata: function(payload) {
                        return payload + " world";
                    }
                }
            }
        }).then(promiseComponent).then(createComponent).then(function(component){
            return onceData(component, 'out', 'error', 'incoming', "hello");
        }).should.become("hello world");
    });
    it("should resolve ondata function on custom resolve port name", function() {
        return Promise.resolve({
            inPorts:{
                incoming:{
                    ondata: function(payload) {
                        return payload + " world";
                    }
                }
            },
            resolvePort: {
                name: 'outgoing'
            }
        }).then(promiseComponent).then(createComponent).then(function(component){
            return onceData(component, 'outgoing', 'error', 'incoming', "hello");
        }).should.become("hello world");
    });
    it("should reject ondata function on custom reject port name", function() {
        return Promise.resolve({
            inPorts:{
                incoming:{
                    ondata: function(payload) {
                        throw payload + " world";
                    }
                }
            },
            resolvePort: {
                name: 'outgoing'
            },
            rejectPort: {
                name: 'sentback'
            }
        }).then(promiseComponent).then(createComponent).then(function(component){
            return onceData(component, 'outgoing', 'sentback', 'incoming', "hello");
        }).should.be.rejectedWith("hello world");
    });
    function createComponent(getComponent) {
        var component = getComponent();
        _.forEach(component.inPorts, function(port, name) {
            port.nodeInstance = component;
            port.name = name;
        });
        _.forEach(component.outPorts, function(port, name) {
            port.nodeInstance = component;
            port.name = name;
        });
        return component;
    }
    function sendData(component, port, payload) {
        var socket = noflo.internalSocket.createSocket();
        component.inPorts[port].attach(socket);
        socket.send(payload);
        socket.disconnect();
        component.inPorts[port].detach(socket);
    }
    function onceData(component, resolvePort, rejectPort, sendPort, sendPayload) {
        return new Promise(function(resolve, reject) {
            var out = noflo.internalSocket.createSocket();
            var error = noflo.internalSocket.createSocket();
            component.outPorts[resolvePort].attach(out);
            component.outPorts[rejectPort].attach(error);
            out.once('data', resolve);
{'fraction_non_alphanumeric': 0.12924577599721304, 'fraction_numerical': 0.0, 'mean_word_length': 1.8982836951034832, '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': '2740192', 'n_tokens_mistral': 2886, 'n_tokens_neox': 2803, 'n_words': 590}