local t = require('test.testutil')
local n = require('test.functional.testnvim')()

local eq, clear, eval, command, next_msg = t.eq, n.clear, n.eval, n.command, n.next_msg
local api = n.api
local exec_lua = n.exec_lua
local retry = t.retry
local assert_alive = n.assert_alive
local check_close = n.check_close

local testlog = 'Xtest-server-notify-log'

describe('notify', function()
  local channel

  before_each(function()
    clear()
    channel = api.nvim_get_chan_info(0).id
  end)

  after_each(function()
    check_close()
    os.remove(testlog)
  end)

  describe('passing a valid channel id', function()
    it('sends the notification/args to the corresponding channel', function()
      eval('rpcnotify(' .. channel .. ', "test-event", 1, 2, 3)')
      eq({ 'notification', 'test-event', { 1, 2, 3 } }, next_msg())
      command('au FileType lua call rpcnotify(' .. channel .. ', "lua!")')
      command('set filetype=lua')
      eq({ 'notification', 'lua!', {} }, next_msg())
    end)
  end)

  describe('channel id 0', function()
    it('broadcasts the notification/args to all channels', function()
      eval('rpcnotify(0, "event1", 1, 2, 3)')
      eval('rpcnotify(0, "event2", 4, 5, 6)')
      eval('rpcnotify(0, "event2", 7, 8, 9)')
      eq({ 'notification', 'event1', { 1, 2, 3 } }, next_msg())
      eq({ 'notification', 'event2', { 4, 5, 6 } }, next_msg())
      eq({ 'notification', 'event2', { 7, 8, 9 } }, next_msg())

      eval('rpcnotify(0, "event2", 10, 11, 12)')
      eval('rpcnotify(0, "event1", 13, 14, 15)')
      eq({ 'notification', 'event2', { 10, 11, 12 } }, next_msg())
      eq({ 'notification', 'event1', { 13, 14, 15 } }, next_msg())
    end)

    it('does not crash for deeply nested variable', function()
      api.nvim_set_var('l', {})
      local nest_level = 1000
      command(('call map(range(%u), "extend(g:, {\'l\': [g:l]})")'):format(nest_level - 1))
      eval('rpcnotify(' .. channel .. ', "event", g:l)')
      local msg = next_msg()
      eq('notification', msg[1])
      eq('event', msg[2])
      local act_ret = msg[3]
      local act_nest_level = 0
      while act_ret do
        if type(act_ret) == 'table' then
          local cur_act_ret = nil
          for k, v in pairs(act_ret) do
            eq(1, k)
            cur_act_ret = v
          end
          if cur_act_ret then
            act_nest_level = act_nest_level + 1
          end
          act_ret = cur_act_ret
        else
          eq(nil, act_ret)
        end
      end
      eq(nest_level, act_nest_level)
    end)
  end)

  it('cancels stale events on channel close #13537', function()
    local catchan = eval("jobstart(['cat'], {'rpc': v:true})")
    local catpath = eval('exepath("cat")')
    api.nvim_set_var('somevar', 0)
    eq(
      { id = catchan, argv = { catpath }, stream = 'job', mode = 'rpc', client = {} },
      exec_lua(function()
        vim.rpcnotify(catchan, 'nvim_call_function', 'chanclose', { catchan, 'rpc' })
        vim.rpcnotify(catchan, 'nvim_set_var', 'somevar', 1) -- Should be cancelled.
        return vim.api.nvim_get_chan_info(catchan)
      end)
    )
    assert_alive()
    eq(
      { false, 'Invalid channel: ' .. catchan },
      exec_lua(function()
        return { pcall(vim.rpcrequest, catchan, 'nvim_eval', '1+1') }
      end, catchan)
    )
    retry(nil, 3000, function()
      eq({}, api.nvim_get_chan_info(catchan))
    end) -- cat be dead :(
    eq(0, api.nvim_get_var('somevar'))
  end)
end)
