package routing_test import ( "testing" "github.com/mathiasbq/supervisor/internal/routing" "github.com/stretchr/testify/assert" ) func TestCanonicalHashDeterministic(t *testing.T) { a := routing.CanonicalHash("system one", "user one") b := routing.CanonicalHash("system one", "user one") assert.Equal(t, a, b, "same inputs must produce same hash") } func TestCanonicalHashDistinguishesInputs(t *testing.T) { cases := [][2]string{ {"sys", "user"}, {"sys", "user2"}, {"sys2", "user"}, {"", "system\x00user"}, // separator collision attempt {"system\x00user", ""}, } seen := make(map[uint64]bool) for _, c := range cases { h := routing.CanonicalHash(c[0], c[1]) assert.False(t, seen[h], "collision on %v", c) seen[h] = true } } func TestCanonicalHashLowBitDistribution(t *testing.T) { // Sanity check: across 1000 distinct inputs, low-bit split is roughly even. zeros, ones := 0, 0 for i := 0; i < 1000; i++ { h := routing.CanonicalHash("sys", string(rune('a'+(i%26)))+string(rune(i))) if h&1 == 0 { zeros++ } else { ones++ } } // Allow ±15% deviation from 500/500. Tighter would be flaky on real data. assert.InDelta(t, 500, zeros, 150) assert.InDelta(t, 500, ones, 150) }