Repository: neovim/neovim
File: test/functional/plugin/lsp/diagnostic_spec.lua (Lua)

Code before:
  describe('vim.lsp.diagnostic.on_refresh', function()
    it('refreshes diagnostics on server-to-client request', function()
      exec_lua(create_server_definition)
      exec_lua(function()
        _G.requests = 0
        _G.server = _G._create_server({
          capabilities = {
            diagnosticProvider = {},
          },
          handlers = {
            ['textDocument/diagnostic'] = function(_, params)
              _G.params = params
              _G.requests = _G.requests + 1
              vim.lsp.diagnostic.on_diagnostic(nil, {
                kind = 'full',
                items = {
                  _G.make_warning('Pull Diagnostic', 4, 4, 4, 4),
                },
              }, {
                params = {
                  textDocument = { uri = fake_uri },
                },
                uri = fake_uri,
                client_id = client_id,
                bufnr = diagnostic_bufnr,
              }, {})
            end,
          },
        })
        client_id = vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
      end)

      local diags = exec_lua(function()
        vim.lsp.diagnostic.on_diagnostic(nil, {
          kind = 'full',
          items = {
            _G.make_error('Pull Diagnostic', 4, 4, 4, 4),
          },
        }, {
          params = {
            textDocument = { uri = fake_uri },
          },
          uri = fake_uri,
          client_id = client_id,
          bufnr = diagnostic_bufnr,
        }, {})

        return vim.diagnostic.get(diagnostic_bufnr)
      end)
      eq(1, #diags)
      eq(1, diags[1].severity)

Review comment (suggestion):
handlers should send results to the client via the callback. See for example https://github.com/neovim/neovim/blob/3e3624db64778b33d868be32ca64e8b67faf3da8/test/functional/plugin/lsp_spec.lua?plain=1#L4778-L4791

Code after:
  describe('vim.lsp.diagnostic.on_refresh', function()
    it('refreshes diagnostics on server-to-client request', function()
      exec_lua(create_server_definition)
      exec_lua(function()
        _G.requests = 0
        _G.server = _G._create_server({
          capabilities = {
            diagnosticProvider = {},
          },
          handlers = {
            ['textDocument/diagnostic'] = function(_, _, callback)
              _G.requests = _G.requests + 1
              callback(nil, {
                kind = 'full',
                items = {
                  _G.make_warning('Pull Diagnostic', 4, 4, 4, 4),
                },
              })
            end,
          },
        })
        client_id = vim.lsp.start({ name = 'dummy', cmd = _G.server.cmd })
      end)

      local diags = exec_lua(function()
        vim.lsp.diagnostic.on_diagnostic(nil, {
          kind = 'full',
          items = {
            _G.make_error('Pull Diagnostic', 4, 4, 4, 4),
          },
        }, {
          params = {
            textDocument = { uri = fake_uri },
          },
          uri = fake_uri,
          client_id = client_id,
          bufnr = diagnostic_bufnr,
        }, {})

        return vim.diagnostic.get(diagnostic_bufnr)
      end)
      eq(1, #diags)
      eq(1, diags[1].severity)

      local requests, refreshed_diags = exec_lua(function()
        vim.lsp.diagnostic.on_refresh(nil, nil, {
          method = 'workspace/diagnostic/refresh',
          client_id = client_id,
        })

        return _G.requests, vim.diagnostic.get(diagnostic_bufnr)