package sanitize
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestStripSystemXML(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "no XML tags",
input: "Hello, this is plain text with no XML.",
expected: "Hello, this is plain text with no XML.",
},
{
name: "empty string",
input: "",
expected: "",
},
{
name: "task-notification block",
input: "Before\n\nabc123\ncompleted\nAgent completed\nSome result text\n\nAfter",
expected: "Before\n\nAfter",
},
{
name: "system-reminder block",
input: "Content before\n\nSome system reminder text\n\nContent after",
expected: "Content before\n\nContent after",
},
{
name: "relevant-memory block",
input: "\n# Relevant Knowledge\n## 1. Something\n\nActual content",
expected: "Actual content",
},
{
name: "system-reminder with attributes",
input: "Before reminder text After",
expected: "Before After",
},
{
name: "multiple different tags",
input: "Start\ndone\nMiddle\nreminder\nEnd",
expected: "Start\n\nMiddle\n\nEnd",
},
{
name: "persisted-output block",
input: "Result: \nLarge output stored at: /tmp/foo\n done",
expected: "Result: done",
},
{
name: "available-deferred-tools block",
input: "\nTool1\nTool2\n\nUser message",
expected: "User message",
},
{
name: "fast_mode_info block",
input: "Text \nFast mode uses same model\n more text",
expected: "Text more text",
},
{
name: "preserves non-system XML",
input: "bugfixFixed thing",
expected: "bugfixFixed thing",
},
{
name: "no angle brackets fast path",
input: "Just plain text without any special characters",
expected: "Just plain text without any special characters",
},
{
name: "collapses triple newlines",
input: "Before\nx\n\n\nAfter",
expected: "Before\n\nAfter",
},
{
name: "real-world task notification",
input: "Here is the analysis:\n\na077fd04aed547ce1\ntoolu_01Kw2GwsArQQR1F9aV3VfzhP\ncompleted\nAgent \"Analyze agency-agents repo structure\" completed\nNow I have all the information I need. Let me compile a comprehensive report.\n\n## Agency-Agents Directory Exploration Report\n\n### 1. Agent File Count and Organization\n\n**Total Agent Files (excluding strategy docs):** 61 agents across 9 categories\n\nThe repo has 61 agents.",
expected: "Here is the analysis:\n\nThe repo has 61 agents.",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := StripSystemXML(tt.input)
assert.Equal(t, tt.expected, result)
})
}
}
func BenchmarkStripSystemXML(b *testing.B) {
// Text with no XML (fast path)
b.Run("no_xml", func(b *testing.B) {
text := "This is plain text without any XML tags at all."
b.ResetTimer()
for i := 0; i < b.N; i++ {
StripSystemXML(text)
}
})
// Text with system XML to strip
b.Run("with_xml", func(b *testing.B) {
text := "Before\n\nabc\ndone\n\nreminder text here\nAfter"
b.ResetTimer()
for i := 0; i < b.N; i++ {
StripSystemXML(text)
}
})
}