152 lines
5.7 KiB
Go
152 lines
5.7 KiB
Go
package consumers_test
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
|
"gitea.maximumdirect.net/eric/ConfigKit/examples/consumers"
|
|
)
|
|
|
|
func Example_nativeFilesystem() {
|
|
result, err := consumers.LoadBundled()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
fmt.Println(result.Config.Name, result.Config.Enabled)
|
|
// Output: embedded false
|
|
}
|
|
|
|
func Example_rootRelativeImports() {
|
|
result, err := consumers.LoadPromptBundle()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
source := result.Sources()[2]
|
|
fmt.Println(result.Config.Name, result.Config.Version, result.Config.Prompts["brief"].Summary, source.Destination.String(), err)
|
|
// Output: Root import example 1.0 A root-loaded prompt. /prompts <nil>
|
|
}
|
|
|
|
func TestPromptBundleUsesRelativeImportDestinations(t *testing.T) {
|
|
result, err := consumers.LoadPromptBundle()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Config.Name != "Root import example" || result.Config.Version != "1.0" || result.Config.Prompts["brief"].Summary != "A root-loaded prompt." {
|
|
t.Fatalf("root import result = %#v", result.Config)
|
|
}
|
|
sources := result.Sources()
|
|
if len(sources) != 3 || sources[0].Destination.String() != configkit.Keys().String() ||
|
|
sources[1].Path != "metadata.yml" || sources[1].Destination.String() != configkit.Keys().String() ||
|
|
sources[2].Path != "prompts/brief.yml" || sources[2].Destination.String() != "/prompts" ||
|
|
sources[2].ImportedFrom == nil || sources[2].ImportedFrom.SourceID != sources[0].ID {
|
|
t.Fatalf("root import sources = %#v", sources)
|
|
}
|
|
}
|
|
|
|
func Example_layeredCatalog() {
|
|
result, err := consumers.LoadCatalog(fixture("layered-main", "config.yml"))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
welcome, err := consumers.ReadWelcome(result)
|
|
fmt.Println(result.Config.Mode, result.Config.Enabled, result.Config.Workers,
|
|
result.Config.Labels["origin"], result.Config.Labels["tenant"], strings.TrimSpace(welcome), err)
|
|
// Output: production false 0 bundle example Welcome from the bundle. <nil>
|
|
}
|
|
|
|
func Example_sourceVersionAdoption() {
|
|
result, err := consumers.LoadVersionedService(configkit.SourceVersionModeIfPresent)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
checks, redacted := 0, true
|
|
for _, source := range result.Sources() {
|
|
if check := source.VersionCheck; check != nil {
|
|
checks++
|
|
redacted = redacted && check.Redacted && check.Version.Text == ""
|
|
}
|
|
}
|
|
required, requiredErr := consumers.LoadVersionedService(configkit.SourceVersionModeRequired)
|
|
fmt.Println(result.Config.Service.Version, result.Config.Service.Metadata.Release,
|
|
checks, redacted, required != nil && requiredErr == nil)
|
|
// Output: service-v2 application-2026.10 2 true true
|
|
}
|
|
|
|
func TestVersionedServiceModesAndMountedProtection(t *testing.T) {
|
|
for _, mode := range []configkit.SourceVersionMode{
|
|
configkit.SourceVersionModeIfPresent,
|
|
configkit.SourceVersionModeRequired,
|
|
} {
|
|
result, err := consumers.LoadVersionedService(mode)
|
|
if err != nil || result == nil || result.Config.Service.Version != "service-v2" ||
|
|
result.Config.Service.Name != "example service" || result.Config.Service.Endpoint != "https://service.example.invalid" ||
|
|
result.Config.Service.Metadata.Release != "application-2026.10" {
|
|
t.Fatalf("versioned service mode %d = %#v, %v", mode, result, err)
|
|
}
|
|
sources := result.Sources()
|
|
if len(sources) != 2 {
|
|
t.Fatalf("versioned service sources = %#v", sources)
|
|
}
|
|
for _, source := range sources {
|
|
check := source.VersionCheck
|
|
if check == nil || check.Path.String() != "/version" || !check.Version.Present ||
|
|
!check.Redacted || check.Version.Text != "" {
|
|
t.Fatalf("safe source version evidence = %#v", sources)
|
|
}
|
|
}
|
|
}
|
|
_, err := consumers.LoadVersionedService(configkit.SourceVersionModeIfPresent,
|
|
configkit.Override{Path: configkit.Keys("service", "version"), Value: "cli-v3", Flag: "--service-version"})
|
|
var loadError *configkit.LoadError
|
|
if !errors.As(err, &loadError) || len(loadError.Diagnostics()) != 1 ||
|
|
loadError.Diagnostics()[0].Code != configkit.CodeOverrideVersion ||
|
|
loadError.Diagnostics()[0].Path.String() != "/service/version" {
|
|
t.Fatalf("mounted main version override error = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCatalogRetainsPresenceAndSourceContext(t *testing.T) {
|
|
result, err := consumers.LoadCatalog(fixture("layered-main", "config.yml"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, key := range []string{"enabled", "workers"} {
|
|
info, found := result.Metadata().Lookup(configkit.Keys(key))
|
|
if !found || !info.Explicit || info.Origin.Layer != configkit.LayerBase {
|
|
t.Fatalf("%s presence = %#v, %v", key, info, found)
|
|
}
|
|
}
|
|
info, found := result.Metadata().Lookup(configkit.Keys("templates", "welcome"))
|
|
if !found || info.Origin.Layer != configkit.LayerUnderlay || len(info.Origin.Locations) != 1 {
|
|
t.Fatalf("welcome origin = %#v, %v", info, found)
|
|
}
|
|
context, found := result.SourceContext(info.Origin.Locations[0].SourceID)
|
|
if !found || context.BaseDir != "defaults" {
|
|
t.Fatalf("welcome context = %#v, %v", context, found)
|
|
}
|
|
welcome, err := consumers.ReadWelcome(result)
|
|
if err != nil || strings.TrimSpace(welcome) != "Welcome from the bundle." {
|
|
t.Fatalf("welcome = %q, %v", welcome, err)
|
|
}
|
|
if result.Config.Labels["tenant"] != "example" || result.Config.Labels["origin"] != "bundle" {
|
|
t.Fatalf("recursive underlay labels = %#v", result.Config.Labels)
|
|
}
|
|
}
|
|
|
|
func TestCatalogValidationRejectsInvalidMode(t *testing.T) {
|
|
_, err := consumers.LoadCatalog(fixture("layered-main", "invalid.yml"))
|
|
var loadError *configkit.LoadError
|
|
if !errors.As(err, &loadError) || len(loadError.Diagnostics()) == 0 ||
|
|
loadError.Diagnostics()[0].Code != configkit.CodeValidateError ||
|
|
loadError.Diagnostics()[0].Path.String() != "/mode" {
|
|
t.Fatalf("invalid mode error = %v", err)
|
|
}
|
|
}
|