| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestAPIErrorCopyScanEscapes(t *testing.T) { |
| 10 | for _, tc := range []struct{ name, file, source, key string }{ |
| 11 | {"variable", "chat/handler.go", `package chat; func f(){ code := "missing_variable"; WriteError(w,r,400,code,nil) }`, "api.missing_variable"}, |
| 12 | {"wiring", "chat_wiring.go", `package equipo; func f(){ personapi.WriteError(w,r,400,"missing_wiring",nil) }`, "api.missing_wiring"}, |
| 13 | {"literal", "newpackage/handler.go", `package newpackage; func f(){ writeJSON(w,400,Error{CopyKey:"api.missing_literal"}) }`, "api.missing_literal"}, |
| 14 | {"alias", "newpackage/handler.go", `package newpackage; const reason = "missing_alias"; func f(){ first := reason; code := first; WriteError(w,r,400,code,nil) }`, "api.missing_alias"}, |
| 15 | {"copy-variable", "newpackage/handler.go", `package newpackage; func f(){ key := "api.missing_key"; writeJSON(w,400,Error{CopyKey:key}) }`, "api.missing_key"}, |
| 16 | {"raw-map", "newpackage/handler.go", `package newpackage; func f(){ writeJSON(w,400,map[string]any{"copy_key":"api.missing_map"}) }`, "api.missing_map"}, |
| 17 | {"raw-map-variable", "newpackage/handler.go", `package newpackage; func f(){ key := "api.missing_map_variable"; writeJSON(w,400,map[string]any{"copy_key":key}) }`, "api.missing_map_variable"}, |
| 18 | {"raw-map-key-constant", "newpackage/handler.go", `package newpackage; const field = "copy_key"; func f(){ writeJSON(w,400,map[string]any{field:"api.missing_map_constant"}) }`, "api.missing_map_constant"}, |
| 19 | {"branches", "newpackage/handler.go", `package newpackage; func f(){ code := "invalid_input"; if bad { code = "missing_branch" }; WriteError(w,r,400,code,nil) }`, "api.missing_branch"}, |
| 20 | } { |
| 21 | t.Run(tc.name, func(t *testing.T) { |
| 22 | dir := t.TempDir() |
| 23 | file := filepath.Join(dir, tc.file) |
| 24 | if err := os.MkdirAll(filepath.Dir(file), 0700); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | if err := os.WriteFile(file, []byte(tc.source), 0600); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | keys, err := scanAPIErrorCopy(dir) |
| 31 | if err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | if !keys[tc.key] { |
| 35 | t.Fatalf("missed %s", tc.key) |
| 36 | } |
| 37 | }) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestAPIErrorCopyScanRejectsUnresolvedAndIgnoresToolResults(t *testing.T) { |
| 42 | dir := t.TempDir() |
| 43 | file := filepath.Join(dir, "tools.go") |
| 44 | write := func(source string) { |
| 45 | t.Helper() |
| 46 | if err := os.WriteFile(file, []byte(source), 0600); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | } |
| 50 | write(`package chat; func f(){code:="provider_error";return toolResult{code:code}}`) |
| 51 | keys, err := scanAPIErrorCopy(dir) |
| 52 | if err != nil || len(keys) != 0 { |
| 53 | t.Fatalf("tool result became HTTP error: %v %v", keys, err) |
| 54 | } |
| 55 | write(`package chat; func f(){code:=dynamic(); WriteError(w,r,400,code,nil)}`) |
| 56 | if _, err := scanAPIErrorCopy(dir); err == nil { |
| 57 | t.Fatal("unknown dynamic error path escaped guard") |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestAPIErrorCopyScanRejectsDynamicRawMap(t *testing.T) { |
| 62 | dir := t.TempDir() |
| 63 | if err := os.WriteFile(filepath.Join(dir, "handler.go"), []byte(`package handler; func f(){ writeJSON(w,400,map[string]any{"copy_key":dynamic()}) }`), 0600); err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | if _, err := scanAPIErrorCopy(dir); err == nil { |
| 67 | t.Fatal("dynamic raw-map copy key escaped the guard") |
| 68 | } |
| 69 | } |
| 70 | |