<commit_before># frozen_string_literal: true

require "test_helper"

module SrcsetTest
  RESOLUTIONS = [
    100, 116, 135, 156, 181, 210, 244, 283,
    328, 380, 441, 512, 594, 689, 799, 927,
    1075, 1247, 1446, 1678, 1946, 2257, 2619,
    3038, 3524, 4087, 4741, 5500, 6380, 7401, 8192
  ].freeze

  DPR_QUALITY = [75, 50, 35, 23, 20].freeze
  DPR_MULTIPLIER = ["1x", "2x", "3x", "4x", "5x", ].freeze

  DOMAIN = "testing.imgix.net"
  TOKEN = "<PASSWORD>"
  JPG_PATH = "image.jpg"

  def mock_client
    Imgix::Client.new(
      domain: DOMAIN,
      include_library_param: false
    ).path(JPG_PATH)
  end

  def mock_signed_client
    Imgix::Client.new(
      domain: DOMAIN,
      secure_url_token: TOKEN,
      include_library_param: false
    ).path(JPG_PATH)
  end

  def signature_base(params)
    TOKEN + "/" + JPG_PATH + params
  end

  def get_sig_from(src)
    src.slice(src.index("s=") + 2, src.length)
  end

  def get_params_from(src)
    src[src.index("?")..src.index("s=") - 2]
  end

  def get_expected_signature(src)
    # Ensure signature param exists.
    assert_includes src, "s="

    params = get_params_from(src)
    signature_base = signature_base(params)

    Digest::MD5.hexdigest(signature_base)
  end

  class SrcsetDefault < Imgix::Test
    include SrcsetTest

    def test_no_parameters
      srcset = path.to_srcset

      expected_number_of_pairs = 31
      assert_equal expected_number_of_pairs, srcset.split(",").length
    end

    def test_srcset_pair_values
      resolutions = RESOLUTIONS
      srcset = path.to_srcset
      srclist = srcset.split(",").map do |srcset_split|
        srcset_split.split(" ")[1].to_i
      end

      (0..srclist.length - 1).each do |i|
        assert_equal(srclist[i], resolutions[i])
      end
    end

    private

    def path
      @client ||= mock_signed_client
    end
  end

  class SrcsetGivenWidth < Imgix::Test
    include SrcsetTest

    def test_srcset_in_dpr_form
      device_pixel_ratio = 1

      srcset.split(",").map do |src|
        ratio = src.split(" ")[1]
        assert_equal "#{device_pixel_ratio}x", ratio
        device_pixel_ratio += 1
      end
    end

    def test_srcset_has_dpr_params
      i = 1
      srcset.split(",").map do |srcset_split|
        src = srcset_split.split(" ")[0]
        assert_includes src, "dpr=#{i}"
        i += 1
      end
    end

    def test_srcset_signs_urls
      srcset.split(",").map do |srcset_split|
        src = srcset_split.split(" ")[0]
        expected_signature = get_expected_signature(src)

        assert_includes src, expected_signature
      end
    end

    def test_srcset_has_variable_qualities
      i = 0
      srcset.split(",").map do |src|
        assert_includes src, "q=#{DPR_QUALITY[i]}"
        i += 1
      end
    end

    def test_srcset_respects_overriding_quality
      quality_override = 100
      srcset = mock_signed_client.to_srcset(w: 100, q: quality_override)

      srcset.split(",").map do |src|
        assert_includes src, "q=#{quality_override}"
      end
    end

    def test_disable_variable_quality
      srcset = mock_signed_client.to_srcset(
        w: 100,
        options: { disable_variable_quality: true }
      )

      srcset.split(",").map do |src|
        assert(!src.include?("q="))
      end
    end

    def test_respects_quality_param_when_disabled
      quality_override = 100
      srcset = mock_signed_client.to_srcset(
        w: 100, q: 100,
        options: { disable_variable_quality: true }
      )

      srcset.split(",").map do |src|
        assert_includes src, "q=#{quality_override}"
      end
    end

    private

    def srcset
      @client ||= mock_signed_client.to_srcset(w: 100)
    end
  end

  class SrcsetGivenHeight < Imgix::Test
    include SrcsetTest

    def test_srcset_generates_width_pairs
      expected_number_of_pairs = 5
      assert_equal expected_number_of_pairs, srcset.split(",").length
    end

    def test_srcset_pair_values
      resolutions = DPR_MULTIPLIER
      srclist = srcset.split(",").map do |srcset_split|
        srcset_split.split(" ")[1].to_i
      end

      (0..srclist.length - 1).each do |i|
        assert_equal(resolutions[i], srclist[i])
      end
    end

    def test_srcset_respects_height_parameter
      srcset.split(",").map do |src|
        assert_includes src, "h="
      end
    end

    def test_srcset_within_bounds
      min, *max = srcset.split(",")

      # parse out the width descriptor as an integer
      min = min.split(" ")[1].to_i
      max = max[max.length - 1].split(" ")[1].to_i

      assert_operator min, :>=, 100
      assert_operator max, :<=, 8192
    end

    # a 17% testing threshold is used to account for rounding
    def test_srcset_iterates_17_percent
      increment_allowed = 0.17

      # create an array of widths
      widths = srcset.split(",").map do |src|
        src.split(" ")[1].to_i<commit_msg>test: add test_srcset_includes_dpr_param<commit_after># frozen_string_literal: true

require "test_helper"

module SrcsetTest
  RESOLUTIONS = [
    100, 116, 135, 156, 181, 210, 244, 283,
    328, 380, 441, 512, 594, 689, 799, 927,
    1075, 1247, 1446, 1678, 1946, 2257, 2619,
    3038, 3524, 4087, 4741, 5500, 6380, 7401, 8192
  ].freeze

  DPR_QUALITY = [75, 50, 35, 23, 20].freeze
  DPR_MULTIPLIER = ["1x", "2x", "3x", "4x", "5x", ].freeze

  DOMAIN = "testing.imgix.net"
  TOKEN = "<PASSWORD>"
  JPG_PATH = "image.jpg"

  def mock_client
    Imgix::Client.new(
      domain: DOMAIN,
      include_library_param: false
    ).path(JPG_PATH)
  end

  def mock_signed_client
    Imgix::Client.new(
      domain: DOMAIN,
      secure_url_token: TOKEN,
      include_library_param: false
    ).path(JPG_PATH)
  end

  def signature_base(params)
    TOKEN + "/" + JPG_PATH + params
  end

  def get_sig_from(src)
    src.slice(src.index("s=") + 2, src.length)
  end

  def get_params_from(src)
    src[src.index("?")..src.index("s=") - 2]
  end

  def get_expected_signature(src)
    # Ensure signature param exists.
    assert_includes src, "s="

    params = get_params_from(src)
    signature_base = signature_base(params)

    Digest::MD5.hexdigest(signature_base)
  end

  class SrcsetDefault < Imgix::Test
    include SrcsetTest

    def test_no_parameters
      srcset = path.to_srcset

      expected_number_of_pairs = 31
      assert_equal expected_number_of_pairs, srcset.split(",").length
    end

    def test_srcset_pair_values
      resolutions = RESOLUTIONS
      srcset = path.to_srcset
      srclist = srcset.split(",").map do |srcset_split|
        srcset_split.split(" ")[1].to_i
      end

      (0..srclist.length - 1).each do |i|
        assert_equal(srclist[i], resolutions[i])
      end
    end

    private

    def path
      @client ||= mock_signed_client
    end
  end

  class SrcsetGivenWidth < Imgix::Test
    include SrcsetTest

    def test_srcset_in_dpr_form
      device_pixel_ratio = 1

      srcset.split(",").map do |src|
        ratio = src.split(" ")[1]
        assert_equal "#{device_pixel_ratio}x", ratio
        device_pixel_ratio += 1
      end
    end

    def test_srcset_has_dpr_params
      i = 1
      srcset.split(",").map do |srcset_split|
        src = srcset_split.split(" ")[0]
        assert_includes src, "dpr=#{i}"
        i += 1
      end
    end

    def test_srcset_signs_urls
      srcset.split(",").map do |srcset_split|
        src = srcset_split.split(" ")[0]
        expected_signature = get_expected_signature(src)

        assert_includes src, expected_signature
      end
    end

    def test_srcset_has_variable_qualities
      i = 0
      srcset.split(",").map do |src|
        assert_includes src, "q=#{DPR_QUALITY[i]}"
        i += 1
      end
    end

    def test_srcset_respects_overriding_quality
      quality_override = 100
      srcset = mock_signed_client.to_srcset(w: 100, q: quality_override)

      srcset.split(",").map do |src|
        assert_includes src, "q=#{quality_override}"
      end
    end

    def test_disable_variable_quality
      srcset = mock_signed_client.to_srcset(
        w: 100,
        options: { disable_variable_quality: true }
      )

      srcset.split(",").map do |src|
        assert(!src.include?("q="))
      end
    end

    def test_respects_quality_param_when_disabled
      quality_override = 100
      srcset = mock_signed_client.to_srcset(
        w: 100, q: 100,
        options: { disable_variable_quality: true }
      )

      srcset.split(",").map do |src|
        assert_includes src, "q=#{quality_override}"
      end
    end

    private

    def srcset
      @client ||= mock_signed_client.to_srcset(w: 100)
    end
  end

  class SrcsetGivenHeight < Imgix::Test
    include SrcsetTest

    def test_srcset_generates_width_pairs
      expected_number_of_pairs = 5
      assert_equal expected_number_of_pairs, srcset.split(",").length
    end

    def test_srcset_pair_values
      resolutions = DPR_MULTIPLIER
      srclist = srcset.split(",").map do |srcset_split|
        srcset_split.split(" ")[1].to_i
      end

      (0..srclist.length - 1).each do |i|
        assert_equal(resolutions[i], srclist[i])
      end
    end

    def test_srcset_respects_height_parameter
      srcset.split(",").map do |src|
        assert_includes src, "h="
      end
    end

    def test_srcset_includes_dpr_param
      srcset.split(",").map do |src|
        assert_includes src, "dpr="
      end
    end

    def test_srcset_within_bounds
      min, *max = srcset.split(",")

      # parse out the width descriptor as an integer
      min = min.split(" ")[1].to_i
      max = max[max.length - 1].split(" ")[1].to_i

      assert_operator min, :>=, 100
      assert_operator max, :<=, 8192
    end

    # a 17% testing threshold is used to account for rounding
    def test_srcset_iterates_17_percent
      increment_allowed = 0.17

      # create an array of widths
      widths = srcset.split(",").map do |src|
        src.split(" ")[1].to_i
{'fraction_non_alphanumeric': 0.10861272836779763, 'fraction_numerical': 0.03673961051997591, 'mean_word_length': 2.594155844155844, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7526127', 'n_tokens_mistral': 3849, 'n_tokens_neox': 3472, 'n_words': 765}