2026-09-30 02:08:20 +00:00
package configuration
import (
"errors"
2026-10-17 05:56:54 +00:00
"strings"
2026-09-30 02:08:20 +00:00
"testing"
"gitea.maximumdirect.net/eric/ConfigKit"
)
func mapping ( label string , values map [ string ] any ) configkit . Input {
return configkit . Input { Source : configkit . Mapping ( label , values ) }
}
2026-10-17 05:56:54 +00:00
func TestLoadChecksOptionalConfigurationVersion ( t * testing . T ) {
for _ , test := range [ ] struct {
name string
version any
include bool
valid bool
} {
{ name : "string one" , version : "1" , include : true , valid : true } ,
{ name : "integer one" , version : int64 ( 1 ) , include : true , valid : true } ,
{ name : "omitted" , valid : true } ,
{ name : "explicit null" , include : true } ,
{ name : "unsupported string" , version : "2" , include : true } ,
} {
t . Run ( test . name , func ( t * testing . T ) {
values := map [ string ] any { }
if test . include {
values [ "version" ] = test . version
}
loaded , err := load ( mapping ( "main" , values ) , nil )
if test . valid {
if err != nil {
t . Fatalf ( "load accepted version: %v" , err )
}
if test . include && ( loaded . result . Config . Version == nil || * loaded . result . Config . Version != "1" ) {
t . Fatalf ( "typed version = %v, want 1" , loaded . result . Config . Version )
}
if ! test . include && loaded . result . Config . Version != nil {
t . Fatalf ( "omitted version = %q, want nil" , * loaded . result . Config . Version )
}
return
}
var loadErr * configkit . LoadError
if err == nil || ! errors . As ( err , & loadErr ) {
t . Fatalf ( "load error = %v, want ConfigKit load error" , err )
}
if strings . Contains ( err . Error ( ) , "2" ) {
t . Fatalf ( "load error disclosed the unsupported value: %v" , err )
}
found := false
for _ , diagnostic := range loadErr . Diagnostics ( ) {
if diagnostic . Path . String ( ) == "/version" && diagnostic . Message == "unsupported configuration specification version" {
found = true
}
}
if ! found {
t . Fatalf ( "load diagnostics = %+v, want safe version diagnostic" , loadErr . Diagnostics ( ) )
}
} )
}
}
func TestLoadConsumesAcceptedUnderlayVersionAndValidatesHeaderlessPayload ( t * testing . T ) {
underlay := mapping ( "declared underlay" , map [ string ] any {
"version" : int64 ( 1 ) ,
} )
loaded , err := load ( mapping ( "headerless main" , map [ string ] any { } ) , [ ] configkit . Underlay { { Inputs : [ ] configkit . Input { underlay } } } )
if err != nil {
t . Fatalf ( "load accepted non-main declaration: %v" , err )
}
if loaded . result . Config . Version != nil {
t . Fatalf ( "consumed non-main declaration remained in effective payload: %q" , * loaded . result . Config . Version )
}
invalidFragment := mapping ( "headerless prompt fragment" , map [ string ] any {
"prompts" : map [ string ] any { "incomplete" : map [ string ] any { "description" : "still validated" } } ,
} )
if _ , err := load ( configkit . Input { Source : configkit . NoSource ( ) } , [ ] configkit . Underlay { { Inputs : [ ] configkit . Input { invalidFragment } } } ) ; err == nil || ! strings . Contains ( err . Error ( ) , "at least one message is required" ) {
t . Fatalf ( "headerless fragment validation error = %v, want final prompt validation" , err )
}
}
2026-09-30 02:08:20 +00:00
func TestLoadComposesLiteralKeysAndEffectivePresence ( t * testing . T ) {
base := mapping ( "defaults" , map [ string ] any {
2026-10-17 06:03:28 +00:00
"version" : int64 ( 1 ) ,
2026-09-30 02:08:20 +00:00
"backends" : map [ string ] any {
"local.v1" : map [ string ] any { "endpoint" : "http://localhost:8080/v1" , "concurrency_limit" : 2 } ,
} ,
2026-10-17 06:03:28 +00:00
"collections" : map [ string ] any { "base" : map [ string ] any {
"release_version" : "" ,
"backends" : map [ string ] any {
"local.v1" : map [ string ] any { "endpoint" : "http://localhost:8080/v1" , "concurrency_limit" : 3 } ,
} ,
"profiles" : map [ string ] any {
"one" : map [ string ] any { "backend" : "local.v1" , "temperature" : 0.8 , "top_p" : 0.9 , "api_key_required" : true ,
"extra_params" : map [ string ] any { "provider" : map [ string ] any { "first" : 1 , "second" : 2 } } } ,
"child.v1" : map [ string ] any { "extends" : "one" } ,
"namespaced" : map [ string ] any { "backend" : "base/local.v1" , "model" : "collection-model" } ,
} ,
} , "invoice" : map [ string ] any {
"release_version" : "r7" ,
"prompts" : map [ string ] any {
"summary" : map [ string ] any {
"version" : "v1.0" ,
"messages" : [ ] any { map [ string ] any { "role" : "user" , "content" : "Summarize." } } ,
"output" : map [ string ] any { "format" : "text" , "validation_mode" : "basic" } ,
} ,
"version" : map [ string ] any {
"messages" : [ ] any { map [ string ] any { "role" : "user" , "content" : "Nested ID." } } ,
"output" : map [ string ] any { "format" : "text" , "validation_mode" : "basic" } ,
} ,
} ,
2026-10-17 05:41:21 +00:00
} } ,
2026-09-30 02:08:20 +00:00
} )
overlay := mapping ( "operator" , map [ string ] any {
"backends" : map [ string ] any { "local.v1" : map [ string ] any { "queue_capacity" : 0 } } ,
2026-10-17 06:03:28 +00:00
"collections" : map [ string ] any { "base" : map [ string ] any { "profiles" : map [ string ] any { "child.v1" : map [ string ] any {
2026-09-30 02:08:20 +00:00
"temperature" : 0 , "top_p" : nil , "api_key_required" : false ,
"extra_params" : map [ string ] any { "provider" : map [ string ] any { "second" : 3 } } ,
2026-10-17 06:03:28 +00:00
} } } } ,
2026-09-30 02:08:20 +00:00
} )
2026-10-17 06:03:28 +00:00
loaded , err := load ( mapping ( "main" , map [ string ] any { "version" : "1" } ) , [ ] configkit . Underlay {
2026-09-30 02:08:20 +00:00
{ Inputs : [ ] configkit . Input { base } } , { Inputs : [ ] configkit . Input { overlay } } ,
} )
if err != nil {
t . Fatal ( err )
}
2026-10-17 06:03:28 +00:00
profile := loaded . result . Config . Collections [ "base" ] . Profiles [ "child.v1" ]
2026-09-30 02:08:20 +00:00
if profile . Temperature == nil || * profile . Temperature != 0 || profile . TopP != nil || profile . APIKeyRequired == nil || * profile . APIKeyRequired {
t . Fatalf ( "effective values were not retained: %+v" , profile )
}
2026-10-17 06:03:28 +00:00
if loaded . profiles [ "base/child.v1" ] [ "temperature" ] != fieldValue || loaded . profiles [ "base/child.v1" ] [ "top_p" ] != fieldNull || loaded . profiles [ "base/child.v1" ] [ "api_key_required" ] != fieldValue {
t . Fatalf ( "effective presence was lost: %+v" , loaded . profiles [ "base/child.v1" ] )
2026-09-30 02:08:20 +00:00
}
provider := profile . ExtraParams [ "provider" ] . ( map [ string ] any )
if provider [ "first" ] != int64 ( 1 ) || provider [ "second" ] != int64 ( 3 ) {
t . Fatalf ( "recursive overlay failed: %+v" , provider )
}
backend := loaded . result . Config . Backends [ "local.v1" ]
if backend . QueueCapacity == nil || * backend . QueueCapacity != 0 || loaded . backends [ "local.v1" ] [ "queue_capacity" ] != fieldValue {
t . Fatalf ( "explicit zero queue capacity was lost: %+v" , backend )
}
2026-10-17 06:03:28 +00:00
if _ , err := loaded . registry . GetBackend ( "base/local.v1" ) ; err != nil {
t . Fatalf ( "collection backend was not indexed by its full ID: %v" , err )
}
collectionProfile , exists := loaded . indexedProfiles [ "base/namespaced" ]
if ! exists || collectionProfile . ID != "base/namespaced" || collectionProfile . ResolvedTarget == nil || collectionProfile . ResolvedTarget . BackendID != "base/local.v1" {
t . Fatalf ( "collection profile snapshot = %+v, exists %t" , collectionProfile , exists )
}
if loaded . result . Config . Collections [ "invoice" ] . Prompts [ "summary" ] . Messages [ 0 ] . Content == nil || loaded . result . Config . Collections [ "invoice" ] . Prompts [ "summary" ] . Version == nil || * loaded . result . Config . Collections [ "invoice" ] . Prompts [ "summary" ] . Version != "v1.0" {
2026-09-30 02:08:20 +00:00
t . Fatal ( "literal prompt ID and version were not decoded" )
}
2026-10-17 06:03:28 +00:00
if loaded . result . Config . Version == nil || * loaded . result . Config . Version != "1" ||
loaded . result . Config . Collections [ "base" ] . ReleaseVersion == nil || * loaded . result . Config . Collections [ "base" ] . ReleaseVersion != "" ||
loaded . result . Config . Collections [ "invoice" ] . ReleaseVersion == nil || * loaded . result . Config . Collections [ "invoice" ] . ReleaseVersion != "r7" {
t . Fatalf ( "root or collection version metadata was not retained: %+v" , loaded . result . Config )
}
if _ , _ , err := loaded . prompt ( "invoice/version" ) ; err != nil {
t . Fatalf ( "resource named version was not indexed under its qualified identity: %v" , err )
}
}
func TestLoadRejectsInvalidCollectionAndResourceKeys ( t * testing . T ) {
for _ , test := range [ ] struct {
name string
values map [ string ] any
message string
} {
{
name : "namespace with separator" ,
values : map [ string ] any { "collections" : map [ string ] any { "bad/name" : map [ string ] any { } } } ,
message : "collection namespace must be nonblank and contain no slash" ,
} ,
{
name : "root resource with separator" ,
values : map [ string ] any { "backends" : map [ string ] any {
"bad/name" : map [ string ] any { "endpoint" : "https://example.test/v1" } ,
} } ,
message : "backend key must be a nonblank local key without a slash" ,
} ,
{
name : "collection resource with separator" ,
values : map [ string ] any { "collections" : map [ string ] any { "good" : map [ string ] any {
"prompts" : map [ string ] any { "bad/name" : map [ string ] any {
"messages" : [ ] any { map [ string ] any { "role" : "user" , "content" : "Hello." } } ,
"output" : map [ string ] any { "format" : "text" , "validation_mode" : "basic" } ,
} } ,
} } } ,
message : "prompt key must be a nonblank local key without a slash" ,
} ,
} {
t . Run ( test . name , func ( t * testing . T ) {
_ , err := load ( mapping ( "invalid names" , test . values ) , nil )
var loadErr * configkit . LoadError
if err == nil || ! errors . As ( err , & loadErr ) {
t . Fatalf ( "load error = %v, want ConfigKit validation error" , err )
}
for _ , diagnostic := range loadErr . Diagnostics ( ) {
if diagnostic . Message == test . message {
return
}
}
t . Fatalf ( "load diagnostics = %+v, want message %q" , loadErr . Diagnostics ( ) , test . message )
} )
}
2026-09-30 02:08:20 +00:00
}
func TestLoadRecordsBackendNormalizationAndNullDefault ( t * testing . T ) {
loaded , err := load ( mapping ( "backend" , map [ string ] any { "backends" : map [ string ] any {
"local" : map [ string ] any { "endpoint" : " HTTP://localhost:8080/v1 " , "api_key_env" : " TOKEN_NAME " ,
"concurrency_limit" : 2 , "queue_capacity" : nil } ,
} } ) , nil )
if err != nil {
t . Fatal ( err )
}
backend := loaded . result . Config . Backends [ "local" ]
if backend . Endpoint == nil || * backend . Endpoint != "http://localhost:8080/v1" ||
backend . APIKeyEnv == nil || * backend . APIKeyEnv != "TOKEN_NAME" ||
backend . QueueCapacity == nil || * backend . QueueCapacity != 1024 {
t . Fatalf ( "backend normalization/default was not applied: %+v" , backend )
}
if loaded . backends [ "local" ] [ "queue_capacity" ] != fieldNull {
t . Fatalf ( "explicit null was not retained: %+v" , loaded . backends [ "local" ] )
}
}
2026-10-13 15:53:27 +00:00
func TestLoadBackendSmokeTestsOmissionAndNull ( t * testing . T ) {
for _ , test := range [ ] struct {
name string
smokeTests any
include bool
inherited bool
} {
{ name : "omitted" } ,
{ name : "null" , smokeTests : nil , include : true , inherited : true } ,
} {
t . Run ( test . name , func ( t * testing . T ) {
backend := map [ string ] any { "endpoint" : "https://backend.example/v1" }
if test . include {
backend [ "smoke_tests" ] = test . smokeTests
}
main := mapping ( "backend" , map [ string ] any {
"backends" : map [ string ] any { "local" : backend } ,
} )
var underlays [ ] configkit . Underlay
if test . inherited {
underlays = [ ] configkit . Underlay { { Inputs : [ ] configkit . Input { mapping ( "defaults" , map [ string ] any {
"backends" : map [ string ] any { "local" : map [ string ] any {
"endpoint" : "https://backend.example/v1" ,
"smoke_tests" : map [ string ] any { "default_profile" : "inherited-profile" } ,
} } ,
} ) } } }
}
loaded , err := load ( main , underlays )
if err != nil {
t . Fatal ( err )
}
if got := loaded . result . Config . Backends [ "local" ] . SmokeTests ; got != nil {
t . Fatalf ( "smoke_tests = %+v, want nil" , got )
}
selected , err := loaded . registry . GetBackend ( "local" )
if err != nil {
t . Fatal ( err )
}
if selected . SmokeTests != nil {
t . Fatalf ( "indexed smoke_tests = %+v, want nil" , selected . SmokeTests )
}
} )
}
}
func TestLoadMergesBackendSmokeTestsAndRetainsFallbacks ( t * testing . T ) {
base := mapping ( "defaults" , map [ string ] any {
"backends" : map [ string ] any { "local" : map [ string ] any {
"endpoint" : "https://backend.example/v1" ,
"smoke_tests" : map [ string ] any {
"default_profile" : "catalog-profile" ,
"reachability" : map [ string ] any { "endpoint" : " https://probe.example/ready?check=1 " } ,
"authentication" : map [ string ] any {
"kind" : " OPENROUTER_KEY " ,
"endpoint" : "https://probe.example/key?scope=current" ,
} ,
} ,
} } ,
} )
overlay := mapping ( "operator" , map [ string ] any {
"backends" : map [ string ] any { "local" : map [ string ] any {
"smoke_tests" : map [ string ] any {
"default_profile" : "not-yet-listed" ,
"authentication" : map [ string ] any { "kind" : " NONE " , "endpoint" : nil } ,
} ,
} } ,
} )
loaded , err := load ( configkit . Input { Source : configkit . NoSource ( ) } , [ ] configkit . Underlay {
{ Inputs : [ ] configkit . Input { base } } ,
{ Inputs : [ ] configkit . Input { overlay } } ,
} )
if err != nil {
t . Fatalf ( "load effective diagnostic metadata: %v" , err )
}
selected , err := loaded . registry . GetBackend ( "local" )
if err != nil {
t . Fatal ( err )
}
metadata := selected . SmokeTests
if selected . Endpoint != "https://backend.example/v1" {
t . Fatalf ( "effective backend endpoint = %q" , selected . Endpoint )
}
if metadata == nil || metadata . DefaultProfile == nil || * metadata . DefaultProfile != "not-yet-listed" {
t . Fatalf ( "default profile overlay was not retained: %+v" , metadata )
}
if metadata . Reachability == nil || metadata . Reachability . Endpoint == nil ||
* metadata . Reachability . Endpoint != "https://probe.example/ready?check=1" {
t . Fatalf ( "full reachability destination was not normalized: %+v" , metadata . Reachability )
}
if metadata . Authentication == nil || metadata . Authentication . Kind == nil || * metadata . Authentication . Kind != "none" ||
metadata . Authentication . Endpoint != nil {
t . Fatalf ( "authentication overlay or null fallback was not retained: %+v" , metadata . Authentication )
}
}
func TestLoadRejectsInvalidFinalBackendSmokeTests ( t * testing . T ) {
for _ , test := range [ ] struct {
name string
data map [ string ] any
path string
} {
{
name : "blank default profile" ,
data : map [ string ] any { "default_profile" : " \t " } ,
path : "/backends/local/smoke_tests/default_profile" ,
} ,
{
name : "authentication kind required" ,
data : map [ string ] any { "authentication" : map [ string ] any { } } ,
path : "/backends/local/smoke_tests/authentication/kind" ,
} ,
{
name : "unknown authentication kind" ,
data : map [ string ] any { "authentication" : map [ string ] any { "kind" : "bearer" } } ,
path : "/backends/local/smoke_tests/authentication/kind" ,
} ,
{
name : "non-http reachability URL" ,
data : map [ string ] any { "reachability" : map [ string ] any { "endpoint" : "ftp://probe.example/ready" } } ,
path : "/backends/local/smoke_tests/reachability/endpoint" ,
} ,
{
name : "blank authentication URL" ,
data : map [ string ] any { "authentication" : map [ string ] any { "kind" : "none" , "endpoint" : " " } } ,
path : "/backends/local/smoke_tests/authentication/endpoint" ,
} ,
{
name : "relative authentication URL" ,
data : map [ string ] any { "authentication" : map [ string ] any { "kind" : "none" , "endpoint" : "/key" } } ,
path : "/backends/local/smoke_tests/authentication/endpoint" ,
} ,
} {
t . Run ( test . name , func ( t * testing . T ) {
_ , err := load ( mapping ( "backend" , map [ string ] any {
"backends" : map [ string ] any { "local" : map [ string ] any {
"endpoint" : "https://backend.example/v1" ,
"smoke_tests" : test . data ,
} } ,
} ) , nil )
var loadError * configkit . LoadError
if ! errors . As ( err , & loadError ) {
t . Fatalf ( "expected final-value validation error, got %v" , err )
}
for _ , diagnostic := range loadError . Diagnostics ( ) {
if diagnostic . Path . String ( ) == test . path {
return
}
}
t . Fatalf ( "expected diagnostic at %s, got %+v" , test . path , loadError . Diagnostics ( ) )
} )
}
}
2026-09-30 02:08:20 +00:00
func TestLoadDefaultsAbsentQueueCapacity ( t * testing . T ) {
loaded , err := load ( mapping ( "backend" , map [ string ] any { "backends" : map [ string ] any {
"local" : map [ string ] any { "endpoint" : "http://localhost:8080/v1" , "concurrency_limit" : 1 } ,
} } ) , nil )
if err != nil {
t . Fatal ( err )
}
backend := loaded . result . Config . Backends [ "local" ]
if backend . QueueCapacity == nil || * backend . QueueCapacity != 1024 || loaded . backends [ "local" ] [ "queue_capacity" ] != fieldAbsent {
t . Fatalf ( "absent queue capacity did not receive its default: %+v" , backend )
}
}
func TestLoadRejectsStrictGroupConflict ( t * testing . T ) {
_ , err := load ( configkit . Input { Source : configkit . NoSource ( ) } , [ ] configkit . Underlay { { Inputs : [ ] configkit . Input {
mapping ( "first" , map [ string ] any { "profiles" : map [ string ] any { "shared" : map [ string ] any { "model" : "one" } } } ) ,
mapping ( "second" , map [ string ] any { "profiles" : map [ string ] any { "shared" : map [ string ] any { "model" : "one" } } } ) ,
} } } )
var loadError * configkit . LoadError
if ! errors . As ( err , & loadError ) {
t . Fatalf ( "expected ConfigKit load error, got %v" , err )
}
}
func TestLoadValidatesOnlyFinalDomainValues ( t * testing . T ) {
underlay := mapping ( "defaults" , map [ string ] any {
"backends" : map [ string ] any { "local" : map [ string ] any { "endpoint" : "not a URL" } } ,
"profiles" : map [ string ] any { "partial" : map [ string ] any { "backend" : "local" } } ,
} )
main := mapping ( "operator" , map [ string ] any { "backends" : map [ string ] any { "local" : map [ string ] any { "endpoint" : "http://localhost:8080/v1" } } } )
loaded , err := load ( main , [ ] configkit . Underlay { { Inputs : [ ] configkit . Input { underlay } } } )
if err != nil {
t . Fatalf ( "replaced lower-layer domain value should not fail: %v" , err )
}
if loaded . result . Config . Profiles [ "partial" ] . Model != nil {
t . Fatal ( "unused partial profile unexpectedly gained a model" )
}
_ , err = load ( configkit . Input { Source : configkit . NoSource ( ) } , [ ] configkit . Underlay { { Inputs : [ ] configkit . Input {
mapping ( "invalid" , map [ string ] any { "backends" : map [ string ] any { "unused" : map [ string ] any { "endpoint" : "not a URL" } } } ) ,
} } } )
var loadError * configkit . LoadError
if ! errors . As ( err , & loadError ) {
t . Fatalf ( "expected path-aware effective backend failure, got %v" , err )
}
if diagnostics := loadError . Diagnostics ( ) ; len ( diagnostics ) != 1 || diagnostics [ 0 ] . Path . String ( ) != "/backends/unused/endpoint" {
t . Fatalf ( "expected endpoint diagnostic, got %+v" , diagnostics )
}
}
2026-10-13 15:53:27 +00:00
func TestLoadValidatesOnlyFinalBackendSmokeTestValues ( t * testing . T ) {
underlay := mapping ( "defaults" , map [ string ] any {
"backends" : map [ string ] any { "local" : map [ string ] any {
"endpoint" : "https://backend.example/v1" ,
"smoke_tests" : map [ string ] any {
"default_profile" : " " ,
"reachability" : map [ string ] any { "endpoint" : "ftp://invalid.example/" } ,
"authentication" : map [ string ] any { "kind" : "unsupported" } ,
} ,
} } ,
} )
main := mapping ( "operator" , map [ string ] any {
"backends" : map [ string ] any { "local" : map [ string ] any {
"smoke_tests" : map [ string ] any {
"default_profile" : "dangling-profile" ,
"reachability" : map [ string ] any { "endpoint" : nil } ,
"authentication" : map [ string ] any { "kind" : "none" } ,
} ,
} } ,
} )
loaded , err := load ( main , [ ] configkit . Underlay { { Inputs : [ ] configkit . Input { underlay } } } )
if err != nil {
t . Fatalf ( "replaced lower metadata should not fail: %v" , err )
}
selected , err := loaded . registry . GetBackend ( "local" )
if err != nil {
t . Fatal ( err )
}
if selected . SmokeTests == nil || selected . SmokeTests . DefaultProfile == nil ||
* selected . SmokeTests . DefaultProfile != "dangling-profile" {
t . Fatalf ( "final dangling default profile was not retained: %+v" , selected . SmokeTests )
}
}
2026-09-30 02:08:20 +00:00
func TestLoadKeepsSyntaxErrorsFromLowerTiers ( t * testing . T ) {
_ , err := load ( mapping ( "operator" , map [ string ] any { "backends" : map [ string ] any { "local" : map [ string ] any { "endpoint" : "http://localhost:8080" } } } ) ,
[ ] configkit . Underlay { { Inputs : [ ] configkit . Input { { Source : configkit . Bytes ( "broken" , [ ] byte ( "backends: [" ) ) } } } } )
var loadError * configkit . LoadError
if ! errors . As ( err , & loadError ) {
t . Fatalf ( "expected lower-tier parsing error, got %v" , err )
}
}