-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_remote_test.go
More file actions
563 lines (532 loc) · 19 KB
/
Copy pathgithub_remote_test.go
File metadata and controls
563 lines (532 loc) · 19 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
package main
import (
"encoding/base64"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
)
// ghTestEnv stands up an httptest GitHub API stub and a ghClient pointed at it.
// Tracks request paths so tests can assert commit/search call counts.
type ghTestEnv struct {
srv *httptest.Server
g *ghClient
tc *ToolCtx
puts atomic.Int32
gets atomic.Int32
requests []string
}
func newGHTestEnv(t *testing.T, handler func(w http.ResponseWriter, r *http.Request, e *ghTestEnv)) *ghTestEnv {
t.Helper()
e := &ghTestEnv{}
e.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
e.requests = append(e.requests, r.Method+" "+r.URL.Path)
switch r.Method {
case http.MethodPut, http.MethodPost, http.MethodPatch, http.MethodDelete:
e.puts.Add(1)
default:
e.gets.Add(1)
}
handler(w, r, e)
}))
t.Cleanup(e.srv.Close)
cfg := testCfg(t)
cfg.GitHubToken = "test-token"
e.tc = &ToolCtx{cfg: cfg, authorID: "dev"}
e.tc.ensureGHCache()
e.g = &ghClient{
token: "test-token",
owner: "velaoc",
api: e.srv.URL,
client: e.srv.Client(),
cache: e.tc.ghCache,
}
return e
}
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(v)
}
func b64(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
}
// fixtureRepo is a tiny remote tree used by search/patch tests.
func fixtureTree() map[string]any {
return map[string]any{
"sha": "tree-sha",
"tree": []any{
map[string]any{"path": "app/models/user.rb", "type": "blob", "sha": "sha-user", "size": float64(40)},
map[string]any{"path": "app/controllers/users_controller.rb", "type": "blob", "sha": "sha-ctrl", "size": float64(50)},
map[string]any{"path": "README.md", "type": "blob", "sha": "sha-readme", "size": float64(20)},
map[string]any{"path": "node_modules/pkg/index.js", "type": "blob", "sha": "sha-nm", "size": float64(30)},
map[string]any{"path": "vendor/bundle/gem.rb", "type": "blob", "sha": "sha-vendor", "size": float64(20)},
map[string]any{"path": "bin/blob.dat", "type": "blob", "sha": "sha-bin", "size": float64(20)},
map[string]any{"path": "app/views", "type": "tree", "sha": "sha-tree"},
},
}
}
func fixtureBlobs() map[string]string {
return map[string]string{
"sha-user": "class User\n def create\n end\nend\n",
"sha-ctrl": "class UsersController\n def create\n end\nend\n",
"sha-readme": "create docs here\n",
"sha-nm": "function create() {}\n",
"sha-vendor": "def create\nend\n",
"sha-bin": "create\x00binary\n",
}
}
func TestRemoteSearchCodeTable(t *testing.T) {
blobs := fixtureBlobs()
e := newGHTestEnv(t, func(w http.ResponseWriter, r *http.Request, e *ghTestEnv) {
switch {
case r.URL.Path == "/user":
writeJSON(w, 200, map[string]any{"login": "velaoc"})
case r.URL.Path == "/repos/velaoc/demo":
writeJSON(w, 200, map[string]any{"default_branch": "main"})
case strings.HasPrefix(r.URL.Path, "/repos/velaoc/demo/git/trees/"):
writeJSON(w, 200, fixtureTree())
case strings.HasPrefix(r.URL.Path, "/repos/velaoc/demo/git/blobs/"):
sha := strings.TrimPrefix(r.URL.Path, "/repos/velaoc/demo/git/blobs/")
body, ok := blobs[sha]
if !ok {
writeJSON(w, 404, map[string]any{"message": "Not Found"})
return
}
writeJSON(w, 200, map[string]any{
"sha": sha, "encoding": "base64", "content": b64(body), "size": len(body),
})
default:
writeJSON(w, 404, map[string]any{"message": "unexpected " + r.URL.Path})
}
})
cases := []struct {
name string
pattern string
path string
glob string
want []string
notWant []string
}{
{
name: "file:line hits",
pattern: `def create`,
want: []string{"app/models/user.rb:2:", "app/controllers/users_controller.rb:2:"},
},
{
name: "path prefix filter",
pattern: `def create`,
path: "app/models",
want: []string{"app/models/user.rb:2:"},
notWant: []string{"controllers", "README"},
},
{
name: "glob filter",
pattern: `create`,
glob: "*.rb",
want: []string{".rb:"},
notWant: []string{"README.md"},
},
{
name: "skips noise dirs and binaries",
pattern: `create`,
notWant: []string{"node_modules", "vendor/", "blob.dat"},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
out := e.g.searchCode("velaoc/demo", tc.pattern, tc.path, tc.glob, "main")
for _, w := range tc.want {
if !strings.Contains(out, w) {
t.Fatalf("missing %q in:\n%s", w, out)
}
}
for _, n := range tc.notWant {
if strings.Contains(out, n) {
t.Fatalf("should not contain %q in:\n%s", n, out)
}
}
})
}
t.Run("truncation reported", func(t *testing.T) {
// Flood one file past maxSearchResults.
var flood strings.Builder
for i := 0; i < maxSearchResults+10; i++ {
flood.WriteString("needle line\n")
}
blobs["sha-user"] = flood.String()
// Bust blob cache for this sha.
delete(e.g.cache.blobs, "sha-user")
out := e.g.searchCode("velaoc/demo", `needle`, "app/models/user.rb", "", "main")
if !strings.Contains(out, "truncated") {
t.Fatalf("expected truncation note: %s", out)
}
hits := 0
for _, line := range strings.Split(out, "\n") {
if strings.Contains(line, "user.rb:") {
hits++
}
}
if hits != maxSearchResults {
t.Fatalf("hits=%d want %d", hits, maxSearchResults)
}
// restore
blobs["sha-user"] = fixtureBlobs()["sha-user"]
delete(e.g.cache.blobs, "sha-user")
})
t.Run("tree cached across searches", func(t *testing.T) {
before := 0
for _, r := range e.requests {
if strings.Contains(r, "/git/trees/") {
before++
}
}
_ = e.g.searchCode("velaoc/demo", `User`, "app/models", "", "main")
_ = e.g.searchCode("velaoc/demo", `create`, "app/models", "", "main")
after := 0
for _, r := range e.requests {
if strings.Contains(r, "/git/trees/") {
after++
}
}
if after != before {
t.Fatalf("tree re-fetched: before=%d after=%d", before, after)
}
})
}
func TestRemotePatchFileTable(t *testing.T) {
type fileState struct {
content string
sha string
}
cases := []struct {
name string
initial string
ops []patchOp
wantErr string
wantCommit bool
wantOut string
wantFile string
}{
{
name: "ambiguous match refused nothing committed",
initial: "foo\nbar\nfoo\n",
ops: []patchOp{{Op: "replace", Find: "foo", Replace: "FOO"}},
wantErr: "matched 2 times",
wantCommit: false,
wantFile: "foo\nbar\nfoo\n",
},
{
name: "zero match refused nothing committed",
initial: "only this\n",
ops: []patchOp{{Op: "replace", Find: "missing", Replace: "x"}},
wantErr: "matched 0 times",
wantCommit: false,
wantFile: "only this\n",
},
{
name: "multi-op success commits once",
initial: "one\ntwo\nthree\n",
ops: []patchOp{
{Op: "replace", Find: "one", Replace: "ONE"},
{Op: "insert_after", Find: "two\n", Text: "two-and-half\n"},
},
wantCommit: true,
wantOut: "patched",
wantFile: "ONE\ntwo\ntwo-and-half\nthree\n",
},
{
name: "multi-op later failure commits nothing",
initial: "one\ntwo\nthree\n",
ops: []patchOp{
{Op: "replace", Find: "one", Replace: "ONE"},
{Op: "replace", Find: "nope", Replace: "x"},
},
wantErr: "matched 0 times",
wantCommit: false,
wantFile: "one\ntwo\nthree\n",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
st := &fileState{content: tc.initial, sha: "sha-v1"}
var putCount int
e := newGHTestEnv(t, func(w http.ResponseWriter, r *http.Request, e *ghTestEnv) {
switch {
case r.URL.Path == "/user":
writeJSON(w, 200, map[string]any{"login": "velaoc"})
case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/contents/"):
writeJSON(w, 200, map[string]any{
"sha": st.sha, "encoding": "base64",
"content": b64(st.content), "path": "app/sample.rb",
})
case r.Method == http.MethodPut && strings.Contains(r.URL.Path, "/contents/"):
putCount++
body, _ := io.ReadAll(r.Body)
var payload struct {
Content string `json:"content"`
SHA string `json:"sha"`
Message string `json:"message"`
}
_ = json.Unmarshal(body, &payload)
if payload.SHA != st.sha {
writeJSON(w, 409, map[string]any{"message": "sha mismatch"})
return
}
raw, _ := base64.StdEncoding.DecodeString(payload.Content)
st.content = string(raw)
st.sha = "sha-v2"
writeJSON(w, 200, map[string]any{
"content": map[string]any{"html_url": "https://github.com/velaoc/demo/blob/main/app/sample.rb"},
})
default:
writeJSON(w, 404, map[string]any{"message": "unexpected " + r.Method + " " + r.URL.Path})
}
})
out := e.g.patchFile("velaoc/demo", "app/sample.rb", tc.ops, "test patch", "")
if tc.wantErr != "" {
if !strings.Contains(out, tc.wantErr) {
t.Fatalf("out=%q want err containing %q", out, tc.wantErr)
}
} else if !strings.Contains(out, tc.wantOut) {
t.Fatalf("out=%q want containing %q", out, tc.wantOut)
}
if tc.wantCommit {
if putCount != 1 {
t.Fatalf("commits=%d want 1; out=%s", putCount, out)
}
} else if putCount != 0 {
t.Fatalf("expected no commit, got %d; out=%s", putCount, out)
}
if st.content != tc.wantFile {
t.Fatalf("remote file=%q want %q", st.content, tc.wantFile)
}
})
}
}
func TestRemoteGithubActionsNoRegression(t *testing.T) {
e := newGHTestEnv(t, func(w http.ResponseWriter, r *http.Request, e *ghTestEnv) {
switch {
case r.URL.Path == "/user":
writeJSON(w, 200, map[string]any{"login": "velaoc"})
case r.URL.Path == "/repos/velaoc/demo":
writeJSON(w, 200, map[string]any{"default_branch": "main", "html_url": "https://github.com/velaoc/demo"})
case strings.HasPrefix(r.URL.Path, "/repos/velaoc/demo/git/trees/"):
writeJSON(w, 200, map[string]any{
"tree": []any{
map[string]any{"path": "README.md", "type": "blob", "sha": "s1", "size": float64(5)},
map[string]any{"path": "app/models/user.rb", "type": "blob", "sha": "s2", "size": float64(10)},
},
})
case strings.Contains(r.URL.Path, "/contents/README.md") && r.Method == http.MethodGet:
writeJSON(w, 200, map[string]any{
"sha": "s1", "content": b64("# hi\n"), "encoding": "base64",
})
case strings.Contains(r.URL.Path, "/contents/README.md") && r.Method == http.MethodPut:
writeJSON(w, 200, map[string]any{
"content": map[string]any{"html_url": "https://github.com/velaoc/demo/blob/main/README.md"},
})
case r.Method == http.MethodPost && r.URL.Path == "/user/repos":
writeJSON(w, 201, map[string]any{"html_url": "https://github.com/velaoc/new-app"})
case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/pulls"):
writeJSON(w, 201, map[string]any{"html_url": "https://github.com/velaoc/demo/pull/1"})
default:
writeJSON(w, 404, map[string]any{"message": "unexpected " + r.Method + " " + r.URL.Path})
}
})
// Wire runGithub through cfg so allowlist/token gates pass.
e.tc.cfg.GitHubToken = "test-token"
// Monkey-patch via direct gh methods for API shape, and runGithub for dispatch.
// newGH always hits real API base — exercise methods on e.g and dispatch validation on tc.
if out := e.g.listTree("velaoc/demo", "main", "app"); !strings.Contains(out, "app/models/user.rb") {
t.Fatalf("list_tree: %s", out)
}
if out := e.g.readFiles("velaoc/demo", "main", []string{"README.md"}, 0, 0); !strings.Contains(out, "# hi") {
t.Fatalf("read_files: %s", out)
}
if out := e.g.putFile("velaoc/demo", "README.md", "# bye\n", "update", ""); !strings.Contains(out, "committed") {
t.Fatalf("put_file: %s", out)
}
if out := e.g.createRepo("new-app", "d"); !strings.Contains(out, "created repo") {
t.Fatalf("create_repo: %s", out)
}
if out := e.g.openPR("velaoc/demo", "t", "feature", "main", "body"); !strings.Contains(out, "opened PR") {
t.Fatalf("open_pr: %s", out)
}
// Dispatch recognizes new actions and rejects unknown.
cfg := testCfg(t)
cfg.GitHubToken = "t"
tc := &ToolCtx{cfg: cfg, authorID: "u"}
// Without a reachable API, search_code still validates args:
out := tc.runGithub(toolArgs{Action: "search_code"})
if !strings.Contains(out, "needs repo and pattern") {
t.Fatalf("search_code validation: %s", out)
}
out = tc.runGithub(toolArgs{Action: "patch_file", Repo: "x", Path: "y"})
if !strings.Contains(out, "ops") {
t.Fatalf("patch_file validation: %s", out)
}
out = tc.runGithub(toolArgs{Action: "not_a_thing"})
if !strings.Contains(out, "unknown action") || !strings.Contains(out, "search_code") || !strings.Contains(out, "patch_file") {
t.Fatalf("unknown action help: %s", out)
}
}
func TestGithubToolDescriptionMentionsSearchAndPatch(t *testing.T) {
cfg := testCfg(t)
cfg.GitHubToken = "t"
var desc, params string
for _, d := range toolDefs(cfg) {
if d.Function.Name == "github" {
desc = d.Function.Description
params = string(d.Function.Parameters)
}
}
for _, want := range []string{
"search_code",
"patch_file",
"SEARCH BEFORE",
"Prefer for small edits",
"REPLACES THE ENTIRE FILE",
"start_line",
"never fetch_url",
} {
if !strings.Contains(desc, want) {
t.Fatalf("description missing %q", want)
}
}
if !strings.Contains(params, "search_code") || !strings.Contains(params, "patch_file") || !strings.Contains(params, `"pattern"`) {
t.Fatalf("params schema incomplete: %s", params)
}
if !strings.Contains(params, "start_line") || !strings.Contains(params, "end_line") {
t.Fatalf("params missing line range: %s", params)
}
}
func TestGithubReadFilesRangedAndPerFileBudget(t *testing.T) {
// Large first file + two small ones: multi-file budget is per path so small
// files still appear in full; large file gets an honest continuation hint.
var large strings.Builder
for i := 1; i <= 300; i++ {
large.WriteString(strings.Repeat("R", 80))
large.WriteByte('\n')
}
files := map[string]string{
"config/routes.rb": large.String(),
"a.rb": "alpha-content\n",
"b.rb": "beta-content\n",
}
e := newGHTestEnv(t, func(w http.ResponseWriter, r *http.Request, e *ghTestEnv) {
switch {
case r.URL.Path == "/user":
writeJSON(w, 200, map[string]any{"login": "velaoc"})
case r.URL.Path == "/repos/velaoc/demo":
writeJSON(w, 200, map[string]any{"default_branch": "main"})
case strings.Contains(r.URL.Path, "/contents/") && r.Method == http.MethodGet:
path := strings.TrimPrefix(r.URL.Path, "/repos/velaoc/demo/contents/")
body, ok := files[path]
if !ok {
writeJSON(w, 404, map[string]any{"message": "missing " + path})
return
}
writeJSON(w, 200, map[string]any{
"sha": "s", "content": b64(body), "encoding": "base64",
})
default:
writeJSON(w, 404, map[string]any{"message": "unexpected " + r.Method + " " + r.URL.Path})
}
})
// Ranged read returns exactly the requested lines.
out := e.g.readFiles("velaoc/demo", "main", []string{"a.rb"}, 0, 0)
if !strings.Contains(out, "alpha-content") {
t.Fatalf("small whole file: %s", out)
}
// Build a tiny multi-line file for exact range.
files["range.rb"] = "L1\nL2\nL3\nL4\n"
out = e.g.readFiles("velaoc/demo", "main", []string{"range.rb"}, 2, 3)
if !strings.Contains(out, "L2\nL3") || strings.Contains(out, "L1") || strings.Contains(out, "L4") {
t.Fatalf("ranged github read: %s", out)
}
// 3-file: large first must not starve the others.
out = e.g.readFiles("velaoc/demo", "main", []string{"config/routes.rb", "a.rb", "b.rb"}, 0, 0)
if !strings.Contains(out, "alpha-content") || !strings.Contains(out, "beta-content") {
t.Fatalf("multi-file starved small files: %s", out)
}
if !strings.Contains(out, "[truncated:") || !strings.Contains(out, "of 300 total") {
t.Fatalf("large file missing honest truncation: %s", out)
}
if !strings.Contains(out, `read_files paths=["config/routes.rb"] start_line=`) {
t.Fatalf("missing continuation hint: %s", out)
}
// Out-of-range fails clearly (not empty).
out = e.g.readFiles("velaoc/demo", "main", []string{"a.rb"}, 99, 0)
if !strings.Contains(out, "past end of file") {
t.Fatalf("OOR: %s", out)
}
if strings.TrimSpace(strings.TrimPrefix(out, "=== a.rb ===")) == "" {
t.Fatalf("OOR must not be empty body: %s", out)
}
}
func TestRemotePutFileMigrationAndSchemaGuards(t *testing.T) {
schema := "ActiveRecord::Schema[8.1].define(version: 2026_08_11_140000) do\nend\n"
var putCount atomic.Int32
e := newGHTestEnv(t, func(w http.ResponseWriter, r *http.Request, e *ghTestEnv) {
switch {
case r.URL.Path == "/user":
writeJSON(w, 200, map[string]any{"login": "velaoc"})
case r.URL.Path == "/repos/velaoc/demo":
writeJSON(w, 200, map[string]any{"default_branch": "main"})
case strings.HasPrefix(r.URL.Path, "/repos/velaoc/demo/git/trees/"):
writeJSON(w, 200, map[string]any{
"tree": []any{
map[string]any{"path": "db/schema.rb", "type": "blob", "sha": "sch", "size": float64(len(schema))},
map[string]any{"path": "db/migrate/20260811120000_init.rb", "type": "blob", "sha": "m1", "size": float64(20)},
},
})
case strings.Contains(r.URL.Path, "/contents/db/schema.rb") && r.Method == http.MethodGet:
writeJSON(w, 200, map[string]any{"sha": "sch", "content": b64(schema), "encoding": "base64"})
case r.Method == http.MethodPut && strings.Contains(r.URL.Path, "/contents/"):
putCount.Add(1)
writeJSON(w, 200, map[string]any{
"content": map[string]any{"html_url": "https://github.com/velaoc/demo/blob/main/x"},
})
case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/contents/"):
writeJSON(w, 404, map[string]any{"message": "Not Found"})
default:
writeJSON(w, 404, map[string]any{"message": "unexpected " + r.Method + " " + r.URL.Path})
}
})
// Stale migration refused before any PUT; message names minimum.
out := e.g.putFile("velaoc/demo",
"db/migrate/20260811130000_add_coffee_fields_to_storefront_products.rb",
"class AddCoffee < ActiveRecord::Migration[8.1]\n def change\n add_column :storefront_products, :roast_level, :string\n end\nend\n",
"add coffee", "")
if !strings.Contains(out, "github error:") || !strings.Contains(out, "20260811140001") {
t.Fatalf("stale remote migration: %s", out)
}
if putCount.Load() != 0 {
t.Fatalf("refused migration must not PUT, puts=%d", putCount.Load())
}
// Later timestamp accepted.
out = e.g.putFile("velaoc/demo",
"db/migrate/20260811150000_add_roast_level.rb",
"class AddRoast < ActiveRecord::Migration[8.1]\n def change\n add_column :storefront_products, :roast_level, :string\n end\nend\n",
"add roast", "")
if !strings.Contains(out, "committed") {
t.Fatalf("valid remote migration: %s", out)
}
// Runtime DDL outside migrate refused at dispatch (no API needed for content check).
cfg := testCfg(t)
cfg.GitHubToken = "t"
tc := &ToolCtx{cfg: cfg, authorID: "u"}
out = tc.runGithub(toolArgs{
Action: "put_file",
Repo: "velaoc/demo",
Path: "lib/foundation/demo_seeds.rb",
Content: "connection.add_column :storefront_products, :roast_level, :string\n",
})
if !strings.Contains(out, "github error:") || !strings.Contains(out, "migration") {
t.Fatalf("runtime DDL put_file: %s", out)
}
}