81 lines
3.1 KiB
Go
81 lines
3.1 KiB
Go
package contract
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProjectLocalConfigurationSchemasValidatePositivesAndRejectNegatives(t *testing.T) {
|
|
read := func(name string) []byte {
|
|
t.Helper()
|
|
body, err := os.ReadFile(filepath.Join("..", "..", "docs", "contracts", "examples", name))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return body
|
|
}
|
|
if err := ValidateLocalConfigRead(read("config-read-task-v0.1.json")); err != nil {
|
|
t.Fatalf("valid task config: %v", err)
|
|
}
|
|
if err := ValidateLocalConfigRead(read("config-read-invalid-extra-property-v0.1.json")); err == nil {
|
|
t.Fatal("config-read schema accepted an additional property")
|
|
}
|
|
page := []byte(`{"schema_version":"task-discovery.v0.3-proposal","dispatcher_id":"11111111-1111-4111-8111-111111111111","next_cursor":"12","tasks":[{"task_id":"22222222-2222-4222-8222-222222222222","tenant_id":"33333333-3333-4333-8333-333333333333","tenant_key":"tenant-A","status":"removed","task_revision":4}]}`)
|
|
if err := ValidateLocalTaskDiscovery(page); err != nil {
|
|
t.Fatalf("valid discovery event page: %v", err)
|
|
}
|
|
for name, raw := range map[string][]byte{
|
|
"old snapshot": read("task-discovery-snapshot-v0.2.json"),
|
|
"changes": []byte(`{"schema_version":"task-discovery.v0.3-proposal","dispatcher_id":"11111111-1111-4111-8111-111111111111","next_cursor":"12","tasks":[],"changes":[]}`),
|
|
"leading zero cursor": []byte(`{"schema_version":"task-discovery.v0.3-proposal","dispatcher_id":"11111111-1111-4111-8111-111111111111","next_cursor":"012","tasks":[]}`),
|
|
} {
|
|
if err := ValidateLocalTaskDiscovery(raw); err == nil {
|
|
t.Fatalf("task-discovery schema accepted %s", name)
|
|
}
|
|
}
|
|
}
|