Adopt native configuration discovery and a single PromptKit entry file
This commit is contained in:
@@ -5,23 +5,20 @@ import (
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/scriptorium/internal/defaults"
|
||||
renderformat "gitea.maximumdirect.net/eric/scriptorium/internal/format"
|
||||
"go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
// Document is Scriptorium's ConfigKit-native application configuration shape.
|
||||
type Document struct {
|
||||
PromptKitSources []SourceSelection `yaml:"promptkit_sources"`
|
||||
SchemaDir string `yaml:"schema_dir"`
|
||||
PromptKit PromptKitDocument `yaml:"promptkit"`
|
||||
TransientRetries *int `yaml:"transient_retries"`
|
||||
Server ServerDocument `yaml:"server"`
|
||||
Defaults DefaultsDocument `yaml:"defaults"`
|
||||
}
|
||||
|
||||
// SourceSelection identifies one PromptKit configuration file or directory.
|
||||
type SourceSelection struct {
|
||||
File string `yaml:"file"`
|
||||
Directory string `yaml:"directory"`
|
||||
// PromptKitDocument selects framework acquisition, not embedded definitions.
|
||||
type PromptKitDocument struct {
|
||||
Configuration string `yaml:"configuration"`
|
||||
SchemaDir string `yaml:"schema_dir"`
|
||||
}
|
||||
|
||||
type ServerDocument struct {
|
||||
@@ -32,10 +29,6 @@ type ServerDocument struct {
|
||||
MaxResponseBytes int64 `yaml:"max_response_bytes"`
|
||||
}
|
||||
|
||||
type DefaultsDocument struct {
|
||||
RenderFormat string `yaml:"render_format"`
|
||||
}
|
||||
|
||||
func documentDefinition() configkit.Definition[Document] {
|
||||
return configkit.Definition[Document]{
|
||||
Defaults: documentDefaults,
|
||||
@@ -46,14 +39,13 @@ func documentDefinition() configkit.Definition[Document] {
|
||||
|
||||
func documentDefaults() Document {
|
||||
return Document{
|
||||
SchemaDir: defaults.SchemaDirDefault,
|
||||
PromptKit: PromptKitDocument{SchemaDir: defaults.SchemaDirDefault},
|
||||
Server: ServerDocument{
|
||||
Addr: defaults.HTTPAddrDefault,
|
||||
MaxRequestBytes: defaults.HTTPMaxRequestBytesDefault,
|
||||
MaxArtifactBytes: defaults.HTTPMaxArtifactBytesDefault,
|
||||
MaxResponseBytes: defaults.HTTPMaxResponseBytesDefault,
|
||||
},
|
||||
Defaults: DefaultsDocument{RenderFormat: string(renderformat.DefaultPreparedRunOutputFormat)},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,28 +65,15 @@ func resolveDocument(document *Document, resolution *configkit.Resolution) error
|
||||
}
|
||||
}
|
||||
|
||||
for i := range document.PromptKitSources {
|
||||
path := configkit.Keys("promptkit_sources").Index(i)
|
||||
normalizePath(path.Key("file"), &document.PromptKitSources[i].File)
|
||||
normalizePath(path.Key("directory"), &document.PromptKitSources[i].Directory)
|
||||
}
|
||||
normalizePath(configkit.Keys("schema_dir"), &document.SchemaDir)
|
||||
normalizePath(configkit.Keys("promptkit", "configuration"), &document.PromptKit.Configuration)
|
||||
normalizePath(configkit.Keys("promptkit", "schema_dir"), &document.PromptKit.SchemaDir)
|
||||
normalizePath(configkit.Keys("server", "artifact_root"), &document.Server.ArtifactRoot)
|
||||
normalizeString(configkit.Keys("server", "addr"), &document.Server.Addr,
|
||||
strings.TrimSpace(document.Server.Addr), "trimmed server address")
|
||||
normalizeString(configkit.Keys("defaults", "render_format"), &document.Defaults.RenderFormat,
|
||||
strings.ToLower(strings.TrimSpace(document.Defaults.RenderFormat)), "normalized render format")
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateDocument(document *Document, validation *configkit.Validation) {
|
||||
for i, source := range document.PromptKitSources {
|
||||
hasFile := source.File != ""
|
||||
hasDirectory := source.Directory != ""
|
||||
if hasFile == hasDirectory {
|
||||
validation.Error(configkit.Keys("promptkit_sources").Index(i), "exactly one of file or directory must be non-empty")
|
||||
}
|
||||
}
|
||||
if document.TransientRetries != nil && *document.TransientRetries < 0 {
|
||||
validation.Error(configkit.Keys("transient_retries"), "must be greater than or equal to zero")
|
||||
}
|
||||
@@ -107,9 +86,4 @@ func validateDocument(document *Document, validation *configkit.Validation) {
|
||||
if document.Server.MaxResponseBytes < 0 {
|
||||
validation.Error(configkit.Keys("server", "max_response_bytes"), "must be greater than or equal to zero")
|
||||
}
|
||||
switch document.Defaults.RenderFormat {
|
||||
case string(renderformat.OutputFormatText), string(renderformat.OutputFormatJSON):
|
||||
default:
|
||||
validation.Error(configkit.Keys("defaults", "render_format"), "must be text or json")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
renderformat "gitea.maximumdirect.net/eric/scriptorium/internal/format"
|
||||
"go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
@@ -15,13 +14,18 @@ type LoadOptions struct {
|
||||
Overrides []configkit.Override
|
||||
}
|
||||
|
||||
var applicationSearch = configkit.Searches(
|
||||
configkit.WorkingDirectory(), configkit.UserConfigDirectory(),
|
||||
configkit.HomeDirectory(), configkit.UnixSystemDirectories(),
|
||||
)
|
||||
|
||||
// Load resolves one ConfigKit document and retains its inspection snapshot.
|
||||
func Load(options LoadOptions) (*configkit.Result[Document], error) {
|
||||
var source configkit.Source
|
||||
if options.Explicit {
|
||||
source = configkit.File(options.Path)
|
||||
} else {
|
||||
source = configkit.SearchConfig("scriptorium", configkit.SearchPaths(defaultConfigSearchPaths...))
|
||||
source = configkit.SearchConfig("scriptorium", applicationSearch)
|
||||
}
|
||||
|
||||
result, err := configkit.Load(source, configkit.Options[Document]{
|
||||
@@ -39,17 +43,14 @@ func Load(options LoadOptions) (*configkit.Result[Document], error) {
|
||||
// Settings converts a validated document without another merge or normalization.
|
||||
func Settings(document Document) AppSettings {
|
||||
settings := AppSettings{
|
||||
SchemaDir: document.SchemaDir,
|
||||
ServerAddr: document.Server.Addr,
|
||||
ArtifactRoot: document.Server.ArtifactRoot,
|
||||
MaxRequestBytes: document.Server.MaxRequestBytes,
|
||||
MaxArtifactBytes: document.Server.MaxArtifactBytes,
|
||||
MaxResponseBytes: document.Server.MaxResponseBytes,
|
||||
DefaultRenderFormat: renderformat.PreparedRunOutputFormat(document.Defaults.RenderFormat),
|
||||
TransientRetries: document.TransientRetries,
|
||||
}
|
||||
if document.PromptKitSources != nil {
|
||||
settings.PromptKitSources = append([]SourceSelection{}, document.PromptKitSources...)
|
||||
SchemaDir: document.PromptKit.SchemaDir,
|
||||
PromptKitConfiguration: document.PromptKit.Configuration,
|
||||
ServerAddr: document.Server.Addr,
|
||||
ArtifactRoot: document.Server.ArtifactRoot,
|
||||
MaxRequestBytes: document.Server.MaxRequestBytes,
|
||||
MaxArtifactBytes: document.Server.MaxArtifactBytes,
|
||||
MaxResponseBytes: document.Server.MaxResponseBytes,
|
||||
TransientRetries: document.TransientRetries,
|
||||
}
|
||||
return settings
|
||||
}
|
||||
|
||||
@@ -5,333 +5,195 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func useConfigSearchPaths(t *testing.T, paths ...string) {
|
||||
t.Helper()
|
||||
previous := defaultConfigSearchPaths
|
||||
defaultConfigSearchPaths = paths
|
||||
t.Cleanup(func() { defaultConfigSearchPaths = previous })
|
||||
}
|
||||
|
||||
func writeApplicationFile(t *testing.T, path, body string) {
|
||||
t.Helper()
|
||||
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(body), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadSelectsOrderedSourceAndKeepsItsSnapshot(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
first := filepath.Join(dir, "first.yml")
|
||||
second := filepath.Join(dir, "second.yml")
|
||||
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 !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, "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)
|
||||
}
|
||||
|
||||
if err := os.Remove(first); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err = Load(LoadOptions{})
|
||||
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)
|
||||
}
|
||||
|
||||
result, err = Load(LoadOptions{Path: first, Explicit: true})
|
||||
if result != nil || err == nil {
|
||||
t.Fatalf("missing explicit source was accepted: result=%+v err=%v", result, err)
|
||||
}
|
||||
var loadError *configkit.LoadError
|
||||
if !errors.As(err, &loadError) {
|
||||
t.Fatalf("typed load error was lost: %v", err)
|
||||
}
|
||||
func isolatedDiscovery(t *testing.T) string {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
t.Chdir(root)
|
||||
t.Setenv("HOME", filepath.Join(root, "home"))
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(root, "xdg"))
|
||||
return root
|
||||
}
|
||||
|
||||
func TestLoadRejectsExplicitEmptyAndDistinguishesEmptyFile(t *testing.T) {
|
||||
useConfigSearchPaths(t, filepath.Join(t.TempDir(), "absent.yml"))
|
||||
if _, err := Load(LoadOptions{Explicit: true, Path: ""}); err == nil {
|
||||
t.Fatal("explicit empty path was accepted")
|
||||
} else {
|
||||
var loadError *configkit.LoadError
|
||||
if !errors.As(err, &loadError) {
|
||||
t.Fatalf("expected upstream source diagnostic, got %v", err)
|
||||
func useIsolatedSearch(t *testing.T) {
|
||||
t.Helper()
|
||||
previous := applicationSearch
|
||||
applicationSearch = configkit.WorkingDirectory()
|
||||
t.Cleanup(func() { applicationSearch = previous })
|
||||
}
|
||||
|
||||
func TestLoadNativeDiscoveryOrderAndFilenames(t *testing.T) {
|
||||
root := isolatedDiscovery(t)
|
||||
// A later candidate must never be loaded as a configuration layer.
|
||||
paths := []string{
|
||||
filepath.Join(root, "scriptorium.yaml"),
|
||||
filepath.Join(root, "xdg", "scriptorium", "main.yaml"),
|
||||
filepath.Join(root, "home", ".scriptorium", "config.yaml"),
|
||||
}
|
||||
for index, path := range paths {
|
||||
writeApplicationFile(t, path, "server:\n addr: '"+string(rune('a'+index))+"'\n")
|
||||
}
|
||||
for index, path := range paths {
|
||||
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 {
|
||||
t.Fatalf("selected wrong source: %+v %+v", result.Config, result.Sources())
|
||||
}
|
||||
if err := os.Remove(path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
dir := t.TempDir()
|
||||
empty := filepath.Join(dir, "empty.yml")
|
||||
writeApplicationFile(t, empty, "")
|
||||
if _, err := Load(LoadOptions{Path: empty, Explicit: true}); err == nil {
|
||||
t.Fatal("empty file was treated as defaults-only")
|
||||
}
|
||||
writeApplicationFile(t, empty, "{}\n")
|
||||
result, err := Load(LoadOptions{Path: empty, Explicit: true})
|
||||
if err != nil || !reflect.DeepEqual(result.Config, documentDefaults()) {
|
||||
t.Fatalf("empty mapping did not load defaults: result=%+v err=%v", result, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDefaultsOnlyAndOverridesBeforeValidation(t *testing.T) {
|
||||
useConfigSearchPaths(t, filepath.Join(t.TempDir(), "absent.yml"))
|
||||
// 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 || !reflect.DeepEqual(result.Config, documentDefaults()) || len(result.Sources()) != 0 {
|
||||
t.Fatalf("defaults-only load: result=%+v err=%v", result, err)
|
||||
}
|
||||
var effective strings.Builder
|
||||
if err := result.WriteEffective(&effective); err != nil || !strings.Contains(effective.String(), "render_format: text") || len(result.Sources()) != 0 {
|
||||
t.Fatalf("defaults-only inspection changed: effective=%q err=%v sources=%+v", effective.String(), err, result.Sources())
|
||||
}
|
||||
|
||||
result, err = Load(LoadOptions{Overrides: []configkit.Override{{
|
||||
Path: configkit.Keys("server", "max_request_bytes"), Value: int64(0), Flag: "--max-request-bytes",
|
||||
}}})
|
||||
if err != nil || result.Config.Server.MaxRequestBytes != 0 || len(result.Sources()) != 1 || result.Sources()[0].Layer != configkit.LayerCLI {
|
||||
t.Fatalf("defaults-only override lost: result=%+v err=%v", result, err)
|
||||
}
|
||||
|
||||
path := filepath.Join(t.TempDir(), "invalid.yml")
|
||||
writeApplicationFile(t, path, "defaults:\n render_format: invalid-choice\n")
|
||||
result, err = Load(LoadOptions{Path: path, Explicit: true, Overrides: []configkit.Override{{
|
||||
Path: configkit.Keys("defaults", "render_format"), Value: "json", Flag: "--render-format",
|
||||
}}})
|
||||
if err != nil || result.Config.Defaults.RenderFormat != "json" {
|
||||
t.Fatalf("override did not precede validation: result=%+v err=%v", result, err)
|
||||
}
|
||||
if len(result.Sources()) != 2 || result.Sources()[0].Layer != configkit.LayerMain || result.Sources()[1].Layer != configkit.LayerCLI {
|
||||
t.Fatalf("source order changed: %+v", result.Sources())
|
||||
}
|
||||
writeApplicationFile(t, path, "defaults: [\n")
|
||||
if _, err := Load(LoadOptions{Path: path, Explicit: true, Overrides: []configkit.Override{{
|
||||
Path: configkit.Keys("defaults", "render_format"), Value: "json", Flag: "--render-format",
|
||||
}}}); err == nil {
|
||||
t.Fatal("override bypassed malformed lower-level YAML")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSettingsCopiesResolvedValuesAndSourceOrder(t *testing.T) {
|
||||
result, err := loadDocumentBytes(`schema_dir: ""
|
||||
promptkit_sources:
|
||||
- directory: ./profiles
|
||||
- file: ./framework.yml
|
||||
transient_retries: 0
|
||||
server:
|
||||
addr: ""
|
||||
artifact_root: ""
|
||||
max_request_bytes: 0
|
||||
defaults:
|
||||
render_format: json
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
settings := Settings(result.Config)
|
||||
if settings.SchemaDir != "" || settings.ServerAddr != "" || settings.MaxRequestBytes != 0 || settings.DefaultRenderFormat != "json" {
|
||||
t.Fatalf("resolved scalar values changed: %+v", settings)
|
||||
}
|
||||
if settings.TransientRetries == nil || *settings.TransientRetries != 0 {
|
||||
t.Fatalf("retry presence lost: %+v", settings.TransientRetries)
|
||||
}
|
||||
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.PromptKitSources != nil {
|
||||
t.Fatalf("default nullable values changed: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDiscoveryErrorQuotesPath(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "directory-\n.yml")
|
||||
if err := os.Mkdir(path, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
useConfigSearchPaths(t, path)
|
||||
_, err := Load(LoadOptions{})
|
||||
var loadError *configkit.LoadError
|
||||
if !errors.As(err, &loadError) {
|
||||
t.Fatalf("expected structured error: %v", err)
|
||||
}
|
||||
var diagnostics strings.Builder
|
||||
if err := configkit.WriteDiagnostics(&diagnostics, loadError.Diagnostics()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(diagnostics.String(), string(configkit.CodeSourceNotRegular)) || strings.Contains(diagnostics.String(), "directory-\n.yml") {
|
||||
t.Fatalf("discovery diagnostics exposed an unescaped path: %s", diagnostics.String())
|
||||
}
|
||||
|
||||
// A decoding diagnostic can contain an authored configuration path even
|
||||
// though the nonregular-file diagnostic omits its filesystem path.
|
||||
file := filepath.Join(dir, "invalid.yml")
|
||||
writeApplicationFile(t, file, "server:\n \"bad\\nkey\": value\n")
|
||||
_, err = Load(LoadOptions{Path: file, Explicit: true})
|
||||
if !errors.As(err, &loadError) {
|
||||
t.Fatalf("expected decoding diagnostic: %v", err)
|
||||
}
|
||||
diagnostics.Reset()
|
||||
if err := configkit.WriteDiagnostics(&diagnostics, loadError.Diagnostics()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(diagnostics.String(), `\n`) || strings.Contains(diagnostics.String(), "bad\nkey") {
|
||||
t.Fatalf("diagnostic path was not escaped: %q", diagnostics.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadComposesExplicitImportsAndSelectedOverlay(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, "config:\n imports:\n - glob: fragments/*.yml\n required: true\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 || !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("promptkit_sources"), Value: []any{map[string]any{"file": "./cli-framework.yml"}}, Flag: "--promptkit-source",
|
||||
}}})
|
||||
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"), "server:\n addr: :8082\n")
|
||||
if _, err := Load(LoadOptions{Path: base, Explicit: true}); err == nil {
|
||||
t.Fatal("conflicting import accepted")
|
||||
} else {
|
||||
var loadError *configkit.LoadError
|
||||
if !errors.As(err, &loadError) {
|
||||
t.Fatalf("conflict lost structured diagnostic: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadOverlayWithoutMainContext(t *testing.T) {
|
||||
useConfigSearchPaths(t, filepath.Join(t.TempDir(), "absent.yml"))
|
||||
if _, err := Load(LoadOptions{Overlay: "local"}); err == nil {
|
||||
t.Fatal("overlay without a main context accepted")
|
||||
} else {
|
||||
var loadError *configkit.LoadError
|
||||
if !errors.As(err, &loadError) {
|
||||
t.Fatalf("no-context overlay lost structured diagnostic: %v", err)
|
||||
}
|
||||
}
|
||||
if _, err := Load(LoadOptions{Overlay: "default"}); err != nil {
|
||||
t.Fatalf("default must select no overlay: %v", err)
|
||||
if err != nil || len(result.Sources()) != 0 || !reflect.DeepEqual(result.Config, documentDefaults()) {
|
||||
t.Fatalf("exhaustion: %+v %v", result, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadDoesNotFallThroughSelectedInvalidSource(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
first, second := filepath.Join(dir, "first.yml"), filepath.Join(dir, "second.yml")
|
||||
writeApplicationFile(t, first, "server: [\n")
|
||||
writeApplicationFile(t, second, "server:\n addr: ':9090'\n")
|
||||
useConfigSearchPaths(t, first, second)
|
||||
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 {
|
||||
t.Fatal("malformed selected file fell through to the second candidate")
|
||||
t.Fatal("invalid first candidate fell through")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadExplicitSelectionAndOverlayPresence(t *testing.T) {
|
||||
root := isolatedDiscovery(t)
|
||||
useIsolatedSearch(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 {
|
||||
t.Fatal("accepted invalid explicit file")
|
||||
}
|
||||
}
|
||||
_, err := Load(LoadOptions{Explicit: true})
|
||||
var native *configkit.LoadError
|
||||
if !errors.As(err, &native) {
|
||||
t.Fatalf("empty path lost typed error: %v", err)
|
||||
}
|
||||
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()) {
|
||||
t.Fatalf("no overlay: %+v %v", result, err)
|
||||
}
|
||||
}
|
||||
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 {
|
||||
t.Fatal("overlay without main context accepted")
|
||||
}
|
||||
}
|
||||
|
||||
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, 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"}}})
|
||||
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 {
|
||||
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 {
|
||||
t.Fatal("peer conflict accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadNativeOverrideEvidenceAndConflicts(t *testing.T) {
|
||||
root := isolatedDiscovery(t)
|
||||
useIsolatedSearch(t)
|
||||
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"},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Config.Server.MaxRequestBytes != 0 || result.Config.PromptKit.Configuration != "framework.yml" {
|
||||
t.Fatal(result.Config)
|
||||
}
|
||||
explanation, ok := result.Explain(configkit.Keys("promptkit", "configuration"))
|
||||
if !ok || len(explanation.History) < 2 {
|
||||
t.Fatalf("native evidence missing: %+v", explanation)
|
||||
}
|
||||
for index, records := range [][]configkit.Override{
|
||||
{{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})
|
||||
var native *configkit.LoadError
|
||||
if !errors.As(err, &native) {
|
||||
t.Fatalf("native conflict lost: %v", err)
|
||||
}
|
||||
found := false
|
||||
want := []configkit.Code{configkit.CodeOverrideDuplicate, configkit.CodeOverrideOverlap}[index]
|
||||
for _, diagnostic := range native.Diagnostics() {
|
||||
found = found || diagnostic.Code == want
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("missing conflict diagnostics: %+v", native.Diagnostics())
|
||||
}
|
||||
}
|
||||
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"}}})
|
||||
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 {
|
||||
t.Fatal("override repaired decode failure")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadImportRequirednessAndDestination(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
for _, tc := range []struct {
|
||||
name, declaration string
|
||||
wantError bool
|
||||
}{
|
||||
{"optional by default", "- path: absent.yml", false},
|
||||
{"required", "- path: absent.yml\n required: true", true},
|
||||
{"removed optional field", "- path: absent.yml\n optional: true", true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
writeApplicationFile(t, main, "config:\n imports:\n "+tc.declaration+"\n")
|
||||
_, err := Load(LoadOptions{Path: main, Explicit: true})
|
||||
if (err != nil) != tc.wantError {
|
||||
t.Fatalf("load error = %v, want error %t", err, tc.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
fragments := filepath.Join(dir, "fragments")
|
||||
if err := os.Mkdir(fragments, 0o700); err != nil {
|
||||
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 {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeApplicationFile(t, filepath.Join(fragments, "server.yml"), "addr: ':8181'\n")
|
||||
writeApplicationFile(t, main, "config:\n imports:\n - path: absent.yml\n required: true\n")
|
||||
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{Path: main, Explicit: true})
|
||||
result, err := Load(LoadOptions{Explicit: true, Path: main})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
info, ok := result.Metadata().Lookup(configkit.Keys("server", "addr"))
|
||||
if result.Config.Server.Addr != ":8181" || !ok || len(info.Origin.Locations) == 0 {
|
||||
t.Fatalf("mounted value or origin missing: config=%+v origin=%+v", result.Config.Server, info)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadAuthoredSelectorCollection(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
for _, subdir := range []string{"fragments", "overlays"} {
|
||||
if err := os.Mkdir(filepath.Join(dir, subdir), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
main := filepath.Join(dir, "config.yml")
|
||||
writeApplicationFile(t, main, "config:\n sequences:\n - destination: [promptkit_sources]\n key: file\n mode: upsert\n imports:\n - dir: fragments\n pattern: '*.yml'\n required: true\n")
|
||||
writeApplicationFile(t, filepath.Join(dir, "fragments", "a.yml"), "promptkit_sources:\n - file: a.yml\n")
|
||||
peer := filepath.Join(dir, "fragments", "b.yml")
|
||||
writeApplicationFile(t, peer, "promptkit_sources:\n - file: b.yml\n")
|
||||
writeApplicationFile(t, filepath.Join(dir, "overlays", "local.yml"), "promptkit_sources:\n - file: c.yml\n")
|
||||
result, err := Load(LoadOptions{Path: main, Explicit: true, Overlay: "local"})
|
||||
if err != nil || !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "a.yml"}, {File: "b.yml"}, {File: "c.yml"}}) {
|
||||
t.Fatalf("authored upsert did not retain selectors: result=%+v err=%v", result, err)
|
||||
}
|
||||
result, err = Load(LoadOptions{Path: main, Explicit: true, Overlay: "local", Overrides: []configkit.Override{{
|
||||
Path: configkit.Keys("promptkit_sources"), Value: []any{map[string]any{"file": "cli.yml"}}, Flag: "--promptkit-source",
|
||||
}}})
|
||||
if err != nil || !reflect.DeepEqual(result.Config.PromptKitSources, []SourceSelection{{File: "cli.yml"}}) {
|
||||
t.Fatalf("CLI list did not replace selectors: result=%+v err=%v", result, err)
|
||||
}
|
||||
writeApplicationFile(t, peer, "promptkit_sources:\n - file: a.yml\n")
|
||||
if _, err := Load(LoadOptions{Path: main, Explicit: true}); err == nil {
|
||||
t.Fatal("duplicate authored peer identity accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadOverrideCannotRepairDecodeError(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "config.yml")
|
||||
writeApplicationFile(t, path, "server:\n max_request_bytes: not-a-number\n")
|
||||
_, err := Load(LoadOptions{Path: path, Explicit: true, Overrides: []configkit.Override{{
|
||||
Path: configkit.Keys("server", "max_request_bytes"), Value: int64(12), Flag: "--max-request-bytes",
|
||||
}}})
|
||||
if err == nil {
|
||||
t.Fatal("override repaired an undecodable source value")
|
||||
t.Fatalf("value/origin: %+v %+v", result.Config, info)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +1,13 @@
|
||||
package config
|
||||
|
||||
import renderformat "gitea.maximumdirect.net/eric/scriptorium/internal/format"
|
||||
|
||||
const (
|
||||
DefaultConfigPath = "/etc/scriptorium/config.yml"
|
||||
DefaultConfigPathLocal = "/usr/local/etc/scriptorium/config.yml"
|
||||
)
|
||||
|
||||
var defaultConfigSearchPaths = []string{
|
||||
DefaultConfigPathLocal,
|
||||
DefaultConfigPath,
|
||||
}
|
||||
|
||||
// AppSettings holds the validated application values used by executable adapters.
|
||||
type AppSettings struct {
|
||||
PromptKitSources []SourceSelection
|
||||
SchemaDir string
|
||||
ServerAddr string
|
||||
ArtifactRoot string
|
||||
MaxRequestBytes int64
|
||||
MaxArtifactBytes int64
|
||||
MaxResponseBytes int64
|
||||
DefaultRenderFormat renderformat.PreparedRunOutputFormat
|
||||
TransientRetries *int
|
||||
PromptKitConfiguration string
|
||||
SchemaDir string
|
||||
ServerAddr string
|
||||
ArtifactRoot string
|
||||
MaxRequestBytes int64
|
||||
MaxArtifactBytes int64
|
||||
MaxResponseBytes int64
|
||||
TransientRetries *int
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user