2026-11-28 19:44:30 +00:00
package main
import (
"bytes"
2026-11-29 04:35:26 +00:00
"encoding/json"
2026-11-28 19:44:30 +00:00
"os"
"path/filepath"
"strings"
"testing"
2026-11-29 04:35:26 +00:00
"forge.maximumdirect.net/eric/schemas/internal/validation"
2026-11-28 19:44:30 +00:00
)
func TestCommand ( t * testing . T ) {
for _ , test := range [ ] struct {
name string
args [ ] string
code int
out string
err string
} {
2026-11-29 16:33:30 +00:00
{ "valid" , [ ] string { "-root" , "../.." , "-schema" , "rpg/party" , "../../rpg/party/v0.1.0/examples/party.yml" } , 0 , "valid (rpg/party@0.2.0; maximumdirect.rpg.party.v1)" , "" } ,
{ "continue after failure" , [ ] string { "-root" , "../.." , "-schema" , "glossary" , "../../tests/invalid/semantic-duplicate-id.yml" , "../../glossary/v0.1.0/examples/weather.json" } , 1 , "weather.json: valid" , "semantic:" } ,
{ "structural" , [ ] string { "-root" , "../.." , "-schema" , "glossary" , "../../tests/invalid/structure-missing-name.yml" } , 1 , "" , "structure:" } ,
{ "decode" , [ ] string { "-root" , "../.." , "-schema" , "rpg/campaign" , "../../tests/invalid/decode-duplicate-key.yml" } , 1 , "" , "load:" } ,
{ "file missing" , [ ] string { "-root" , "../.." , "-schema" , "rpg/campaign" , filepath . Join ( t . TempDir ( ) , "missing.yml" ) } , 1 , "" , "missing.yml" } ,
2026-11-29 04:31:27 +00:00
{ "missing selection" , [ ] string { "-root" , "../.." , "../../rpg/party/v0.1.0/examples/party.yml" } , 2 , "" , "Usage:" } ,
{ "unknown selection" , [ ] string { "-root" , "../.." , "-schema" , "rpg/party@9.0.0" , "../../rpg/party/v0.1.0/examples/party.yml" } , 2 , "" , "schema selection:" } ,
2026-11-29 16:33:30 +00:00
{ "marker mismatch" , [ ] string { "-root" , "../.." , "-schema" , "glossary" , "../../rpg/party/v0.1.0/examples/party.yml" } , 1 , "" , "structure:" } ,
2026-11-28 19:44:30 +00:00
{ "no documents" , nil , 2 , "" , "Usage:" } ,
{ "bad flag" , [ ] string { "-unknown" } , 2 , "" , "flag provided" } ,
{ "help" , [ ] string { "-h" } , 0 , "" , "Usage:" } ,
2026-11-29 16:33:30 +00:00
{ "missing set" , [ ] string { "-root" , t . TempDir ( ) , "-schema" , "rpg/party" , "../../rpg/party/v0.1.0/examples/party.yml" } , 2 , "" , "schema set:" } ,
2026-11-28 19:44:30 +00:00
} {
t . Run ( test . name , func ( t * testing . T ) {
var stdout , stderr bytes . Buffer
code := run ( test . args , & stdout , & stderr )
if code != test . code || ! strings . Contains ( stdout . String ( ) , test . out ) || ! strings . Contains ( stderr . String ( ) , test . err ) {
t . Fatalf ( "exit=%d, stdout=%q, stderr=%q" , code , & stdout , & stderr )
}
} )
}
}
2026-11-29 04:35:26 +00:00
func TestExactDefinitions ( t * testing . T ) {
root := t . TempDir ( )
data , err := os . ReadFile ( "../../catalog.json" )
if err != nil {
t . Fatal ( err )
}
var catalog validation . Catalog
if err := json . Unmarshal ( data , & catalog ) ; err != nil {
t . Fatal ( err )
}
write := func ( path string , body [ ] byte ) {
t . Helper ( )
target := filepath . Join ( root , path )
if err := os . MkdirAll ( filepath . Dir ( target ) , 0755 ) ; err != nil {
t . Fatal ( err )
}
if err := os . WriteFile ( target , body , 0644 ) ; err != nil {
t . Fatal ( err )
}
}
var glossary validation . Record
for _ , record := range catalog . Schemas {
data , err := os . ReadFile ( "../../" + record . Path )
if err != nil {
t . Fatal ( err )
}
write ( record . Path , data )
if record . Name == "glossary" {
glossary = record
}
}
data , err = os . ReadFile ( "../../" + glossary . Path )
if err != nil {
t . Fatal ( err )
}
var schema map [ string ] any
if err := json . Unmarshal ( data , & schema ) ; err != nil {
t . Fatal ( err )
}
2026-11-29 16:33:30 +00:00
glossary . Version = "99.0.0"
glossary . Path = "glossary/v99.0.0/schema.json"
2026-11-29 04:35:26 +00:00
glossary . ID = "https://schemas.maximumdirect.net/" + glossary . Path
schema [ "$id" ] = glossary . ID
schema [ "required" ] = [ ] string { "schema_version" , "collections" , "synthetic_test_field" }
data , err = json . Marshal ( schema )
if err != nil {
t . Fatal ( err )
}
write ( glossary . Path , data )
catalog . Schemas = append ( catalog . Schemas , glossary )
data , err = json . Marshal ( catalog )
if err != nil {
t . Fatal ( err )
}
write ( "catalog.json" , data )
write ( "document.json" , [ ] byte ( ` { "schema_version":"maximumdirect.glossary.v1","collections": { }} ` ) )
2026-11-29 16:33:30 +00:00
for selection , want := range map [ string ] int { "glossary" : 1 , "glossary@0.1.0" : 2 , "glossary@99.0.0" : 2 } {
2026-11-29 04:35:26 +00:00
var stdout , stderr bytes . Buffer
if got := run ( [ ] string { "-root" , root , "-schema" , selection , filepath . Join ( root , "document.json" ) } , & stdout , & stderr ) ; got != want {
t . Fatalf ( "%s exit %d, want %d: %s" , selection , got , want , & stderr )
}
}
}
2026-11-28 19:44:30 +00:00
func TestJSONSyntax ( t * testing . T ) {
file := filepath . Join ( t . TempDir ( ) , "document.json" )
2026-11-29 04:29:11 +00:00
if err := os . WriteFile ( file , [ ] byte ( "schema_version: maximumdirect.rpg.campaign.v1\nid: sample" ) , 0600 ) ; err != nil {
2026-11-28 19:44:30 +00:00
t . Fatal ( err )
}
var stdout , stderr bytes . Buffer
2026-11-29 16:33:30 +00:00
if run ( [ ] string { "-root" , "../.." , "-schema" , "rpg/campaign" , file } , & stdout , & stderr ) != 1 || ! strings . Contains ( stderr . String ( ) , "decode: expected JSON syntax" ) {
2026-11-28 19:44:30 +00:00
t . Fatalf ( "expected JSON syntax failure, got %q" , & stderr )
}
}
2026-11-28 23:25:12 +00:00
func TestDocumentDecoding ( t * testing . T ) {
for _ , test := range [ ] struct {
file string
body string
code int
} {
2026-11-29 16:33:30 +00:00
{ "literal.json" , ` { "schema_version":"maximumdirect.rpg.campaign.v1","id":"weather","name":"Weather 🌧"} ` , 0 } ,
{ "escaped.json" , ` { "schema_version":"maximumdirect.rpg.campaign.v1","id":"weather","name":"Weather \ud83c\udf27"} ` , 1 } ,
2026-11-29 04:29:11 +00:00
{ "malformed-float.yml" , "schema_version: maximumdirect.rpg.campaign.v1\nid: weather\nreading: !!float e3\n" , 1 } ,
2026-11-28 23:25:12 +00:00
} {
t . Run ( test . file , func ( t * testing . T ) {
file := filepath . Join ( t . TempDir ( ) , test . file )
if err := os . WriteFile ( file , [ ] byte ( test . body ) , 0600 ) ; err != nil {
t . Fatal ( err )
}
var stdout , stderr bytes . Buffer
2026-11-29 16:33:30 +00:00
if code := run ( [ ] string { "-root" , "../.." , "-schema" , "rpg/campaign" , file } , & stdout , & stderr ) ; code != test . code {
2026-11-28 23:25:12 +00:00
t . Fatalf ( "want exit %d, got %d: stdout=%q stderr=%q" , test . code , code , & stdout , & stderr )
}
2026-11-29 16:33:30 +00:00
if test . code == 0 && ! strings . Contains ( stdout . String ( ) , "valid (rpg/campaign@0.2.0; maximumdirect.rpg.campaign.v1)" ) {
2026-11-28 23:25:12 +00:00
t . Fatalf ( "missing success report: %q" , & stdout )
}
2026-11-29 16:33:30 +00:00
if test . code != 0 && ! strings . Contains ( stderr . String ( ) , "load:" ) {
2026-11-28 23:25:12 +00:00
t . Fatalf ( "missing decoding failure: %q" , & stderr )
}
} )
}
}