<commit_before>import expect from 'expect'
import Kite from '../kite'
import KiteServer from './'
import SockJS from 'sockjs-client'
import WebSocket from 'ws'

const logLevel = 0

describe('KiteServer', () => {
  it('should expose SockJS and WebSocket as transport class', done => {
    expect(KiteServer.transport.SockJS).toExist()
    expect(KiteServer.transport.WebSocket).toExist()
    done()
  })
})

describe('KiteServer with SockJS', () => {
  it('should be able to accept kite connections', done => {
    const kite = new Kite({
      url: 'http://0.0.0.0:7780',
      autoReconnect: false,
      autoConnect: false,
      transportClass: Kite.transport.SockJS,
      logLevel,
    })

    const math = new KiteServer({
      name: 'math',
      auth: false,
      serverClass: KiteServer.transport.SockJS,
      logLevel,
      api: {
        square: function(x, callback) {
          callback(null, x * x)
        },
      },
    })

    kite.on('open', () => {
      kite.tell('square', 5).then(res => expect(res).toBe(25)).finally(() => {
        kite.disconnect()
        math.close()
        done()
      })
    })

    math.listen(7780)
    kite.connect()
  })
})

describe('KiteServer with WebSocket', () => {
  it('should be able to accept kite connections', done => {
    const kite = new Kite({
      url: 'http://0.0.0.0:7780',
      serverClass: KiteServer.transport.WebSocket,
      autoReconnect: false,
      autoConnect: false,
      logLevel,
    })

    const math = new KiteServer({
      name: 'math',
      auth: false,
      logLevel,
      api: {
        square: function(x, callback) {
          callback(null, x * x)
        },
      },
    })

    kite.on('open', () => {
      kite.tell('square', 5).then(res => expect(res).toBe(25)).finally(() => {
        kite.disconnect()
        math.close()
        done()
      })
    })

    math.listen(7780)
    kite.connect()
  })
})

describe('KiteServer connection', () => {
  describe('with existing connection', () => {
    it('should throw if the given connection is closed', done => {
      const math = new KiteServer({
        name: 'math',
        auth: false,
        logLevel,
        api: {
          square: function(x, callback) {
            callback(null, x * x)
          },
        },
      })

      math.listen(7780)

      const connection = new WebSocket('ws://localhost:7780')

      connection.on('open', () => {
        connection.on('close', () => {
          math.close()
          expect(() => {
            new Kite({ connection: connection, logLevel })
          }).toThrow(/Given connection is closed/)
          done()
        })
        connection.close()
      })
    })

    it('should work with a WebSocket connection', done => {
      const math = new KiteServer({
        name: 'math',
        auth: false,
        logLevel,
        api: {
          square: function(x, callback) {
            callback(null, x * x)
          },
        },
      })

      math.listen(7780)

      const connection = new WebSocket('ws://localhost:7780')

      const kite = new Kite({
        connection,
        logLevel,
      })

      kite.on('open', () => {
        kite.tell('square', 5).then(res => expect(res).toBe(25)).finally(() => {
          kite.disconnect()
          math.close()
          done()
        })
      })
    })

    it('should work with a SockJS connection', done => {
      const math = new KiteServer({
        name: 'math',
        auth: false,
        serverClass: KiteServer.transport.SockJs,
        logLevel,
        api: {
          square: function(x, callback) {
            callback(null, x * x)
          },
        },
      })

      math.listen(7780)

      const connection = new SockJS('http://0.0.0.0:7780')

      const kite = new Kite({
        connection,
        logLevel,
      })

      kite.on('open', () => {
        kite.tell('square', 5).then(res => expect(res).toBe(25)).finally(() => {
          kite.disconnect()
          math.close()
          done()
        })
      })
    })<commit_msg>Fix typo in server test<commit_after>import expect from 'expect'
import Kite from '../kite'
import KiteServer from './'
import SockJS from 'sockjs-client'
import WebSocket from 'ws'

const logLevel = 0

describe('KiteServer', () => {
  it('should expose SockJS and WebSocket as transport class', done => {
    expect(KiteServer.transport.SockJS).toExist()
    expect(KiteServer.transport.WebSocket).toExist()
    done()
  })
})

describe('KiteServer with SockJS', () => {
  it('should be able to accept kite connections', done => {
    const kite = new Kite({
      url: 'http://0.0.0.0:7780',
      autoReconnect: false,
      autoConnect: false,
      transportClass: Kite.transport.SockJS,
      logLevel,
    })

    const math = new KiteServer({
      name: 'math',
      auth: false,
      serverClass: KiteServer.transport.SockJS,
      logLevel,
      api: {
        square: function(x, callback) {
          callback(null, x * x)
        },
      },
    })

    kite.on('open', () => {
      kite.tell('square', 5).then(res => expect(res).toBe(25)).finally(() => {
        kite.disconnect()
        math.close()
        done()
      })
    })

    math.listen(7780)
    kite.connect()
  })
})

describe('KiteServer with WebSocket', () => {
  it('should be able to accept kite connections', done => {
    const kite = new Kite({
      url: 'http://0.0.0.0:7780',
      serverClass: KiteServer.transport.WebSocket,
      autoReconnect: false,
      autoConnect: false,
      logLevel,
    })

    const math = new KiteServer({
      name: 'math',
      auth: false,
      logLevel,
      api: {
        square: function(x, callback) {
          callback(null, x * x)
        },
      },
    })

    kite.on('open', () => {
      kite.tell('square', 5).then(res => expect(res).toBe(25)).finally(() => {
        kite.disconnect()
        math.close()
        done()
      })
    })

    math.listen(7780)
    kite.connect()
  })
})

describe('KiteServer connection', () => {
  describe('with existing connection', () => {
    it('should throw if the given connection is closed', done => {
      const math = new KiteServer({
        name: 'math',
        auth: false,
        logLevel,
        api: {
          square: function(x, callback) {
            callback(null, x * x)
          },
        },
      })

      math.listen(7780)

      const connection = new WebSocket('ws://localhost:7780')

      connection.on('open', () => {
        connection.on('close', () => {
          math.close()
          expect(() => {
            new Kite({ connection: connection, logLevel })
          }).toThrow(/Given connection is closed/)
          done()
        })
        connection.close()
      })
    })

    it('should work with a WebSocket connection', done => {
      const math = new KiteServer({
        name: 'math',
        auth: false,
        logLevel,
        api: {
          square: function(x, callback) {
            callback(null, x * x)
          },
        },
      })

      math.listen(7780)

      const connection = new WebSocket('ws://localhost:7780')

      const kite = new Kite({
        connection,
        logLevel,
      })

      kite.on('open', () => {
        kite.tell('square', 5).then(res => expect(res).toBe(25)).finally(() => {
          kite.disconnect()
          math.close()
          done()
        })
      })
    })

    it('should work with a SockJS connection', done => {
      const math = new KiteServer({
        name: 'math',
        auth: false,
        serverClass: KiteServer.transport.SockJS,
        logLevel,
        api: {
          square: function(x, callback) {
            callback(null, x * x)
          },
        },
      })

      math.listen(7780)

      const connection = new SockJS('http://0.0.0.0:7780')

      const kite = new Kite({
        connection,
        logLevel,
      })

      kite.on('open', () => {
        kite.tell('square', 5).then(res => expect(res).toBe(25)).finally(() => {
          kite.disconnect()
          math.close()
          done()
        })
      })
    })
{'fraction_non_alphanumeric': 0.15532544378698224, 'fraction_numerical': 0.016025641025641024, 'mean_word_length': 1.851669595782074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '6478294', 'n_tokens_mistral': 2610, 'n_tokens_neox': 2487, 'n_words': 542}