Migrate configuration tests to captured resources
This commit is contained in:
63
internal/configuration/test_helpers_test.go
Normal file
63
internal/configuration/test_helpers_test.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"go.maximumdirect.net/configkit"
|
||||
"go.maximumdirect.net/promptkit/internal/settings"
|
||||
)
|
||||
|
||||
type testDocument struct {
|
||||
PromptKit settings.Settings `yaml:"promptkit"`
|
||||
Application map[string]any `yaml:",inline"`
|
||||
}
|
||||
|
||||
type testLoaded struct {
|
||||
*loaded
|
||||
inspection configkit.Inspection
|
||||
}
|
||||
|
||||
func testDefinition() configkit.Definition[testDocument] {
|
||||
return configkit.Definition[testDocument]{
|
||||
SequenceIdentities: SequenceIdentities(),
|
||||
Sensitive: Sensitive,
|
||||
Resolve: func(value *testDocument, resolution *configkit.Resolution) error {
|
||||
Resolve(&value.PromptKit, resolution)
|
||||
return nil
|
||||
},
|
||||
Validate: func(value *testDocument, validation *configkit.Validation) { Validate(&value.PromptKit, validation) },
|
||||
}
|
||||
}
|
||||
|
||||
// The adapter fixtures are wrapperless settings fragments. Mount each with
|
||||
// ConfigKit's native destination and run the same rules and capture as callers.
|
||||
func load(main configkit.Input, underlays []configkit.Underlay) (*testLoaded, error) {
|
||||
mount := func(input configkit.Input) configkit.Input {
|
||||
if input.Destination.String() == "" {
|
||||
input.Destination = configkit.Keys("promptkit")
|
||||
}
|
||||
return input
|
||||
}
|
||||
main = mount(main)
|
||||
selected := make([]configkit.Underlay, len(underlays))
|
||||
for i, underlay := range underlays {
|
||||
selected[i] = underlay
|
||||
selected[i].Inputs = make([]configkit.Input, len(underlay.Inputs))
|
||||
for j, input := range underlay.Inputs {
|
||||
selected[i].Inputs[j] = mount(input)
|
||||
}
|
||||
}
|
||||
result, err := configkit.LoadLayered(main, configkit.LayeredOptions[testDocument]{Definition: testDefinition(), Underlays: selected})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load configuration: %w", err)
|
||||
}
|
||||
view, ok := result.InspectSubtree(configkit.Keys("promptkit"))
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("inspection unavailable")
|
||||
}
|
||||
state, err := Capture(&result.Config.PromptKit, view, result.Diagnostics())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("capture configuration: %w", err)
|
||||
}
|
||||
return &testLoaded{loaded: state, inspection: view}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user