-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindexer_embed_test.go
More file actions
112 lines (105 loc) · 2.79 KB
/
indexer_embed_test.go
File metadata and controls
112 lines (105 loc) · 2.79 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package ragcode
import (
"strings"
"testing"
"github.com/doITmagic/rag-code-mcp/internal/codetypes"
)
func TestBuildEmbedText_NoTruncation(t *testing.T) {
ch := codetypes.CodeChunk{
Package: "mypkg",
Name: "MyFunc",
Signature: "func MyFunc() string",
Code: "return \"hello\"",
Docstring: "MyFunc returns hello.",
FilePath: "foo.go",
}
text, truncated := buildEmbedText(ch, maxEmbedChars)
if truncated {
t.Fatal("expected no truncation for small chunk")
}
if !strings.Contains(text, ch.Docstring) {
t.Errorf("embed text missing docstring")
}
if !strings.Contains(text, ch.Signature) {
t.Errorf("embed text missing signature")
}
if !strings.Contains(text, ch.Code) {
t.Errorf("embed text missing code")
}
}
func TestBuildEmbedText_TruncatesCode(t *testing.T) {
bigCode := strings.Repeat("x", 40_000)
ch := codetypes.CodeChunk{
Package: "mypkg",
Name: "BigFunc",
Signature: "func BigFunc()",
Code: bigCode,
FilePath: "big.go",
}
limit := 30_000
text, truncated := buildEmbedText(ch, limit)
if !truncated {
t.Fatal("expected truncation for large code body")
}
runes := []rune(text)
if len(runes) > limit {
t.Errorf("truncated text has %d runes, want <= %d", len(runes), limit)
}
if !strings.Contains(text, "func BigFunc()") {
t.Errorf("embed text missing signature after truncation")
}
}
func TestBuildEmbedText_WithDocstring_TruncatesCode(t *testing.T) {
bigCode := strings.Repeat("y", 40_000)
ch := codetypes.CodeChunk{
Signature: "func Fn()",
Code: bigCode,
Docstring: "This is a docstring.",
FilePath: "fn.go",
}
limit := 30_000
text, truncated := buildEmbedText(ch, limit)
if !truncated {
t.Fatal("expected truncation")
}
if !strings.Contains(text, "This is a docstring.") {
t.Errorf("docstring missing after truncation")
}
if len([]rune(text)) > limit {
t.Errorf("text exceeds limit after truncation")
}
}
func TestBuildEmbedText_ExactlyAtLimit(t *testing.T) {
limit := 100
// meta = "func Fn()\n\n" → 11 chars
meta := "func Fn()\n\n"
codeLen := limit - len([]rune(meta))
ch := codetypes.CodeChunk{
Signature: "func Fn()",
Code: strings.Repeat("a", codeLen),
FilePath: "fn.go",
}
text, truncated := buildEmbedText(ch, limit)
if truncated {
t.Fatalf("expected no truncation at exact boundary; len=%d", len([]rune(text)))
}
_ = text
}
func TestBuildEmbedText_EmptyCode(t *testing.T) {
ch := codetypes.CodeChunk{
Signature: "func Empty()",
Code: "",
Docstring: "Empty function.",
FilePath: "empty.go",
}
text, truncated := buildEmbedText(ch, maxEmbedChars)
if truncated {
t.Fatal("expected no truncation for empty code")
}
if !strings.Contains(text, "Empty function.") {
t.Errorf("docstring missing")
}
if !strings.Contains(text, "func Empty()") {
t.Errorf("signature missing")
}
}