<commit_before>'use strict';

/* jshint -W030 */

var should        = require('should')
  , MultiUserChat = require('../../index')
  , helper        = require('../helper')
  , dataForm      = require('xmpp-ftw').utils['xep-0004']

describe('Room configuration', function() {

    var muc, socket, xmpp, manager

    before(function() {
        socket = new helper.SocketEventer()
        xmpp = new helper.XmppEventer()
        manager = {
            socket: socket,
            client: xmpp,
            trackId: function(id, callback) {
                this.callback = callback
            },
            makeCallback: function(error, data) {
                this.callback(error, data)
            }
        }
        muc = new MultiUserChat()
        muc.init(manager)
    })

    beforeEach(function() {
        socket.removeAllListeners()
        xmpp.removeAllListeners()
        muc.init(manager)
    })

    describe('Get configuration', function() {

        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({})
                xmpp.removeAllListeners('stanza')
                done()
            })
            socket.send('xmpp.muc.room.config.get', {})
        })

        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({})
                xmpp.removeAllListeners('stanza')
                done()
            })
            socket.send('xmpp.muc.room.config.get', {}, true)
        })

        it('Errors if \'room\' key missing', function(done) {
            xmpp.once('stanza', function() {
                done('Unexpected outgoing stanza')
            })
            var callback = function(error, success) {
                should.not.exist(success)
                error.type.should.equal('modify')
                error.condition.should.equal('client-error')
                error.description.should.equal('Missing \'room\' key')
                error.request.should.eql(request)
                xmpp.removeAllListeners('stanza')
                done()
            }
            var request = {}
            socket.send('xmpp.muc.room.config.get', request, callback)
        })

        it('Handles error response stanza', function(done) {
            var room = '<EMAIL>'
            xmpp.once('stanza', function(stanza) {
                stanza.is('iq').should.be.true
                stanza.attrs.type.should.equal('get')
                stanza.attrs.to.should.equal(room)
                should.exist(stanza.attrs.id)
                stanza.getChild('query', muc.NS_OWNER).should.exist
                manager.makeCallback(helper.getStanza('iq-error'))
            })
            var callback = function(error, success) {
                should.not.exist(success)
                error.should.eql({
                    type: 'cancel',
                    condition: 'error-condition'
                })
                done()
            }
            socket.send(
                'xmpp.muc.room.config.get',
                { room: room },
                callback
            )
        })

        it('Returns room configuration', function(done) {
            var room = '<EMAIL>'
            xmpp.once('stanza', function(stanza) {
                stanza.is('iq').should.be.true
                stanza.attrs.type.should.equal('get')
                stanza.attrs.to.should.equal(room)
                should.exist(stanza.attrs.id)
                stanza.getChild('query', muc.NS_OWNER).should.exist
                manager.makeCallback(helper.getStanza('config-get-result'))
            })
            var callback = function(error, data) {
                should.not.exist(error)
                data.title.should.equal('Configuration for "fire" Room')
                data.instructions.should.equal(
                    'Use this form to update room configuration'
                )
                data.fields.length.should.equal(1)
                data.fields[0].label
                    .should.equal('Short Description of Room')
                data.fields[0].type = 'text-single'
                data.fields[0].var = 'muc#roomconfig_roomdesc'
                data.fields[0].value = 'Come chat around the fire'
                data.fields[0].required.should.equal.false
                done()
            }
            socket.send(
                'xmpp.muc.room.config.get',
                { room: room },
                callback
            )
        })
    })

    describe('Set configuration', function() {

        it('Errors when no callback provided', function(done) {
            xmpp.once('stanza', function() {
                done('Unexpected outgoing stanza')<commit_msg>No longer always set 'required'<commit_after>'use strict';

/* jshint -W030 */

var should        = require('should')
  , MultiUserChat = require('../../index')
  , helper        = require('../helper')
  , dataForm      = require('xmpp-ftw').utils['xep-0004']

describe('Room configuration', function() {

    var muc, socket, xmpp, manager

    before(function() {
        socket = new helper.SocketEventer()
        xmpp = new helper.XmppEventer()
        manager = {
            socket: socket,
            client: xmpp,
            trackId: function(id, callback) {
                this.callback = callback
            },
            makeCallback: function(error, data) {
                this.callback(error, data)
            }
        }
        muc = new MultiUserChat()
        muc.init(manager)
    })

    beforeEach(function() {
        socket.removeAllListeners()
        xmpp.removeAllListeners()
        muc.init(manager)
    })

    describe('Get configuration', function() {

        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({})
                xmpp.removeAllListeners('stanza')
                done()
            })
            socket.send('xmpp.muc.room.config.get', {})
        })

        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({})
                xmpp.removeAllListeners('stanza')
                done()
            })
            socket.send('xmpp.muc.room.config.get', {}, true)
        })

        it('Errors if \'room\' key missing', function(done) {
            xmpp.once('stanza', function() {
                done('Unexpected outgoing stanza')
            })
            var callback = function(error, success) {
                should.not.exist(success)
                error.type.should.equal('modify')
                error.condition.should.equal('client-error')
                error.description.should.equal('Missing \'room\' key')
                error.request.should.eql(request)
                xmpp.removeAllListeners('stanza')
                done()
            }
            var request = {}
            socket.send('xmpp.muc.room.config.get', request, callback)
        })

        it('Handles error response stanza', function(done) {
            var room = '<EMAIL>'
            xmpp.once('stanza', function(stanza) {
                stanza.is('iq').should.be.true
                stanza.attrs.type.should.equal('get')
                stanza.attrs.to.should.equal(room)
                should.exist(stanza.attrs.id)
                stanza.getChild('query', muc.NS_OWNER).should.exist
                manager.makeCallback(helper.getStanza('iq-error'))
            })
            var callback = function(error, success) {
                should.not.exist(success)
                error.should.eql({
                    type: 'cancel',
                    condition: 'error-condition'
                })
                done()
            }
            socket.send(
                'xmpp.muc.room.config.get',
                { room: room },
                callback
            )
        })

        it('Returns room configuration', function(done) {
            var room = '<EMAIL>'
            xmpp.once('stanza', function(stanza) {
                stanza.is('iq').should.be.true
                stanza.attrs.type.should.equal('get')
                stanza.attrs.to.should.equal(room)
                should.exist(stanza.attrs.id)
                stanza.getChild('query', muc.NS_OWNER).should.exist
                manager.makeCallback(helper.getStanza('config-get-result'))
            })
            var callback = function(error, data) {
                should.not.exist(error)
                data.title.should.equal('Configuration for "fire" Room')
                data.instructions.should.equal(
                    'Use this form to update room configuration'
                )
                data.fields.length.should.equal(1)
                data.fields[0].label
                    .should.equal('Short Description of Room')
                data.fields[0].type = 'text-single'
                data.fields[0].var = 'muc#roomconfig_roomdesc'
                data.fields[0].value = 'Come chat around the fire'
                done()
            }
            socket.send(
                'xmpp.muc.room.config.get',
                { room: room },
                callback
            )
        })
    })

    describe('Set configuration', function() {

        it('Errors when no callback provided', function(done) {
            xmpp.once('stanza', function() {
                done('Unexpected outgoing stanza')
{'fraction_non_alphanumeric': 0.12526578533789406, 'fraction_numerical': 0.002311176851252658, 'mean_word_length': 1.4932011984328186, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5617041', 'n_tokens_mistral': 2955, 'n_tokens_neox': 2769, 'n_words': 460}