149 lines
5.8 KiB
Go
149 lines
5.8 KiB
Go
package config
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"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 || result.Config.Server != documentDefaults().Server {
|
|
t.Fatalf("defaults: %+v %v", result, err)
|
|
}
|
|
result, err = loadDocumentBytes("transient_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.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)
|
|
}
|
|
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("server:\n addr: ' :9000 '\n artifact_root: ' ./a/../artifacts '\n")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Config.Server.Addr != ":9000" || result.Config.Server.ArtifactRoot != "artifacts" {
|
|
t.Fatalf("normalized: %+v", result.Config)
|
|
}
|
|
for _, key := range []string{"addr", "artifact_root"} {
|
|
explanation, ok := result.Explain(configkit.Keys("server", 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(), "artifact_root: artifacts") {
|
|
t.Fatal(output.String())
|
|
}
|
|
unchanged, err := loadDocumentBytes("server:\n artifact_root: artifacts\n")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
explanation, _ := unchanged.Explain(configkit.Keys("server", "artifact_root"))
|
|
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",
|
|
"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)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNativeSettingsExtensionsAndRawKeyBoundary(t *testing.T) {
|
|
result, err := loadDocumentBytes("promptkit:\n custom: permitted\n backends:\n - id: local\n endpoint: http://localhost:1234\n extension: retained\n extra_params: {ordinary: visible}\n")
|
|
if err != nil || result.Config.PromptKit.Application["custom"] != "permitted" || result.Config.PromptKit.Backends[0].Application["extension"] != "retained" {
|
|
t.Fatalf("native extensions: %+v %v", result, err)
|
|
}
|
|
var effective bytes.Buffer
|
|
if err := result.WriteEffective(&effective); err != nil || !strings.Contains(effective.String(), "visible") {
|
|
t.Fatalf("ordinary extra params: %q %v", effective.String(), err)
|
|
}
|
|
for _, test := range []struct{ body, path string }{
|
|
{"promptkit: {configuration: null}", "/promptkit/configuration"},
|
|
{"promptkit: {schema_dir: null}", "/promptkit/schema_dir"},
|
|
{"promptkit: {backends: [{id: b, api_key: forbidden-secret}]}", "/promptkit/backends/0/api_key"},
|
|
{"promptkit: {profiles: [{id: p, api_key: forbidden-secret}]}", "/promptkit/profiles/0/api_key"},
|
|
{"promptkit: {collections: {team: {backends: [{id: b, api_key: forbidden-secret}]}}}", "/promptkit/collections/team/backends/0/api_key"},
|
|
{"promptkit: {collections: {team: {profiles: [{id: p, api_key: forbidden-secret}]}}}", "/promptkit/collections/team/profiles/0/api_key"},
|
|
} {
|
|
_, err := loadDocumentBytes(test.body)
|
|
var native *configkit.LoadError
|
|
if !errors.As(err, &native) {
|
|
t.Fatalf("%s: %v", test.path, err)
|
|
}
|
|
found := false
|
|
for _, diagnostic := range native.Diagnostics() {
|
|
found = found || diagnostic.Path.String() == test.path && diagnostic.Code == configkit.CodeValidateError
|
|
if strings.Contains(diagnostic.Message, "forbidden-secret") {
|
|
t.Fatalf("secret leaked: %+v", diagnostic)
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatalf("missing diagnostic %s: %+v", test.path, native.Diagnostics())
|
|
}
|
|
}
|
|
}
|