224 lines
7.8 KiB
Go
224 lines
7.8 KiB
Go
package sharedglossary
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
ck "go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
func TestLoadComposition(t *testing.T) {
|
|
dir := t.TempDir()
|
|
files := map[string]string{
|
|
"first.yml": `config:
|
|
imports: [{path: fragment.yml, required: true}]
|
|
schema_version: maximumdirect.glossary.v1
|
|
collections:
|
|
people:
|
|
entries:
|
|
- {id: one, name: Old, category: person}
|
|
- {id: two, name: Two, category: person}
|
|
places:
|
|
entries: []
|
|
`,
|
|
"fragment.yml": `collections:
|
|
lore:
|
|
entries: [{id: lore, name: Lore, category: topic}]
|
|
`,
|
|
"second.yml": `schema_version: maximumdirect.glossary.v1
|
|
collections:
|
|
people:
|
|
entries: [{id: one, name: New, category: person}]
|
|
`,
|
|
"third.json": `{"schema_version":"maximumdirect.glossary.v1","collections":{"spells":{"entries":[{"id":"one","name":"Spell","category":"spell"}]}}}`,
|
|
}
|
|
for name, body := range files {
|
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for _, tc := range []struct {
|
|
name string
|
|
files []string
|
|
collections []string
|
|
}{
|
|
{"one multi collection file", []string{"first.yml"}, []string{"people", "places", "lore"}},
|
|
{"multiple single collection files", []string{"second.yml", "third.json"}, []string{"people", "spells"}},
|
|
{"mixed files", []string{"first.yml", "second.yml", "third.json"}, []string{"people", "places", "lore", "spells"}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var inputs []ck.Input
|
|
for _, name := range tc.files {
|
|
inputs = append(inputs, ck.Input{Source: ck.File(filepath.Join(dir, name))})
|
|
}
|
|
result, err := Load(context.Background(), inputs)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
collections := result.Config["collections"].(map[string]any)
|
|
for _, name := range tc.collections {
|
|
if _, ok := collections[name]; !ok {
|
|
t.Errorf("missing collection %s in %v", name, collections)
|
|
}
|
|
}
|
|
if slices.Contains(tc.files, "first.yml") {
|
|
var paths []string
|
|
for _, source := range result.Sources() {
|
|
paths = append(paths, source.Path)
|
|
}
|
|
if !slices.Contains(paths, filepath.Join(dir, "fragment.yml")) {
|
|
t.Errorf("missing imported source: %v", paths)
|
|
}
|
|
}
|
|
if slices.Contains(tc.files, "first.yml") && slices.Contains(tc.files, "second.yml") {
|
|
entries := collections["people"].(map[string]any)["entries"].([]any)
|
|
if len(entries) != 2 || entries[0].(map[string]any)["name"] != "New" {
|
|
t.Fatalf("layered entries = %#v", entries)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadErrorsAndEmptyInput(t *testing.T) {
|
|
result, err := Load(context.Background(), nil)
|
|
if err != nil || result != nil {
|
|
t.Fatalf("no inputs = %v, %v", result, err)
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
if _, err := Load(ctx, nil); !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("cancellation = %v", err)
|
|
}
|
|
dir := t.TempDir()
|
|
for _, tc := range []struct{ name, body, contains string }{
|
|
{"duplicates", "schema_version: maximumdirect.glossary.v1\ncollections:\n people:\n entries: [{id: a, name: A, category: person}, {id: a, name: B, category: person}]\n", "a"},
|
|
{"missing marker", "collections: {}\n", "schema_version"},
|
|
{"invalid type", "schema_version: maximumdirect.glossary.v1\ncollections: []\n", "collections"},
|
|
{"unsupported input", "schema_version: !!str maximumdirect.glossary.v1\ncollections: {}\n", ""},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
path := filepath.Join(dir, strings.ReplaceAll(tc.name, " ", "_")+".yml")
|
|
if err := os.WriteFile(path, []byte(tc.body), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err := Load(context.Background(), []ck.Input{{Source: ck.File(path)}})
|
|
var loadErr *ck.LoadError
|
|
if !errors.As(err, &loadErr) {
|
|
t.Fatalf("expected native load error, got %v", err)
|
|
}
|
|
var messages []string
|
|
for _, diagnostic := range loadErr.Diagnostics() {
|
|
messages = append(messages, diagnostic.Message)
|
|
}
|
|
if !strings.Contains(strings.Join(messages, " "), tc.contains) {
|
|
t.Fatalf("diagnostics = %v", messages)
|
|
}
|
|
var paths []string
|
|
for _, source := range loadErr.Sources() {
|
|
paths = append(paths, source.Path)
|
|
}
|
|
if !slices.Contains(paths, path) {
|
|
t.Fatalf("missing source context: %v", paths)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSchemaFailureUsesContributingSource(t *testing.T) {
|
|
dir := t.TempDir()
|
|
first := filepath.Join(dir, "first.yml")
|
|
last := filepath.Join(dir, "last.yml")
|
|
if err := os.WriteFile(last, []byte("schema_version: maximumdirect.glossary.v1\ncollections: {}\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, tc := range []struct{ name, entry, path string }{
|
|
{"invalid value", "{id: a, name: 17, category: person}", "/collections/7~1~0/entries/0/name"},
|
|
{"missing field", "{id: a, category: person}", "/collections/7~1~0/entries/0"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
body := "schema_version: maximumdirect.glossary.v1\ncollections:\n '7/~':\n entries: [" + tc.entry + "]\n"
|
|
if err := os.WriteFile(first, []byte(body), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err := Load(context.Background(), []ck.Input{{Source: ck.File(first)}, {Source: ck.File(last)}})
|
|
var loadErr *ck.LoadError
|
|
if !errors.As(err, &loadErr) {
|
|
t.Fatalf("expected native load error, got %v", err)
|
|
}
|
|
sources := make(map[string]string)
|
|
for _, source := range loadErr.Sources() {
|
|
sources[source.ID] = source.Path
|
|
}
|
|
diagnostics := loadErr.Diagnostics()
|
|
if len(diagnostics) != 1 || diagnostics[0].Path.String() != tc.path {
|
|
t.Fatalf("unexpected diagnostics: %+v", diagnostics)
|
|
}
|
|
location := diagnostics[0].Primary
|
|
if location == nil || sources[location.SourceID] != first {
|
|
t.Fatalf("diagnostic points to %v with sources %v", location, sources)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLoadResolvesInheritanceAndPreservesExtensions(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "shared.yml")
|
|
body := `schema_version: maximumdirect.glossary.v1
|
|
custom: {revision: 1, ratio: 1.5}
|
|
collections:
|
|
original:
|
|
provenance: {title: Synthetic source, custom: true}
|
|
entries: [{id: a, name: Alpha, category: spell, rank: 1}]
|
|
derived:
|
|
extends: original
|
|
instruction: {text: data}
|
|
`
|
|
if err := os.WriteFile(path, []byte(body), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err := Load(context.Background(), []ck.Input{{Source: ck.File(path)}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
collections := result.Config["collections"].(map[string]any)
|
|
derived := collections["derived"].(map[string]any)
|
|
entries := derived["entries"].([]any)
|
|
if len(entries) != 1 || entries[0].(map[string]any)["rank"] != int64(1) || derived["provenance"].(map[string]any)["title"] != "Synthetic source" || derived["instruction"].(map[string]any)["text"] != "data" {
|
|
t.Fatalf("resolved data = %#v", result.Config)
|
|
}
|
|
custom := result.Config["custom"].(map[string]any)
|
|
if custom["revision"] != int64(1) || custom["ratio"] != 1.5 {
|
|
t.Fatalf("extensions = %#v", custom)
|
|
}
|
|
}
|
|
|
|
func TestEquivalentYAMLAndJSON(t *testing.T) {
|
|
dir := t.TempDir()
|
|
documents := map[string]string{
|
|
"input.yml": "schema_version: maximumdirect.glossary.v1\ncollections:\n terms:\n entries: [{id: a, name: Alpha, category: term}, {id: A, name: Alpha, category: term}]\n",
|
|
"input.json": `{"schema_version":"maximumdirect.glossary.v1","collections":{"terms":{"entries":[{"id":"a","name":"Alpha","category":"term"},{"id":"A","name":"Alpha","category":"term"}]}}}`,
|
|
}
|
|
var results []map[string]any
|
|
for name, body := range documents {
|
|
path := filepath.Join(dir, name)
|
|
if err := os.WriteFile(path, []byte(body), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err := Load(context.Background(), []ck.Input{{Source: ck.File(path)}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
results = append(results, result.Config)
|
|
}
|
|
if !reflect.DeepEqual(results[0], results[1]) {
|
|
t.Fatalf("documents differ: %#v, %#v", results[0], results[1])
|
|
}
|
|
}
|