Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

neovim/tests/lsp/denols_spec.lua

Raw
local denols = require('lsp.denols')

describe('lsp.denols', function()
  describe('exported config', function()
    it('returns a table', function()
      assert.is_table(denols)
    end)

    it('has cmd set to deno lsp', function()
      assert.is_table(denols.cmd)
      assert.equal('deno', denols.cmd[1])
      assert.equal('lsp', denols.cmd[2])
    end)

    it('has NO_COLOR=true in cmd_env', function()
      assert.is_table(denols.cmd_env)
      assert.equal(true, denols.cmd_env.NO_COLOR)
    end)

    it('has correct filetypes', function()
      local expected = {
        'javascript',
        'javascriptreact',
        'typescript',
        'typescriptreact',
        'json',
        'jsonc',
        'markdown',
      }
      assert.is_table(denols.filetypes)
      for _, v in ipairs(expected) do
        assert(vim.tbl_contains(denols.filetypes, v), string.format('missing filetypes: %s', v))
      end
    end)

    it('has root_markers for deno projects', function()
      local expected = { 'deno.json', 'deno.jsonc' }
      assert.is_table(denols.root_markers)
      for _, v in ipairs(expected) do
        assert(vim.tbl_contains(denols.root_markers, v), string.format('missing root_markers: %s', v))
      end
    end)

    it('has deno settings with enable=true', function()
      assert.is_table(denols.settings)
      assert.is_table(denols.settings.deno)
      assert.equal(true, denols.settings.deno.enable)
    end)

    it('has suggest.imports.hosts configured for deno.land', function()
      local hosts = denols.settings.deno.suggest.imports.hosts
      assert.is_table(hosts)
      assert.equal(true, hosts['https://deno.land'])
    end)

    it('has handlers for deno: URI resolution', function()
      assert.is_table(denols.handlers)
      assert.is_function(denols.handlers['textDocument/definition'])
      assert.is_function(denols.handlers['textDocument/typeDefinition'])
      assert.is_function(denols.handlers['textDocument/references'])
    end)

    it('has on_attach function', function()
      assert.is_function(denols.on_attach)
    end)

    it('on_attach creates DenolsCache command on the buffer', function()
      local mock_client = { id = 1, name = 'denols', config = {} }
      local mock_bufnr = vim.api.nvim_create_buf(false, true)
      denols.on_attach(mock_client, mock_bufnr)
      local commands = vim.api.nvim_buf_get_commands(mock_bufnr, {})
      assert.is_table(commands)
      assert.is_table(commands['DenolsCache'])
      vim.api.nvim_buf_delete(mock_bufnr, { force = true })
    end)

    it('has commands table with DenolsCache', function()
      assert.is_table(denols.commands)
      assert.is_table(denols.commands.DenolsCache)
      assert.is_function(denols.commands.DenolsCache[1])
    end)
  end)

  describe('denols_handler', function()
    local denols_handler

    before_each(function()
      denols_handler = require('lsp.denols')._test.denols_handler
    end)

    it('returns nil for nil result', function()
      local result = denols_handler(nil, nil, nil, nil)
      assert.is_nil(result)
    end)

    it('returns nil for empty result table', function()
      local result = denols_handler(nil, {}, nil, nil)
      assert.is_nil(result)
    end)

    it('passes through non-deno: URIs unchanged', function()
      local err = nil
      local result = {
        { uri = 'file:///path/to/file.ts', range = { start = { line = 1 } } },
      }
      local ctx = { method = 'textDocument/definition' }
      local config = {}
      local captured = nil
      local original_handler = vim.lsp.handlers['textDocument/definition']
      vim.lsp.handlers['textDocument/definition'] = function(_, res)
        captured = res
      end
      denols_handler(err, result, ctx, config)
      vim.lsp.handlers['textDocument/definition'] = original_handler
      assert.is_table(captured)
      ---@cast captured table
      assert.equal('file:///path/to/file.ts', captured[1].uri)
    end)

    it('calls virtual_text_document for deno: URIs and preserves them', function()
      local err = nil
      local result = {
        { uri = 'deno:https://deno.land/std/assert/assert.ts', range = { start = { line = 0 } } },
      }
      local ctx = { method = 'textDocument/definition' }
      local config = {}
      local captured = nil
      local original_handler = vim.lsp.handlers['textDocument/definition']
      vim.lsp.handlers['textDocument/definition'] = function(_, res)
        captured = res
      end
      denols_handler(err, result, ctx, config)
      vim.lsp.handlers['textDocument/definition'] = original_handler
      assert.is_table(captured)
      ---@cast captured table
      assert.is_true(captured[1].uri:match('^deno:') ~= nil)
    end)
  end)
end)