138 lines
3.6 KiB
Go
138 lines
3.6 KiB
Go
package agent
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoad(t *testing.T) {
|
|
r := strings.NewReader(`{"hooks": {"postTurn": "notify-send done"}}`)
|
|
cfg, err := Load(r)
|
|
if err != nil {
|
|
t.Fatalf("Load failed: %v", err)
|
|
}
|
|
if len(cfg.Hooks["postTurn"]) != 1 || cfg.Hooks["postTurn"][0] != "notify-send done" {
|
|
t.Errorf("Expected hook 'notify-send done', got %q", cfg.Hooks["postTurn"])
|
|
}
|
|
}
|
|
|
|
func TestLoadEmpty(t *testing.T) {
|
|
cfg, err := Load(strings.NewReader(`{}`))
|
|
if err != nil {
|
|
t.Fatalf("Load failed: %v", err)
|
|
}
|
|
if len(cfg.Hooks) != 0 {
|
|
t.Errorf("Expected no hooks, got %v", cfg.Hooks)
|
|
}
|
|
}
|
|
|
|
func TestLoadInvalidJSON(t *testing.T) {
|
|
_, err := Load(strings.NewReader(`{bad`))
|
|
if err == nil {
|
|
t.Error("expected error for invalid JSON")
|
|
}
|
|
}
|
|
|
|
func TestHookCmdsString(t *testing.T) {
|
|
cfg, err := Load(strings.NewReader(`{"hooks": {"pre": "single"}}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(cfg.Hooks["pre"]) != 1 || cfg.Hooks["pre"][0] != "single" {
|
|
t.Errorf("got %v, want [single]", cfg.Hooks["pre"])
|
|
}
|
|
}
|
|
|
|
func TestHookCmdsArray(t *testing.T) {
|
|
cfg, err := Load(strings.NewReader(`{"hooks": {"pre": ["a", "b"]}}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(cfg.Hooks["pre"]) != 2 || cfg.Hooks["pre"][0] != "a" || cfg.Hooks["pre"][1] != "b" {
|
|
t.Errorf("got %v, want [a b]", cfg.Hooks["pre"])
|
|
}
|
|
}
|
|
|
|
func TestHookCmdsInvalid(t *testing.T) {
|
|
_, err := Load(strings.NewReader(`{"hooks": {"pre": 42}}`))
|
|
if err == nil {
|
|
t.Error("expected error for invalid hook type")
|
|
}
|
|
}
|
|
|
|
func TestPromptString(t *testing.T) {
|
|
cfg, err := Load(strings.NewReader(`{"prompt": "be helpful"}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.Prompt.IsExec || len(cfg.Prompt.Value) != 1 || cfg.Prompt.Value[0] != "be helpful" {
|
|
t.Errorf("Prompt = %v", cfg.Prompt)
|
|
}
|
|
}
|
|
|
|
func TestPromptArray(t *testing.T) {
|
|
cfg, err := Load(strings.NewReader(`{"prompt": ["echo hello", "echo world"]}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !cfg.Prompt.IsExec {
|
|
t.Error("expected IsExec=true for array prompt")
|
|
}
|
|
if len(cfg.Prompt.Value) != 2 || cfg.Prompt.Value[0] != "echo hello" || cfg.Prompt.Value[1] != "echo world" {
|
|
t.Errorf("Prompt.Value = %v", cfg.Prompt.Value)
|
|
}
|
|
}
|
|
|
|
func TestPromptInvalid(t *testing.T) {
|
|
_, err := Load(strings.NewReader(`{"prompt": 42}`))
|
|
if err == nil {
|
|
t.Error("expected error for invalid prompt type")
|
|
}
|
|
}
|
|
|
|
func TestLoadAllFields(t *testing.T) {
|
|
cfg, err := Load(strings.NewReader(`{
|
|
"prompt": "be helpful",
|
|
"maxTokens": 4096,
|
|
"temperature": 0.7,
|
|
"frequencyPenalty": 0.5,
|
|
"presencePenalty": 0.3
|
|
}`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.Prompt.IsExec || len(cfg.Prompt.Value) != 1 || cfg.Prompt.Value[0] != "be helpful" {
|
|
t.Errorf("Prompt = %v", cfg.Prompt)
|
|
}
|
|
if cfg.MaxTokens != 4096 {
|
|
t.Errorf("MaxTokens = %d", cfg.MaxTokens)
|
|
}
|
|
if cfg.Temperature == nil || *cfg.Temperature != 0.7 {
|
|
t.Errorf("Temperature = %v", cfg.Temperature)
|
|
}
|
|
if cfg.FrequencyPenalty == nil || *cfg.FrequencyPenalty != 0.5 {
|
|
t.Errorf("FrequencyPenalty = %v", cfg.FrequencyPenalty)
|
|
}
|
|
if cfg.PresencePenalty == nil || *cfg.PresencePenalty != 0.3 {
|
|
t.Errorf("PresencePenalty = %v", cfg.PresencePenalty)
|
|
}
|
|
}
|
|
|
|
func TestToolsEnabled(t *testing.T) {
|
|
// Omitted: defaults to true.
|
|
cfg, _ := Load(strings.NewReader(`{}`))
|
|
if !cfg.ToolsEnabled() {
|
|
t.Error("expected ToolsEnabled()=true when omitted")
|
|
}
|
|
// Explicit false.
|
|
cfg, _ = Load(strings.NewReader(`{"tools": false}`))
|
|
if cfg.ToolsEnabled() {
|
|
t.Error("expected ToolsEnabled()=false")
|
|
}
|
|
// Explicit true.
|
|
cfg, _ = Load(strings.NewReader(`{"tools": true}`))
|
|
if !cfg.ToolsEnabled() {
|
|
t.Error("expected ToolsEnabled()=true")
|
|
}
|
|
}
|