119 lines
3.2 KiB
Go
119 lines
3.2 KiB
Go
package builtin
|
|
|
|
import (
|
|
"context"
|
|
"io/fs"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/promptkit/internal/backend"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func TestBuiltInProfilesValidateThroughRepository(t *testing.T) {
|
|
repo := NewRepository()
|
|
ids := loadBuiltInProfileIDs(t)
|
|
if len(ids) == 0 {
|
|
t.Fatal("expected built-in profiles")
|
|
}
|
|
|
|
for id := range ids {
|
|
t.Run(id, func(t *testing.T) {
|
|
p, err := repo.GetProfile(context.Background(), id)
|
|
if err != nil {
|
|
t.Fatalf("expected built-in profile %q to load, got %v", id, err)
|
|
}
|
|
if p.ID != id {
|
|
t.Fatalf("expected profile id %q, got %q", id, p.ID)
|
|
}
|
|
if !builtInBackendIDs[p.BackendID] {
|
|
t.Fatalf("expected profile %q to select a maintained built-in, got %q", id, p.BackendID)
|
|
}
|
|
if p.Endpoint != "" || p.APIKeyEnv != "" {
|
|
t.Fatalf("expected profile %q to inherit backend connection settings, got endpoint=%q api_key_env=%q", id, p.Endpoint, p.APIKeyEnv)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestBuiltInProfilesDoNotContainDuplicateIDsOrRawAPIKeys(t *testing.T) {
|
|
loadBuiltInProfileIDs(t)
|
|
}
|
|
|
|
func TestRakestrawhomeGemmaProfileUsesNativeDefaults(t *testing.T) {
|
|
p, err := NewRepository().GetProfile(context.Background(), "rakestrawhome-gemma-4-31b")
|
|
if err != nil {
|
|
t.Fatalf("load Rakestrawhome Gemma profile: %v", err)
|
|
}
|
|
if p.ID != "rakestrawhome-gemma-4-31b" ||
|
|
p.BackendID != backend.RakestrawHomeID ||
|
|
p.Model != "google/gemma-4-31b-it" ||
|
|
p.Endpoint != "" ||
|
|
p.Temperature != 0 ||
|
|
p.MaxTokens != 0 ||
|
|
p.TopP != 0 ||
|
|
p.TimeoutSeconds != 0 ||
|
|
p.ServiceTier != "" ||
|
|
p.ReasoningEffort != "" ||
|
|
p.APIKeyEnv != "" ||
|
|
p.APIKeyRequired ||
|
|
p.ExtraParams != nil {
|
|
t.Fatalf("unexpected Rakestrawhome Gemma profile: %#v", p)
|
|
}
|
|
}
|
|
|
|
var builtInBackendIDs = map[string]bool{
|
|
backend.OpenRouterID: true,
|
|
backend.RakestrawHomeID: true,
|
|
}
|
|
|
|
func loadBuiltInProfileIDs(t *testing.T) map[string]string {
|
|
t.Helper()
|
|
|
|
ids := map[string]string{}
|
|
err := fs.WalkDir(assets, assetRoot, func(name string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.IsDir() || !strings.HasSuffix(name, ".yml") {
|
|
return nil
|
|
}
|
|
|
|
data, err := assets.ReadFile(name)
|
|
if err != nil {
|
|
t.Fatalf("failed to read built-in profile %s: %v", name, err)
|
|
}
|
|
|
|
var raw map[string]any
|
|
if err := yaml.Unmarshal(data, &raw); err != nil {
|
|
t.Fatalf("failed to decode built-in profile %s: %v", name, err)
|
|
}
|
|
if _, ok := raw["api_key"]; ok {
|
|
t.Fatalf("built-in profile %s contains raw api_key", name)
|
|
}
|
|
backendID, ok := raw["backend"].(string)
|
|
if !ok || !builtInBackendIDs[backendID] {
|
|
t.Fatalf("built-in profile %s does not select a maintained built-in: %#v", name, raw["backend"])
|
|
}
|
|
if _, ok := raw["endpoint"]; ok {
|
|
t.Fatalf("built-in profile %s repeats endpoint", name)
|
|
}
|
|
if _, ok := raw["api_key_env"]; ok {
|
|
t.Fatalf("built-in profile %s repeats api_key_env", name)
|
|
}
|
|
id, ok := raw["id"].(string)
|
|
if !ok || strings.TrimSpace(id) == "" {
|
|
t.Fatalf("built-in profile %s has missing id", name)
|
|
}
|
|
if previous, ok := ids[id]; ok {
|
|
t.Fatalf("duplicate built-in profile id %q in %s and %s", id, previous, name)
|
|
}
|
|
ids[id] = name
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("failed to walk built-in profiles: %v", err)
|
|
}
|
|
return ids
|
|
}
|