Share configuration settings across the public boundary
This commit is contained in:
67
settings_contract_test.go
Normal file
67
settings_contract_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package promptkit_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"go.maximumdirect.net/configkit"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func TestSharedSettingsDecode(t *testing.T) {
|
||||
type application struct {
|
||||
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||||
}
|
||||
result, err := configkit.Load[application](configkit.Bytes("settings", []byte(`promptkit:
|
||||
owner: application
|
||||
backends:
|
||||
- id: local
|
||||
smoke_tests:
|
||||
reachability: {endpoint: https://example.test/health}
|
||||
authentication: {kind: none}
|
||||
label: owned
|
||||
collections:
|
||||
group:
|
||||
prompts:
|
||||
- id: example
|
||||
inputs: []
|
||||
messages: [{role: user, content: hello}]
|
||||
output: {format: text, validation_mode: basic}
|
||||
custom: true
|
||||
`)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := result.Config.PromptKit
|
||||
if got.Application["owner"] != "application" || got.Backends[0].Application["label"] != "owned" || got.Collections["group"].Prompts[0].Application["custom"] != true {
|
||||
t.Fatalf("application fields: %+v", got)
|
||||
}
|
||||
if got.Profiles != nil || got.Collections["group"].Prompts[0].Inputs == nil {
|
||||
t.Fatalf("container presence: %+v", got)
|
||||
}
|
||||
encoded, err := json.Marshal(got.Backends[0].SmokeTests)
|
||||
if err != nil || !strings.Contains(string(encoded), `"reachability":{"endpoint":"https://example.test/health"}`) {
|
||||
t.Fatalf("smoke JSON = %s, %v", encoded, err)
|
||||
}
|
||||
_ = promptkit.Settings{Collections: map[string]promptkit.CollectionSettings{"group": {Backends: []promptkit.BackendSettings{{SmokeTests: &promptkit.BackendSmokeTestConfig{Reachability: &promptkit.ReachabilityProbeConfig{}, Authentication: &promptkit.AuthenticationProbeConfig{}}}}, Profiles: []promptkit.ProfileSettings{{}}, Prompts: []promptkit.PromptSettings{{Inputs: []promptkit.PromptInputSettings{{}}, Messages: []promptkit.PromptMessageSettings{{CacheControl: &promptkit.PromptCacheControlSettings{}}}, Output: &promptkit.PromptOutputSettings{}}}}}}
|
||||
}
|
||||
|
||||
func TestSharedSettingsKeepNestedFieldsStrict(t *testing.T) {
|
||||
type application struct {
|
||||
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||||
}
|
||||
_, err := configkit.Load[application](configkit.Bytes("invalid", []byte("promptkit:\n prompts:\n - id: example\n output: {unknown: true}\n")))
|
||||
if err == nil {
|
||||
t.Fatal("unknown nested field accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSharedSmokeSettingsWorkWithBackendSource(t *testing.T) {
|
||||
kind := "none"
|
||||
source := promptkit.BackendSource("backend", []promptkit.BackendConfig{{ID: "example", SmokeTests: &promptkit.BackendSmokeTestConfig{Authentication: &promptkit.AuthenticationProbeConfig{Kind: &kind}}}})
|
||||
result, err := configkit.Load[promptkit.Settings](source)
|
||||
if err != nil || result.Config.Backends[0].SmokeTests == nil || *result.Config.Backends[0].SmokeTests.Authentication.Kind != kind {
|
||||
t.Fatalf("backend source = %+v, %v", result, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user