Load shared PromptKit settings with native secrets bootstrap

This commit is contained in:
2026-11-23 03:25:05 +00:00
parent 90a3790f44
commit 69ef533d7e
5 changed files with 170 additions and 73 deletions

View File

@@ -4,7 +4,6 @@ import (
"errors"
"os"
"path/filepath"
"reflect"
"testing"
"go.maximumdirect.net/configkit"
@@ -48,11 +47,11 @@ func TestLoadNativeDiscoveryOrderAndFilenames(t *testing.T) {
writeApplicationFile(t, path, "server:\n addr: '"+string(rune('a'+index))+"'\n")
}
for index, path := range paths {
result, err := Load(LoadOptions{Path: "ignored.yml"})
result, _, err := Load(LoadOptions{Path: "ignored.yml"})
if err != nil {
t.Fatal(err)
}
if result.Config.Server.Addr != string(rune('a'+index)) || len(result.Sources()) != 1 || result.Sources()[0].Path != path {
if result.Config.Server.Addr != string(rune('a'+index)) || len(result.Sources()) < 2 || result.Sources()[0].Path != path {
t.Fatalf("selected wrong source: %+v %+v", result.Config, result.Sources())
}
if err := os.Remove(path); err != nil {
@@ -62,8 +61,8 @@ func TestLoadNativeDiscoveryOrderAndFilenames(t *testing.T) {
// Do not let a host's system configuration influence exhaustion checks.
// The system preset's filename expansion remains upstream-owned.
useIsolatedSearch(t)
result, err := Load(LoadOptions{})
if err != nil || len(result.Sources()) != 0 || !reflect.DeepEqual(result.Config, documentDefaults()) {
result, _, err := Load(LoadOptions{})
if err != nil || len(result.Sources()) == 0 || result.Config.Server.Addr != documentDefaults().Server.Addr {
t.Fatalf("exhaustion: %+v %v", result, err)
}
}
@@ -72,7 +71,7 @@ func TestLoadDoesNotFallThroughSelectedInvalidSource(t *testing.T) {
root := isolatedDiscovery(t)
writeApplicationFile(t, filepath.Join(root, "scriptorium.yml"), "server: [\n")
writeApplicationFile(t, filepath.Join(root, "scriptorium.yaml"), "{}\n")
if _, err := Load(LoadOptions{}); err == nil {
if _, _, err := Load(LoadOptions{}); err == nil {
t.Fatal("invalid first candidate fell through")
}
}
@@ -83,11 +82,11 @@ func TestLoadExplicitSelectionAndOverlayPresence(t *testing.T) {
for _, content := range []string{"", "server: [\n"} {
path := filepath.Join(root, "selected.yml")
writeApplicationFile(t, path, content)
if _, err := Load(LoadOptions{Explicit: true, Path: path}); err == nil {
if _, _, err := Load(LoadOptions{Explicit: true, Path: path}); err == nil {
t.Fatal("accepted invalid explicit file")
}
}
_, err := Load(LoadOptions{Explicit: true})
_, _, err := Load(LoadOptions{Explicit: true})
var native *configkit.LoadError
if !errors.As(err, &native) {
t.Fatalf("empty path lost typed error: %v", err)
@@ -95,15 +94,15 @@ func TestLoadExplicitSelectionAndOverlayPresence(t *testing.T) {
base := filepath.Join(root, "selected.yml")
writeApplicationFile(t, base, "{}\n")
for _, overlay := range []string{"", "default"} {
result, err := Load(LoadOptions{Explicit: true, Path: base, Overlay: overlay})
if err != nil || !reflect.DeepEqual(result.Config, documentDefaults()) {
result, _, err := Load(LoadOptions{Explicit: true, Path: base, Overlay: overlay})
if err != nil || result.Config.Server.Addr != documentDefaults().Server.Addr {
t.Fatalf("no overlay: %+v %v", result, err)
}
}
if _, err := Load(LoadOptions{Explicit: true, Path: base, Overlay: "missing"}); err == nil {
if _, _, err := Load(LoadOptions{Explicit: true, Path: base, Overlay: "missing"}); err == nil {
t.Fatal("missing overlay accepted")
}
if _, err := Load(LoadOptions{Overlay: "local"}); err == nil {
if _, _, err := Load(LoadOptions{Overlay: "local"}); err == nil {
t.Fatal("overlay without main context accepted")
}
}
@@ -111,19 +110,19 @@ func TestLoadExplicitSelectionAndOverlayPresence(t *testing.T) {
func TestLoadImportsOverlayAndNativeOverrides(t *testing.T) {
root := t.TempDir()
main := filepath.Join(root, "config.yml")
writeApplicationFile(t, main, "config:\n imports:\n - glob: fragments/*.yml\n required: true\npromptkit:\n configuration: base.yml\n")
writeApplicationFile(t, main, "config:\n imports:\n - glob: fragments/*.yml\n required: true\n")
writeApplicationFile(t, filepath.Join(root, "fragments", "a.yml"), "server:\n addr: :8081\n")
writeApplicationFile(t, filepath.Join(root, "fragments", "b.yml"), "promptkit:\n schema_dir: ./schemas\n")
writeApplicationFile(t, filepath.Join(root, "overlays", "local.yml"), "promptkit:\n configuration: overlay.yml\nserver:\n addr: :9090\n")
result, err := Load(LoadOptions{Explicit: true, Path: main, Overlay: "local", Overrides: []configkit.Override{{Path: configkit.Keys("promptkit", "configuration"), Value: "native.yml", Flag: "--set"}}})
writeApplicationFile(t, filepath.Join(root, "fragments", "b.yml"), "promptkit:\n custom_field: present\n")
writeApplicationFile(t, filepath.Join(root, "overlays", "local.yml"), "server:\n addr: :9090\n")
result, _, err := Load(LoadOptions{Explicit: true, Path: main, Overlay: "local", Overrides: []configkit.Override{{Path: configkit.Keys("server", "max_request_bytes"), Value: int64(0), Flag: "--set"}}})
if err != nil {
t.Fatal(err)
}
if result.Config.PromptKit.Configuration != "native.yml" || result.Config.PromptKit.SchemaDir != "schemas" || result.Config.Server.Addr != ":9090" || len(result.Sources()) != 5 {
if result.Config.PromptKit.Application["custom_field"] != "present" || result.Config.Server.MaxRequestBytes != 0 || result.Config.Server.Addr != ":9090" {
t.Fatalf("composition: %+v %+v", result.Config, result.Sources())
}
writeApplicationFile(t, filepath.Join(root, "fragments", "b.yml"), "server:\n addr: :8082\n")
if _, err := Load(LoadOptions{Explicit: true, Path: main}); err == nil {
if _, _, err := Load(LoadOptions{Explicit: true, Path: main}); err == nil {
t.Fatal("peer conflict accepted")
}
}
@@ -131,17 +130,17 @@ func TestLoadImportsOverlayAndNativeOverrides(t *testing.T) {
func TestLoadNativeOverrideEvidenceAndConflicts(t *testing.T) {
root := isolatedDiscovery(t)
useIsolatedSearch(t)
result, err := Load(LoadOptions{Overrides: []configkit.Override{
result, _, err := Load(LoadOptions{Overrides: []configkit.Override{
{Path: configkit.Keys("server", "max_request_bytes"), Value: int64(0), Flag: "--set"},
{Path: configkit.Keys("promptkit", "configuration"), Value: " ./one/../framework.yml ", Flag: "--set"},
{Path: configkit.Keys("server", "artifact_root"), Value: " ./one/../artifacts ", Flag: "--set"},
}})
if err != nil {
t.Fatal(err)
}
if result.Config.Server.MaxRequestBytes != 0 || result.Config.PromptKit.Configuration != "framework.yml" {
if result.Config.Server.MaxRequestBytes != 0 || result.Config.Server.ArtifactRoot != "artifacts" {
t.Fatal(result.Config)
}
explanation, ok := result.Explain(configkit.Keys("promptkit", "configuration"))
explanation, ok := result.Explain(configkit.Keys("server", "artifact_root"))
if !ok || len(explanation.History) < 2 {
t.Fatalf("native evidence missing: %+v", explanation)
}
@@ -149,7 +148,7 @@ func TestLoadNativeOverrideEvidenceAndConflicts(t *testing.T) {
{{Path: configkit.Keys("server", "addr"), Value: "a", Flag: "--set"}, {Path: configkit.Keys("server", "addr"), Value: "b", Flag: "--set"}},
{{Path: configkit.Keys("server"), Value: map[string]any{"addr": "a"}, Flag: "--set"}, {Path: configkit.Keys("server", "addr"), Value: "b", Flag: "--set"}},
} {
_, err := Load(LoadOptions{Overrides: records})
_, _, err := Load(LoadOptions{Overrides: records})
var native *configkit.LoadError
if !errors.As(err, &native) {
t.Fatalf("native conflict lost: %v", err)
@@ -165,12 +164,12 @@ func TestLoadNativeOverrideEvidenceAndConflicts(t *testing.T) {
}
path := filepath.Join(root, "invalid.yml")
writeApplicationFile(t, path, "server:\n max_request_bytes: -1\n")
result, err = Load(LoadOptions{Explicit: true, Path: path, Overrides: []configkit.Override{{Path: configkit.Keys("server", "max_request_bytes"), Value: int64(0), Flag: "--set"}}})
result, _, err = Load(LoadOptions{Explicit: true, Path: path, Overrides: []configkit.Override{{Path: configkit.Keys("server", "max_request_bytes"), Value: int64(0), Flag: "--set"}}})
if err != nil || result.Config.Server.MaxRequestBytes != 0 {
t.Fatalf("override did not precede validation: %+v %v", result, err)
}
writeApplicationFile(t, path, "server:\n max_request_bytes: not-a-number\n")
if _, err := Load(LoadOptions{Explicit: true, Path: path, Overrides: []configkit.Override{{Path: configkit.Keys("server", "max_request_bytes"), Value: int64(0), Flag: "--set"}}}); err == nil {
if _, _, err := Load(LoadOptions{Explicit: true, Path: path, Overrides: []configkit.Override{{Path: configkit.Keys("server", "max_request_bytes"), Value: int64(0), Flag: "--set"}}}); err == nil {
t.Fatal("override repaired decode failure")
}
}
@@ -179,16 +178,16 @@ func TestLoadImportRequirednessAndDestination(t *testing.T) {
root := t.TempDir()
main := filepath.Join(root, "config.yml")
writeApplicationFile(t, main, "config:\n imports:\n - path: absent.yml\n")
if _, err := Load(LoadOptions{Explicit: true, Path: main}); err != nil {
if _, _, err := Load(LoadOptions{Explicit: true, Path: main}); err != nil {
t.Fatal(err)
}
writeApplicationFile(t, main, "config:\n imports:\n - path: absent.yml\n required: true\n")
if _, err := Load(LoadOptions{Explicit: true, Path: main}); err == nil {
if _, _, err := Load(LoadOptions{Explicit: true, Path: main}); err == nil {
t.Fatal("required import accepted")
}
writeApplicationFile(t, filepath.Join(root, "fragments", "server.yml"), "addr: ':8181'\n")
writeApplicationFile(t, main, "config:\n imports:\n - dir: fragments\n pattern: '*.yml'\n destination: [server]\n required: true\n")
result, err := Load(LoadOptions{Explicit: true, Path: main})
result, _, err := Load(LoadOptions{Explicit: true, Path: main})
if err != nil {
t.Fatal(err)
}
@@ -197,3 +196,38 @@ func TestLoadImportRequirednessAndDestination(t *testing.T) {
t.Fatalf("value/origin: %+v %+v", result.Config, info)
}
}
func TestLoadConfiguredSecrets(t *testing.T) {
root := isolatedDiscovery(t)
useIsolatedSearch(t)
const variable = "SCRIPTORIUM_TEST_BOOTSTRAP_VALUE"
t.Setenv(variable, "")
main := filepath.Join(root, "config.yml")
writeApplicationFile(t, filepath.Join(root, "secrets", variable), "synthetic")
writeApplicationFile(t, main, "config: {secrets_dir: secrets}\n")
_, report, err := Load(LoadOptions{Explicit: true, Path: main})
if err != nil || os.Getenv(variable) != "" || len(report.Entries()) != 1 || report.Entries()[0].Status != configkit.SecretFileSkippedExisting || !report.Entries()[0].ExistingEmpty {
t.Fatalf("preserve empty: report=%+v err=%v", report.Entries(), err)
}
writeApplicationFile(t, main, "config: {secrets_dir: secrets, secrets_overwrite: true}\nserver: {max_request_bytes: -1}\n")
_, report, err = Load(LoadOptions{Explicit: true, Path: main})
var loadErr *configkit.LoadError
if !errors.As(err, &loadErr) || os.Getenv(variable) != "synthetic" || len(report.Entries()) != 1 || report.Entries()[0].Status != configkit.SecretFileLoaded {
t.Fatalf("later failure: report=%+v err=%v", report.Entries(), err)
}
writeApplicationFile(t, main, "config: {secrets_dir: missing}\n")
_, report, err = Load(LoadOptions{Explicit: true, Path: main})
var secretsErr *configkit.SecretsError
if !errors.As(err, &secretsErr) || report == nil {
t.Fatalf("bootstrap error: report=%+v err=%v", report, err)
}
writeApplicationFile(t, main, "{}\n")
_, report, err = Load(LoadOptions{Explicit: true, Path: main})
if err != nil || report == nil || len(report.Entries()) != 0 {
t.Fatalf("no directory: report=%+v err=%v", report, err)
}
_, report, err = Load(LoadOptions{})
if err != nil || report == nil || len(report.Entries()) != 0 {
t.Fatalf("optional exhaustion: report=%+v err=%v", report, err)
}
}