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, renderToolListing(toolInfos)) p.Set(SectionToolDocs, renderToolDocs(toolInfos)) preamble := p.String() // Must contain core sections for _, want := range []string{ "# Core System Prompt\nTest", "# Environment\n- Test env", "# Agent Prompt\nTest instructions", "# Available Tools", "- **file_read** — Read a file with line numbers", "- **shell** — Execute shell command", "# Tool Documentation", "**file_read**\n\nRead a file with line numbers...", "**Schema**:\n```json\n{\"type\":\"object\"}\n```", } { 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, renderToolListing(nil)) p.Set(SectionToolDocs, renderToolDocs(nil)) preamble := p.String() if strings.Contains(preamble, "# Available Tools") { t.Error("no tools → no Available Tools section") } if strings.Contains(preamble, "# Tool Documentation") { t.Error("no tools → no Tool Documentation 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) } }