Prepare shared glossary envelopes and combined input
This commit is contained in:
@@ -1,85 +0,0 @@
|
||||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "https://forge.maximumdirect.net/eric/narratio-reference-catalog/schemas/glossary-v1.schema.json",
|
||||
"title": "Glossary named collections v1",
|
||||
"$ref": "#/$defs/document",
|
||||
"$defs": {
|
||||
"entry": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"id",
|
||||
"name",
|
||||
"category"
|
||||
],
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"name": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"category": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
"description": {
|
||||
"type": "string"
|
||||
},
|
||||
"aliases": {
|
||||
"type": ["array", "null"],
|
||||
"items": {
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
}
|
||||
},
|
||||
"plural": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string",
|
||||
"minLength": 1
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"additionalProperties": true
|
||||
},
|
||||
"partialCollection": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"instruction": {
|
||||
"type": "string"
|
||||
},
|
||||
"entries": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/$defs/entry"
|
||||
}
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
},
|
||||
"collection": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/$defs/partialCollection"
|
||||
},
|
||||
{
|
||||
"required": [
|
||||
"entries"
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"document": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"$ref": "#/$defs/collection"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,159 +1,20 @@
|
||||
// Package glossary owns the shared native glossary representation and validation.
|
||||
// Package glossary composes and projects shared glossary documents.
|
||||
package glossary
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/santhosh-tekuri/jsonschema/v6"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/domaininput"
|
||||
)
|
||||
|
||||
// Collection preserves an explicitly supplied empty instruction.
|
||||
type Collection struct {
|
||||
Instruction *string `yaml:"instruction,omitempty" json:"instruction,omitempty"`
|
||||
Entries []Entry `yaml:"entries" json:"entries"`
|
||||
}
|
||||
// CatalogRevision identifies the selected synthetic catalog revision.
|
||||
const CatalogRevision = "02dd53c6ea026347452d497350c40249c949f8ce"
|
||||
|
||||
// Entry retains native fields and any additional vocabulary metadata.
|
||||
type Entry struct {
|
||||
ID string `yaml:"id" json:"id"`
|
||||
Name string `yaml:"name" json:"name"`
|
||||
Category string `yaml:"category" json:"category"`
|
||||
Description string `yaml:"description,omitempty" json:"description,omitempty"`
|
||||
Aliases []string `yaml:"aliases,omitempty" json:"aliases,omitempty"`
|
||||
Plural *string `yaml:"plural,omitempty" json:"plural,omitempty"`
|
||||
Extra map[string]any `yaml:",inline" json:"-"`
|
||||
}
|
||||
|
||||
// MarshalJSON retains extension fields when validating or serializing typed entries.
|
||||
func (entry Entry) MarshalJSON() ([]byte, error) {
|
||||
type fields Entry
|
||||
b, err := json.Marshal(fields(entry))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result map[string]any
|
||||
if err := json.Unmarshal(b, &result); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for key, value := range entry.Extra {
|
||||
if _, known := result[key]; !known {
|
||||
result[key] = value
|
||||
}
|
||||
}
|
||||
return json.Marshal(result)
|
||||
}
|
||||
|
||||
// CatalogRevision identifies the upstream source of the embedded schema.
|
||||
const CatalogRevision = "a1a87edafd59a1de545a1c0b4efe5feeb80348e0"
|
||||
|
||||
//go:embed glossary-v1.schema.json
|
||||
var schemaData []byte
|
||||
|
||||
const schemaURL = "https://forge.maximumdirect.net/eric/narratio-reference-catalog/schemas/glossary-v1.schema.json"
|
||||
|
||||
var compiled struct {
|
||||
sync.Once
|
||||
document, partial *jsonschema.Schema
|
||||
err error
|
||||
}
|
||||
|
||||
type offlineLoader struct{}
|
||||
|
||||
func (offlineLoader) Load(url string) (any, error) {
|
||||
return nil, fmt.Errorf("schema resource unavailable: %s", url)
|
||||
}
|
||||
|
||||
func schemas() (*jsonschema.Schema, *jsonschema.Schema, error) {
|
||||
compiled.Do(func() {
|
||||
var doc any
|
||||
if compiled.err = json.Unmarshal(schemaData, &doc); compiled.err != nil {
|
||||
return
|
||||
}
|
||||
c := jsonschema.NewCompiler()
|
||||
c.UseLoader(offlineLoader{})
|
||||
if compiled.err = c.AddResource(schemaURL, doc); compiled.err != nil {
|
||||
return
|
||||
}
|
||||
if compiled.document, compiled.err = c.Compile(schemaURL); compiled.err != nil {
|
||||
return
|
||||
}
|
||||
compiled.partial, compiled.err = c.Compile(schemaURL + "#/$defs/partialCollection")
|
||||
})
|
||||
return compiled.document, compiled.partial, compiled.err
|
||||
}
|
||||
|
||||
// ValidatePartial validates a decoded collection contribution without requiring entries.
|
||||
func ValidatePartial(value any) error {
|
||||
_, schema, err := schemas()
|
||||
if err != nil {
|
||||
return fmt.Errorf("compile glossary schema: %w", err)
|
||||
}
|
||||
v, err := jsonValue(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := schema.Validate(v); err != nil {
|
||||
return fmt.Errorf("glossary contribution: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateDocument validates decoded complete named collections and their IDs.
|
||||
// ValidateDocument validates a completed shared glossary mapping.
|
||||
func ValidateDocument(value any) error {
|
||||
schema, _, err := schemas()
|
||||
if err != nil {
|
||||
return fmt.Errorf("compile glossary schema: %w", err)
|
||||
document, ok := value.(map[string]any)
|
||||
if !ok {
|
||||
return fmt.Errorf("glossary document must be a mapping")
|
||||
}
|
||||
v, err := jsonValue(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := schema.Validate(v); err != nil {
|
||||
return fmt.Errorf("glossary document: %w", err)
|
||||
}
|
||||
doc := v.(map[string]any)
|
||||
names := make([]string, 0, len(doc))
|
||||
for name := range doc {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
if err := uniqueIDs("collection "+name, doc[name]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func jsonValue(value any) (any, error) {
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("encode glossary data: %w", err)
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.UseNumber()
|
||||
var result any
|
||||
if err := decoder.Decode(&result); err != nil {
|
||||
return nil, fmt.Errorf("decode glossary data: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func uniqueIDs(context string, value any) error {
|
||||
collection := value.(map[string]any)
|
||||
entries, _ := collection["entries"].([]any)
|
||||
seen := make(map[string]bool, len(entries))
|
||||
for _, raw := range entries {
|
||||
id := raw.(map[string]any)["id"].(string)
|
||||
if seen[id] {
|
||||
return fmt.Errorf("%s: duplicate entry id %q", context, id)
|
||||
}
|
||||
seen[id] = true
|
||||
}
|
||||
return nil
|
||||
return domaininput.ValidateGlossary(document)
|
||||
}
|
||||
|
||||
@@ -1,68 +1,13 @@
|
||||
package glossary
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
import "testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func TestValidation(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, input string
|
||||
partial, valid bool
|
||||
}{
|
||||
{"empty", "party: {entries: []}", false, true},
|
||||
{"null aliases", "party: {entries: [{id: a, name: A, category: person, aliases: null}]}", false, true},
|
||||
{"invalid aliases", "party: {entries: [{id: a, name: A, category: person, aliases: wrong}]}", false, false},
|
||||
{"invalid alias item", "party: {entries: [{id: a, name: A, category: person, aliases: [null]}]}", false, false},
|
||||
{"null description", "party: {entries: [{id: a, name: A, category: person, description: null}]}", false, false},
|
||||
{"null instruction", "party: {instruction: null, entries: []}", false, false},
|
||||
{"missing entries", "party: {instruction: hi}", false, false},
|
||||
{"instruction only", "instruction: hi", true, true},
|
||||
{"optional and extensions", "party: {entries: [{id: a, name: A, category: person, aliases: [Alias], plural: People, description: text, extra: {rank: 2}}]}", false, true},
|
||||
{"missing category", "party: {entries: [{id: a, name: A}]}", false, false},
|
||||
{"duplicate within collection", "party: {entries: [{id: a, name: A, category: person}, {id: a, name: B, category: person}]}", false, false},
|
||||
{"same id across collections", "party: {entries: [{id: a, name: A, category: person}]}\nother: {entries: [{id: a, name: B, category: person}]}", false, true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var value any
|
||||
if err := yaml.Unmarshal([]byte(tc.input), &value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var err error
|
||||
if tc.partial {
|
||||
err = ValidatePartial(value)
|
||||
} else {
|
||||
err = ValidateDocument(value)
|
||||
}
|
||||
if (err == nil) != tc.valid {
|
||||
t.Fatalf("valid=%v: %v", tc.valid, err)
|
||||
}
|
||||
if tc.name == "duplicate within collection" && !strings.Contains(err.Error(), "party") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTypedCollectionRetainsExtensionsAndEmptyInstruction(t *testing.T) {
|
||||
var collection Collection
|
||||
if err := yaml.Unmarshal([]byte("instruction: ''\nentries: [{id: a, name: A, category: person, source_note: test}]\n"), &collection); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if collection.Instruction == nil || *collection.Instruction != "" || collection.Entries[0].Extra["source_note"] != "test" {
|
||||
t.Fatalf("lost data: %+v", collection)
|
||||
}
|
||||
b, err := json.Marshal(map[string]Collection{"party": collection})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(b), `"source_note":"test"`) || !strings.Contains(string(b), `"instruction":""`) {
|
||||
t.Fatalf("lost data: %s", b)
|
||||
}
|
||||
if err := ValidateDocument(map[string]Collection{"party": collection}); err != nil {
|
||||
t.Fatal(err)
|
||||
func TestValidateDocumentUsesSharedEnvelope(t *testing.T) {
|
||||
valid := map[string]any{"schema_version": glossaryMarker, "collections": map[string]any{"party": map[string]any{"entries": []any{}}}}
|
||||
if err := ValidateDocument(valid); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ValidateDocument(map[string]any{"party": map[string]any{"entries": []any{}}}); err == nil {
|
||||
t.Fatal("wrapperless glossary accepted")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/domaininput"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@@ -19,97 +20,113 @@ type Sources struct {
|
||||
|
||||
// Character contains the roster fields needed for a party vocabulary entry.
|
||||
type Character struct {
|
||||
ID string
|
||||
Name string
|
||||
Aliases []string
|
||||
ID, Name string
|
||||
Aliases []string
|
||||
}
|
||||
|
||||
// Documents holds the four completed standalone named collections.
|
||||
// Documents holds individual and combined standalone shared envelopes.
|
||||
type Documents map[string][]byte
|
||||
|
||||
// Resolve builds independent, validated vocabulary snapshots using native composition.
|
||||
const glossaryMarker = "maximumdirect.glossary.v1"
|
||||
|
||||
func envelope(collections map[string]any) map[string]any {
|
||||
return map[string]any{"schema_version": glossaryMarker, "collections": collections}
|
||||
}
|
||||
|
||||
// Resolve builds and validates individual collections and their combined projection.
|
||||
func Resolve(sources Sources, characters []Character) (Documents, error) {
|
||||
party := make([]any, 0, len(characters))
|
||||
ordered := append([]Character(nil), characters...)
|
||||
sort.Slice(ordered, func(i, j int) bool { return ordered[i].ID < ordered[j].ID })
|
||||
entries := make([]any, 0, len(ordered))
|
||||
for _, c := range ordered {
|
||||
entry := map[string]any{"id": c.ID, "name": c.Name, "category": "person"}
|
||||
if len(c.Aliases) > 0 {
|
||||
entry["aliases"] = c.Aliases
|
||||
aliases := make([]any, len(c.Aliases))
|
||||
for i, alias := range c.Aliases {
|
||||
aliases[i] = alias
|
||||
}
|
||||
entry["aliases"] = aliases
|
||||
}
|
||||
party = append(party, entry)
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
collections := map[string]any{"party": map[string]any{"entries": party}}
|
||||
selected := map[string]map[string]any{"party": envelope(map[string]any{"party": map[string]any{"entries": entries}})}
|
||||
for _, item := range []struct {
|
||||
name, path string
|
||||
inline map[string]any
|
||||
}{
|
||||
{"campaign", sources.CampaignFile, sources.CampaignInline},
|
||||
{"setting", sources.SettingFile, nil},
|
||||
{"spells", sources.SpellsFile, nil},
|
||||
} {
|
||||
value, err := resolveCollection(item.name, item.path, item.inline)
|
||||
}{{"campaign", sources.CampaignFile, sources.CampaignInline}, {"setting", sources.SettingFile, nil}, {"spells", sources.SpellsFile, nil}} {
|
||||
doc, err := resolveCollection(item.name, item.path, item.inline)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
collections[item.name] = value
|
||||
selected[item.name] = doc
|
||||
}
|
||||
result := make(Documents, len(collections))
|
||||
combined := make(map[string]any, len(selected))
|
||||
result := make(Documents, 5)
|
||||
for _, name := range []string{"party", "campaign", "setting", "spells"} {
|
||||
doc := map[string]any{name: collections[name]}
|
||||
if err := ValidateDocument(doc); err != nil {
|
||||
doc := selected[name]
|
||||
if err := domaininput.ValidateGlossary(doc); err != nil {
|
||||
return nil, fmt.Errorf("%s vocabulary: %w", name, err)
|
||||
}
|
||||
combined[name] = doc["collections"].(map[string]any)[name]
|
||||
data, err := yaml.Marshal(doc)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("serialize %s vocabulary: %w", name, err)
|
||||
}
|
||||
result[name] = data
|
||||
}
|
||||
doc := envelope(combined)
|
||||
if err := domaininput.ValidateGlossary(doc); err != nil {
|
||||
return nil, fmt.Errorf("combined vocabulary: %w", err)
|
||||
}
|
||||
data, err := yaml.Marshal(doc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result["combined"] = data
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func resolveCollection(name, path string, inline map[string]any) (any, error) {
|
||||
if path == "" && inline == nil {
|
||||
return map[string]any{"entries": []any{}}, nil
|
||||
}
|
||||
definition := configkit.Definition[map[string]any]{SequenceIdentities: []configkit.SequenceIdentity{
|
||||
configkit.SequenceIdentityMatching(configkit.SequenceSelector{configkit.SequenceAnyMappingKey(), configkit.SequenceLiteralKey("entries")}, "id"),
|
||||
}}
|
||||
var underlays []configkit.Underlay
|
||||
if path == "" && inline != nil {
|
||||
empty := configkit.Mapping("empty vocabulary", map[string]any{name: map[string]any{"entries": []any{}}})
|
||||
underlays = append(underlays, configkit.Underlay{Inputs: []configkit.Input{{Source: empty}}})
|
||||
}
|
||||
if path != "" {
|
||||
func resolveCollection(name, path string, inline map[string]any) (map[string]any, error) {
|
||||
selector := configkit.SequenceSelector{configkit.SequenceLiteralKey("collections"), configkit.SequenceAnyMappingKey(), configkit.SequenceLiteralKey("entries")}
|
||||
definition := configkit.Definition[map[string]any]{SequenceIdentities: []configkit.SequenceIdentity{configkit.SequenceIdentityMatching(selector, "id")}}
|
||||
var base configkit.Input
|
||||
if path == "" {
|
||||
base = configkit.Input{Source: configkit.Mapping("empty vocabulary", envelope(map[string]any{name: map[string]any{"entries": []any{}}}))}
|
||||
} else {
|
||||
absolute, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("resolve %s vocabulary path %q: %w", name, path, err)
|
||||
return nil, fmt.Errorf("resolve %s vocabulary: %w", name, err)
|
||||
}
|
||||
// Vocabulary files may import sibling fragments relative to their own
|
||||
// locations; let ConfigKit resolve those imports in the host filesystem.
|
||||
root := filepath.VolumeName(absolute) + string(filepath.Separator)
|
||||
file := configkit.Input{Source: configkit.File(absolute), Imports: true, Filesystem: &configkit.FilesystemContext{Root: root}}
|
||||
loaded, err := configkit.LoadLayered(file, configkit.LayeredOptions[map[string]any]{Definition: definition})
|
||||
base = configkit.Input{Source: configkit.File(absolute), Imports: true, Filesystem: &configkit.FilesystemContext{Root: root}}
|
||||
}
|
||||
var doc map[string]any
|
||||
if inline == nil {
|
||||
loaded, err := configkit.LoadLayered(base, configkit.LayeredOptions[map[string]any]{Definition: definition})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load %s vocabulary %q: %w", name, path, err)
|
||||
return nil, fmt.Errorf("load %s vocabulary: %w", name, err)
|
||||
}
|
||||
if _, ok := loaded.Config[name]; !ok {
|
||||
return nil, fmt.Errorf("%s vocabulary %q does not define collection %q", name, path, name)
|
||||
doc = loaded.Config
|
||||
} else {
|
||||
main := configkit.Input{Source: configkit.Mapping("inline vocabulary", inline), Destination: configkit.Keys("collections", name)}
|
||||
loaded, err := configkit.LoadLayered(main, configkit.LayeredOptions[map[string]any]{Definition: definition, Underlays: []configkit.Underlay{{Inputs: []configkit.Input{base}}}})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("compose %s vocabulary: %w", name, err)
|
||||
}
|
||||
if inline == nil {
|
||||
return loaded.Config[name], nil
|
||||
}
|
||||
underlays = []configkit.Underlay{{Inputs: []configkit.Input{file}}}
|
||||
doc = loaded.Config
|
||||
}
|
||||
main := configkit.Input{Source: configkit.Mapping("inline vocabulary", inline), Destination: configkit.Keys(name)}
|
||||
loaded, err := configkit.LoadLayered(main, configkit.LayeredOptions[map[string]any]{Definition: definition, Underlays: underlays})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("compose %s vocabulary: %w", name, err)
|
||||
if err := domaininput.ValidateGlossary(doc); err != nil {
|
||||
return nil, fmt.Errorf("%s vocabulary: %w", name, err)
|
||||
}
|
||||
value, ok := loaded.Config[name]
|
||||
collections := doc["collections"].(map[string]any)
|
||||
collection, ok := collections[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%s vocabulary is missing", name)
|
||||
return nil, fmt.Errorf("%s vocabulary does not define collection %q", name, name)
|
||||
}
|
||||
return value, nil
|
||||
projection := make(map[string]any, len(doc))
|
||||
for key, value := range doc {
|
||||
projection[key] = value
|
||||
}
|
||||
projection["collections"] = map[string]any{name: collection}
|
||||
return projection, nil
|
||||
}
|
||||
|
||||
@@ -9,98 +9,81 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func TestResolveComposesAndProjectsVocabulary(t *testing.T) {
|
||||
func TestResolveComposesSharedVocabulary(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "campaign.yml")
|
||||
data := `campaign:
|
||||
instruction: Use established names.
|
||||
entries:
|
||||
- {id: watch, name: Old Watch, category: organization, aliases: [Old]}
|
||||
- {id: harbor, name: North Harbor, category: place, source_note: synthetic}
|
||||
campaign := filepath.Join(dir, "campaign.yml")
|
||||
data := `schema_version: maximumdirect.glossary.v1
|
||||
custom_root: synthetic
|
||||
collections:
|
||||
campaign:
|
||||
provenance: {id: synthetic, title: Synthetic}
|
||||
instruction: inert extension
|
||||
entries:
|
||||
- {id: watch, name: Old Watch, category: organization, aliases: [Old]}
|
||||
- {id: harbor, name: North Harbor, category: place, source_note: synthetic}
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(data), 0600); err != nil {
|
||||
if err := os.WriteFile(campaign, []byte(data), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
inline := map[string]any{"entries": []any{map[string]any{"id": "watch", "name": "Silver Watch", "category": "organization"}}}
|
||||
docs, err := Resolve(Sources{CampaignFile: path, CampaignInline: inline}, []Character{{ID: "z", Name: "Zed"}, {ID: "a", Name: "Ada", Aliases: []string{"A"}}})
|
||||
docs, err := Resolve(Sources{CampaignFile: campaign, CampaignInline: inline}, []Character{{ID: "z", Name: "Zed"}, {ID: "a", Name: "Ada", Aliases: []string{"A"}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var campaign map[string]map[string]any
|
||||
if err := yaml.Unmarshal(docs["campaign"], &campaign); err != nil {
|
||||
t.Fatal(err)
|
||||
if len(docs) != 5 {
|
||||
t.Fatalf("documents: %v", docs)
|
||||
}
|
||||
entries := campaign["campaign"]["entries"].([]any)
|
||||
first := entries[0].(map[string]any)
|
||||
if first["name"] != "Silver Watch" || first["aliases"] != nil || !strings.Contains(string(docs["campaign"]), "source_note: synthetic") {
|
||||
t.Fatalf("campaign: %s", docs["campaign"])
|
||||
for _, name := range []string{"party", "campaign", "setting", "spells", "combined"} {
|
||||
var value map[string]any
|
||||
if err := yaml.Unmarshal(docs[name], &value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ValidateDocument(value); err != nil {
|
||||
t.Fatalf("%s: %v", name, err)
|
||||
}
|
||||
collections := value["collections"].(map[string]any)
|
||||
if name == "combined" {
|
||||
if len(collections) != 4 {
|
||||
t.Fatalf("combined collections: %v", collections)
|
||||
}
|
||||
} else if _, ok := collections[name]; !ok || len(collections) != 1 {
|
||||
t.Fatalf("%s collections: %v", name, collections)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(string(docs["campaign"]), "instruction: Use established names.") {
|
||||
t.Fatalf("instruction: %s", docs["campaign"])
|
||||
if strings.Contains(string(docs["campaign"]), "Old Watch") || !strings.Contains(string(docs["campaign"]), "Silver Watch") || !strings.Contains(string(docs["campaign"]), "source_note: synthetic") || !strings.Contains(string(docs["campaign"]), "custom_root: synthetic") || !strings.Contains(string(docs["combined"]), "provenance:") {
|
||||
t.Fatalf("composition lost data: %s\n%s", docs["campaign"], docs["combined"])
|
||||
}
|
||||
if strings.Index(string(docs["party"]), "id: a") > strings.Index(string(docs["party"]), "id: z") {
|
||||
t.Fatalf("party order: %s", docs["party"])
|
||||
}
|
||||
for _, name := range []string{"setting", "spells"} {
|
||||
if !strings.Contains(string(docs[name]), "entries: []") {
|
||||
t.Fatalf("empty %s: %s", name, docs[name])
|
||||
}
|
||||
}
|
||||
if err := os.WriteFile(path, []byte("config:\n sequences:\n - destination: [campaign, entries]\n mode: replace\n"+data), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
docs, err = Resolve(Sources{CampaignFile: path, CampaignInline: map[string]any{"entries": []any{}}}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(docs["campaign"]), "entries: []") || !strings.Contains(string(docs["campaign"]), "instruction:") {
|
||||
t.Fatalf("replacement: %s", docs["campaign"])
|
||||
t.Fatalf("party ordering: %s", docs["party"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveHandlesInheritancePartialAndFailures(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "setting.yml")
|
||||
if err := os.WriteFile(path, []byte("common:\n entries: [{id: tower, name: Tower, category: place}]\nsetting:\n extends: common\n instruction: ''\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
docs, err := Resolve(Sources{SettingFile: path, CampaignInline: map[string]any{"instruction": "Only instruction"}}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(docs["setting"]), "Tower") || !strings.Contains(string(docs["setting"]), "instruction: \"\"") {
|
||||
t.Fatalf("inherited: %s", docs["setting"])
|
||||
}
|
||||
if !strings.Contains(string(docs["campaign"]), "entries: []") {
|
||||
t.Fatalf("partial: %s", docs["campaign"])
|
||||
}
|
||||
if _, err := Resolve(Sources{SpellsFile: path}, nil); err == nil || !strings.Contains(err.Error(), "spells") {
|
||||
t.Fatalf("missing selection: %v", err)
|
||||
}
|
||||
if _, err := Resolve(Sources{CampaignFile: filepath.Join(dir, "missing.yml")}, nil); err == nil {
|
||||
t.Fatal("missing file accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveImportsSiblingVocabularyFragments(t *testing.T) {
|
||||
func TestResolveSelectedImportsAndFailures(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
for name, content := range map[string]string{
|
||||
"settings/coast.yml": "config:\n imports: [{path: ../fragments/places.yml}]\nsetting: {entries: [{id: coast, name: Coast, category: place}]}\n",
|
||||
"fragments/places.yml": "setting: {entries: [{id: harbor, name: Harbor, category: place}]}\n",
|
||||
"settings/coast.yml": "config:\n imports: [{path: ../fragments/places.yml}]\nschema_version: maximumdirect.glossary.v1\ncollections:\n setting: {entries: [{id: coast, name: Coast, category: place}]}\n",
|
||||
"fragments/places.yml": "collections:\n setting: {entries: [{id: harbor, name: Harbor, category: place}]}\n",
|
||||
} {
|
||||
path := filepath.Join(dir, name)
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(content), 0600); err != nil {
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
docs, err := Resolve(Sources{SettingFile: filepath.Join(dir, "settings", "coast.yml")}, nil)
|
||||
path := filepath.Join(dir, "settings", "coast.yml")
|
||||
docs, err := Resolve(Sources{SettingFile: path, CampaignInline: map[string]any{"provenance": map[string]any{"id": "synthetic"}}}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(docs["setting"]), "Harbor") || !strings.Contains(string(docs["setting"]), "Coast") {
|
||||
t.Fatalf("setting: %s", docs["setting"])
|
||||
if !strings.Contains(string(docs["setting"]), "Harbor") || !strings.Contains(string(docs["setting"]), "Coast") || !strings.Contains(string(docs["campaign"]), "entries: []") {
|
||||
t.Fatalf("documents: %s / %s", docs["setting"], docs["campaign"])
|
||||
}
|
||||
if _, err := Resolve(Sources{SpellsFile: path}, nil); err == nil {
|
||||
t.Fatal("wrong collection accepted")
|
||||
}
|
||||
if _, err := Resolve(Sources{CampaignFile: filepath.Join(dir, "missing.yml")}, nil); err == nil {
|
||||
t.Fatal("missing selected file accepted")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user