summaryrefslogtreecommitdiff
path: root/internal/lsp/triggers_config_test.go
blob: 93d312a89d07d762c173f01493b0ce9c0de397de (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package lsp

import (
	"bytes"
	"encoding/json"
	"io"
	"log"
	"testing"
	"time"
)

func TestShouldSuppressForChatTriggerEOL_CustomConfig(t *testing.T) {
	s := newTestServer()
	// Customize: only ")#" at EOL suppresses
	s.chatSuffix = "#"
	s.chatPrefixes = []string{")"}

	p := CompletionParams{TextDocument: TextDocumentIdentifier{URI: "file:///x"}, Position: Position{Line: 0, Character: 6}}
	if !s.shouldSuppressForChatTriggerEOL("ok)#", p) {
		t.Fatalf("expected suppression for custom prefix+suffix at EOL")
	}
	if s.shouldSuppressForChatTriggerEOL("ok]#", p) {
		t.Fatalf("did not expect suppression for non-matching prefix")
	}
}

func TestNewServer_AssignsTriggerGlobals_AndParsingUsesThem(t *testing.T) {
	var out bytes.Buffer
	s := NewServer(bytes.NewReader(nil), &out, log.New(io.Discard, "", 0), ServerOptions{
		InlineOpen: "<", InlineClose: ">", ChatSuffix: ")", ChatPrefixes: []string{":"},
	})
	_ = s // ensure server constructed applies globals
	if inlineOpenChar != '<' || inlineCloseChar != '>' {
		t.Fatalf("inline markers not applied: %q %q", string(inlineOpenChar), string(inlineCloseChar))
	}
	if chatSuffixChar != ')' || len(chatPrefixSingles) == 0 || chatPrefixSingles[0] != ":" {
		t.Fatalf("chat markers not applied: suffix=%q prefixes=%v", string(chatSuffixChar), chatPrefixSingles)
	}
	if txt, l, r, ok := findStrictInlineTag("x<do>y"); !ok || txt != "do" || l != 1 || r != 5 {
		t.Fatalf("findStrictInlineTag failed: ok=%v txt=%q l=%d r=%d", ok, txt, l, r)
	}
	if got := stripTrailingTrigger("note:)"); got != "note:" {
		t.Fatalf("stripTrailingTrigger failed: %q", got)
	}
}

func TestIsTriggerEvent_BareDoubleOpenBlocksEvenWithContextTriggerChar(t *testing.T) {
	s := newTestServer()
	s.inlineOpen = ">" // ensure bare ">>" check is active
	s.triggerChars = []string{"."}
	// LSP context indicates TriggerCharacter '.' but current line is bare ">>"
	ctx := struct {
		TriggerKind      int    `json:"triggerKind"`
		TriggerCharacter string `json:"triggerCharacter"`
	}{TriggerKind: 2, TriggerCharacter: "."}
	raw, _ := json.Marshal(ctx)
	p := CompletionParams{Position: Position{Line: 0, Character: 2}, Context: json.RawMessage(raw)}
	if s.isTriggerEvent(p, ">>") {
		t.Fatalf("bare double-open should block trigger event even with context trigger char")
	}
}

func TestDetectAndHandleChat_CustomConfig_InsertsReply(t *testing.T) {
	var out bytes.Buffer
	s := NewServer(bytes.NewReader(nil), &out, log.New(io.Discard, "", 0), ServerOptions{ChatSuffix: "#", ChatPrefixes: []string{")"}})
	s.llmClient = fakeLLM{resp: "Hello\nmulti-line reply"}
	uri := "file:///chat2.go"
	s.setDocument(uri, "ok)#\n\n")
	out.Reset()
	s.detectAndHandleChat(uri)
	// Give time for applyEdit request
	for i := 0; i < 20 && out.Len() == 0; i++ {
		time.Sleep(10 * time.Millisecond)
	}
	if out.Len() == 0 {
		t.Fatalf("no output written for custom chat config")
	}
}