Files
go-sip/contracts/current_contract_test.go
T

53 lines
1.5 KiB
Go

package contracts_test
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"git.ipao.vip/rogee/go-sip/contracts"
)
// Shared canonical definitions are composed into one entrypoint per surface.
// Historical upstream bundles are not fallback validators for these inputs.
func TestCurrentContractExamples(t *testing.T) {
for _, surface := range []string{"config-read", "task-discovery", "mq"} {
t.Run(surface, func(t *testing.T) {
schema, err := contracts.CompileCurrent(surface + ".schema.json")
if err != nil {
t.Fatal(err)
}
positives, err := filepath.Glob(filepath.Join("schema", "examples", surface+"-*.json"))
if err != nil || len(positives) == 0 {
t.Fatalf("missing positive examples: %v", err)
}
negatives, err := filepath.Glob(filepath.Join("examples", "invalid", surface+"-*.json"))
if err != nil || len(negatives) == 0 {
t.Fatalf("missing negative examples: %v", err)
}
for _, tc := range []struct {
files []string
valid bool
}{{positives, true}, {negatives, false}} {
for _, path := range tc.files {
t.Run(strings.TrimSuffix(filepath.Base(path), ".json"), func(t *testing.T) {
raw, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
var value any
if err := json.Unmarshal(raw, &value); err != nil {
t.Fatal(err)
}
if err := schema.Validate(value); (err == nil) != tc.valid {
t.Fatalf("valid=%v, validation error=%v", tc.valid, err)
}
})
}
}
})
}
}