<commit_before>var should  = require('should')
  , Disco   = require('../../lib/disco')
  , ltx     = require('ltx')
  , helper  = require('../helper')

describe('Disco', function() {

    var disco, socket, xmpp, manager

    before(function() {
        socket = new helper.Eventer()
        xmpp = new helper.Eventer()
        manager = {
            socket: socket,
            client: xmpp,
            trackId: function(id, callback) {
                this.callback = callback
            },
            makeCallback: function(error, data) {
                this.callback(error, data)
            }
        }
        disco = new Disco()
        disco.init(manager)
    })

    describe('Can handle incoming requests', function() {
    
        it('Shouldn\'t handle non-IQ stanzas', function() {
            disco.handles(ltx.parse('<message/>')).should.be.false
        })

        it('Shouldn\'t handle IQ without a <query/> child', function() {
            disco.handles(ltx.parse('<iq />')).should.be.false
        })

        it('Shouldn\'t handle query without appropriate xmlns', function() {
            var query = ltx.parse('<iq><query xmlns="not-disco"/></iq>')
            disco.handles(query).should.be.false
        })

        it('Should handle DISCO#info requests', function() {
            var query = ltx.parse('<iq><query xmlns="' + disco.NS_INFO + '" /></iq>')
            disco.handles(query).should.be.true
        })

        it('Sends DISCO#info requests to client', function(done) {
            var request = '<iq id="1" from="<EMAIL>" type="get">'
                + '<query xmlns="' + disco. NS_INFO + '" /></iq>'
            xmpp.once('stanza', function(stanza) {
                stanza.attrs.type.should.equal('result')
                var query = stanza.getChild('query')
                should.exist(query)
                query.attrs.xmlns.should.equal(disco.NS_INFO)
                query.children.length.should.equal(2)
                var children = query.children
                children[0].name.should.equal('kind1')
                children[0].attrs.name.should.equal('name1')
                children[0].attrs.category.should.equal('cat1')
                children[0].attrs.var.should.equal('var1')
                children[0].attrs.jid.should.equal('jid1')
                children[0].attrs.node.should.equal('node1')
                children[1].name.should.eql('kind2')
                done()
            })
            socket.once('xmpp.discover.client', function(data, callback) {
                var features = [
                    { kind: 'kind1', name: 'name1', category: 'cat1', var: 'var1', jid: 'jid1', node: 'node1' },
                    { kind: 'kind2' },
                    {}
                ]
                callback(features)
            })
            disco.handle(ltx.parse(request)).should.be.true            
        })

        describe('Can make DISCO#items requests', function() {

            it('Should error when no \'of\' property passed', function(done) {
                xmpp.once('stanza', function() {
                    done('Unexpected outgoing stanza')
                })
                socket.emit('xmpp.discover.items', {}, function(error, success) {
                    should.not.exist(success)
                    error.type.should.equal('modify')
                    error.condition.should.equal('client-error')
                    error.description.should.equal("Missing 'of' key")
                    error.request.should.eql({})
                    xmpp.removeAllListeners('stanza')
                    done()
                })
            })

            it('Errors when no callback provided', function(done) {
                xmpp.once('stanza', function() {
                    done('Unexpected outgoing stanza')
                })
                socket.once('xmpp.error.client', function(error) {
                    error.type.should.equal('modify')
                    error.condition.should.equal('client-error')
                    error.description.should.equal("Missing callback")
                    error.request.should.eql({ of: 'example.com' })
                    xmpp.removeAllListeners('stanza')
                    done()
                })
                socket.emit('xmpp.discover.items', { of: 'example.com' })
            })

            it('Errors when non-function callback provided', function(done) {
                xmpp.once('stanza', function() {
                    done('Unexpected outgoing stanza')
                })
                socket.once('xmpp.error.client', function(error) {
                    error.type.should.equal('modify')
                    error.condition.should.equal('client-error')
                    error.description.should.equal("Missing callback")
                    error.request.should.eql({ of: 'example.com' })
                    xmpp.removeAllListeners('stanza')
                    done()
                })
                socket.emit('xmpp.discover.items', { of: 'example.com' }, true)
            })

            it('Can handle error response from server', function(done) {
                var of = 'wonderland.lit'
                xmpp.once('stanza', function(stanza) {<commit_msg>Tests for outgoing stanza. Fixes #2.<commit_after>var should  = require('should')
  , Disco   = require('../../lib/disco')
  , ltx     = require('ltx')
  , helper  = require('../helper')

describe('Disco', function() {

    var disco, socket, xmpp, manager

    before(function() {
        socket = new helper.Eventer()
        xmpp = new helper.Eventer()
        manager = {
            socket: socket,
            client: xmpp,
            trackId: function(id, callback) {
                this.callback = callback
            },
            makeCallback: function(error, data) {
                this.callback(error, data)
            }
        }
        disco = new Disco()
        disco.init(manager)
    })

    describe('Can handle incoming requests', function() {
    
        it('Shouldn\'t handle non-IQ stanzas', function() {
            disco.handles(ltx.parse('<message/>')).should.be.false
        })

        it('Shouldn\'t handle IQ without a <query/> child', function() {
            disco.handles(ltx.parse('<iq />')).should.be.false
        })

        it('Shouldn\'t handle query without appropriate xmlns', function() {
            var query = ltx.parse('<iq><query xmlns="not-disco"/></iq>')
            disco.handles(query).should.be.false
        })

        it('Should handle DISCO#info requests', function() {
            var query = ltx.parse('<iq><query xmlns="' + disco.NS_INFO + '" /></iq>')
            disco.handles(query).should.be.true
        })

        it('Sends DISCO#info requests to client', function(done) {
            var request = '<iq id="1" from="<EMAIL>" type="get">'
                + '<query xmlns="' + disco. NS_INFO + '" /></iq>'
            xmpp.once('stanza', function(stanza) {
                stanza.attrs.type.should.equal('result')
                var query = stanza.getChild('query')
                should.exist(query)
                query.attrs.xmlns.should.equal(disco.NS_INFO)
                query.children.length.should.equal(2)
                var children = query.children
                children[0].name.should.equal('kind1')
                children[0].attrs.name.should.equal('name1')
                children[0].attrs.category.should.equal('cat1')
                children[0].attrs.var.should.equal('var1')
                children[0].attrs.jid.should.equal('jid1')
                children[0].attrs.node.should.equal('node1')
                children[1].name.should.eql('kind2')
                done()
            })
            socket.once('xmpp.discover.client', function(data, callback) {
                var features = [
                    { kind: 'kind1', name: 'name1', category: 'cat1', var: 'var1', jid: 'jid1', node: 'node1' },
                    { kind: 'kind2' },
                    {}
                ]
                callback(features)
            })
            disco.handle(ltx.parse(request)).should.be.true            
        })

        describe('Can make DISCO#items requests', function() {

            it('Should error when no \'of\' property passed', function(done) {
                xmpp.once('stanza', function() {
                    done('Unexpected outgoing stanza')
                })
                socket.emit('xmpp.discover.items', {}, function(error, success) {
                    should.not.exist(success)
                    error.type.should.equal('modify')
                    error.condition.should.equal('client-error')
                    error.description.should.equal("Missing 'of' key")
                    error.request.should.eql({})
                    xmpp.removeAllListeners('stanza')
                    done()
                })
            })

            it('Errors when no callback provided', function(done) {
                xmpp.once('stanza', function() {
                    done('Unexpected outgoing stanza')
                })
                socket.once('xmpp.error.client', function(error) {
                    error.type.should.equal('modify')
                    error.condition.should.equal('client-error')
                    error.description.should.equal("Missing callback")
                    error.request.should.eql({ of: 'example.com' })
                    xmpp.removeAllListeners('stanza')
                    done()
                })
                socket.emit('xmpp.discover.items', { of: 'example.com' })
            })

            it('Errors when non-function callback provided', function(done) {
                xmpp.once('stanza', function() {
                    done('Unexpected outgoing stanza')
                })
                socket.once('xmpp.error.client', function(error) {
                    error.type.should.equal('modify')
                    error.condition.should.equal('client-error')
                    error.description.should.equal("Missing callback")
                    error.request.should.eql({ of: 'example.com' })
                    xmpp.removeAllListeners('stanza')
                    done()
                })
                socket.emit('xmpp.discover.items', { of: 'example.com' }, true)
            })

            it('Sends expected stanza', function(done) {
                var of = 'wonderland.lit'
                xmpp.once('stanza', function(stanza) {
                    stanza.is('iq').should.be.true
                    stanza.attrs.to.should.equal(of)
                    stanza.getChild('query', disco.NS_ITEMS)
                        .should.exist
                    done()
                })
                socket.emit('xmpp.discover.items', { of: of }, function() {})
            })

            it('Sends expected stanza with node', function(done) {
                var request = { of: 'wonderland.lit', node: 'some-node' }
                xmpp.once('stanza', function(stanza) {
                    stanza.getChild('query', disco.NS_ITEMS)
                        .attrs.node.should.equal(request.node)
                    done()
                })
                socket.emit('xmpp.discover.items', request, function() {})
            })

            it('Can handle error response from server', function(done) {
                var of = 'wonderland.lit'
                xmpp.once('stanza', function(stanza) {
{'fraction_non_alphanumeric': 0.13904629064597682, 'fraction_numerical': 0.004097288815273298, 'mean_word_length': 1.5080891998250985, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '304242', 'n_tokens_mistral': 3167, 'n_tokens_neox': 3005, 'n_words': 521}