<commit_before>juno = require '../lib/julia-client'

# Testing-specific settings
juno.connection.process.jlpath = -> "julia"
juno.connection.process.workingDir = -> process.env.HOME || process.env.USERPROFILE
juno.connection.process.pipeConsole = true

describe "the package", ->
  it "activates without errors", ->
    waitsForPromise ->
      atom.packages.activatePackage 'ink'
    waitsForPromise ->
      atom.packages.activatePackage 'julia-client'

describe "managing the client", ->
  client = juno.connection.client
  clientStatus = -> [client.isConnected(), client.isActive(), client.isWorking()]
  {echo, evalsimple} = client.import ['echo', 'evalsimple']

  describe "before booting", ->

    it "can validate the existence of a julia binary", ->
      path = require 'path'
      checkPath = (p) -> juno.connection.process.checkPath p
      waitsForPromise ->
        checkPath(path.join(__dirname, "juno-spec.coffee")).then (exists) ->
          expect(exists).toBe(true)
      waitsForPromise ->
        checkPath(path.join(__dirname, "doesn't-exist.coffee")).then (exists) ->
          expect(exists).toBe(false)

    it "can validate the existence of a julia command", ->
      checkPath = (p) -> juno.connection.process.checkPath p
      waitsForPromise ->
        checkPath("julia").then (exists) -> expect(exists).toBe(true)
      waitsForPromise ->
        checkPath("nojulia").then (exists) -> expect(exists).toBe(false)

  describe "when booting the client", ->
    bootPromise = null

    it "recognises the client's state before boot", ->
      expect(clientStatus()).toEqual [false, false, false]

    it "initiates the boot", ->
      bootPromise = juno.connection.boot()

    it "recognises the client's state during boot", ->
      expect(clientStatus()).toEqual [false, true, true]

    it "waits for the boot to complete", ->
      waitsFor 'client to boot', 60*1000, (done) ->
        bootPromise.then (pong) ->
          expect(pong).toBe('pong')
          done()

    it "recognises the client's state after boot", ->
      expect(clientStatus()).toEqual [true, true, false]

  describe "while the client is active", ->

    it "can send and receive nested objects, strings and arrays", ->
      msg = {x: 1, y: [1,2,3], z: "foo"}
      waitsForPromise ->
        echo(msg).then (response) ->
          expect(response).toEqual(msg)

    it "can evaluate code and return the result", ->
      for x in [1..10]
        do (x) ->
          waitsForPromise ->
            evalsimple("#{x}^2").then (result) ->
              expect(result).toBe(Math.pow(x, 2))

    it "can rpc into the frontend", ->
      client.handle 'test', (x) -> Math.pow(x, 2)
      for x in [1..10]
        do (x) ->
          waitsForPromise ->
            evalsimple("@rpc test(#{x})").then (result) ->
              expect(result).toBe(Math.pow(x, 2))

    it "can retrieve promise values from the frontend", ->
      client.handle 'test', (x) ->
        new Promise (resolve) ->
          resolve x
      waitsForPromise ->
        evalsimple("@rpc test(2)").then (x) ->
          expect(x).toBe(2)

    describe "when callbacks are pending", ->
      cbs = null
      it "enters loading state", ->
        cbs = (evalsimple("peakflops(1000)") for i in [1..5])
        expect(client.isWorking()).toBe(true)

      it "stops loading after they are done", ->
        for cb in cbs
          do (cb) ->
            waitsForPromise ->
              cb
        runs ->
          expect(client.isWorking()).toBe(false)

    it "can handle a large number of callbacks", ->
      n = 1000
      cbs = (evalsimple("sleep(rand()); #{i}^2") for i in [0...n])
      for i in [0...n]
        do (i) ->
          waitsForPromise ->
            cbs[i].then (result) -> expect(result).toBe(Math.pow(i, 2))

  describe "when the process is shut down", ->

    it "rejects pending callbacks", ->
      waitsFor (done) ->
        evalsimple('exit()').catch -> done()

    it "recognises the client's state after exit", ->
      expect(clientStatus()).toEqual [false, false, false]
<commit_msg>various event tests<commit_after>juno = require '../lib/julia-client'

# Testing-specific settings
juno.connection.process.jlpath = -> "julia"
juno.connection.process.workingDir = -> process.env.HOME || process.env.USERPROFILE
juno.connection.process.pipeConsole = true

describe "the package", ->
  it "activates without errors", ->
    waitsForPromise ->
      atom.packages.activatePackage 'ink'
    waitsForPromise ->
      atom.packages.activatePackage 'julia-client'

describe "managing the client", ->
  client = juno.connection.client
  clientStatus = -> [client.isConnected(), client.isActive(), client.isWorking()]
  {echo, evalsimple} = client.import ['echo', 'evalsimple']
  client.onConnected (connectSpy = jasmine.createSpy 'connect')
  client.onDisconnected (disconnectSpy = jasmine.createSpy 'disconnect')

  describe "before booting", ->

    it "can validate the existence of a julia binary", ->
      path = require 'path'
      checkPath = (p) -> juno.connection.process.checkPath p
      waitsForPromise ->
        checkPath(path.join(__dirname, "juno-spec.coffee")).then (exists) ->
          expect(exists).toBe(true)
      waitsForPromise ->
        checkPath(path.join(__dirname, "doesn't-exist.coffee")).then (exists) ->
          expect(exists).toBe(false)

    it "can validate the existence of a julia command", ->
      checkPath = (p) -> juno.connection.process.checkPath p
      waitsForPromise ->
        checkPath("julia").then (exists) -> expect(exists).toBe(true)
      waitsForPromise ->
        checkPath("nojulia").then (exists) -> expect(exists).toBe(false)

  describe "when booting the client", ->
    bootPromise = null

    it "recognises the client's state before boot", ->
      expect(clientStatus()).toEqual [false, false, false]

    it "initiates the boot", ->
      bootPromise = juno.connection.boot()

    it "recognises the client's state during boot", ->
      expect(clientStatus()).toEqual [false, true, true]

    it "waits for the boot to complete", ->
      waitsFor 'client to boot', 60*1000, (done) ->
        bootPromise.then (pong) ->
          expect(pong).toBe('pong')
          done()

    it "recognises the client's state after boot", ->
      expect(clientStatus()).toEqual [true, true, false]

    it "emits a connection event", ->
      expect(connectSpy.calls.length).toBe(1)

  describe "while the client is active", ->

    it "can send and receive nested objects, strings and arrays", ->
      msg = {x: 1, y: [1,2,3], z: "foo"}
      waitsForPromise ->
        echo(msg).then (response) ->
          expect(response).toEqual(msg)

    it "can evaluate code and return the result", ->
      for x in [1..10]
        do (x) ->
          waitsForPromise ->
            evalsimple("#{x}^2").then (result) ->
              expect(result).toBe(Math.pow(x, 2))

    it "can rpc into the frontend", ->
      client.handle 'test', (x) -> Math.pow(x, 2)
      for x in [1..10]
        do (x) ->
          waitsForPromise ->
            evalsimple("@rpc test(#{x})").then (result) ->
              expect(result).toBe(Math.pow(x, 2))

    it "can retrieve promise values from the frontend", ->
      client.handle 'test', (x) ->
        new Promise (resolve) ->
          resolve x
      waitsForPromise ->
        evalsimple("@rpc test(2)").then (x) ->
          expect(x).toBe(2)

    describe "when callbacks are pending", ->
      {cbs, workingSpy, doneSpy} = {}

      it "registers loading listeners", ->
        client.onWorking (workingSpy = jasmine.createSpy 'working')
        client.onDone (doneSpy = jasmine.createSpy 'done')

      it "enters loading state", ->
        cbs = (evalsimple("peakflops(1000)") for i in [1..5])
        expect(client.isWorking()).toBe(true)

      it "emits a working event", ->
        expect(workingSpy.calls.length).toBe(1)

      it "stops loading after they are done", ->
        for cb in cbs
          do (cb) ->
            waitsForPromise ->
              cb
        runs ->
          expect(client.isWorking()).toBe(false)

      it "emits a done event", ->
        expect(doneSpy.calls.length).toBe(1)

    it "can handle a large number of concurrent callbacks", ->
      n = 1000
      cbs = (evalsimple("sleep(rand()); #{i}^2") for i in [0...n])
      t = new Date().getTime()
      for i in [0...n]
        do (i) ->
          waitsForPromise ->
            cbs[i].then (result) -> expect(result).toBe(Math.pow(i, 2))
      runs ->
        expect(new Date().getTime() - t).toBeLessThan(1500)

  describe "when the process is shut down", ->

    it "rejects pending callbacks", ->
      waitsFor (done) ->
        evalsimple('exit()').catch -> done()

    it "resets the working state", ->
      expect(client.isWorking()).toBe(false)

    it "emits a disconnection event", ->
      expect(disconnectSpy.calls.length).toBe(1)

    it "recognises the client's state after exit", ->
      expect(clientStatus()).toEqual [false, false, false]

{'fraction_non_alphanumeric': 0.14716939407341884, 'fraction_numerical': 0.008845643520566122, 'mean_word_length': 2.4815242494226326, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 134, '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': '2059522', 'n_tokens_mistral': 2861, 'n_tokens_neox': 2734, 'n_words': 725}