package scrubber
import (
"bytes"
"fmt"
"io"
"strings"
"testing"
)
// ── Basic functionality ─────────────────────────────────────────────────
func TestScrubber_NoMemoryBlock(t *testing.T) {
s := New(nil)
result := s.ProcessString("Hello, world!")
if result != "Hello, world!" {
t.Errorf("got %q, want %q", result, "Hello, world!")
}
if s.BlocksStripped() != 0 {
t.Errorf("BlocksStripped = %d, want 0", s.BlocksStripped())
}
}
func TestScrubber_SingleMemoryBlock(t *testing.T) {
s := New(nil)
result := s.ProcessString("Before secret After")
if result != "Before After" {
t.Errorf("got %q, want %q", result, "Before After")
}
if s.BlocksStripped() != 1 {
t.Errorf("BlocksStripped = %d, want 1", s.BlocksStripped())
}
}
func TestScrubber_MultipleMemoryBlocks(t *testing.T) {
s := New(nil)
result := s.ProcessString("A x B y C")
if result != "A B C" {
t.Errorf("got %q, want %q", result, "A B C")
}
if s.BlocksStripped() != 2 {
t.Errorf("BlocksStripped = %d, want 2", s.BlocksStripped())
}
}
func TestScrubber_EmptyContent(t *testing.T) {
s := New(nil)
result := s.ProcessString("")
if result != "" {
t.Errorf("got %q, want %q", result, "")
}
}
func TestScrubber_OnlyOpenTag(t *testing.T) {
// Missing close tag -- block is implicitly closed at end of stream.
s := New(nil)
result := s.ProcessString("Before stuff")
if result != "Before " {
t.Errorf("got %q, want %q", result, "Before ")
}
}
func TestScrubber_OnlyCloseTag(t *testing.T) {
// Stray close tag with no open -- treated as literal text.
s := New(nil)
result := s.ProcessString("text more")
if result != "text more" {
t.Errorf("got %q, want %q", result, "text more")
}
}
func TestScrubber_EmptyMemoryBlock(t *testing.T) {
s := New(nil)
result := s.ProcessString("beforeafter")
if result != "beforeafter" {
t.Errorf("got %q, want %q", result, "beforeafter")
}
}
func TestScrubber_OnlyMemoryBlock(t *testing.T) {
s := New(nil)
result := s.ProcessString("everything")
if result != "" {
t.Errorf("got %q, want %q", result, "")
}
}
// ── Nested tags ─────────────────────────────────────────────────────────
func TestScrubber_NestedMemoryBlocks(t *testing.T) {
s := New(nil)
result := s.ProcessString("A outer inner back B")
if result != "A B" {
t.Errorf("got %q, want %q", result, "A B")
}
if s.BlocksStripped() != 2 {
t.Errorf("BlocksStripped = %d, want 2", s.BlocksStripped())
}
}
func TestScrubber_DeeplyNestedMemoryBlocks(t *testing.T) {
s := New(nil)
result := s.ProcessString("12321")
if result != "" {
t.Errorf("got %q, want %q", result, "")
}
if s.BlocksStripped() != 3 {
t.Errorf("BlocksStripped = %d, want 3", s.BlocksStripped())
}
}
func TestScrubber_UnbalancedNested_MoreOpens(t *testing.T) {
// More open tags than close tags -- remaining blocks implicitly closed
// at end of stream. Everything after the first is stripped
// because the outer block never closes.
s := New(nil)
result := s.ProcessString("A outer inner B")
if result != "A " {
t.Errorf("got %q, want %q", result, "A ")
}
}
func TestScrubber_UnbalancedNested_MoreCloses(t *testing.T) {
// Extra close tags are literal text.
s := New(nil)
result := s.ProcessString("ab")
if result != "b" {
t.Errorf("got %q, want %q", result, "b")
}
}
// ── Partial chunk handling ──────────────────────────────────────────────
func TestScrubber_PartialOpenTag_SplitAtAngle(t *testing.T) {
// Open tag split right after '<'
var buf bytes.Buffer
s := New(&buf)
_, _ = s.Write([]byte("Hello <"))
_, _ = s.Write([]byte("memory>secret World"))
if err := s.Flush(); err != nil {
t.Fatalf("Flush error: %v", err)
}
result := buf.String()
if result != "Hello World" {
t.Errorf("got %q, want %q", result, "Hello World")
}
}
func TestScrubber_PartialOpenTag_SplitMidTag(t *testing.T) {
tests := []struct {
name string
chunk1 string
chunk2 string
expected string
}{
{name: "split after secret Bye", expected: "Hi Bye"},
{name: "split after secret Bye", expected: "Hi Bye"},
{name: "split after secret Bye", expected: "Hi Bye"},
{name: "split after secret Bye", expected: "Hi Bye"},
{name: "split after secret Bye", expected: "Hi Bye"},
{name: "split after secret Bye", expected: "Hi Bye"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
_, _ = s.Write([]byte(tt.chunk1))
_, _ = s.Write([]byte(tt.chunk2))
if err := s.Flush(); err != nil {
t.Fatalf("Flush error: %v", err)
}
if got := buf.String(); got != tt.expected {
t.Errorf("got %q, want %q", got, tt.expected)
}
})
}
}
func TestScrubber_PartialCloseTag_SplitMidTag(t *testing.T) {
tests := []struct {
name string
chunk1 string
chunk2 string
expected string
}{
{name: "close split after <", chunk1: "AB secret <", chunk2: "/memory> CD", expected: "AB CD"},
{name: "close split after ", chunk1: "AB secret ", chunk2: "memory> CD", expected: "AB CD"},
{name: "close split after secret CD", expected: "AB CD"},
{name: "close split after secret CD", expected: "AB CD"},
{name: "close split after secret CD", expected: "AB CD"},
{name: "close split after secret CD", expected: "AB CD"},
{name: "close split after secret CD", expected: "AB CD"},
{name: "close split after secret CD", expected: "AB CD"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
_, _ = s.Write([]byte(tt.chunk1))
_, _ = s.Write([]byte(tt.chunk2))
if err := s.Flush(); err != nil {
t.Fatalf("Flush error: %v", err)
}
if got := buf.String(); got != tt.expected {
t.Errorf("got %q, want %q", got, tt.expected)
}
})
}
}
func TestScrubber_PartialTag_NotActuallyATag(t *testing.T) {
tests := []struct {
name string
chunk1 string
chunk2 string
expected string
}{
{
name: "literal < followed by x", chunk1: "abc <", chunk2: "x not a tag> def",
expected: "abc def",
},
{
name: "literal def",
expected: "abc def",
},
{
name: "literal followed by x", chunk1: "abc ", chunk2: "x not a tag> def",
expected: "abc def",
},
{
name: "partial in memory block not a close tag",
chunk1: "content ", chunk2: "notmemory> more",
// "" is not "" -- it doesn't close the block.
// Everything inside the memory block is stripped until the real
// is found.
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
_, _ = s.Write([]byte(tt.chunk1))
_, _ = s.Write([]byte(tt.chunk2))
if err := s.Flush(); err != nil {
t.Fatalf("Flush error: %v", err)
}
if got := buf.String(); got != tt.expected {
t.Errorf("got %q, want %q", got, tt.expected)
}
})
}
}
func TestScrubber_LiteralAngleBrackets(t *testing.T) {
s := New(nil)
result := s.ProcessString("if (x < 5 && y > 3) { doStuff(); }")
if result != "if (x < 5 && y > 3) { doStuff(); }" {
t.Errorf("got %q, want %q", result, "if (x < 5 && y > 3) { doStuff(); }")
}
}
func TestScrubber_LiteralLtInMemoryBlock(t *testing.T) {
s := New(nil)
result := s.ProcessString("before if (x < 5) { } after")
if result != "before after" {
t.Errorf("got %q, want %q", result, "before after")
}
}
// ── Chunk boundary edge cases ──────────────────────────────────────────
func TestScrubber_OpenTagAtExactChunkBoundary(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
_, _ = s.Write([]byte("Hello "))
_, _ = s.Write([]byte("secret"))
_, _ = s.Write([]byte(" World"))
if err := s.Flush(); err != nil {
t.Fatalf("Flush error: %v", err)
}
if got := buf.String(); got != "Hello World" {
t.Errorf("got %q, want %q", got, "Hello World")
}
}
func TestScrubber_CloseTagAtExactChunkBoundary(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
_, _ = s.Write([]byte("Hello secret"))
_, _ = s.Write([]byte(" World"))
if err := s.Flush(); err != nil {
t.Fatalf("Flush error: %v", err)
}
if got := buf.String(); got != "Hello World" {
t.Errorf("got %q, want %q", got, "Hello World")
}
}
func TestScrubber_SingleByteChunks(t *testing.T) {
input := "abcdefghi"
var buf bytes.Buffer
s := New(&buf)
for i := 0; i < len(input); i++ {
_, _ = s.Write([]byte{input[i]})
}
if err := s.Flush(); err != nil {
t.Fatalf("Flush error: %v", err)
}
if got := buf.String(); got != "abcghi" {
t.Errorf("got %q, want %q", got, "abcghi")
}
}
func TestScrubber_TwoByteChunks(t *testing.T) {
input := "abc"
var buf bytes.Buffer
s := New(&buf)
for i := 0; i < len(input); i += 2 {
end := min(i+2, len(input))
_, _ = s.Write([]byte(input[i:end]))
}
if err := s.Flush(); err != nil {
t.Fatalf("Flush error: %v", err)
}
if got := buf.String(); got != "ac" {
t.Errorf("got %q, want %q", got, "ac")
}
}
func TestScrubber_ChunkEndingWithOpenTagStart(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
_, _ = s.Write([]byte("text <"))
_, _ = s.Write([]byte("notmemory> more"))
if err := s.Flush(); err != nil {
t.Fatalf("Flush error: %v", err)
}
if got := buf.String(); got != "text more" {
t.Errorf("got %q, want %q", got, "text more")
}
}
// ── Streaming Write interface ──────────────────────────────────────────
func TestScrubber_Write_ReturnsBytesConsumed(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
input := []byte("hello")
n, err := s.Write(input)
if err != nil {
t.Fatalf("Write error: %v", err)
}
if n != len(input) {
t.Errorf("Write returned n=%d, want %d", n, len(input))
}
}
func TestScrubber_Write_EmptyChunk(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
n, err := s.Write(nil)
if err != nil {
t.Fatalf("Write(nil) error: %v", err)
}
if n != 0 {
t.Errorf("Write(nil) returned n=%d, want 0", n)
}
}
func TestScrubber_Flush_EmptyState(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
// Flush with no writes should be a no-op.
if err := s.Flush(); err != nil {
t.Fatalf("Flush error: %v", err)
}
if buf.Len() != 0 {
t.Errorf("buf has %d bytes after empty flush, want 0", buf.Len())
}
}
// ── Process / ProcessString ────────────────────────────────────────────
func TestScrubber_Process_StreamingEquivalence(t *testing.T) {
input := "A x B y C"
// ProcessString (one-shot)
s1 := New(nil)
oneShot := s1.ProcessString(input)
// Process with chunking (streaming)
s2 := New(nil)
var streamingResult strings.Builder
chunks := []string{"A x B ", "y C"}
for _, ch := range chunks {
streamingResult.WriteString(s2.Process(ch))
}
_ = s2.Flush()
streamingResult.WriteString(s2.Process("")) // drain any final output
if streamingResult.String() != oneShot {
t.Errorf("streaming result %q != one-shot %q", streamingResult.String(), oneShot)
}
}
func TestScrubber_Process_ReturnsOnlyNewOutput(t *testing.T) {
s := New(nil)
r1 := s.Process("hello secret world")
r3 := s.Process("")
if r1 != "hello " {
t.Errorf("first Process = %q, want %q", r1, "hello ")
}
if r2 != " world" {
t.Errorf("second Process = %q, want %q", r2, " world")
}
if r3 != "" {
t.Errorf("third Process = %q, want %q", r3, "")
}
}
func TestScrubber_ProcessString_CompleteInput(t *testing.T) {
s := New(nil)
r1 := s.ProcessString("A x B")
r2 := s.ProcessString("C y D")
if r1 != "A B" {
t.Errorf("first = %q, want %q", r1, "A B")
}
if r2 != "C D" {
t.Errorf("second = %q, want %q", r2, "C D")
}
}
// ── Reset ──────────────────────────────────────────────────────────────
func TestScrubber_Reset_MidStream(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
_, _ = s.Write([]byte("A secret"))
if s.InMemoryBlock() == false {
t.Error("expected to be in memory block")
}
s.Reset()
buf.Reset()
if s.InMemoryBlock() {
t.Error("after Reset, should not be in memory block")
}
if s.BlocksStripped() != 0 {
t.Errorf("BlocksStripped = %d after Reset, want 0", s.BlocksStripped())
}
_, _ = s.Write([]byte("clean gone text"))
_ = s.Flush()
if got := buf.String(); got != "clean text" {
t.Errorf("after Reset: got %q, want %q", got, "clean text")
}
}
// ── Stats tracking ─────────────────────────────────────────────────────
func TestScrubber_Stats(t *testing.T) {
s := New(nil)
s.ProcessString("x abcdef y ghijk z")
if s.BlocksStripped() != 2 {
t.Errorf("BlocksStripped = %d, want 2", s.BlocksStripped())
}
// Tags: = 8, = 9 each → 17*2 = 34
// Content: "abcdef" = 6, "ghijk" = 5 → 11
// Total stripped: 45
expectedBytes := int64(45)
if s.BytesStripped() != expectedBytes {
t.Errorf("BytesStripped = %d, want %d", s.BytesStripped(), expectedBytes)
}
}
func TestScrubber_Stats_Reset(t *testing.T) {
s := New(nil)
s.ProcessString("a")
if s.BlocksStripped() == 0 {
t.Fatal("expected blocks to be stripped")
}
s.Reset()
if s.BlocksStripped() != 0 {
t.Errorf("BlocksStripped = %d after Reset, want 0", s.BlocksStripped())
}
if s.BytesStripped() != 0 {
t.Errorf("BytesStripped = %d after Reset, want 0", s.BytesStripped())
}
}
// ── InMemoryBlock / Depth ──────────────────────────────────────────────
func TestScrubber_InMemoryBlock(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
if s.InMemoryBlock() {
t.Error("InMemoryBlock should be false initially")
}
_, _ = s.Write([]byte("before "))
if !s.InMemoryBlock() {
t.Error("InMemoryBlock should be true after open tag")
}
_, _ = s.Write([]byte("content after"))
if s.InMemoryBlock() {
t.Error("InMemoryBlock should be false after close tag")
}
}
func TestScrubber_Depth(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
if s.Depth() != 0 {
t.Errorf("Depth = %d initially, want 0", s.Depth())
}
_, _ = s.Write([]byte(""))
if s.Depth() != 1 {
t.Errorf("Depth = %d after first open, want 1", s.Depth())
}
_, _ = s.Write([]byte(""))
if s.Depth() != 2 {
t.Errorf("Depth = %d after second open, want 2", s.Depth())
}
_, _ = s.Write([]byte(""))
if s.Depth() != 1 {
t.Errorf("Depth = %d after first close, want 1", s.Depth())
}
_, _ = s.Write([]byte(""))
if s.Depth() != 0 {
t.Errorf("Depth = %d after second close, want 0", s.Depth())
}
}
// ── Nil writer ─────────────────────────────────────────────────────────
func TestScrubber_NilWriter(t *testing.T) {
s := New(nil)
input := []byte("some hidden text")
n, err := s.Write(input)
if err != nil {
t.Fatalf("Write error with nil writer: %v", err)
}
if n != len(input) {
t.Errorf("Write returned n=%d, want %d", n, len(input))
}
if err := s.Flush(); err != nil {
t.Fatalf("Flush error with nil writer: %v", err)
}
}
// ── IO Writer integration ──────────────────────────────────────────────
func TestScrubber_IOWriter_Chain(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
_, err := io.WriteString(s, "Hello hidden World")
if err != nil {
t.Fatalf("io.WriteString error: %v", err)
}
_ = s.Flush()
if got := buf.String(); got != "Hello World" {
t.Errorf("got %q, want %q", got, "Hello World")
}
}
// ── Complex scenarios ──────────────────────────────────────────────────
func TestScrubber_MemoryBlockAcrossManyChunks(t *testing.T) {
content := "This is a very long memory block with lots of content " +
"that should be stripped. " + strings.Repeat("blah ", 200)
input := "visible_start" + content + "visible_end"
var buf bytes.Buffer
s := New(&buf)
for i := 0; i < len(input); i += 3 {
end := min(i+3, len(input))
_, _ = s.Write([]byte(input[i:end]))
}
_ = s.Flush()
expected := "visible_startvisible_end"
if got := buf.String(); got != expected {
t.Errorf("got %q (len=%d), want %q (len=%d)", got, len(got), expected, len(expected))
}
}
func TestScrubber_ConsecutivePartialTags(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
_, _ = s.Write([]byte("a <")) // partial
_, _ = s.Write([]byte("m")) // still partial
_, _ = s.Write([]byte("emory>")) // complete!
_, _ = s.Write([]byte("hidden"))
_, _ = s.Write([]byte("")) // partial close
_, _ = s.Write([]byte("memory>")) // complete close
_, _ = s.Write([]byte("b"))
_ = s.Flush()
if got := buf.String(); got != "a b" {
t.Errorf("got %q, want %q", got, "a b")
}
}
func TestScrubber_MemoryLikeButNotExactly(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{name: "memory without angle brackets", input: "memory and /memory are not tags", want: "memory and /memory are not tags"},
{name: "angle bracket but wrong tag name", input: "use here", want: "use here"},
{name: "unclosed angle bracket", input: "the more text", want: "text more text"},
{name: "back-to-back memory blocks", input: "AxyB", want: "AB"},
{name: "memory block at start", input: "xB", want: "B"},
{name: "memory block at end", input: "Ax", want: "A"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := New(nil)
got := s.ProcessString(tt.input)
if got != tt.want {
t.Errorf("got %q, want %q", got, tt.want)
}
})
}
}
func TestScrubber_MalformedNested(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{name: "interleaved close tags", input: "ab", want: "b"},
{name: "open after close", input: "b", want: ""},
{name: "deeply nested with missing close", input: "ABCD", want: "A"},
{name: "partial nest resolution", input: "ABCDE", want: "AE"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := New(nil)
got := s.ProcessString(tt.input)
if got != tt.want {
t.Errorf("got %q, want %q", got, tt.want)
}
})
}
}
// ── UTF-8 / multibyte handling ─────────────────────────────────────────
func TestScrubber_UnicodeContent(t *testing.T) {
s := New(nil)
result := s.ProcessString("BEFORE こんにちは世界 AFTER")
if result != "BEFORE AFTER" {
t.Errorf("got %q, want %q", result, "BEFORE AFTER")
}
}
func TestScrubber_UnicodeOutsideMemoryBlock(t *testing.T) {
s := New(nil)
result := s.ProcessString("👋 hello secret 🌍 world")
if result != "👋 hello 🌍 world" {
t.Errorf("got %q, want %q", result, "👋 hello 🌍 world")
}
}
func TestScrubber_UnicodeSplitAcrossChunks(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
_, _ = s.Write([]byte("Hi ")) // completes the open tag
_, _ = s.Write([]byte("世界 Bye"))
_ = s.Flush()
if got := buf.String(); got != "Hi Bye" {
t.Errorf("got %q, want %q", got, "Hi Bye")
}
}
// ── Large input ─────────────────────────────────────────────────────────
func TestScrubber_LargeInput(t *testing.T) {
var input strings.Builder
var expected strings.Builder
input.WriteString("START ")
expected.WriteString("START ")
for i := range 100 {
fmt.Fprintf(&input, " visible_%d ", i)
fmt.Fprintf(&expected, " visible_%d ", i)
input.WriteString("")
fmt.Fprintf(&input, " hidden_block_%d ", i)
input.WriteString("")
}
input.WriteString(" END")
expected.WriteString(" END")
s := New(nil)
got := s.ProcessString(input.String())
want := expected.String()
if got != want {
t.Errorf("large output mismatch (got len=%d, want len=%d)", len(got), len(want))
}
}
// ── Regression: edge cases ─────────────────────────────────────────────
func TestScrubber_Regression_DoubleChevron(t *testing.T) {
s := New(nil)
result := s.ProcessString("a <hidden b")
if result != "a < b" {
t.Errorf("got %q, want %q", result, "a < b")
}
}
func TestScrubber_Regression_TagInTag(t *testing.T) {
s := New(nil)
result := s.ProcessString("x >test y")
if result != "x abc<", expected: ""},
{name: "pending < only inside memory block", input: "<", expected: ""},
{name: "pending inside memory block", input: "abc", expected: ""},
{name: "pending abcabc<", expected: "hi"},
{name: "nested with pending inside", input: "ABC<", expected: "A"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
s := New(&buf)
_, _ = s.Write([]byte(tt.input))
if err := s.Flush(); err != nil {
t.Fatalf("Flush error: %v", err)
}
if got := buf.String(); got != tt.expected {
t.Errorf("got %q, want %q", got, tt.expected)
}
})
}
}
func TestScrubber_Flush_PendingInMemoryBlock_StatsCorrect(t *testing.T) {
// Finding 3: BytesStripped should count pending bytes that are stripped
// on Flush inside a memory block.
var buf bytes.Buffer
s := New(&buf)
_, _ = s.Write([]byte("abc<"))
// At this point: bytesStripped = 8 () + 3 (abc) = 11, pending = "<"
if err := s.Flush(); err != nil {
t.Fatalf("Flush error: %v", err)
}
// After Flush: pending "<" should be counted as stripped
// Total: 8 + 3 + 1 = 12
expectedBytes := int64(12)
if s.BytesStripped() != expectedBytes {
t.Errorf("BytesStripped = %d, want %d", s.BytesStripped(), expectedBytes)
}
// No output should be emitted
if buf.Len() != 0 {
t.Errorf("output = %q, want empty", buf.String())
}
}
func TestScrubber_Flush_PendingInMemoryBlock_TwoChunks(t *testing.T) {
// Finding 1 two-chunk variant: first chunk ends with pending inside
// memory block, second chunk is empty.
var buf bytes.Buffer
s := New(&buf)
_, _ = s.Write([]byte("xyz<"))
_, _ = s.Write([]byte("")) // empty chunk
if err := s.Flush(); err != nil {
t.Fatalf("Flush error: %v", err)
}
if got := buf.String(); got != "" {
t.Errorf("got %q, want %q", got, "")
}
if s.BytesStripped() == 0 {
t.Error("BytesStripped should include both the open tag and the stripped content")
}
}
func TestScrubber_Flush_PendingOutsideMemoryBlock(t *testing.T) {
// Pending bytes OUTSIDE a memory block should still be emitted.
// This is the normal case for a partial tag that turns out not to be a tag.
var buf bytes.Buffer
s := New(&buf)
_, _ = s.Write([]byte("hello xyz<"))
_ = s.Flush()
if got := buf.String(); got != "A " {
t.Errorf("got %q, want %q", got, "A ")
}
// bytesStripped: open tag (8) + "xyz" (3) + pending "<" (1) = 12
if s.BytesStripped() != 12 {
t.Errorf("BytesStripped = %d, want 12", s.BytesStripped())
}
}
func TestScrubber_Write_ZeroLengthChunkWithPending(t *testing.T) {
// Zero-length write while pending is non-empty should leave pending
// untouched (no crash, no side effects).
var buf bytes.Buffer
s := New(&buf)
// Write text ending with a partial tag prefix.
_, _ = s.Write([]byte("data " so it should pass through.
_, _ = s.Write([]byte("x abc"))
_ = s.Flush()
if got := buf.String(); got != "data test"))
ds := s.DebugString()
if !strings.Contains(ds, "depth=1") {
t.Errorf("DebugString should contain depth=1: %s", ds)
}
}
// ── Benchmarks ─────────────────────────────────────────────────────────
func BenchmarkScrubber_NoMemoryBlock(b *testing.B) {
s := New(io.Discard)
input := []byte("This is normal text without any memory blocks at all.")
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
_, _ = s.Write(input)
}
_ = s.Flush()
}
func BenchmarkScrubber_SingleMemoryBlock(b *testing.B) {
s := New(io.Discard)
input := []byte("Before hidden content here After")
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
s.Reset()
_, _ = s.Write(input)
_ = s.Flush()
}
}
func BenchmarkScrubber_ManySmallChunks(b *testing.B) {
text := "This is a long text with some hidden content that should be stripped and more text here"
var chunks [][]byte
for i := 0; i < len(text); i += 3 {
end := min(i+3, len(text))
chunks = append(chunks, []byte(text[i:end]))
}
s := New(io.Discard)
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
s.Reset()
for _, ch := range chunks {
_, _ = s.Write(ch)
}
_ = s.Flush()
}
}
func BenchmarkScrubber_ProcessString(b *testing.B) {
s := New(nil)
input := "Before hidden content here After"
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
s.ProcessString(input)
}
}
func BenchmarkScrubber_LargeInputManyBlocks(b *testing.B) {
var input strings.Builder
for i := range 100 {
fmt.Fprintf(&input, "visible_%d ", i)
input.WriteString("")
fmt.Fprintf(&input, "hidden_%d ", i)
input.WriteString("")
}
data := []byte(input.String())
s := New(io.Discard)
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
s.Reset()
_, _ = s.Write(data)
_ = s.Flush()
}
}
func BenchmarkScrubber_NestedBlocks(b *testing.B) {
s := New(io.Discard)
input := []byte("A B C DEFG")
b.ResetTimer()
b.ReportAllocs()
for b.Loop() {
s.Reset()
_, _ = s.Write(input)
_ = s.Flush()
}
}