49 lines
1.5 KiB
Go
49 lines
1.5 KiB
Go
package contract
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
|
|
"git.ipao.vip/rogee/go-sip/contracts"
|
|
"github.com/santhosh-tekuri/jsonschema/v6"
|
|
)
|
|
|
|
func TestV1RuntimeSchemaResolvesOnlyEmbeddedContracts(t *testing.T) {
|
|
const version = contracts.SourceCommit
|
|
schema, err := schemaFromBundle(version, "mq.schema.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, name := range []string{"command-query", "call-query-result", "ai-config-result", "call-execute", "event-recording-uploaded"} {
|
|
raw, err := contracts.Files.ReadFile("upstream/" + version + "/examples/" + name + ".json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
value, err := jsonschema.UnmarshalJSON(bytes.NewReader(raw))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := schema.Validate(value); err != nil {
|
|
t.Fatalf("%s: %v", name, err)
|
|
}
|
|
}
|
|
loader := bundleLoader{version: version}
|
|
for _, address := range []string{"https://example.invalid/mq.schema.json", "file:///etc/passwd", "https://go-sip.local/contracts/other/mq.schema.json"} {
|
|
if _, err := loader.Load(address); err == nil {
|
|
t.Fatalf("external/cross-version schema accepted: %s", address)
|
|
}
|
|
}
|
|
again, err := schemaFromBundle(version, "mq.schema.json")
|
|
if err != nil || again != schema {
|
|
t.Fatal("immutable compiled schema not reused")
|
|
}
|
|
}
|
|
|
|
func TestBundleSchemaRejectsInvalidResources(t *testing.T) {
|
|
for _, name := range []string{"../mq.schema.json", "", "missing.schema.json"} {
|
|
if _, err := schemaFromBundle(contracts.SourceCommit, name); err == nil {
|
|
t.Fatalf("invalid resource accepted: %q", name)
|
|
}
|
|
}
|
|
}
|