90 lines
3.5 KiB
Go
90 lines
3.5 KiB
Go
package glossary
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func TestResolveComposesSharedVocabulary(t *testing.T) {
|
|
dir := t.TempDir()
|
|
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(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: campaign, CampaignInline: inline}, []Character{{ID: "z", Name: "Zed"}, {ID: "a", Name: "Ada", Aliases: []string{"A"}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(docs) != 5 {
|
|
t.Fatalf("documents: %v", docs)
|
|
}
|
|
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"]), "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 ordering: %s", docs["party"])
|
|
}
|
|
}
|
|
|
|
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}]\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), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
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") || !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")
|
|
}
|
|
}
|