-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathresult_test.go
More file actions
263 lines (250 loc) · 7.6 KB
/
result_test.go
File metadata and controls
263 lines (250 loc) · 7.6 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package codingcontext
import (
"context"
"log/slog"
"os"
"testing"
"github.com/kitproj/coding-context-cli/pkg/codingcontext/markdown"
"github.com/kitproj/coding-context-cli/pkg/codingcontext/mcp"
)
func TestResult_Prompt(t *testing.T) {
tests := []struct {
name string
setup func(t *testing.T, dir string)
taskName string
want string
}{
{
name: "task only without rules",
setup: func(t *testing.T, dir string) {
createTask(t, dir, "test-task", "task_name: test-task", "Task content\n")
},
taskName: "test-task",
want: "Task content\n",
},
{
name: "single rule and task",
setup: func(t *testing.T, dir string) {
createTask(t, dir, "test-task", "task_name: test-task", "Task content\n")
createRule(t, dir, ".agents/rules/rule1.md", "", "Rule 1 content\n")
},
taskName: "test-task",
want: "Rule 1 content\n\nTask content\n",
},
{
name: "multiple rules and task",
setup: func(t *testing.T, dir string) {
createTask(t, dir, "test-task", "task_name: test-task", "Task content\n")
createRule(t, dir, ".agents/rules/rule1.md", "", "Rule 1 content\n")
createRule(t, dir, ".agents/rules/rule2.md", "", "Rule 2 content\n")
},
taskName: "test-task",
want: "Rule 1 content\n\nRule 2 content\n\nTask content\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := t.TempDir()
tt.setup(t, tmpDir)
ctx := New(
WithSearchPaths("file://"+tmpDir),
WithLogger(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))),
)
result, err := ctx.Run(context.Background(), tt.taskName)
if err != nil {
t.Fatalf("Run() error = %v", err)
}
if result.Prompt != tt.want {
t.Errorf("Result.Prompt = %q, want %q", result.Prompt, tt.want)
}
})
}
}
func TestResult_MCPServers(t *testing.T) {
tests := []struct {
name string
result Result
want map[string]mcp.MCPServerConfig
}{
{
name: "no MCP servers",
result: Result{
Rules: []markdown.Markdown[markdown.RuleFrontMatter]{},
Task: markdown.Markdown[markdown.TaskFrontMatter]{
FrontMatter: markdown.TaskFrontMatter{},
},
},
want: map[string]mcp.MCPServerConfig{},
},
{
name: "MCP servers from rules with IDs",
result: Result{
Rules: []markdown.Markdown[markdown.RuleFrontMatter]{
{
FrontMatter: markdown.RuleFrontMatter{
ID: "jira-server",
MCPServer: mcp.MCPServerConfig{Type: mcp.TransportTypeStdio, Command: "jira"},
},
},
{
FrontMatter: markdown.RuleFrontMatter{
ID: "api-server",
MCPServer: mcp.MCPServerConfig{Type: mcp.TransportTypeHTTP, URL: "https://api.example.com"},
},
},
},
Task: markdown.Markdown[markdown.TaskFrontMatter]{
FrontMatter: markdown.TaskFrontMatter{},
},
},
want: map[string]mcp.MCPServerConfig{
"jira-server": {Type: mcp.TransportTypeStdio, Command: "jira"},
"api-server": {Type: mcp.TransportTypeHTTP, URL: "https://api.example.com"},
},
},
{
name: "MCP servers from rules without explicit IDs in frontmatter",
result: Result{
Rules: []markdown.Markdown[markdown.RuleFrontMatter]{
{
FrontMatter: markdown.RuleFrontMatter{
ID: "rule-file-1", // ID is auto-set to filename during loading
MCPServer: mcp.MCPServerConfig{Type: mcp.TransportTypeStdio, Command: "server1"},
},
},
{
FrontMatter: markdown.RuleFrontMatter{
ID: "rule-file-2", // ID is auto-set to filename during loading
MCPServer: mcp.MCPServerConfig{Type: mcp.TransportTypeStdio, Command: "server2"},
},
},
},
Task: markdown.Markdown[markdown.TaskFrontMatter]{
FrontMatter: markdown.TaskFrontMatter{},
},
},
want: map[string]mcp.MCPServerConfig{
"rule-file-1": {Type: mcp.TransportTypeStdio, Command: "server1"},
"rule-file-2": {Type: mcp.TransportTypeStdio, Command: "server2"},
},
},
{
name: "multiple rules with MCP servers and empty rule",
result: Result{
Rules: []markdown.Markdown[markdown.RuleFrontMatter]{
{
FrontMatter: markdown.RuleFrontMatter{
ID: "server1-id",
MCPServer: mcp.MCPServerConfig{Type: mcp.TransportTypeStdio, Command: "server1"},
},
},
{
FrontMatter: markdown.RuleFrontMatter{
ID: "server2-id",
MCPServer: mcp.MCPServerConfig{Type: mcp.TransportTypeStdio, Command: "server2"},
},
},
{
FrontMatter: markdown.RuleFrontMatter{
ID: "empty-rule",
},
},
},
Task: markdown.Markdown[markdown.TaskFrontMatter]{
FrontMatter: markdown.TaskFrontMatter{},
},
},
want: map[string]mcp.MCPServerConfig{
"server1-id": {Type: mcp.TransportTypeStdio, Command: "server1"},
"server2-id": {Type: mcp.TransportTypeStdio, Command: "server2"},
// Empty rule MCP server is filtered out
},
},
{
name: "rule without MCP server",
result: Result{
Rules: []markdown.Markdown[markdown.RuleFrontMatter]{
{
FrontMatter: markdown.RuleFrontMatter{
ID: "no-server-rule",
},
},
},
Task: markdown.Markdown[markdown.TaskFrontMatter]{
FrontMatter: markdown.TaskFrontMatter{},
},
},
want: map[string]mcp.MCPServerConfig{
// Empty rule MCP server is filtered out
},
},
{
name: "mixed rules with explicit and auto-generated IDs",
result: Result{
Rules: []markdown.Markdown[markdown.RuleFrontMatter]{
{
FrontMatter: markdown.RuleFrontMatter{
ID: "explicit-id", // Explicit ID in frontmatter
MCPServer: mcp.MCPServerConfig{Type: mcp.TransportTypeStdio, Command: "server1"},
},
},
{
FrontMatter: markdown.RuleFrontMatter{
ID: "some-rule", // ID auto-set to filename during loading
MCPServer: mcp.MCPServerConfig{Type: mcp.TransportTypeStdio, Command: "server2"},
},
},
{
FrontMatter: markdown.RuleFrontMatter{
ID: "another-id", // Explicit ID in frontmatter
MCPServer: mcp.MCPServerConfig{Type: mcp.TransportTypeHTTP, URL: "https://example.com"},
},
},
},
Task: markdown.Markdown[markdown.TaskFrontMatter]{
FrontMatter: markdown.TaskFrontMatter{},
},
},
want: map[string]mcp.MCPServerConfig{
"explicit-id": {Type: mcp.TransportTypeStdio, Command: "server1"},
"some-rule": {Type: mcp.TransportTypeStdio, Command: "server2"},
"another-id": {Type: mcp.TransportTypeHTTP, URL: "https://example.com"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.result.MCPServers()
if len(got) != len(tt.want) {
t.Errorf("MCPServers() returned %d servers, want %d", len(got), len(tt.want))
t.Logf("Got keys: %v", mapKeys(got))
t.Logf("Want keys: %v", mapKeys(tt.want))
return
}
for key, wantServer := range tt.want {
gotServer, ok := got[key]
if !ok {
t.Errorf("MCPServers() missing key %q", key)
continue
}
if gotServer.Type != wantServer.Type {
t.Errorf("MCPServers()[%q].Type = %v, want %v", key, gotServer.Type, wantServer.Type)
}
if gotServer.Command != wantServer.Command {
t.Errorf("MCPServers()[%q].Command = %q, want %q", key, gotServer.Command, wantServer.Command)
}
if gotServer.URL != wantServer.URL {
t.Errorf("MCPServers()[%q].URL = %q, want %q", key, gotServer.URL, wantServer.URL)
}
}
})
}
}
// Helper function to get map keys for debugging
func mapKeys(m map[string]mcp.MCPServerConfig) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}