forge-toolkit: хелперы для разработки MCP-серверов на Go. - toolkit: args/schema/result/env/probe/server - toolkit/validate: валидаторы синтаксиса - configreload: live-reload конфига по контент-хэшу - docs: ARCHITECTURE, quickstart, mcp-contract - template/ и examples/hello-tool/ - CI (Gitea Actions), лицензия Apache-2.0
79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
package validate
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestContentValid(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
content string
|
|
fileType string
|
|
valid bool
|
|
}{
|
|
{"json ok", `{"a":1}`, "json", true},
|
|
{"json bad", `{`, "json", false},
|
|
{"yaml ok", "a: 1\nb: 2\n", "yaml", true},
|
|
{"yaml bad", "a: [1,\n", "yaml", false},
|
|
{"toml ok", "a = 1\n", "toml", true},
|
|
{"toml bad", "a = = 1\n", "toml", false},
|
|
{"xml ok", "<a><b/></a>", "xml", true},
|
|
{"xml bad", "<a><b></a>", "xml", false},
|
|
{"ini ok", "[s]\nk = v\n", "ini", true},
|
|
{"ini bad", "no separator line\n", "ini", false},
|
|
{"env ok", "FOO=bar\nexport BAZ=1\n", "env", true},
|
|
{"env bad", "1BAD=x\n", "env", false},
|
|
{"dockerfile ok", "FROM alpine\nRUN echo hi\n", "dockerfile", true},
|
|
{"dockerfile bad", "FROM alpine\nBOGUS x\n", "dockerfile", false},
|
|
{"unknown type", "whatever", "nope", false},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
r := Content(tc.content, tc.fileType)
|
|
if tc.fileType == "nope" {
|
|
if r != nil {
|
|
t.Fatalf("unknown type must return nil, got %+v", r)
|
|
}
|
|
return
|
|
}
|
|
if r == nil {
|
|
t.Fatal("nil result")
|
|
}
|
|
if r.Valid != tc.valid {
|
|
t.Errorf("Valid = %v, want %v (errors=%v)", r.Valid, tc.valid, r.Errors)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDetectFileType(t *testing.T) {
|
|
cases := map[string]string{
|
|
"a.json": "json",
|
|
"dir/b.yaml": "yaml",
|
|
"c.YML": "yaml",
|
|
"Dockerfile": "dockerfile",
|
|
"path/to/Dockerfile": "dockerfile",
|
|
".env": "env",
|
|
".env.local": "env",
|
|
"conf/app.conf": "ini",
|
|
"x.txt": "",
|
|
}
|
|
for in, want := range cases {
|
|
if got := DetectFileType(in); got != want {
|
|
t.Errorf("DetectFileType(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFormatResult(t *testing.T) {
|
|
ok := &Result{Valid: true, FileType: "json"}
|
|
if got := ok.FormatResult("/a.json"); !strings.HasPrefix(got, "✓") {
|
|
t.Errorf("valid format = %q", got)
|
|
}
|
|
bad := &Result{FileType: "json", Errors: []string{"boom"}}
|
|
if got := bad.FormatResult("/a.json"); !strings.HasPrefix(got, "✗") {
|
|
t.Errorf("invalid format = %q", got)
|
|
}
|
|
}
|