Validate shared domain documents offline
This commit is contained in:
107
internal/integrations/domainrefs/validate_test.go
Normal file
107
internal/integrations/domainrefs/validate_test.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package domainrefs
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func document(t *testing.T, source string) map[string]any {
|
||||
t.Helper()
|
||||
doc, err := Load("fixture.json", "application/json", []byte(source))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return doc
|
||||
}
|
||||
|
||||
func TestGlossaryConformance(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name, source string
|
||||
valid bool
|
||||
}{
|
||||
{"empty", `{"schema_version":"maximumdirect.glossary.v1","collections":{}}`, true},
|
||||
{"extensions and aliases", `{"schema_version":"maximumdirect.glossary.v1","extension":{"count":42},"collections":{"a":{"extension":true,"provenance":{"extension":1.5},"entries":[{"id":"x","name":"N","category":"c","extension":{"instruction":"data"}},{"id":"y","name":"N","category":"c","aliases":null},{"id":"z","name":"N","category":"c","aliases":[]}]},"b":{"entries":[{"id":"x","name":"N","category":"c","aliases":["Alias"]}]}}}`, true},
|
||||
{"empty entries", `{"schema_version":"maximumdirect.glossary.v1","collections":{"a":{"entries":[]}}}`, true},
|
||||
{"duplicate IDs", `{"schema_version":"maximumdirect.glossary.v1","collections":{"a":{"entries":[{"id":"x","name":"N","category":"c"},{"id":"x","name":"N","category":"c"}]}}}`, false},
|
||||
{"invalid unselected collection", `{"schema_version":"maximumdirect.glossary.v1","collections":{"a":{"entries":[]},"b":{"entries":[{"id":"x","name":"N","category":"c","aliases":5}]}}}`, false},
|
||||
{"invalid provenance", `{"schema_version":"maximumdirect.glossary.v1","collections":{"a":{"entries":[],"provenance":null}}}`, false},
|
||||
{"wrong marker", `{"schema_version":"wrong","collections":{}}`, false},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
doc := document(t, test.source)
|
||||
err := ValidateGlossary(doc)
|
||||
if (err == nil) != test.valid {
|
||||
t.Fatalf("ValidateGlossary = %v, valid %v", err, test.valid)
|
||||
}
|
||||
if test.name == "extensions and aliases" && doc["extension"].(map[string]any)["count"] != int64(42) {
|
||||
t.Fatal("validation changed resolved extension")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartyConformance(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name, source string
|
||||
valid bool
|
||||
}{
|
||||
{"empty", `{"schema_version":"maximumdirect.rpg.party.v1","characters":{}}`, true},
|
||||
{"optional and extensions", `{"schema_version":"maximumdirect.rpg.party.v1","extension":true,"characters":{"a":{"extension":1,"character":{"name":"Same","extension":{"v":1.5}}},"b":{"character":{"name":"Same","aliases":null,"classes":[]},"player":{"name":"P","extension":true}},"c":{"character":{"name":"C","aliases":[],"classes":[{"name":"Mage","level":9223372036854775807,"extension":1}]}}}}`, true},
|
||||
{"null classes", `{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"N","classes":null}}}}`, false},
|
||||
{"invalid player", `{"schema_version":"maximumdirect.rpg.party.v1","characters":{"a":{"character":{"name":"N"},"player":null}}}`, false},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
err := ValidateParty(document(t, test.source))
|
||||
if (err == nil) != test.valid {
|
||||
t.Fatalf("ValidateParty = %v, valid %v", err, test.valid)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectionSelection(t *testing.T) {
|
||||
doc := document(t, `{"schema_version":"maximumdirect.glossary.v1","collections":{"a":{"entries":[]},"B":{"entries":[]}}}`)
|
||||
if err := ValidateGlossary(doc); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
selection, defaults []string
|
||||
want []string
|
||||
failure string
|
||||
}{
|
||||
{"default", nil, []string{"a"}, []string{"a"}, ""},
|
||||
{"empty", []string{}, []string{"a"}, []string{}, ""},
|
||||
{"deduplicated and sorted", []string{"a", "B", "a"}, nil, []string{"B", "a"}, ""},
|
||||
{"exact", []string{"b"}, nil, nil, "b"},
|
||||
{"empty name", []string{""}, nil, nil, "empty"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := SelectCollections(doc, test.selection, test.defaults)
|
||||
if test.failure != "" {
|
||||
if err == nil || !strings.Contains(err.Error(), test.failure) {
|
||||
t.Fatalf("selection error = %v", err)
|
||||
}
|
||||
} else if err != nil || !reflect.DeepEqual(got, test.want) {
|
||||
t.Fatalf("selection = %#v, %v; want %#v", got, err, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentValidation(t *testing.T) {
|
||||
doc := document(t, `{"schema_version":"maximumdirect.rpg.party.v1","characters":{}}`)
|
||||
var wg sync.WaitGroup
|
||||
for range 8 {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := ValidateParty(doc); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
Reference in New Issue
Block a user