package privacy import ( "testing" "github.com/stretchr/testify/assert" ) func TestStripPrivateTags(t *testing.T) { tests := []struct { name string input string expected string }{ { name: "no tags", input: "Hello world", expected: "Hello world", }, { name: "single private tag", input: "Hello secret world", expected: "Hello world", }, { name: "multiple private tags", input: "Hello secret1 and secret2 world", expected: "Hello and world", }, { name: "nested content in private tag", input: "Hello secret with\nnewline world", expected: "Hello world", }, { name: "multiline private tag", input: "Hello \nmultiline\nsecret\n world", expected: "Hello world", }, { name: "empty private tag", input: "Hello world", expected: "Hello world", }, { name: "entirely private", input: "everything is secret", expected: "", }, { name: "unmatched opening tag", input: "Hello unclosed", expected: "Hello unclosed", }, { name: "unmatched closing tag", input: "Hello world", expected: "Hello world", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := StripPrivateTags(tt.input) assert.Equal(t, tt.expected, result) }) } } func TestStripMemoryTags(t *testing.T) { tests := []struct { name string input string expected string }{ { name: "no tags", input: "Hello world", expected: "Hello world", }, { name: "single memory tag", input: "Hello memory world", expected: "Hello world", }, { name: "multiline memory tag", input: "Hello \nmemory\ncontent\n world", expected: "Hello world", }, { name: "entirely memory context", input: "all memory", expected: "", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := StripMemoryTags(tt.input) assert.Equal(t, tt.expected, result) }) } } func TestStripAllTags(t *testing.T) { tests := []struct { name string input string expected string }{ { name: "no tags", input: "Hello world", expected: "Hello world", }, { name: "both tag types", input: "Hello secret and memory world", expected: "Hello and world", }, { name: "interleaved tags", input: "A B C D E", expected: "A C E", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := StripAllTags(tt.input) assert.Equal(t, tt.expected, result) }) } } func TestIsEntirelyPrivate(t *testing.T) { tests := []struct { name string input string expected bool }{ { name: "not private", input: "Hello world", expected: false, }, { name: "entirely private", input: "secret", expected: true, }, { name: "entirely private with whitespace", input: " secret ", expected: true, }, { name: "partially private", input: "Hello secret", expected: false, }, { name: "multiple private tags covering everything", input: "ab", expected: true, }, { name: "empty string", input: "", expected: true, // Empty after stripping means nothing remains }, { name: "only whitespace", input: " ", expected: true, // Whitespace-only after stripping is empty }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := IsEntirelyPrivate(tt.input) assert.Equal(t, tt.expected, result) }) } } func TestClean(t *testing.T) { tests := []struct { name string input string expected string }{ { name: "no tags or whitespace", input: "Hello world", expected: "Hello world", }, { name: "strips private tags and trims", input: " Hello secret world ", expected: "Hello world", }, { name: "strips memory tags and trims", input: " Hello memory world ", expected: "Hello world", }, { name: "strips both tag types and trims", input: "\n Hello secret and memory world \n", expected: "Hello and world", }, { name: "entirely stripped content", input: " secret ", expected: "", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := Clean(tt.input) assert.Equal(t, tt.expected, result) }) } } // Edge cases and security-related tests func TestPrivacyEdgeCases(t *testing.T) { t.Run("nested tags are handled correctly", func(t *testing.T) { // Inner tag should be stripped as part of outer content input := "outer inner outer" result := StripPrivateTags(input) // The regex is non-greedy, so it matches the first closing tag assert.Equal(t, " outer", result) }) t.Run("html-like content is not confused with tags", func(t *testing.T) { input := "Hello
world
" result := StripPrivateTags(input) assert.Equal(t, "Hello
world
", result) }) t.Run("case sensitive tags", func(t *testing.T) { input := "Hello secret world" result := StripPrivateTags(input) // Should not strip uppercase tags assert.Equal(t, "Hello secret world", result) }) t.Run("special characters in private content", func(t *testing.T) { input := "Hello secret$%^&*() world" result := StripPrivateTags(input) assert.Equal(t, "Hello world", result) }) t.Run("unicode content", func(t *testing.T) { input := "Hello 秘密 🔒 world" result := StripPrivateTags(input) assert.Equal(t, "Hello world", result) }) t.Run("very long private content", func(t *testing.T) { longSecret := "" for i := 0; i < 10000; i++ { longSecret += "x" } input := "Hello " + longSecret + " world" result := StripPrivateTags(input) assert.Equal(t, "Hello world", result) }) }