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

  alias Berrypod.Analytics.UAParser

  describe "parse/1" do
    test "detects Chrome on macOS" do
      ua =
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"

      assert UAParser.parse(ua) == {"Chrome", "macOS"}
    end

    test "detects Firefox on Windows" do
      ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0"
      assert UAParser.parse(ua) == {"Firefox", "Windows"}
    end

    test "detects Safari on iOS (iPhone)" do
      ua =
        "Mozilla/5.0 (iPhone; CPU iPhone OS 17_2_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1"

      assert UAParser.parse(ua) == {"Safari", "iOS"}
    end

    test "detects Chrome on Android" do
      ua =
        "Mozilla/5.0 (Linux; Android 14; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36"

      assert UAParser.parse(ua) == {"Chrome", "Android"}
    end

    test "detects Edge on Windows" do
      ua =
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0"

      assert UAParser.parse(ua) == {"Edge", "Windows"}
    end

    test "detects Chrome on Linux" do
      ua =
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"

      assert UAParser.parse(ua) == {"Chrome", "Linux"}
    end

    test "detects bots" do
      ua = "Googlebot/2.1 (+http://www.google.com/bot.html)"
      assert UAParser.parse(ua) == {"Bot", "Other"}
    end

    test "detects Bingbot" do
      ua =
        "Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)"

      assert UAParser.parse(ua) == {"Bot", "Other"}
    end

    test "returns Other/Other for nil" do
      assert UAParser.parse(nil) == {"Other", "Other"}
    end

    test "returns Other/Other for empty string" do
      assert UAParser.parse("") == {"Other", "Other"}
    end

    test "detects Opera" do
      ua =
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 OPR/106.0.0.0"

      assert UAParser.parse(ua) == {"Opera", "Windows"}
    end

    test "detects Safari on iPad (iOS)" do
      ua =
        "Mozilla/5.0 (iPad; CPU OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1"

      assert UAParser.parse(ua) == {"Safari", "iOS"}
    end
  end
end
