# frozen_string_literal: true

require "test_helper"

class HubLoadTest < ActiveSupport::TestCase
  test "normal load level with low queue depth" do
    HubLoad.stub :queue_depth, 500 do
      assert_equal :normal, HubLoad.calculate_load_level
    end
  end

  test "moderate load level with moderate queue depth" do
    HubLoad.stub :queue_depth, 3000 do
      assert_equal :moderate, HubLoad.calculate_load_level
    end
  end

  test "high load level with high queue depth" do
    HubLoad.stub :queue_depth, 7500 do
      assert_equal :high, HubLoad.calculate_load_level
    end
  end

  test "critical load level with very high queue depth" do
    HubLoad.stub :queue_depth, 15000 do
      assert_equal :critical, HubLoad.calculate_load_level
    end
  end

  test "current_sampling returns correct rates for normal load" do
    HubLoad.stub :queue_depth, 500 do
      sampling = HubLoad.current_sampling

      assert_equal 1.0, sampling[:allowed_requests]
      assert_equal 1.0, sampling[:blocked_requests]
      assert_equal 1.0, sampling[:rate_limited_requests]
      assert_equal :normal, sampling[:load_level]
      assert_equal 500, sampling[:queue_depth]
      assert sampling[:effective_until].present?
    end
  end

  test "current_sampling reduces allowed requests under moderate load" do
    HubLoad.stub :queue_depth, 3000 do
      sampling = HubLoad.current_sampling

      assert_equal 0.5, sampling[:allowed_requests]
      assert_equal 1.0, sampling[:blocked_requests]
      assert_equal 1.0, sampling[:rate_limited_requests]
      assert_equal :moderate, sampling[:load_level]
    end
  end

  test "current_sampling reduces allowed requests under high load" do
    HubLoad.stub :queue_depth, 7500 do
      sampling = HubLoad.current_sampling

      assert_equal 0.2, sampling[:allowed_requests]
      assert_equal 1.0, sampling[:blocked_requests]
      assert_equal 1.0, sampling[:rate_limited_requests]
      assert_equal :high, sampling[:load_level]
    end
  end

  test "current_sampling minimizes allowed requests under critical load" do
    HubLoad.stub :queue_depth, 15000 do
      sampling = HubLoad.current_sampling

      assert_equal 0.05, sampling[:allowed_requests]
      assert_equal 1.0, sampling[:blocked_requests]
      assert_equal 1.0, sampling[:rate_limited_requests]
      assert_equal :critical, sampling[:load_level]
    end
  end

  test "effective_until is approximately 10 seconds in future" do
    sampling = HubLoad.current_sampling
    effective_until = Time.parse(sampling[:effective_until])

    time_diff = effective_until - Time.current
    assert time_diff > 9, "effective_until should be ~10 seconds in future"
    assert time_diff < 11, "effective_until should be ~10 seconds in future"
  end

  test "overloaded? returns false for normal and moderate load" do
    HubLoad.stub :queue_depth, 500 do
      assert_not HubLoad.overloaded?
    end

    HubLoad.stub :queue_depth, 3000 do
      assert_not HubLoad.overloaded?
    end
  end

  test "overloaded? returns true for high and critical load" do
    HubLoad.stub :queue_depth, 7500 do
      assert HubLoad.overloaded?
    end

    HubLoad.stub :queue_depth, 15000 do
      assert HubLoad.overloaded?
    end
  end

  test "stats returns complete load information" do
    HubLoad.stub :queue_depth, 3000 do
      stats = HubLoad.stats

      assert_equal 3000, stats[:queue_depth]
      assert_equal :moderate, stats[:load_level]
      assert_equal false, stats[:overloaded]
      assert_equal 0.5, stats[:sampling_rates][:allowed]
      assert_equal 1.0, stats[:sampling_rates][:blocked]
    end
  end

  test "handles queue depth query errors gracefully" do
    # Simulate SolidQueue error
    SolidQueue::Job.stub :where, -> (*) { raise StandardError, "DB error" } do
      depth = HubLoad.queue_depth
      assert_equal 0, depth # Should return 0 on error
    end
  end
end
