package dedup import ( "testing" "time" ) func TestShouldIgnoreOwnOrigin(t *testing.T) { f := NewFilter("machine-a") p := Payload{ Text: "hello", Ts: time.Now().UnixNano(), Origin: "machine-a", // same as our origin } if !f.ShouldIgnore(p) { t.Error("should ignore payload from own origin") } } func TestShouldIgnoreStaleTimestamp(t *testing.T) { f := NewFilter("machine-a") f.MarkWritten("previous text") // Wait a tiny bit so the timestamp is definitely in the past. time.Sleep(time.Millisecond) p := Payload{ Text: "hello", Ts: f.LastWriteTs() - 1, // older than our last write Origin: "machine-b", } if !f.ShouldIgnore(p) { t.Error("should ignore payload with stale timestamp") } } func TestShouldIgnoreDuplicateHash(t *testing.T) { f := NewFilter("machine-a") f.MarkWritten("hello world") p := Payload{ Text: "hello world", Ts: time.Now().UnixNano(), Origin: "machine-b", } if !f.ShouldIgnore(p) { t.Error("should ignore payload with recently written hash") } } func TestShouldAcceptNewContent(t *testing.T) { f := NewFilter("machine-a") f.MarkWritten("old content") time.Sleep(time.Millisecond) p := Payload{ Text: "new content", Ts: time.Now().UnixNano(), Origin: "machine-b", } if f.ShouldIgnore(p) { t.Error("should accept new content from other origin") } } func TestShouldIgnoreDuplicateImageHash(t *testing.T) { f := NewFilter("machine-a") f.MarkWrittenPayload(Payload{Mime: "image/png", Data: "aGVsbG8="}) p := Payload{ Mime: "image/png", Data: "aGVsbG8=", Ts: time.Now().UnixNano(), Origin: "machine-b", } if !f.ShouldIgnore(p) { t.Error("should ignore image payload with recently written data hash") } } func TestCacheEviction(t *testing.T) { f := NewFilter("machine-a") // Fill the cache with 20 different texts. for i := 0; i < defaultCacheSize; i++ { f.MarkWritten("text") } // The latest write is "text" — it should be in cache. p := Payload{ Text: "text", Ts: time.Now().UnixNano(), Origin: "machine-b", } if !f.ShouldIgnore(p) { t.Error("'text' should still be in cache after 20 writes") } } func TestMarkWrittenUpdatesTimestamp(t *testing.T) { f := NewFilter("machine-a") before := f.LastWriteTs() if before != 0 { t.Errorf("initial LastWriteTs = %d, want 0", before) } time.Sleep(time.Millisecond) f.MarkWritten("some text") after := f.LastWriteTs() if after <= before { t.Errorf("LastWriteTs not updated: %d <= %d", after, before) } }