<commit_before>local ChildProcessStream = require('nvim.child_process_stream')
local TcpStream = require('nvim.tcp_stream')
local SocketStream = require('nvim.socket_stream')
local Session = require('nvim.session')

local nvim_prog = os.getenv('NVIM_PROG') or 'nvim'
local child_session
local socket_session
local socket_file
local tcp_session
do
  math.randomseed(os.time())
  socket_file = string.format("/tmp/nvim.socket-%d", math.random(1000,9999))
end

function test_session(description, session_factory, session_destroy)
  local get_api_info = function (session)
    local ok, res = session:request('vim_get_api_info')
    return ok, unpack(res)
  end
  describe(description, function()
    local closed, session

    before_each(function()
      closed = false
      session = session_factory()
    end)

    after_each(function()
      if not closed then
        session:request('vim_command', 'qa!')
      end
    end)

    it('can make requests to nvim', function()
      assert.are.same({true, {1, 2, 3}},
        {session:request('vim_eval', '[1, 2, 3]')})
    end)

    it('can get api metadata', function()
      local res, channel_id, api_t = get_api_info(session)
      assert.is_true(res)
      assert.is_true(type(channel_id) == "number")
      assert.is_true(type(api_t) == "table")
      assert.is_true(type(api_t["functions"]) == "table")
      assert.is_true(type(api_t["error_types"]) == "table")
      assert.is_true(type(api_t["types"]) == "table")
    end)

    it('can receive messages from nvim', function()
      local _, channel_id, _ = get_api_info(session)
      assert.are.same({true, 1},
        {session:request('vim_eval', string.format('rpcnotify(%d, "lua_event", 1, [1])', channel_id))})
      assert.are.same({'notification', 'lua_event', {1, {1}}},
        session:next_message())
    end)

    it('can receive requests from nvim', function()
      local notified = 0
      local _, channel_id, _ = get_api_info(session)
      local function on_request(method, args)
        if method == 'lua_notify' then
          assert.are.same({true, 1},
            {session:request('vim_eval', string.format('rpcnotify(%d, "lua_event", 2, [2])', channel_id))})
          assert.are.same({true, 1},
            {session:request('vim_eval', string.format('rpcnotify(%d, "lua_event", 2, [2])', channel_id))})
          return 'notified!'
        end
        assert.are.same('lua_method', method)
        assert.are.same({1, {1}}, args)
        return {'hello from lua!'}
      end

      local function on_notification(method, args)
        notified = notified + 1
        assert.are.same('lua_event', method)
        assert.are.same({2, {2}}, args)
        assert.are.same({true, {2, {3}}},
          {session:request('vim_eval', '[2, [3]')})
      end

      session:run(on_request, on_notification, function()
        assert.are.same({true, 'notified!'},
          {session:request('vim_eval' , string.format('rpcrequest(%d, "lua_notify")', channel_id))})
        assert.are.same({true, 'notified!'},
          {session:request('vim_eval' , string.format('rpcrequest(%d, "lua_notify")', channel_id))})
        assert.are.same({true, 'notified!'},
          {session:request('vim_eval' , string.format('rpcrequest(%d, "lua_notify")', channel_id))})
        assert.are.same({true, {'hello from lua!'}},
          {session:request('vim_eval', string.format('rpcrequest(%d, "lua_method", 1, [1])', channel_id))})
        session:stop()
      end)
      assert.are.equal(6, notified)
    end)

    it('can deal with recursive requests from nvim', function()
      local requested = 0
      local _, channel_id, _ = get_api_info(session)

      local function on_request(method, args)
        assert.are.same("method", method)
        requested = requested + 1
        if requested < 10 then
          session:request('vim_eval' , string.format('rpcrequest(%d, "method")', channel_id))
        end
        return requested
      end

      session:run(on_request, nil, function()
        session:request('vim_eval' , string.format('rpcrequest(%d, "method")', channel_id))
        session:stop()
      end)
      assert.are.equal(10, requested)
    end)

    it('can receive errors from nvim', function()
      local _, channel_id, _ = get_api_info(session)
      local status, result = session:request('vim_eval',
        string.format('rpcrequest(%d, "method", 1, 2', channel_id))
      assert.is_false(status)
      assert.are.equal('Failed to evaluate expression', result[2])
    end)

    it('can break out of event loop with a timeout', function()
      local responded = false
      session:run(nil, nil, function()
        session:request('vim_command' , 'sleep 5')
        responded = true
      end, 50)
      assert.is_false(responded)
      if session_destroy then
        session_destroy()
      else
        session:close()
      end
      closed = true
    end)
  end)
end

-- Session using ChildProcessStream
test_session("Session using ChidProcessStream", function ()
  local proc_stream = ChildProcessStream.spawn({
    nvim_prog, '-u', 'NONE', '--embed',
  })<commit_msg>test: Accept new/old error messages for invalid rpcrequest() syntax (#36)<commit_after>local ChildProcessStream = require('nvim.child_process_stream')
local TcpStream = require('nvim.tcp_stream')
local SocketStream = require('nvim.socket_stream')
local Session = require('nvim.session')

local nvim_prog = os.getenv('NVIM_PROG') or 'nvim'
local child_session
local socket_session
local socket_file
local tcp_session
do
  math.randomseed(os.time())
  socket_file = string.format("/tmp/nvim.socket-%d", math.random(1000,9999))
end

function test_session(description, session_factory, session_destroy)
  local get_api_info = function (session)
    local ok, res = session:request('vim_get_api_info')
    return ok, unpack(res)
  end
  describe(description, function()
    local closed, session

    before_each(function()
      closed = false
      session = session_factory()
    end)

    after_each(function()
      if not closed then
        session:request('vim_command', 'qa!')
      end
    end)

    it('can make requests to nvim', function()
      assert.are.same({true, {1, 2, 3}},
        {session:request('vim_eval', '[1, 2, 3]')})
    end)

    it('can get api metadata', function()
      local res, channel_id, api_t = get_api_info(session)
      assert.is_true(res)
      assert.is_true(type(channel_id) == "number")
      assert.is_true(type(api_t) == "table")
      assert.is_true(type(api_t["functions"]) == "table")
      assert.is_true(type(api_t["error_types"]) == "table")
      assert.is_true(type(api_t["types"]) == "table")
    end)

    it('can receive messages from nvim', function()
      local _, channel_id, _ = get_api_info(session)
      assert.are.same({true, 1},
        {session:request('vim_eval', string.format('rpcnotify(%d, "lua_event", 1, [1])', channel_id))})
      assert.are.same({'notification', 'lua_event', {1, {1}}},
        session:next_message())
    end)

    it('can receive requests from nvim', function()
      local notified = 0
      local _, channel_id, _ = get_api_info(session)
      local function on_request(method, args)
        if method == 'lua_notify' then
          assert.are.same({true, 1},
            {session:request('vim_eval', string.format('rpcnotify(%d, "lua_event", 2, [2])', channel_id))})
          assert.are.same({true, 1},
            {session:request('vim_eval', string.format('rpcnotify(%d, "lua_event", 2, [2])', channel_id))})
          return 'notified!'
        end
        assert.are.same('lua_method', method)
        assert.are.same({1, {1}}, args)
        return {'hello from lua!'}
      end

      local function on_notification(method, args)
        notified = notified + 1
        assert.are.same('lua_event', method)
        assert.are.same({2, {2}}, args)
        assert.are.same({true, {2, {3}}},
          {session:request('vim_eval', '[2, [3]')})
      end

      session:run(on_request, on_notification, function()
        assert.are.same({true, 'notified!'},
          {session:request('vim_eval' , string.format('rpcrequest(%d, "lua_notify")', channel_id))})
        assert.are.same({true, 'notified!'},
          {session:request('vim_eval' , string.format('rpcrequest(%d, "lua_notify")', channel_id))})
        assert.are.same({true, 'notified!'},
          {session:request('vim_eval' , string.format('rpcrequest(%d, "lua_notify")', channel_id))})
        assert.are.same({true, {'hello from lua!'}},
          {session:request('vim_eval', string.format('rpcrequest(%d, "lua_method", 1, [1])', channel_id))})
        session:stop()
      end)
      assert.are.equal(6, notified)
    end)

    it('can deal with recursive requests from nvim', function()
      local requested = 0
      local _, channel_id, _ = get_api_info(session)

      local function on_request(method, args)
        assert.are.same("method", method)
        requested = requested + 1
        if requested < 10 then
          session:request('vim_eval' , string.format('rpcrequest(%d, "method")', channel_id))
        end
        return requested
      end

      session:run(on_request, nil, function()
        session:request('vim_eval' , string.format('rpcrequest(%d, "method")', channel_id))
        session:stop()
      end)
      assert.are.equal(10, requested)
    end)

    it('can receive errors from nvim', function()
      local _, channel_id, _ = get_api_info(session)
      local status, result = session:request('vim_eval',
        string.format('rpcrequest(%d, "method", 1, 2', channel_id))
      assert.is_false(status)
      -- Improved parsing in nvim changed the error message between 0.2.2 and
      -- 0.3.0, but accept either to ease transition between versions
      if string.match(result[2], 'Failed') then
        assert.are.equal('Failed to evaluate expression', result[2])
      else
        assert.are.equal('Vim:E116: Invalid arguments for function rpcrequest', result[2])
      end
    end)

    it('can break out of event loop with a timeout', function()
      local responded = false
      session:run(nil, nil, function()
        session:request('vim_command' , 'sleep 5')
        responded = true
      end, 50)
      assert.is_false(responded)
      if session_destroy then
        session_destroy()
      else
        session:close()
      end
      closed = true
    end)
  end)
end

-- Session using ChildProcessStream
test_session("Session using ChidProcessStream", function ()
  local proc_stream = ChildProcessStream.spawn({
    nvim_prog, '-u', 'NONE', '--embed',
  })
{'fraction_non_alphanumeric': 0.14618724046810117, 'fraction_numerical': 0.010664401661004153, 'mean_word_length': 3.220230983671844, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5774112', 'n_tokens_mistral': 3411, 'n_tokens_neox': 3270, 'n_words': 748}