2026-11-27 16:10:23 +00:00
package glossary
import (
"encoding/json"
"strings"
"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 } ,
2026-11-27 21:18:00 +00:00
{ "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 } ,
2026-11-27 16:10:23 +00:00
{ "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 )
}
}