78 lines
2.7 KiB
Go
78 lines
2.7 KiB
Go
package appconfig
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
ck "go.maximumdirect.net/configkit"
|
|
pk "go.maximumdirect.net/promptkit"
|
|
)
|
|
|
|
func TestLoadCombinesApplicationAndPromptSettings(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := os.Mkdir(filepath.Join(dir, "overlays"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for name, data := range map[string]string{
|
|
"config.yml": "version: 3\nllm:\n transient_retries: 4\n",
|
|
"prompts.yml": "profiles:\n - id: local\n model: fixture\ncollections:\n example:\n prompts:\n - id: sample\n messages:\n - role: user\n content_file: message.txt\n output:\n format: text\n validation_mode: none\n",
|
|
"message.txt": "A source-local message.",
|
|
"overlays/selected.yml": "context:\n description: overlay\n",
|
|
} {
|
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(data), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
result, _, err := Load(context.Background(), ck.Input{Source: ck.File(filepath.Join(dir, "config.yml"))}, "selected")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Config.Application.Version != 3 || result.Config.Application.LLM.TransientRetries != 4 {
|
|
t.Fatalf("application settings: %+v", result.Config.Application)
|
|
}
|
|
if result.Config.Application.Context.Description != "overlay" {
|
|
t.Fatalf("overlay description = %q", result.Config.Application.Context.Description)
|
|
}
|
|
settings, ok := result.Value(ck.Keys("promptkit"))
|
|
if !ok {
|
|
t.Fatal("missing PromptKit settings")
|
|
}
|
|
if _, ok := settings.(pk.Settings); !ok {
|
|
t.Fatalf("PromptKit settings have type %T", settings)
|
|
}
|
|
engine, err := pk.NewFromConfig(result, pk.RuntimeConfig{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
prompt, err := engine.InspectPrompt(context.Background(), "example/sample")
|
|
if err != nil {
|
|
t.Fatalf("inspect source-local prompt: %v", err)
|
|
}
|
|
if len(prompt.Messages) != 1 || prompt.Messages[0].Content != "A source-local message." {
|
|
t.Fatalf("captured messages = %+v", prompt.Messages)
|
|
}
|
|
}
|
|
|
|
func TestLoadWithoutMainDoesNotDiscoverCompanions(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "prompts.yml"), []byte("profiles: [\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Chdir(dir)
|
|
result, _, err := Load(context.Background(), ck.Input{Source: ck.NoSource()}, "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Config.Application.Version != 3 {
|
|
t.Fatalf("version = %d", result.Config.Application.Version)
|
|
}
|
|
for _, source := range result.Sources() {
|
|
if strings.HasSuffix(source.Path, "prompts.yml") || strings.HasSuffix(source.Path, "glossary.yml") {
|
|
t.Fatalf("unexpected companion source: %+v", source)
|
|
}
|
|
}
|
|
}
|