120 lines
4.7 KiB
Go
120 lines
4.7 KiB
Go
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
func loadDocumentBytes(content string) (*configkit.Result[Document], error) {
|
|
return configkit.Load(configkit.Bytes("application.yml", []byte(content)), configkit.Options[Document]{Definition: documentDefinition()})
|
|
}
|
|
|
|
func TestDocumentDefaultsAndExplicitValues(t *testing.T) {
|
|
result, err := loadDocumentBytes("{}\n")
|
|
if err != nil || !reflect.DeepEqual(result.Config, documentDefaults()) {
|
|
t.Fatalf("defaults: %+v %v", result, err)
|
|
}
|
|
result, err = loadDocumentBytes("promptkit:\n configuration: ''\n schema_dir: ''\ntransient_retries: 0\nserver:\n addr: ''\n artifact_root: ''\n max_request_bytes: 0\n max_artifact_bytes: 0\n max_response_bytes: 0\n")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := result.Config
|
|
if got.PromptKit.Configuration != "" || got.PromptKit.SchemaDir != "" || got.Server.Addr != "" || got.Server.ArtifactRoot != "" || got.Server.MaxRequestBytes != 0 || got.Server.MaxArtifactBytes != 0 || got.Server.MaxResponseBytes != 0 || got.TransientRetries == nil || *got.TransientRetries != 0 {
|
|
t.Fatalf("explicit values: %+v", got)
|
|
}
|
|
settings := Settings(got)
|
|
if settings.SchemaDir != "" || settings.PromptKitConfiguration != "" || settings.ServerAddr != "" || settings.MaxRequestBytes != 0 || settings.TransientRetries == nil || *settings.TransientRetries != 0 {
|
|
t.Fatalf("settings changed resolved values: %+v", settings)
|
|
}
|
|
result, err = loadDocumentBytes("transient_retries: null\n")
|
|
if err != nil || result.Config.TransientRetries != nil {
|
|
t.Fatalf("nullable retries: %+v %v", result, err)
|
|
}
|
|
}
|
|
|
|
func TestDocumentNormalizesWithoutAcquiringFramework(t *testing.T) {
|
|
result, err := loadDocumentBytes("promptkit:\n configuration: ' ./missing/../framework.yml '\n schema_dir: ' ./a/../schemas '\nserver:\n addr: ' :9000 '\n artifact_root: ' ./a/../artifacts '\n")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Config.PromptKit.Configuration != "framework.yml" || result.Config.PromptKit.SchemaDir != "schemas" || result.Config.Server.Addr != ":9000" || result.Config.Server.ArtifactRoot != "artifacts" {
|
|
t.Fatalf("normalized: %+v", result.Config)
|
|
}
|
|
for _, key := range []string{"configuration", "schema_dir"} {
|
|
explanation, ok := result.Explain(configkit.Keys("promptkit", key))
|
|
if !ok {
|
|
t.Fatal("missing path evidence", key)
|
|
}
|
|
normalized := false
|
|
for _, change := range explanation.History {
|
|
normalized = normalized || change.Origin.Operation == configkit.ChangeNormalization
|
|
}
|
|
if !normalized {
|
|
t.Fatalf("normalization evidence missing: %+v", explanation)
|
|
}
|
|
}
|
|
var output bytes.Buffer
|
|
if err := result.WriteEffective(&output); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(output.String(), "configuration: framework.yml") || !strings.Contains(output.String(), "schema_dir: schemas") {
|
|
t.Fatal(output.String())
|
|
}
|
|
unchanged, err := loadDocumentBytes("promptkit:\n configuration: framework.yml\n")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
explanation, _ := unchanged.Explain(configkit.Keys("promptkit", "configuration"))
|
|
for _, change := range explanation.History {
|
|
if change.Origin.Operation == configkit.ChangeNormalization {
|
|
t.Fatal("unchanged path was normalized")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDocumentValidationIsSafeAndPathSpecific(t *testing.T) {
|
|
for _, key := range []string{"transient_retries", "max_request_bytes", "max_artifact_bytes", "max_response_bytes"} {
|
|
content := key + ": -9173\n"
|
|
path := "/" + key
|
|
if key != "transient_retries" {
|
|
content = "server:\n " + content
|
|
path = "/server" + path
|
|
}
|
|
_, err := loadDocumentBytes(content)
|
|
var native *configkit.LoadError
|
|
if !errors.As(err, &native) {
|
|
t.Fatalf("typed error: %v", err)
|
|
}
|
|
found := false
|
|
for _, diagnostic := range native.Diagnostics() {
|
|
found = found || diagnostic.Code == configkit.CodeValidateError && diagnostic.Path.String() == path
|
|
if strings.Contains(diagnostic.Message, "9173") {
|
|
t.Fatalf("rejected value leaked: %+v", diagnostic)
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("no validation evidence for %s: %+v", path, native.Diagnostics())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDocumentRejectsRetiredAndInvalidShapes(t *testing.T) {
|
|
for _, content := range []string{
|
|
"promptkit_sources: []\n", "schema_dir: .\n", "defaults:\n render_format: text\n",
|
|
"promptkit: null\n", "promptkit:\n configuration: null\n", "promptkit:\n schema_dir: null\n",
|
|
"promptkit:\n profiles: []\n", "promptkit:\n backends: []\n", "promptkit:\n collections: {}\n",
|
|
"server: null\n", "server:\n addr: null\n", "server:\n max_request_bytes: null\n",
|
|
"server:\n unknown_field: 1\n", "prompt_dir: ./old-prompts\n",
|
|
"profile_dir: ./old-profiles\n", "backends: []\n",
|
|
} {
|
|
if _, err := loadDocumentBytes(content); err == nil {
|
|
t.Fatalf("accepted %q", content)
|
|
}
|
|
}
|
|
}
|