133 lines
6.0 KiB
Go
133 lines
6.0 KiB
Go
package configuration
|
|
|
|
import (
|
|
"context"
|
|
"io/fs"
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
func schemaPrompt(id string, output map[string]any) map[string]any {
|
|
return map[string]any{"id": id, "messages": []any{map[string]any{"role": "user", "content": "hello"}}, "output": output}
|
|
}
|
|
|
|
func TestSchemaFileCaptureUsesEffectiveSource(t *testing.T) {
|
|
first := configkit.NewFilesystem("first", fstest.MapFS{"a/placeholder": {Data: []byte("x")}, "a/schema.json": {Data: []byte("first")}})
|
|
second := configkit.NewFilesystem("second", fstest.MapFS{"b/placeholder": {Data: []byte("x")}, "b/schema.json": {Data: []byte("second")}})
|
|
base := configkit.Input{Source: configkit.Mapping("base", map[string]any{"prompts": []any{
|
|
schemaPrompt("base", map[string]any{"format": "json", "validation_mode": "json_schema", "schema_file": "schema.json"}),
|
|
map[string]any{"id": "inherited", "extends": map[string]any{"id": "base"}},
|
|
map[string]any{"id": "override", "extends": map[string]any{"id": "base"}},
|
|
}}), Filesystem: &configkit.FilesystemContext{FS: &first, Root: ".", BaseDir: "a"}}
|
|
overlay := configkit.Input{Source: configkit.Mapping("override", map[string]any{"prompts": []any{
|
|
map[string]any{"id": "override", "extends": map[string]any{"id": "base"}, "output": map[string]any{"schema_file": "schema.json"}},
|
|
schemaPrompt("missing", map[string]any{"format": "json", "validation_mode": "json_schema", "schema_file": "missing.json"}),
|
|
}}), Filesystem: &configkit.FilesystemContext{FS: &second, Root: ".", BaseDir: "b"}}
|
|
state, err := load(overlay, []configkit.Underlay{{Inputs: []configkit.Input{base}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for id, want := range map[string]string{"base": "first", "inherited": "first", "override": "second"} {
|
|
definition, _, err := state.prompt(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, err := fs.ReadFile(definition.SchemaFile.FS, definition.SchemaFile.Path)
|
|
if err != nil || string(data) != want {
|
|
t.Fatalf("%s = %q, %v", id, data, err)
|
|
}
|
|
}
|
|
missing, err := state.InspectPrompt(context.Background(), "missing")
|
|
if err != nil || missing.Output.SchemaFile == nil || *missing.Output.SchemaFile != "missing.json" {
|
|
t.Fatalf("missing inspection = %+v, %v", missing, err)
|
|
}
|
|
}
|
|
|
|
func TestSchemaInlineCompositionAndSelection(t *testing.T) {
|
|
base := mapping("base", map[string]any{"prompts": []any{
|
|
schemaPrompt("inline-base", map[string]any{"format": "json", "validation_mode": "json_schema", "schema": map[string]any{"type": "object", "properties": map[string]any{"a": map[string]any{"type": "string"}}}}),
|
|
schemaPrompt("switch-base", map[string]any{"format": "json", "validation_mode": "none", "schema_file": "unused.json"}),
|
|
}})
|
|
main := mapping("override", map[string]any{"prompts": []any{
|
|
map[string]any{"id": "inline", "extends": map[string]any{"id": "inline-base"}, "output": map[string]any{"schema": map[string]any{"properties": map[string]any{"b": map[string]any{"type": "number"}}}}},
|
|
map[string]any{"id": "switch", "extends": map[string]any{"id": "switch-base"}, "output": map[string]any{"validation_mode": "json_schema", "schema_file": nil, "schema": false}},
|
|
}})
|
|
state, err := load(main, []configkit.Underlay{{Inputs: []configkit.Input{base}}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
definition, inspection, err := state.prompt("inline")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
properties := definition.Validation.Schema.(map[string]any)["properties"].(map[string]any)
|
|
if len(properties) != 2 || properties["a"] == nil || properties["b"] == nil {
|
|
t.Fatalf("merged properties: %#v", properties)
|
|
}
|
|
properties["a"] = false
|
|
again, _, err := state.prompt("inline")
|
|
if err != nil || again.Validation.Schema.(map[string]any)["properties"].(map[string]any)["a"] == false {
|
|
t.Fatalf("schema not detached: %+v, %v", again, err)
|
|
}
|
|
if inspection.PromptHash == "" {
|
|
t.Fatal("missing definition hash")
|
|
}
|
|
switched, _, err := state.prompt("switch")
|
|
if err != nil || switched.Validation.Schema != false || switched.Validation.SchemaFile != nil {
|
|
t.Fatalf("switch = %+v, %v", switched, err)
|
|
}
|
|
}
|
|
|
|
func TestSchemaSelectionMetadataFailures(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
output map[string]any
|
|
want string
|
|
}{
|
|
{"missing root", map[string]any{"schema_path": "x.json"}, "schema_root"},
|
|
{"missing context", map[string]any{"schema_file": "x.json"}, "filesystem context"},
|
|
{"blank path", map[string]any{"schema": false, "schema_path": " "}, "schema_path"},
|
|
{"conflicting", map[string]any{"schema": false, "schema_file": "x.json"}, "exactly one"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
tc.output["format"], tc.output["validation_mode"] = "json", "json_schema"
|
|
_, err := load(mapping("prompt", map[string]any{"prompts": []any{schemaPrompt("p", tc.output)}}), nil)
|
|
if err == nil || !strings.Contains(err.Error(), tc.want) {
|
|
t.Fatalf("error = %v, want %s", err, tc.want)
|
|
}
|
|
})
|
|
}
|
|
_, err := load(mapping("inactive", map[string]any{"prompts": []any{schemaPrompt("p", map[string]any{"format": "text", "validation_mode": "none", "schema_file": "../unused", "schema_path": " "})}}), nil)
|
|
if err != nil {
|
|
t.Fatalf("inactive references: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSchemaSelectionsContributeToDefinitionHash(t *testing.T) {
|
|
filesystem := configkit.NewFilesystem("fixtures", fstest.MapFS{})
|
|
hash := func(output map[string]any) string {
|
|
t.Helper()
|
|
output["format"], output["validation_mode"] = "json", "json_schema"
|
|
input := mapping("prompt", map[string]any{"prompts": []any{schemaPrompt("p", output)}})
|
|
input.Filesystem = &configkit.FilesystemContext{FS: &filesystem, Root: ".", BaseDir: "."}
|
|
state, err := load(input, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, inspection, err := state.prompt("p")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return inspection.PromptHash
|
|
}
|
|
if hash(map[string]any{"schema": false}) == hash(map[string]any{"schema": true}) {
|
|
t.Fatal("distinct inline values produced the same definition hash")
|
|
}
|
|
if hash(map[string]any{"schema_file": "one.json"}) == hash(map[string]any{"schema_file": "two.json"}) {
|
|
t.Fatal("distinct references produced the same definition hash")
|
|
}
|
|
}
|