Replace legacy framework config with source selectors
This commit is contained in:
@@ -25,8 +25,9 @@ func TestDocumentDefaultsAndExplicitValues(t *testing.T) {
|
||||
t.Fatalf("unexpected defaults-only result: %+v sources=%+v", defaultsOnly.Config, defaultsOnly.Sources())
|
||||
}
|
||||
|
||||
result, err := loadDocumentBytes(`prompt_dir: ""
|
||||
profile_dir: ""
|
||||
result, err := loadDocumentBytes(`promptkit_sources:
|
||||
- file: " ./bundle/../framework.yml "
|
||||
- directory: "./profiles"
|
||||
schema_dir: ""
|
||||
transient_retries: 0
|
||||
server:
|
||||
@@ -37,12 +38,6 @@ server:
|
||||
max_response_bytes: 0
|
||||
defaults:
|
||||
render_format: " JSON "
|
||||
backends:
|
||||
local:
|
||||
endpoint: "http://fixture.invalid/v1"
|
||||
queue_capacity: 0
|
||||
extra_params:
|
||||
sample_count: 23
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatalf("explicit-value load: %v", err)
|
||||
@@ -54,114 +49,70 @@ backends:
|
||||
if config.TransientRetries == nil || *config.TransientRetries != 0 {
|
||||
t.Fatalf("explicit zero retry budget lost: %+v", config.TransientRetries)
|
||||
}
|
||||
if backend := config.Backends["local"]; backend.QueueCapacity == nil || *backend.QueueCapacity != 0 || backend.ExtraParams["sample_count"] != int64(23) {
|
||||
t.Fatalf("backend presence or dynamic integer changed: %+v", backend)
|
||||
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\nbackends:\n local:\n queue_capacity: null\n")
|
||||
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.Backends["local"].QueueCapacity != nil {
|
||||
if nullable.Config.TransientRetries != nil || nullable.Config.PromptKitSources != nil {
|
||||
t.Fatalf("nullable fields did not remain nil: %+v", nullable.Config)
|
||||
}
|
||||
|
||||
nullBackends, err := loadDocumentBytes("backends: null\n")
|
||||
if err != nil || nullBackends.Config.Backends != nil {
|
||||
t.Fatalf("nullable backend map did not remain nil: %+v error=%v", nullBackends, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentResolvesBackendInheritance(t *testing.T) {
|
||||
result, err := loadDocumentBytes(`backends:
|
||||
base:
|
||||
endpoint: http://base.invalid/v1
|
||||
api_key_env: BASE_API_KEY
|
||||
concurrency_limit: 2
|
||||
queue_capacity: 4
|
||||
extra_params:
|
||||
enabled: true
|
||||
shared: visible
|
||||
count: 7
|
||||
child:
|
||||
extends: base
|
||||
queue_capacity: 0
|
||||
extra_params:
|
||||
enabled: false
|
||||
nullable-child:
|
||||
extends: base
|
||||
queue_capacity: null
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatalf("load inherited document: %v", 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
child := result.Config.Backends["child"]
|
||||
if child.Endpoint != "http://base.invalid/v1" || child.APIKeyEnv != "BASE_API_KEY" || child.ConcurrencyLimit != 2 {
|
||||
t.Fatalf("child did not inherit backend settings: %+v", child)
|
||||
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)
|
||||
}
|
||||
if child.QueueCapacity == nil || *child.QueueCapacity != 0 {
|
||||
t.Fatalf("explicit zero queue capacity was not retained: %+v", child.QueueCapacity)
|
||||
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 child.ExtraParams["enabled"] != false || child.ExtraParams["shared"] != "visible" || child.ExtraParams["count"] != int64(7) {
|
||||
t.Fatalf("child parameters were not combined with explicit presence: %#v", child.ExtraParams)
|
||||
}
|
||||
nullableChild := result.Config.Backends["nullable-child"]
|
||||
if nullableChild.QueueCapacity != nil {
|
||||
t.Fatalf("explicit null inherited the parent's queue capacity: %+v", nullableChild.QueueCapacity)
|
||||
}
|
||||
|
||||
metadata := result.Metadata()
|
||||
endpointPath := configkit.Keys("backends", "child", "endpoint")
|
||||
field, known := metadata.Lookup(endpointPath)
|
||||
if !known || !field.Explicit || !metadata.IsExplicit(endpointPath) {
|
||||
t.Fatalf("inherited endpoint presence was lost: field=%+v known=%t", field, known)
|
||||
}
|
||||
wantFrom := configkit.Keys("backends", "base", "endpoint").String()
|
||||
wantTo := endpointPath.String()
|
||||
wantReference := configkit.Keys("backends", "child", "extends").String()
|
||||
foundInheritance := false
|
||||
for _, step := range field.Origin.Inheritance {
|
||||
if step.From.String() == wantFrom && step.To.String() == wantTo && step.Reference.String() == wantReference {
|
||||
foundInheritance = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !foundInheritance {
|
||||
t.Fatalf("inherited endpoint provenance missing: %+v", field.Origin.Inheritance)
|
||||
}
|
||||
|
||||
settings := Settings(result.Config)
|
||||
wantIDs := []string{"base", "child", "nullable-child"}
|
||||
if len(settings.Backends) != len(wantIDs) {
|
||||
t.Fatalf("backend definitions were dropped: %+v", settings.Backends)
|
||||
}
|
||||
for i, want := range wantIDs {
|
||||
if settings.Backends[i].ID != want {
|
||||
t.Fatalf("backend IDs are not sorted: %+v", settings.Backends)
|
||||
}
|
||||
}
|
||||
convertedChild := settings.Backends[1]
|
||||
if convertedChild.Endpoint != child.Endpoint || convertedChild.APIKeyEnv != child.APIKeyEnv || convertedChild.ConcurrencyLimit != 2 {
|
||||
t.Fatalf("inherited settings changed during conversion: %+v", convertedChild)
|
||||
}
|
||||
if convertedChild.QueueCapacity == nil || *convertedChild.QueueCapacity != 0 {
|
||||
t.Fatalf("zero queue capacity presence changed during conversion: %+v", convertedChild.QueueCapacity)
|
||||
}
|
||||
if value := convertedChild.ExtraParams["count"]; value != int64(7) {
|
||||
t.Fatalf("dynamic integer changed during conversion: %T %v", value, value)
|
||||
}
|
||||
if settings.Backends[2].QueueCapacity != nil {
|
||||
t.Fatalf("nullable queue capacity changed during conversion: %+v", settings.Backends[2].QueueCapacity)
|
||||
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(`prompt_dir: " ./one/../two "
|
||||
profile_dir: " "
|
||||
result, err := loadDocumentBytes(`promptkit_sources:
|
||||
- file: " ./one/../two "
|
||||
- file: "./blank.yml"
|
||||
directory: " "
|
||||
- directory: "./three/../three"
|
||||
server:
|
||||
artifact_root: " ./data/../artifacts "
|
||||
addr: " :9000 "
|
||||
@@ -171,19 +122,22 @@ defaults:
|
||||
if err != nil {
|
||||
t.Fatalf("load normalized document: %v", err)
|
||||
}
|
||||
if result.Config.PromptDir != "two" || result.Config.ProfileDir != "" || result.Config.Server.ArtifactRoot != "artifacts" || result.Config.Server.Addr != ":9000" {
|
||||
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.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{"prompt_dir: two", "profile_dir: ''", "artifact_root: artifacts", "addr: :9000", "render_format: json"} {
|
||||
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())
|
||||
}
|
||||
}
|
||||
explanation, known := result.Explain(configkit.Keys("prompt_dir"))
|
||||
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)
|
||||
}
|
||||
@@ -196,10 +150,26 @@ defaults:
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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"},
|
||||
@@ -218,7 +188,7 @@ func TestDocumentValidationIsSafeAndPathSpecific(t *testing.T) {
|
||||
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") {
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -237,7 +207,13 @@ func TestDocumentRejectsNativeShapeErrors(t *testing.T) {
|
||||
"server:\n max_request_bytes: null\n",
|
||||
"defaults:\n render_format: null\n",
|
||||
"server:\n unknown_field: 1\n",
|
||||
"backends:\n local:\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",
|
||||
} {
|
||||
_, err := loadDocumentBytes(content)
|
||||
if err == nil {
|
||||
|
||||
Reference in New Issue
Block a user