Adopt native configuration discovery and a single PromptKit entry file
This commit is contained in:
@@ -11,213 +11,109 @@ import (
|
||||
)
|
||||
|
||||
func loadDocumentBytes(content string) (*configkit.Result[Document], error) {
|
||||
return configkit.Load(configkit.Bytes("application.yml", []byte(content)), configkit.Options[Document]{
|
||||
Definition: documentDefinition(),
|
||||
})
|
||||
return configkit.Load(configkit.Bytes("application.yml", []byte(content)), configkit.Options[Document]{Definition: documentDefinition()})
|
||||
}
|
||||
|
||||
func TestDocumentDefaultsAndExplicitValues(t *testing.T) {
|
||||
defaultsOnly, err := configkit.Load(configkit.NoSource(), configkit.Options[Document]{Definition: documentDefinition()})
|
||||
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.Fatalf("defaults-only load: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(defaultsOnly.Config, documentDefaults()) || len(defaultsOnly.Sources()) != 0 {
|
||||
t.Fatalf("unexpected defaults-only result: %+v sources=%+v", defaultsOnly.Config, defaultsOnly.Sources())
|
||||
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)
|
||||
}
|
||||
|
||||
result, err := loadDocumentBytes(`promptkit_sources:
|
||||
- file: " ./bundle/../framework.yml "
|
||||
- directory: "./profiles"
|
||||
schema_dir: ""
|
||||
transient_retries: 0
|
||||
server:
|
||||
addr: ""
|
||||
artifact_root: ""
|
||||
max_request_bytes: 0
|
||||
max_artifact_bytes: 0
|
||||
max_response_bytes: 0
|
||||
defaults:
|
||||
render_format: " JSON "
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatalf("explicit-value load: %v", err)
|
||||
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)
|
||||
}
|
||||
config := result.Config
|
||||
if config.SchemaDir != "" || config.Server.Addr != "" || config.Server.MaxRequestBytes != 0 || config.Server.MaxArtifactBytes != 0 || config.Server.MaxResponseBytes != 0 {
|
||||
t.Fatalf("explicit empty and zero values did not replace defaults: %+v", config)
|
||||
}
|
||||
if config.TransientRetries == nil || *config.TransientRetries != 0 {
|
||||
t.Fatalf("explicit zero retry budget lost: %+v", config.TransientRetries)
|
||||
}
|
||||
if want := []SourceSelection{{File: "framework.yml"}, {Directory: "profiles"}}; !reflect.DeepEqual(config.PromptKitSources, want) {
|
||||
t.Fatalf("source selectors did not decode: %+v", config.PromptKitSources)
|
||||
}
|
||||
if config.Defaults.RenderFormat != "json" {
|
||||
t.Fatalf("render format normalization failed: %q", config.Defaults.RenderFormat)
|
||||
}
|
||||
|
||||
nullable, err := loadDocumentBytes("transient_retries: null\npromptkit_sources: null\n")
|
||||
if err != nil {
|
||||
t.Fatalf("nullable-value load: %v", err)
|
||||
}
|
||||
if nullable.Config.TransientRetries != nil || nullable.Config.PromptKitSources != nil {
|
||||
t.Fatalf("nullable fields did not remain nil: %+v", nullable.Config)
|
||||
result, err = loadDocumentBytes("transient_retries: null\n")
|
||||
if err != nil || result.Config.TransientRetries != nil {
|
||||
t.Fatalf("nullable retries: %+v %v", result, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentSourceSelectionsDoNotAcquirePaths(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
content string
|
||||
want []SourceSelection
|
||||
}{
|
||||
{name: "omitted", content: "{}\n"},
|
||||
{name: "null", content: "promptkit_sources: null\n"},
|
||||
{name: "empty", content: "promptkit_sources: []\n"},
|
||||
{name: "nonexistent file", content: "promptkit_sources:\n - file: ./missing/framework.yml\n", want: []SourceSelection{{File: "missing/framework.yml"}}},
|
||||
{name: "nonexistent directory", content: "promptkit_sources:\n - directory: ./missing/framework\n", want: []SourceSelection{{Directory: "missing/framework"}}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
result, err := loadDocumentBytes(tc.content)
|
||||
if err != nil {
|
||||
t.Fatalf("load source selectors: %v", err)
|
||||
}
|
||||
if len(result.Config.PromptKitSources) != len(tc.want) || len(tc.want) > 0 && !reflect.DeepEqual(result.Config.PromptKitSources, tc.want) {
|
||||
t.Fatalf("source selectors changed: got=%+v want=%+v", result.Config.PromptKitSources, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSettingsCopiesPromptKitSourcesInOrder(t *testing.T) {
|
||||
document := Document{PromptKitSources: []SourceSelection{
|
||||
{File: "./first.yml"},
|
||||
{Directory: "./second"},
|
||||
}}
|
||||
settings := Settings(document)
|
||||
if !reflect.DeepEqual(settings.PromptKitSources, document.PromptKitSources) {
|
||||
t.Fatalf("source ordering changed: got=%+v want=%+v", settings.PromptKitSources, document.PromptKitSources)
|
||||
}
|
||||
document.PromptKitSources[0].File = "changed.yml"
|
||||
if settings.PromptKitSources[0].File != "./first.yml" {
|
||||
t.Fatalf("settings retained the document's source slice: %+v", settings.PromptKitSources)
|
||||
}
|
||||
if empty := Settings(Document{PromptKitSources: []SourceSelection{}}).PromptKitSources; empty == nil {
|
||||
t.Fatal("explicit empty source list was not copied")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentRecordsEffectiveNormalization(t *testing.T) {
|
||||
result, err := loadDocumentBytes(`promptkit_sources:
|
||||
- file: " ./one/../two "
|
||||
- file: "./blank.yml"
|
||||
directory: " "
|
||||
- directory: "./three/../three"
|
||||
server:
|
||||
artifact_root: " ./data/../artifacts "
|
||||
addr: " :9000 "
|
||||
defaults:
|
||||
render_format: " JSON "
|
||||
`)
|
||||
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.Fatalf("load normalized document: %v", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := []SourceSelection{{File: "two"}, {File: "blank.yml"}, {Directory: "three"}}; !reflect.DeepEqual(result.Config.PromptKitSources, want) {
|
||||
t.Fatalf("unexpected normalized source selections: %+v", result.Config.PromptKitSources)
|
||||
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)
|
||||
}
|
||||
if result.Config.Server.ArtifactRoot != "artifacts" || result.Config.Server.Addr != ":9000" {
|
||||
t.Fatalf("unexpected normalized values: %+v", result.Config)
|
||||
}
|
||||
var effective bytes.Buffer
|
||||
if err := result.WriteEffective(&effective); err != nil {
|
||||
t.Fatalf("write effective configuration: %v", err)
|
||||
}
|
||||
for _, fragment := range []string{"file: two", "directory: ''", "directory: three", "artifact_root: artifacts", "addr: :9000", "render_format: json"} {
|
||||
if !strings.Contains(effective.String(), fragment) {
|
||||
t.Fatalf("effective output omitted %q: %s", fragment, effective.String())
|
||||
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)
|
||||
}
|
||||
}
|
||||
explanation, known := result.Explain(configkit.Keys("promptkit_sources").Index(0).Key("file"))
|
||||
if !known || explanation.Effective.YAML != "two\n" {
|
||||
t.Fatalf("explanation does not agree with effective value: %+v known=%t", explanation, known)
|
||||
var output bytes.Buffer
|
||||
if err := result.WriteEffective(&output); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found := false
|
||||
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 {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("normalization was not recorded: %+v", explanation.History)
|
||||
}
|
||||
|
||||
unchanged, err := loadDocumentBytes("promptkit_sources:\n - file: framework.yml\n")
|
||||
if err != nil {
|
||||
t.Fatalf("load unchanged source path: %v", err)
|
||||
}
|
||||
unchangedExplanation, known := unchanged.Explain(configkit.Keys("promptkit_sources").Index(0).Key("file"))
|
||||
if !known {
|
||||
t.Fatal("unchanged source path was not inspectable")
|
||||
}
|
||||
for _, change := range unchangedExplanation.History {
|
||||
if change.Origin.Operation == configkit.ChangeNormalization {
|
||||
t.Fatalf("unchanged source path recorded a normalization: %+v", unchangedExplanation.History)
|
||||
t.Fatal("unchanged path was normalized")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentValidationIsSafeAndPathSpecific(t *testing.T) {
|
||||
for _, tc := range []struct{ name, content, path string }{
|
||||
{"empty source selector", "promptkit_sources:\n - file: ''\n", "/promptkit_sources/0"},
|
||||
{"multiple source selectors", "promptkit_sources:\n - file: secret-path\n directory: private-path\n", "/promptkit_sources/0"},
|
||||
{"negative retry", "transient_retries: -9173\n", "/transient_retries"},
|
||||
{"negative request limit", "server:\n max_request_bytes: -9173\n", "/server/max_request_bytes"},
|
||||
{"negative artifact limit", "server:\n max_artifact_bytes: -9173\n", "/server/max_artifact_bytes"},
|
||||
{"negative response limit", "server:\n max_response_bytes: -9173\n", "/server/max_response_bytes"},
|
||||
{"empty format", "defaults:\n render_format: \"\"\n", "/defaults/render_format"},
|
||||
{"unknown format", "defaults:\n render_format: private-invalid-value\n", "/defaults/render_format"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, err := loadDocumentBytes(tc.content)
|
||||
var loadError *configkit.LoadError
|
||||
if !errors.As(err, &loadError) {
|
||||
t.Fatalf("expected typed validation error, got %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, diagnostic := range loadError.Diagnostics() {
|
||||
if diagnostic.Code == configkit.CodeValidateError && diagnostic.Path.String() == tc.path {
|
||||
found = true
|
||||
}
|
||||
if strings.Contains(diagnostic.Message, "9173") || strings.Contains(diagnostic.Message, "private-invalid-value") || strings.Contains(diagnostic.Message, "secret-path") || strings.Contains(diagnostic.Message, "private-path") {
|
||||
t.Fatalf("diagnostic disclosed rejected value: %+v", diagnostic)
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("expected validation at %s, got %+v", tc.path, loadError.Diagnostics())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentRejectsNativeShapeErrors(t *testing.T) {
|
||||
for _, content := range []string{
|
||||
"server: null\n",
|
||||
"defaults: null\n",
|
||||
"server:\n addr: null\n",
|
||||
"server:\n max_request_bytes: null\n",
|
||||
"defaults:\n render_format: null\n",
|
||||
"server:\n unknown_field: 1\n",
|
||||
"promptkit_sources:\n - file: ./selected.yml\n unknown_field: 1\n",
|
||||
"promptkit_sources:\n - file: ./selected.yml\n directory: ./also-selected\n",
|
||||
"promptkit_sources:\n - file: null\n",
|
||||
"promptkit_sources: ./not-a-sequence.yml\n",
|
||||
"prompt_dir: ./old-prompts\n",
|
||||
"profile_dir: ./old-profiles\n",
|
||||
"backends:\n local:\n endpoint: http://example.invalid/v1\n",
|
||||
} {
|
||||
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)
|
||||
if err == nil {
|
||||
t.Fatalf("unexpected acceptance of %q", 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user