ollie/cmd/olliesrv/internal/agent/runtime_test.go

84 lines
3.0 KiB
Go

package agent
import (
"strings"
"testing"
"ollie/toolsrv/protocol"
)
func TestEnvironmentBlockStable(t *testing.T) {
got := EnvironmentBlock("/work", "linux", true, "default")
want := "# Environment\n\n- Working directory: /work\n- Platform: linux\n- Is git repo: true\n- Sandbox: default"
if got != want {
t.Fatalf("EnvironmentBlock() = %q, want %q", got, want)
}
if strings.Contains(got, "Current date") {
t.Fatal("environment block contains volatile current date")
}
}
func TestPreambleSections(t *testing.T) {
toolInfos := []protocol.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"}`)},
{Name: "file_write", Description: "Create or overwrite a file", Prompt: "**file_write**\n\nCreate or overwrite a file...", InputSchema: []byte(`{"type":"object"}`), Server: "remote"},
{Name: "shell", Description: "Execute shell command", InputSchema: []byte(`{}`)},
}
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()
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)
}
}
if strings.Contains(preamble, "file_write") {
t.Error("preamble should exclude tools with Server != \"\"")
}
}
func TestRenderToolsStableOrder(t *testing.T) {
infos := []protocol.ToolInfo{{Name: "z_tool", Description: "z"}, {Name: "a_tool", Description: "a"}}
first := RenderTools(infos)
second := RenderTools([]protocol.ToolInfo{infos[1], infos[0]})
if first != second || strings.Index(first, "a_tool") > strings.Index(first, "z_tool") {
t.Fatalf("tool rendering is not deterministic: %q", first)
}
}
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") || !strings.Contains(preamble, "# Core System Prompt") {
t.Fatal("unexpected no-tools preamble")
}
}
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") || !strings.Contains(got, "second") {
t.Fatalf("preamble replacement failed: %q", got)
}
}
func TestPreambleEmptySectionsOmitted(t *testing.T) {
p := &Preamble{}
p.Set(SectionSystem, "system")
p.Set(SectionEnv, "")
p.Set(SectionAgent, "agent")
if got := p.String(); strings.Contains(got, "\n\n") {
t.Errorf("empty sections should be omitted, got: %q", got)
}
}