134 lines
6.0 KiB
Go
134 lines
6.0 KiB
Go
package catalog
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
)
|
|
|
|
func spellReference(content, mediaType string, selection []string) contracts.ReferenceSet {
|
|
return contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
|
SpellCatalogReferenceSlot: {Items: []contracts.ReferenceItem{{SlotName: SpellCatalogReferenceSlot, MediaType: mediaType, Content: []byte(content), Collections: selection}}},
|
|
}}
|
|
}
|
|
|
|
func TestSharedGlossaryRecognition(t *testing.T) {
|
|
content := `{
|
|
"schema_version":"maximumdirect.glossary.v1",
|
|
"collections":{
|
|
"spells":{"entries":[
|
|
{"id":"z","name":"Aegis of Emberfall","category":"custom","aliases":["Emberfall Aegis"]},
|
|
{"id":"a","name":" cure wounds ","category":"anything","aliases":["Campaign Woundweave"]},
|
|
{"id":"b","name":"Null Alias","category":"custom","aliases":null},
|
|
{"id":"c","name":"No Alias","category":"custom"}
|
|
]},
|
|
"other":{"entries":[{"id":"other","name":"Hidden Spell","category":"custom"}]}
|
|
}}`
|
|
got, err := ResolveEffectiveCatalog(spellReference(content, "application/json", nil))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for alias, want := range map[string]string{
|
|
"emberfall aegis": "Aegis of Emberfall",
|
|
"campaign woundweave": "Cure Wounds",
|
|
"cure wounds": "Cure Wounds",
|
|
"null alias": "Null Alias",
|
|
"no alias": "No Alias",
|
|
} {
|
|
if actual, ok := got.Lookup(alias); !ok || actual != want {
|
|
t.Errorf("Lookup(%q) = %q, %t; want %q", alias, actual, ok, want)
|
|
}
|
|
}
|
|
if _, ok := got.Lookup("Hidden Spell"); ok {
|
|
t.Fatal("unselected collection affected recognition")
|
|
}
|
|
if got.BaseID() != SRD5E2014ID || got.Digest() == "" || !reflect.DeepEqual(got.CollectionNames(), []string{"spells"}) {
|
|
t.Fatalf("unexpected catalog metadata: %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestSharedGlossarySelectionAndMedia(t *testing.T) {
|
|
yaml := "schema_version: maximumdirect.glossary.v1\ncollections:\n spells: {entries: []}\n custom: {entries: [{id: x, name: Star Lance, category: spell}]}\n"
|
|
for _, media := range []string{"application/yaml", "application/x-yaml"} {
|
|
got, err := ResolveEffectiveCatalog(spellReference(yaml, media, []string{"custom", "custom"}))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := got.Lookup("Star Lance"); !ok {
|
|
t.Fatal("selected spell not recognized")
|
|
}
|
|
}
|
|
base, err := ResolveEffectiveCatalog(contracts.ReferenceSet{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
empty, err := ResolveEffectiveCatalog(spellReference(yaml, "application/yaml", []string{}))
|
|
if err != nil || !reflect.DeepEqual(base.PromptSpells(), empty.PromptSpells()) {
|
|
t.Fatalf("empty selection did not retain embedded vocabulary: %v", err)
|
|
}
|
|
for _, selection := range [][]string{nil, {"missing"}} {
|
|
_, err := ResolveEffectiveCatalog(spellReference("schema_version: maximumdirect.glossary.v1\ncollections: {}\n", "application/yaml", selection))
|
|
if err == nil || !strings.Contains(err.Error(), "collection") {
|
|
t.Fatalf("expected missing collection error, got %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSharedGlossaryDeterministicDisplayAndCollisions(t *testing.T) {
|
|
first := `{"schema_version":"maximumdirect.glossary.v1","collections":{"spells":{"entries":[{"id":"z","name":"MOON LIGHT","category":"x","aliases":["Lunar Ray"]},{"id":"a","name":"Moon Light","category":"x","aliases":["Lunar Ray"]}]}}}`
|
|
second := `{"schema_version":"maximumdirect.glossary.v1","collections":{"spells":{"entries":[{"id":"a","name":"Moon Light","category":"x","aliases":["Lunar Ray"]},{"id":"z","name":"MOON LIGHT","category":"x","aliases":["Lunar Ray"]}]}}}`
|
|
a, err := ResolveEffectiveCatalog(spellReference(first, "application/json", nil))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := ResolveEffectiveCatalog(spellReference(second, "application/json", nil))
|
|
if err != nil || a.Digest() != b.Digest() || !reflect.DeepEqual(a.PromptSpells(), b.PromptSpells()) {
|
|
t.Fatalf("entry order changed catalog: %v", err)
|
|
}
|
|
if name, ok := a.Lookup("lunar ray"); !ok || name != "Moon Light" {
|
|
t.Fatalf("deterministic display = %q, %t", name, ok)
|
|
}
|
|
for _, entries := range []string{
|
|
`[{"id":"a","name":"Alpha","category":"x","aliases":["Shared"]},{"id":"b","name":"Beta","category":"x","aliases":["Shared"]}]`,
|
|
`[{"id":"a","name":"Alpha","category":"x","aliases":["Beta"]},{"id":"b","name":"Beta","category":"x"}]`,
|
|
`[{"id":"a","name":" ","category":"x"}]`,
|
|
`[{"id":"a","name":"Alpha","category":"x","aliases":[" "]}]`,
|
|
} {
|
|
doc := `{"schema_version":"maximumdirect.glossary.v1","collections":{"spells":{"entries":` + entries + `}}}`
|
|
_, err := ResolveEffectiveCatalog(spellReference(doc, "application/json", nil))
|
|
if err == nil || !strings.Contains(err.Error(), "collection \"spells\" entry") {
|
|
t.Fatalf("expected contextual suitability error: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSharedGlossaryCollectionOrderDeterminesExternalDisplay(t *testing.T) {
|
|
doc := `{"schema_version":"maximumdirect.glossary.v1","collections":{"z":{"entries":[{"id":"a","name":"STAR LANCE","category":"spell"}]},"a":{"entries":[{"id":"z","name":"Star Lance","category":"spell"}]}}}`
|
|
got, err := ResolveEffectiveCatalog(spellReference(doc, "application/json", []string{"z", "a"}))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if name, ok := got.Lookup("star lance"); !ok || name != "Star Lance" {
|
|
t.Fatalf("collection ordering display = %q, %t", name, ok)
|
|
}
|
|
if !reflect.DeepEqual(got.CollectionNames(), []string{"a", "z"}) {
|
|
t.Fatalf("selected collections = %#v", got.CollectionNames())
|
|
}
|
|
}
|
|
|
|
func TestSharedGlossaryRejectsInvalidReferences(t *testing.T) {
|
|
for _, content := range []string{
|
|
`{"schema_version":"notarius.dnd.spell-catalog-overlay.v1","catalogs":[]}`,
|
|
`{"schema_version":"maximumdirect.glossary.v1","collections":{"spells":{"entries":[]}, "other":{"entries":[{"id":"x","name":"X","category":"x","aliases":3}]}}}`,
|
|
} {
|
|
if _, err := ResolveEffectiveCatalog(spellReference(content, "application/json", nil)); err == nil {
|
|
t.Fatal("invalid complete glossary accepted")
|
|
}
|
|
}
|
|
if _, err := ResolveEffectiveCatalog(spellReference("x", "text/plain", nil)); err == nil {
|
|
t.Fatal("unsupported media accepted")
|
|
}
|
|
}
|