Complete shared glossary validation and acceptance checks
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -108,12 +109,78 @@ func TestLoadErrorsAndEmptyInput(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := Load(context.Background(), []ck.Input{{Source: ck.File(path)}})
|
||||
if err == nil || !strings.Contains(err.Error(), tc.contains) {
|
||||
t.Fatalf("Load() = %v", err)
|
||||
var loadErr *ck.LoadError
|
||||
if !errors.As(err, &loadErr) {
|
||||
t.Fatalf("expected native load error, got %v", err)
|
||||
}
|
||||
if tc.name == "missing marker" && !strings.Contains(err.Error(), path) {
|
||||
t.Fatalf("missing source context: %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 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])
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user