Replace legacy framework config with source selectors
This commit is contained in:
@@ -29,19 +29,19 @@ func TestLoadSelectsOrderedSourceAndKeepsItsSnapshot(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
first := filepath.Join(dir, "first.yml")
|
||||
second := filepath.Join(dir, "second.yml")
|
||||
writeApplicationFile(t, first, "prompt_dir: ./first\n")
|
||||
writeApplicationFile(t, second, "prompt_dir: ./second\n")
|
||||
writeApplicationFile(t, first, "promptkit_sources:\n - file: ./first.yml\n")
|
||||
writeApplicationFile(t, second, "promptkit_sources:\n - directory: ./second\n")
|
||||
useConfigSearchPaths(t, first, second)
|
||||
|
||||
result, err := Load(LoadOptions{Path: filepath.Join(dir, "ignored.yml")})
|
||||
if err != nil {
|
||||
t.Fatalf("load first implicit source: %v", err)
|
||||
}
|
||||
if result.Config.PromptDir != "first" || len(result.Sources()) != 1 || result.Sources()[0].Path != first {
|
||||
if !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "first.yml"}}) || len(result.Sources()) != 1 || result.Sources()[0].Path != first {
|
||||
t.Fatalf("unexpected implicit source: config=%+v sources=%+v", result.Config, result.Sources())
|
||||
}
|
||||
writeApplicationFile(t, first, "prompt_dir: ./changed\n")
|
||||
if result.Config.PromptDir != "first" {
|
||||
writeApplicationFile(t, first, "promptkit_sources:\n - file: ./changed.yml\n")
|
||||
if !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "first.yml"}}) {
|
||||
t.Fatalf("result did not retain resolved snapshot: %+v", result.Config)
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func TestLoadSelectsOrderedSourceAndKeepsItsSnapshot(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err = Load(LoadOptions{})
|
||||
if err != nil || result.Config.PromptDir != "second" || result.Sources()[0].Path != second {
|
||||
if err != nil || !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{Directory: "second"}}) || result.Sources()[0].Path != second {
|
||||
t.Fatalf("second implicit source not selected: result=%+v err=%v", result, err)
|
||||
}
|
||||
|
||||
@@ -124,8 +124,11 @@ func TestLoadDefaultsOnlyAndOverridesBeforeValidation(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSettingsCopiesResolvedValuesAndSortsBackends(t *testing.T) {
|
||||
func TestSettingsCopiesResolvedValuesAndSourceOrder(t *testing.T) {
|
||||
result, err := loadDocumentBytes(`schema_dir: ""
|
||||
promptkit_sources:
|
||||
- directory: ./profiles
|
||||
- file: ./framework.yml
|
||||
transient_retries: 0
|
||||
server:
|
||||
addr: ""
|
||||
@@ -133,13 +136,6 @@ server:
|
||||
max_request_bytes: 0
|
||||
defaults:
|
||||
render_format: json
|
||||
backends:
|
||||
zed:
|
||||
endpoint: http://zed.invalid/v1
|
||||
extra_params:
|
||||
count: 4
|
||||
alpha:
|
||||
queue_capacity: 0
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -151,17 +147,11 @@ backends:
|
||||
if settings.TransientRetries == nil || *settings.TransientRetries != 0 {
|
||||
t.Fatalf("retry presence lost: %+v", settings.TransientRetries)
|
||||
}
|
||||
if len(settings.Backends) != 2 || settings.Backends[0].ID != "alpha" || settings.Backends[1].ID != "zed" {
|
||||
t.Fatalf("backend order changed: %+v", settings.Backends)
|
||||
}
|
||||
if settings.Backends[0].QueueCapacity == nil || *settings.Backends[0].QueueCapacity != 0 || settings.Backends[1].QueueCapacity != nil {
|
||||
t.Fatalf("queue presence changed: %+v", settings.Backends)
|
||||
}
|
||||
if value := settings.Backends[1].ExtraParams["count"]; value != int64(4) {
|
||||
t.Fatalf("dynamic number changed shape: %T %v", value, value)
|
||||
if want := []SourceSelection{{Directory: "profiles"}, {File: "framework.yml"}}; !reflect.DeepEqual(settings.PromptKitSources, want) {
|
||||
t.Fatalf("source ordering changed: got=%+v want=%+v", settings.PromptKitSources, want)
|
||||
}
|
||||
|
||||
if got := Settings(documentDefaults()); got.TransientRetries != nil || got.Backends != nil {
|
||||
if got := Settings(documentDefaults()); got.TransientRetries != nil || got.PromptKitSources != nil {
|
||||
t.Fatalf("default nullable values changed: %+v", got)
|
||||
}
|
||||
}
|
||||
@@ -188,25 +178,29 @@ func TestLoadComposesExplicitImportsAndSelectedOverlay(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
base := filepath.Join(dir, "config.yml")
|
||||
writeApplicationFile(t, base, "configkit:\n imports:\n - glob: fragments/*.yml\nprompt_dir: ./base-prompts\nserver:\n addr: :8081\n")
|
||||
writeApplicationFile(t, filepath.Join(dir, "fragments", "a.yml"), "backends:\n alpha:\n endpoint: http://alpha.invalid/v1\n")
|
||||
writeApplicationFile(t, filepath.Join(dir, "fragments", "b.yml"), "backends:\n beta:\n endpoint: http://beta.invalid/v1\n")
|
||||
writeApplicationFile(t, filepath.Join(dir, "overlays", "local.yml"), "prompt_dir: ./overlay-prompts\nserver:\n addr: :9090\n")
|
||||
writeApplicationFile(t, base, "configkit:\n imports:\n - glob: fragments/*.yml\npromptkit_sources:\n - file: ./base-framework.yml\n")
|
||||
writeApplicationFile(t, filepath.Join(dir, "fragments", "a.yml"), "server:\n addr: :8081\n")
|
||||
writeApplicationFile(t, filepath.Join(dir, "fragments", "b.yml"), "defaults:\n render_format: json\n")
|
||||
writeApplicationFile(t, filepath.Join(dir, "overlays", "local.yml"), "promptkit_sources:\n - directory: ./overlay-framework\nserver:\n addr: :9090\n")
|
||||
|
||||
result, err := Load(LoadOptions{Path: base, Explicit: true})
|
||||
if err != nil || result.Config.PromptDir != "base-prompts" || len(result.Config.Backends) != 2 || len(result.Sources()) != 3 {
|
||||
if err != nil || !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "base-framework.yml"}}) || result.Config.Server.Addr != ":8081" || result.Config.Defaults.RenderFormat != "json" || len(result.Sources()) != 3 {
|
||||
t.Fatalf("disjoint imports did not compose: result=%+v err=%v", result, err)
|
||||
}
|
||||
result, err = Load(LoadOptions{Path: base, Explicit: true, Overlay: "local", Overrides: []configkit.Override{{
|
||||
Path: configkit.Keys("server", "addr"), Value: ":7070", Flag: "--addr",
|
||||
Path: configkit.Keys("promptkit_sources"), Value: []any{map[string]any{"file": "./cli-framework.yml"}}, Flag: "--promptkit-source",
|
||||
}}})
|
||||
if err != nil || result.Config.PromptDir != "overlay-prompts" || result.Config.Server.Addr != ":7070" || len(result.Sources()) != 5 {
|
||||
if err != nil || !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "cli-framework.yml"}}) || result.Config.Server.Addr != ":9090" || len(result.Sources()) != 5 {
|
||||
t.Fatalf("overlay or explicit override changed: result=%+v err=%v", result, err)
|
||||
}
|
||||
result, err = Load(LoadOptions{Path: base, Explicit: true, Overlay: "local"})
|
||||
if err != nil || !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{Directory: "overlay-framework"}}) {
|
||||
t.Fatalf("overlay did not replace the source list: result=%+v err=%v", result, err)
|
||||
}
|
||||
if _, err := Load(LoadOptions{Path: base, Explicit: true, Overlay: "missing"}); err == nil {
|
||||
t.Fatal("missing selected overlay accepted")
|
||||
}
|
||||
writeApplicationFile(t, filepath.Join(dir, "fragments", "b.yml"), "backends:\n alpha:\n endpoint: http://conflict.invalid/v1\n")
|
||||
writeApplicationFile(t, filepath.Join(dir, "fragments", "b.yml"), "server:\n addr: :8082\n")
|
||||
if _, err := Load(LoadOptions{Path: base, Explicit: true}); err == nil {
|
||||
t.Fatal("conflicting import accepted")
|
||||
} else {
|
||||
@@ -217,89 +211,6 @@ func TestLoadComposesExplicitImportsAndSelectedOverlay(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadComposesInheritanceAfterImportsOverlayAndOverrides(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(dir, "fragments"), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Join(dir, "overlays"), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
base := filepath.Join(dir, "config.yml")
|
||||
writeApplicationFile(t, base, "configkit:\n imports:\n - path: fragments/base.yml\nbackends:\n child:\n extends: base\n")
|
||||
writeApplicationFile(t, filepath.Join(dir, "fragments", "base.yml"), `prompt_dir: ./fragment-prompts
|
||||
backends:
|
||||
base:
|
||||
endpoint: http://base.invalid/v1
|
||||
api_key_env: BASE_API_KEY
|
||||
concurrency_limit: 2
|
||||
queue_capacity: 4
|
||||
`)
|
||||
writeApplicationFile(t, filepath.Join(dir, "overlays", "local.yml"), `server:
|
||||
addr: :9090
|
||||
backends:
|
||||
base:
|
||||
endpoint: http://overlay.invalid/v1
|
||||
child:
|
||||
queue_capacity: 0
|
||||
`)
|
||||
writeApplicationFile(t, filepath.Join(dir, "overlays", "cancel.yml"), `backends:
|
||||
child:
|
||||
extends: null
|
||||
endpoint: http://child.invalid/v1
|
||||
`)
|
||||
|
||||
addrOverride := []configkit.Override{{
|
||||
Path: configkit.Keys("server", "addr"), Value: ":7070", Flag: "--addr",
|
||||
}}
|
||||
result, err := Load(LoadOptions{Path: base, Explicit: true, Overlay: "local", Overrides: addrOverride})
|
||||
if err != nil {
|
||||
t.Fatalf("load imported and overlaid inheritance: %v", err)
|
||||
}
|
||||
child := result.Config.Backends["child"]
|
||||
if result.Config.PromptDir != "fragment-prompts" || result.Config.Server.Addr != ":7070" {
|
||||
t.Fatalf("path or CLI override changed during assembly: %+v", result.Config)
|
||||
}
|
||||
if child.Endpoint != "http://overlay.invalid/v1" || child.APIKeyEnv != "BASE_API_KEY" || child.ConcurrencyLimit != 2 {
|
||||
t.Fatalf("child did not inherit the assembled parent: %+v", child)
|
||||
}
|
||||
if child.QueueCapacity == nil || *child.QueueCapacity != 0 {
|
||||
t.Fatalf("overlay zero did not replace the inherited capacity: %+v", child.QueueCapacity)
|
||||
}
|
||||
|
||||
result, err = Load(LoadOptions{Path: base, Explicit: true, Overlay: "cancel", Overrides: addrOverride})
|
||||
if err != nil {
|
||||
t.Fatalf("load cancelled inheritance: %v", err)
|
||||
}
|
||||
child = result.Config.Backends["child"]
|
||||
if child.Endpoint != "http://child.invalid/v1" || child.APIKeyEnv != "" || child.ConcurrencyLimit != 0 || child.QueueCapacity != nil {
|
||||
t.Fatalf("cancelled child retained parent settings: %+v", child)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadRejectsReservedInheritanceKeyInExtraParams(t *testing.T) {
|
||||
result, err := loadDocumentBytes(`backends:
|
||||
worker:
|
||||
endpoint: http://worker.invalid/v1
|
||||
extra_params:
|
||||
extends: missing
|
||||
`)
|
||||
if err == nil || result != nil {
|
||||
t.Fatalf("missing sibling in reserved key accepted: result=%+v err=%v", result, err)
|
||||
}
|
||||
var loadError *configkit.LoadError
|
||||
if !errors.As(err, &loadError) {
|
||||
t.Fatalf("structured inheritance error was lost: %v", err)
|
||||
}
|
||||
wantPath := configkit.Keys("backends", "worker", "extra_params", "extends").String()
|
||||
for _, diagnostic := range loadError.Diagnostics() {
|
||||
if diagnostic.Code == configkit.CodeInheritanceMissing && diagnostic.Path.String() == wantPath {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatalf("missing inheritance diagnostic at %s: %+v", wantPath, loadError.Diagnostics())
|
||||
}
|
||||
|
||||
func TestLoadRejectsOverlayWithoutBase(t *testing.T) {
|
||||
useConfigSearchPaths(t, filepath.Join(t.TempDir(), "absent.yml"))
|
||||
if _, err := Load(LoadOptions{Overlay: "local"}); err == nil {
|
||||
|
||||
Reference in New Issue
Block a user