defmodule Berrypod.Analytics.SaltTest do
  use ExUnit.Case, async: true

  alias Berrypod.Analytics.Salt

  describe "hash_visitor/2" do
    test "returns an 8-byte binary" do
      hash = Salt.hash_visitor({127, 0, 0, 1}, "Mozilla/5.0")
      assert is_binary(hash)
      assert byte_size(hash) == 8
    end

    test "same inputs produce the same hash" do
      hash1 = Salt.hash_visitor({127, 0, 0, 1}, "Mozilla/5.0")
      hash2 = Salt.hash_visitor({127, 0, 0, 1}, "Mozilla/5.0")
      assert hash1 == hash2
    end

    test "different IPs produce different hashes" do
      hash1 = Salt.hash_visitor({127, 0, 0, 1}, "Mozilla/5.0")
      hash2 = Salt.hash_visitor({192, 168, 1, 1}, "Mozilla/5.0")
      assert hash1 != hash2
    end

    test "different user agents produce different hashes" do
      hash1 = Salt.hash_visitor({127, 0, 0, 1}, "Mozilla/5.0 Chrome")
      hash2 = Salt.hash_visitor({127, 0, 0, 1}, "Mozilla/5.0 Firefox")
      assert hash1 != hash2
    end

    test "accepts IP as a binary string" do
      hash1 = Salt.hash_visitor("127.0.0.1", "Mozilla/5.0")
      hash2 = Salt.hash_visitor({127, 0, 0, 1}, "Mozilla/5.0")
      assert hash1 == hash2
    end
  end
end
