92 lines
2.8 KiB
Go
92 lines
2.8 KiB
Go
package profile
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/promptkit/internal/domain"
|
|
)
|
|
|
|
func TestMemoryRepositoryOwnsNormalizedDefinitions(t *testing.T) {
|
|
nested := map[string]any{"value": "original"}
|
|
definitions := []domain.ExecutionProfile{{
|
|
ID: " profile ",
|
|
BackendID: " backend ",
|
|
Endpoint: " https://example.test/v1/ ",
|
|
Model: "model",
|
|
ExtraParams: map[string]any{"nested": nested},
|
|
}}
|
|
|
|
repository, err := NewMemoryRepository(definitions)
|
|
if err != nil {
|
|
t.Fatalf("construct repository: %v", err)
|
|
}
|
|
nested["value"] = "mutated input"
|
|
|
|
first, err := repository.GetProfile(context.Background(), "profile")
|
|
if err != nil {
|
|
t.Fatalf("load profile: %v", err)
|
|
}
|
|
if first.ID != "profile" || first.BackendID != "backend" || first.Endpoint != "https://example.test/v1/" {
|
|
t.Fatalf("normalized definition = %#v", first)
|
|
}
|
|
if first.ExtraParams["nested"].(map[string]any)["value"] != "original" {
|
|
t.Fatalf("repository retained input mutation: %#v", first.ExtraParams)
|
|
}
|
|
first.ExtraParams["nested"].(map[string]any)["value"] = "mutated result"
|
|
|
|
second, err := repository.GetProfile(context.Background(), "profile")
|
|
if err != nil {
|
|
t.Fatalf("reload profile: %v", err)
|
|
}
|
|
if second.ExtraParams["nested"].(map[string]any)["value"] != "original" {
|
|
t.Fatalf("repository retained result mutation: %#v", second.ExtraParams)
|
|
}
|
|
}
|
|
|
|
func TestMemoryRepositoryPreservesInheritanceAndRejectsInvalidDefinitions(t *testing.T) {
|
|
derived, err := NewMemoryRepository([]domain.ExecutionProfile{{
|
|
ID: "derived",
|
|
BaseProfileID: "base",
|
|
ReasoningEffort: "high",
|
|
}})
|
|
if err != nil {
|
|
t.Fatalf("construct derived repository: %v", err)
|
|
}
|
|
profile, err := derived.GetProfile(context.Background(), "derived")
|
|
if err != nil {
|
|
t.Fatalf("load derived profile: %v", err)
|
|
}
|
|
if profile.BaseProfileID != "base" || profile.ReasoningEffort != "high" {
|
|
t.Fatalf("derived profile = %#v", profile)
|
|
}
|
|
if _, err := derived.GetProfile(context.Background(), "missing"); !errors.Is(err, ErrProfileNotFound) {
|
|
t.Fatalf("missing profile error = %v, want ErrProfileNotFound", err)
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
definitions []domain.ExecutionProfile
|
|
}{
|
|
{
|
|
name: "normalized duplicate IDs",
|
|
definitions: []domain.ExecutionProfile{
|
|
{ID: " duplicate ", Endpoint: "https://one.example/v1", Model: "one"},
|
|
{ID: "duplicate", Endpoint: "https://two.example/v1", Model: "two"},
|
|
},
|
|
},
|
|
{
|
|
name: "invalid extra parameters",
|
|
definitions: []domain.ExecutionProfile{{ID: "invalid", Endpoint: "https://example.test/v1", Model: "model", ExtraParams: map[string]any{"invalid": func() {}}}},
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
if _, err := NewMemoryRepository(test.definitions); err == nil {
|
|
t.Fatal("expected repository construction error")
|
|
}
|
|
})
|
|
}
|
|
}
|