-
Notifications
You must be signed in to change notification settings - Fork 0
/
llm_context_test.go
216 lines (203 loc) · 5.25 KB
/
llm_context_test.go
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"reflect"
"testing"
)
func TestParseContext(t *testing.T) {
tests := []struct {
name string
input string
want []*ContextItemProvider
wantErr bool
}{
{
name: "Single file",
input: "file.go",
want: []*ContextItemProvider{
{
parts: []string{"file.go"},
itemType: ContextItemTypeFile,
},
},
wantErr: false,
},
{
name: "File with symbol",
input: "file.go MyFunction",
want: []*ContextItemProvider{
{
parts: []string{"file.go", "MyFunction"},
itemType: ContextItemTypeSymbol,
},
},
wantErr: false,
},
{
name: "File with range",
input: "file.go 10:20",
want: []*ContextItemProvider{
{
parts: []string{"file.go", "10:20"},
itemType: ContextItemTypeRange,
},
},
wantErr: false,
},
{
name: "File with multiple items",
input: "file.go MyFunction 10:20 AnotherSymbol",
want: []*ContextItemProvider{
{
parts: []string{"file.go", "MyFunction", "10:20", "AnotherSymbol"},
itemType: ContextItemTypeSymbol,
},
{
parts: []string{"file.go", "MyFunction", "10:20", "AnotherSymbol"},
itemType: ContextItemTypeRange,
},
{
parts: []string{"file.go", "MyFunction", "10:20", "AnotherSymbol"},
itemType: ContextItemTypeSymbol,
},
},
wantErr: false,
},
{
name: "Multiple lines",
input: "file1.go MyFunction\nfile2.go 10:20",
want: []*ContextItemProvider{
{
parts: []string{"file1.go", "MyFunction"},
itemType: ContextItemTypeSymbol,
},
{
parts: []string{"file2.go", "10:20"},
itemType: ContextItemTypeRange,
},
},
wantErr: false,
},
{
name: "Invalid range",
input: "file.go 20:10",
want: nil,
wantErr: true,
},
{
name: "Quoted filename with spaces",
input: `"my file.go" MyFunction`,
want: []*ContextItemProvider{
{
parts: []string{"my file.go", "MyFunction"},
itemType: ContextItemTypeSymbol,
},
},
wantErr: false,
},
{
name: "Unclosed quote",
input: `"my file.go MyFunction`,
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseContext(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("ParseContext() error = %v, wantErr %v", err, tt.wantErr)
return
}
for i, gotItem := range got {
if i >= len(tt.want) {
t.Errorf("ParseContext() = %v, want %v", got, tt.want)
}
wantItem := tt.want[i]
if gotItem.itemType != wantItem.itemType {
t.Errorf("Got item %v, want %v", gotItem, wantItem)
}
if len(gotItem.parts) != len(wantItem.parts) {
t.Errorf("Got item %v, want %v", gotItem, wantItem)
}
for p, gotPart := range gotItem.parts {
if gotPart != wantItem.parts[p] {
t.Errorf("Got item %v, want %v", gotItem, wantItem)
}
}
}
})
}
}
func TestContextItemProvider_GetItem(t *testing.T) {
// Mock ContextApi implementation
mockApi := &MockContextApi{}
tests := []struct {
name string
provider *ContextItemProvider
want *ContextItem
wantErr bool
}{
{
name: "Get file",
provider: &ContextItemProvider{
parts: []string{"file.go"},
itemType: ContextItemTypeFile,
getter: func(ca ContextApi) (*ContextItem, error) {
content, _ := ca.GetFile("file.go")
return &ContextItem{Filename: "file.go", Content: content}, nil
},
},
want: &ContextItem{Filename: "file.go", Content: "file content"},
wantErr: false,
},
{
name: "Get symbol",
provider: &ContextItemProvider{
parts: []string{"file.go", "MyFunction"},
itemType: ContextItemTypeSymbol,
getter: func(ca ContextApi) (*ContextItem, error) {
content, _ := ca.GetSymbol("file.go", "MyFunction")
return &ContextItem{Filename: "file.go", Identifier: "MyFunction", Content: content}, nil
},
},
want: &ContextItem{Filename: "file.go", Identifier: "MyFunction", Content: "function content"},
wantErr: false,
},
{
name: "Get range",
provider: &ContextItemProvider{
parts: []string{"file.go", "10:20"},
itemType: ContextItemTypeRange,
getter: func(ca ContextApi) (*ContextItem, error) {
content, _ := ca.GetRange("file.go", 10, 20)
return &ContextItem{Filename: "file.go", Identifier: "10:20", Content: content}, nil
},
},
want: &ContextItem{Filename: "file.go", Identifier: "10:20", Content: "range content"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.provider.GetItem(mockApi)
if (err != nil) != tt.wantErr {
t.Errorf("ContextItemProvider.GetItem() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ContextItemProvider.GetItem() = %v, want %v", got, tt.want)
}
})
}
}
// MockContextApi is a mock implementation of ContextApi for testing
type MockContextApi struct{}
func (m *MockContextApi) GetSymbol(filename, symbolName string) (string, error) {
return "function content", nil
}
func (m *MockContextApi) GetRange(filename string, start, end int) (string, error) {
return "range content", nil
}
func (m *MockContextApi) GetFile(filename string) (string, error) {
return "file content", nil
}