93 lines
3.1 KiB
Go
93 lines
3.1 KiB
Go
package promptkit_test
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/ConfigKit"
|
|
"gitea.maximumdirect.net/eric/promptkit"
|
|
)
|
|
|
|
func TestConfigurationInspectionDiffsCapturedValues(t *testing.T) {
|
|
left := comparisonInspection(t, "earlier-model")
|
|
right := comparisonInspection(t, "later-model")
|
|
|
|
differences, err := left.Diff(right)
|
|
if err != nil {
|
|
t.Fatalf("compare captured configurations: %v", err)
|
|
}
|
|
if len(differences) != 1 {
|
|
t.Fatalf("difference count = %d, want 1: %+v", len(differences), differences)
|
|
}
|
|
difference := differences[0]
|
|
if got, want := difference.Path.String(), "/profiles/offline/model"; got != want {
|
|
t.Fatalf("difference path = %q, want %q", got, want)
|
|
}
|
|
if !strings.Contains(difference.Left.YAML, "earlier-model") || !strings.Contains(difference.Right.YAML, "later-model") {
|
|
t.Fatalf("difference snapshots = (%q, %q)", difference.Left.YAML, difference.Right.YAML)
|
|
}
|
|
|
|
reversed, err := right.Diff(left)
|
|
if err != nil {
|
|
t.Fatalf("compare reversed configurations: %v", err)
|
|
}
|
|
if len(reversed) != 1 || reversed[0].Path.String() != difference.Path.String() ||
|
|
!strings.Contains(reversed[0].Left.YAML, "later-model") ||
|
|
!strings.Contains(reversed[0].Right.YAML, "earlier-model") {
|
|
t.Fatalf("reversed difference = %+v", reversed)
|
|
}
|
|
|
|
equal := comparisonInspection(t, "earlier-model")
|
|
if equal, err := left.Diff(equal); err != nil || len(equal) != 0 {
|
|
t.Fatalf("compare equal configurations = (%+v, %v), want no differences", equal, err)
|
|
}
|
|
if equal, err := left.Diff(left); err != nil || len(equal) != 0 {
|
|
t.Fatalf("compare view with itself = (%+v, %v), want no differences", equal, err)
|
|
}
|
|
}
|
|
|
|
func TestConfigurationInspectionDiffRejectsZeroViews(t *testing.T) {
|
|
valid := comparisonInspection(t, "current-model")
|
|
var zero promptkit.ConfigurationInspection
|
|
|
|
for _, comparison := range []struct {
|
|
name string
|
|
left, right promptkit.ConfigurationInspection
|
|
}{
|
|
{name: "zero left", left: zero, right: valid},
|
|
{name: "zero right", left: valid, right: zero},
|
|
{name: "both zero", left: zero, right: zero},
|
|
} {
|
|
t.Run(comparison.name, func(t *testing.T) {
|
|
differences, err := comparison.left.Diff(comparison.right)
|
|
if !errors.Is(err, promptkit.ErrInvalidConfig) || len(differences) != 0 {
|
|
t.Fatalf("Diff() = (%+v, %v), want ErrInvalidConfig and no records", differences, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func comparisonInspection(t *testing.T, model string) promptkit.ConfigurationInspection {
|
|
t.Helper()
|
|
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{
|
|
Underlays: []configkit.Underlay{{Inputs: []configkit.Input{{Source: configkit.Mapping("comparison fixture", map[string]any{
|
|
"backends": map[string]any{"local": map[string]any{
|
|
"endpoint": "https://example.invalid/v1",
|
|
}},
|
|
"profiles": map[string]any{"offline": map[string]any{
|
|
"backend": "local",
|
|
"model": model,
|
|
}},
|
|
})}}}},
|
|
}})
|
|
if err != nil {
|
|
t.Fatalf("construct comparison engine: %v", err)
|
|
}
|
|
inspection, err := engine.InspectConfiguration()
|
|
if err != nil {
|
|
t.Fatalf("inspect comparison engine: %v", err)
|
|
}
|
|
return inspection
|
|
}
|