143 lines
4.2 KiB
Go
143 lines
4.2 KiB
Go
package configuration
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/ConfigKit"
|
|
"gitea.maximumdirect.net/eric/promptkit/internal/domain"
|
|
)
|
|
|
|
func TestLoadedListsSortedEffectiveIdentities(t *testing.T) {
|
|
state, err := Load(mapping("identities", map[string]any{
|
|
"backends": map[string]any{
|
|
"local": map[string]any{"endpoint": "http://localhost:8080/v1"},
|
|
},
|
|
"profiles": map[string]any{
|
|
"z/profile": map[string]any{"backend": "local", "model": "z-model"},
|
|
"partial/profile": map[string]any{"backend": "local"},
|
|
"a/profile": map[string]any{"backend": "local", "model": "a-model"},
|
|
" z/profile ": map[string]any{"backend": "local", "model": "spaced-model"},
|
|
},
|
|
"prompts": map[string]any{
|
|
"z/prompt": map[string]any{"v1": listingPromptDefinition()},
|
|
"a/prompt": map[string]any{"v1": listingPromptDefinition()},
|
|
" z/prompt ": map[string]any{
|
|
"2": listingPromptDefinition(),
|
|
"10": listingPromptDefinition(),
|
|
},
|
|
},
|
|
}), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
profiles, err := state.ListProfiles(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wantProfiles := []string{" z/profile ", "a/profile", "partial/profile", "z/profile"}
|
|
if !reflect.DeepEqual(profiles, wantProfiles) {
|
|
t.Fatalf("profile IDs = %#v, want %#v", profiles, wantProfiles)
|
|
}
|
|
|
|
prompts, err := state.ListPrompts(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wantPrompts := []domain.PromptIdentity{
|
|
{ID: " z/prompt ", Version: "10"},
|
|
{ID: " z/prompt ", Version: "2"},
|
|
{ID: "a/prompt", Version: "v1"},
|
|
{ID: "z/prompt", Version: "v1"},
|
|
}
|
|
if !reflect.DeepEqual(prompts, wantPrompts) {
|
|
t.Fatalf("prompt identities = %#v, want %#v", prompts, wantPrompts)
|
|
}
|
|
}
|
|
|
|
func TestLoadedListsAreIndependent(t *testing.T) {
|
|
state, err := Load(mapping("identities", map[string]any{
|
|
"profiles": map[string]any{"profile": map[string]any{}},
|
|
"prompts": map[string]any{"example": map[string]any{"v1": listingPromptDefinition()}},
|
|
}), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
profiles, err := state.ListProfiles(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
profiles = append(profiles, "caller profile")
|
|
profiles[0] = "changed profile"
|
|
|
|
prompts, err := state.ListPrompts(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
prompts[0].ID = "changed prompt"
|
|
prompts[0].Version = "changed version"
|
|
|
|
profiles, err = state.ListProfiles(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(profiles, []string{"profile"}) {
|
|
t.Fatalf("caller mutation changed profile inventory: %#v", profiles)
|
|
}
|
|
prompts, err = state.ListPrompts(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wantPrompts := []domain.PromptIdentity{{ID: "example", Version: "v1"}}
|
|
if !reflect.DeepEqual(prompts, wantPrompts) {
|
|
t.Fatalf("caller mutation changed prompt inventory: %#v", prompts)
|
|
}
|
|
}
|
|
|
|
func TestLoadedListsEmptyInventory(t *testing.T) {
|
|
state, err := Load(configkit.Input{Source: configkit.NoSource()}, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
profiles, err := state.ListProfiles(context.Background())
|
|
if err != nil || len(profiles) != 0 {
|
|
t.Fatalf("empty profile listing = %#v, %v", profiles, err)
|
|
}
|
|
prompts, err := state.ListPrompts(context.Background())
|
|
if err != nil || len(prompts) != 0 {
|
|
t.Fatalf("empty prompt listing = %#v, %v", prompts, err)
|
|
}
|
|
}
|
|
|
|
func TestLoadedListsHonorCanceledContext(t *testing.T) {
|
|
state, err := Load(mapping("identities", map[string]any{
|
|
"profiles": map[string]any{"partial": map[string]any{}},
|
|
"prompts": map[string]any{"example": map[string]any{"v1": listingPromptDefinition()}},
|
|
}), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
profiles, err := state.ListProfiles(ctx)
|
|
if !errors.Is(err, context.Canceled) || len(profiles) != 0 {
|
|
t.Fatalf("canceled profile listing = %#v, %v", profiles, err)
|
|
}
|
|
prompts, err := state.ListPrompts(ctx)
|
|
if !errors.Is(err, context.Canceled) || len(prompts) != 0 {
|
|
t.Fatalf("canceled prompt listing = %#v, %v", prompts, err)
|
|
}
|
|
}
|
|
|
|
func listingPromptDefinition() map[string]any {
|
|
return map[string]any{
|
|
"messages": []any{map[string]any{"role": "user", "content": "Hello."}},
|
|
"output": map[string]any{"format": "text", "validation_mode": "basic"},
|
|
}
|
|
}
|