summaryrefslogtreecommitdiff
path: root/internal/lsp/completion_toggle_test.go
blob: 57ee1fd24cbf52048658dcbf6ec7e51ff5d2402e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package lsp

import (
	"bytes"
	"encoding/json"
	"strings"
	"testing"
)

func TestHandleCompletionRespectsDisableCommand(t *testing.T) {
	s := newTestServer()
	var buf bytes.Buffer
	s.out = &buf

	// Disable completions and trigger handler
	s.setCompletionsDisabled(true)

	params := CompletionParams{TextDocument: TextDocumentIdentifier{URI: "file:///test.go"}, Position: Position{Line: 0, Character: 0}}
	req := Request{JSONRPC: "2.0", ID: json.RawMessage("1"), Method: "textDocument/completion", Params: mustJSON(params)}

	s.handleCompletion(req)

	payload := buf.String()
	parts := strings.SplitN(payload, "\r\n\r\n", 2)
	if len(parts) != 2 {
		t.Fatalf("unexpected response framing: %q", payload)
	}
	var resp Response
	if err := json.Unmarshal([]byte(parts[1]), &resp); err != nil {
		t.Fatalf("unmarshal response: %v", err)
	}
	resultMap, ok := resp.Result.(map[string]any)
	if !ok {
		t.Fatalf("expected map result, got %T", resp.Result)
	}
	switch v := resultMap["items"].(type) {
	case []any:
		if len(v) != 0 {
			t.Fatalf("expected no completion items when disabled, got %d", len(v))
		}
	case nil:
		// ok: encoder emitted null for slice
	default:
		t.Fatalf("expected items slice or null, got %T", v)
	}
}