ollie/agent/runtime_test.go

109 lines
2.6 KiB
Go

package agent
import (
"strings"
"testing"
"ollie/toolsrv"
)
func TestPreambleSections(t *testing.T) {
toolInfos := []toolsrv.ToolInfo{
{
Name: "file_read",
Description: "Read a file with line numbers",
Prompt: "**file_read**\n\nRead a file with line numbers...",
InputSchema: []byte(`{"type":"object"}`),
Server: "",
},
{
Name: "file_write",
Description: "Create or overwrite a file",
Prompt: "**file_write**\n\nCreate or overwrite a file...",
InputSchema: []byte(`{"type":"object"}`),
Server: "remote", // should be excluded
},
{
Name: "shell",
Description: "Execute shell command",
Prompt: "",
InputSchema: []byte(`{}`),
Server: "",
},
}
p := &Preamble{}
p.Set(SectionSystem, "# Core System Prompt\nTest")
p.Set(SectionEnv, "# Environment\n- Test env")
p.Set(SectionAgent, "# Agent Prompt\nTest instructions")
p.Set(SectionTools, renderTools(toolInfos))
preamble := p.String()
// Must contain core sections
for _, want := range []string{
"# Core System Prompt\nTest",
"# Environment\n- Test env",
"# Agent Prompt\nTest instructions",
"# Tools",
"## file_read",
"**file_read**\n\nRead a file with line numbers...",
"## shell",
"Execute shell command",
} {
if !strings.Contains(preamble, want) {
t.Errorf("preamble missing: %q", want)
}
}
// Remote server tools excluded
if strings.Contains(preamble, "file_write") {
t.Error("preamble should exclude tools with Server != \"\"")
}
}
func TestPreambleNoTools(t *testing.T) {
p := &Preamble{}
p.Set(SectionSystem, "# Core System Prompt\nTest")
p.Set(SectionEnv, "# Environment\n- Test env")
p.Set(SectionAgent, "# Agent Prompt\nTest instructions")
p.Set(SectionTools, renderTools(nil))
preamble := p.String()
if strings.Contains(preamble, "# Tools") {
t.Error("no tools → no Tools section")
}
if !strings.Contains(preamble, "# Core System Prompt") {
t.Error("core section missing")
}
}
func TestPreambleSetReplaces(t *testing.T) {
p := &Preamble{}
p.Set(SectionSystem, "first")
p.Set(SectionEnv, "env")
p.Set(SectionSystem, "second")
got := p.String()
if strings.Contains(got, "first") {
t.Error("Set should replace, not append")
}
if !strings.Contains(got, "second") {
t.Error("Set should store new value")
}
}
func TestPreambleEmptySectionsOmitted(t *testing.T) {
p := &Preamble{}
p.Set(SectionSystem, "system")
p.Set(SectionEnv, "")
p.Set(SectionAgent, "agent")
got := p.String()
// Should be "system\nagent" with no extra newlines from empty env
if strings.Contains(got, "\n\n") {
t.Errorf("empty sections should be omitted, got: %q", got)
}
}