161 lines
5.3 KiB
Go
161 lines
5.3 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"},
|
|
},
|
|
"collections": map[string]any{
|
|
"z": map[string]any{"profiles": map[string]any{"profile": map[string]any{"backend": "local", "model": "z-model"}}, "prompts": map[string]any{"prompt": listingPromptDefinition()}},
|
|
"partial": map[string]any{"profiles": map[string]any{"profile": map[string]any{"backend": "local"}}},
|
|
"a": map[string]any{"profiles": map[string]any{"profile": map[string]any{"backend": "local", "model": "a-model"}}, "prompts": map[string]any{"prompt": listingPromptDefinition()}},
|
|
" z": map[string]any{"profiles": map[string]any{"profile ": map[string]any{"backend": "local", "model": "spaced-model"}}, "prompts": map[string]any{"prompt ": listingPromptDefinition()}},
|
|
},
|
|
}), nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
backends, err := state.ListBackends(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wantBackends := []string{"local"}
|
|
if !reflect.DeepEqual(backends, wantBackends) {
|
|
t.Fatalf("backend IDs = %#v, want %#v", backends, wantBackends)
|
|
}
|
|
|
|
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: stringPointer("v1")},
|
|
{ID: "a/prompt", Version: stringPointer("v1")},
|
|
{ID: "z/prompt", Version: stringPointer("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{
|
|
"backends": map[string]any{"backend": map[string]any{"endpoint": "https://example.test/v1"}},
|
|
"profiles": map[string]any{"profile": map[string]any{}},
|
|
"prompts": map[string]any{"example": 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"
|
|
backends, err := state.ListBackends(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
backends[0] = "changed backend"
|
|
|
|
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)
|
|
}
|
|
backends, err = state.ListBackends(context.Background())
|
|
if err != nil || !reflect.DeepEqual(backends, []string{"backend"}) {
|
|
t.Fatalf("caller mutation changed backend inventory: %#v, %v", backends, err)
|
|
}
|
|
prompts, err = state.ListPrompts(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wantPrompts := []domain.PromptIdentity{{ID: "example", Version: stringPointer("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)
|
|
}
|
|
backends, err := state.ListBackends(context.Background())
|
|
if err != nil || len(backends) != 0 {
|
|
t.Fatalf("empty backend listing = %#v, %v", backends, 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": 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)
|
|
}
|
|
backends, err := state.ListBackends(ctx)
|
|
if !errors.Is(err, context.Canceled) || len(backends) != 0 {
|
|
t.Fatalf("canceled backend listing = %#v, %v", backends, 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{
|
|
"version": "v1",
|
|
"messages": []any{map[string]any{"role": "user", "content": "Hello."}},
|
|
"output": map[string]any{"format": "text", "validation_mode": "basic"},
|
|
}
|
|
}
|