Capture schema roots from configuration sources
This commit is contained in:
102
internal/configuration/schema_roots_test.go
Normal file
102
internal/configuration/schema_roots_test.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
"go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
type countingSchemaFS struct {
|
||||
opens int
|
||||
files fstest.MapFS
|
||||
}
|
||||
|
||||
func (f *countingSchemaFS) Open(name string) (fs.File, error) {
|
||||
if name != "." && name != "app" && name != "operator" {
|
||||
f.opens++
|
||||
}
|
||||
return f.files.Open(name)
|
||||
}
|
||||
|
||||
func TestSchemaRootsCaptureEffectiveContexts(t *testing.T) {
|
||||
first := &countingSchemaFS{files: fstest.MapFS{"app/placeholder": {Data: []byte("application")}}}
|
||||
second := &countingSchemaFS{files: fstest.MapFS{"operator/placeholder": {Data: []byte("operator")}}}
|
||||
firstFS := configkit.NewFilesystem("application", first)
|
||||
secondFS := configkit.NewFilesystem("override", second)
|
||||
prompt := map[string]any{"id": "p", "messages": []any{map[string]any{"role": "user", "content": "hello"}}, "output": map[string]any{"format": "text", "validation_mode": "none"}}
|
||||
base := configkit.Input{Source: configkit.Mapping("base", map[string]any{
|
||||
"schema_root": "schemas", "prompts": []any{prompt},
|
||||
"collections": map[string]any{"alpha": map[string]any{"schema_root": "old", "prompts": []any{prompt}}, "beta": map[string]any{"schema_root": "old", "prompts": []any{prompt}}},
|
||||
}), Filesystem: &configkit.FilesystemContext{FS: &firstFS, Root: ".", BaseDir: "app"}}
|
||||
override := configkit.Input{Source: configkit.Mapping("override", map[string]any{"collections": map[string]any{"alpha": map[string]any{"schema_root": "new"}, "beta": map[string]any{"schema_root": nil}}}), Filesystem: &configkit.FilesystemContext{FS: &secondFS, Root: ".", BaseDir: "operator"}}
|
||||
state, err := load(override, []configkit.Underlay{{Inputs: []configkit.Input{base}}})
|
||||
if err != nil {
|
||||
var loadErr *configkit.LoadError
|
||||
if errors.As(err, &loadErr) {
|
||||
t.Fatalf("%v: %+v", err, loadErr.Diagnostics())
|
||||
}
|
||||
t.Fatal(err)
|
||||
}
|
||||
for id, want := range map[string]string{"p": "app/schemas", "alpha/p": "operator/new", "beta/p": "app/schemas"} {
|
||||
definition, _, err := state.prompt(id)
|
||||
if err != nil || definition.SchemaRoot == nil || definition.SchemaRoot.Path != want {
|
||||
t.Fatalf("%s root = %+v, %v", id, definition, err)
|
||||
}
|
||||
}
|
||||
if first.opens != 0 || second.opens != 0 {
|
||||
t.Fatalf("root capture opened files: %d, %d", first.opens, second.opens)
|
||||
}
|
||||
for id, resource := range map[string]string{"p": "app/placeholder", "alpha/p": "operator/placeholder", "beta/p": "app/placeholder"} {
|
||||
definition, _, err := state.prompt(id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
content, err := fs.ReadFile(definition.SchemaRoot.FS, resource)
|
||||
want := "application"
|
||||
if id == "alpha/p" {
|
||||
want = "operator"
|
||||
}
|
||||
if err != nil || string(content) != want {
|
||||
t.Fatalf("%s source = %q, %v", id, content, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemaRootRequiresValueAndContext(t *testing.T) {
|
||||
filesystem := configkit.NewFilesystem("fixture", fstest.MapFS{})
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
root any
|
||||
context *configkit.FilesystemContext
|
||||
valid bool
|
||||
}{
|
||||
{"absent", nil, nil, true},
|
||||
{"blank", " ", &configkit.FilesystemContext{FS: &filesystem, Root: ".", BaseDir: "."}, false},
|
||||
{"missing context", "schemas", nil, false},
|
||||
{"explicit dot", ".", &configkit.FilesystemContext{FS: &filesystem, Root: ".", BaseDir: "."}, true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
input := configkit.Input{Source: configkit.Mapping("root", map[string]any{"schema_root": tc.root}), Filesystem: tc.context}
|
||||
_, err := load(input, nil)
|
||||
if (err == nil) != tc.valid {
|
||||
var loadErr *configkit.LoadError
|
||||
if errors.As(err, &loadErr) {
|
||||
t.Fatalf("%v: %+v", err, loadErr.Diagnostics())
|
||||
}
|
||||
t.Fatalf("root error = %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemaRootValidationUsesEffectiveValue(t *testing.T) {
|
||||
filesystem := configkit.NewFilesystem("fixture", fstest.MapFS{})
|
||||
underlay := configkit.Input{Source: configkit.Mapping("old", map[string]any{"schema_root": " "})}
|
||||
main := configkit.Input{Source: configkit.Mapping("new", map[string]any{"schema_root": "."}), Filesystem: &configkit.FilesystemContext{FS: &filesystem, Root: ".", BaseDir: "."}}
|
||||
if _, err := load(main, []configkit.Underlay{{Inputs: []configkit.Input{underlay}}}); err != nil {
|
||||
t.Fatalf("overwritten invalid root was validated: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user