<commit_before>class FakeServer
  SUPPORTED_MECHANISMS = ['PLAIN', 'SCRAM-SHA-256', 'SCRAM-SHA-512']

  def self.start(server)
    thread = Thread.new { new(server).start }
    thread.abort_on_exception = true
    thread
  end

  def initialize(server)
    @server = server
    @authenticating = false
    @auth_mechanism = nil
  end

  def start
    loop do
      client = @server.accept

      begin
        handle_client(client)
      rescue => e
        puts e
        break
      ensure
        client.close
      end
    end
  end

  def handle_client(client)
    encoder = Kafka::Protocol::Encoder.new(client)
    decoder = Kafka::Protocol::Decoder.new(client)

    loop do
      request_bytes = decoder.bytes
      request_data = Kafka::Protocol::Decoder.new(StringIO.new(request_bytes))
      api_key = request_data.int16
      _api_version = request_data.int16
      correlation_id = request_data.int32
      _client_id = request_data.string

      message = request_data.string

      response = StringIO.new
      response_encoder = Kafka::Protocol::Encoder.new(response)
      response_encoder.write_int32(correlation_id)

      case api_key
      when 17 then
        response_encoder.write_int16(0)
        response_encoder.write_array(SUPPORTED_MECHANISMS) { |m| response_encoder.write_string(m) }
        encoder.write_bytes(response.string)
        auth(message, encoder, decoder)
        break
      else
        response_encoder.write_string(message)
        encoder.write_bytes(response.string)
      end
    end
  end

  def auth(auth_mechanism, encoder, decoder)
    case auth_mechanism
    when 'PLAIN'
      message = decoder.bytes
      _authzid, username, password = message.split("\000")
      if username == 'spec_username' && password == '<PASSWORD>'
        encoder.write_bytes('')
      end
    when 'SCRAM-SHA-256', 'SCRAM-SHA-512'
      scram_sasl_authenticate(auth_mechanism[6..-1], encoder, decoder)
    else
      puts "UNKNOWN AUTH MECHANISM"
    end
  end

  def scram_sasl_authenticate(algorithm, encoder, decoder)<commit_msg>Changed case to if and removed underscore from unused variables<commit_after>class FakeServer
  SUPPORTED_MECHANISMS = ['PLAIN', 'SCRAM-SHA-256', 'SCRAM-SHA-512']

  def self.start(server)
    thread = Thread.new { new(server).start }
    thread.abort_on_exception = true
    thread
  end

  def initialize(server)
    @server = server
    @authenticating = false
    @auth_mechanism = nil
  end

  def start
    loop do
      client = @server.accept

      begin
        handle_client(client)
      rescue => e
        puts e
        break
      ensure
        client.close
      end
    end
  end

  def handle_client(client)
    encoder = Kafka::Protocol::Encoder.new(client)
    decoder = Kafka::Protocol::Decoder.new(client)

    loop do
      request_bytes = decoder.bytes
      request_data = Kafka::Protocol::Decoder.new(StringIO.new(request_bytes))
      api_key = request_data.int16
      api_version = request_data.int16
      correlation_id = request_data.int32
      client_id = request_data.string

      message = request_data.string

      response = StringIO.new
      response_encoder = Kafka::Protocol::Encoder.new(response)
      response_encoder.write_int32(correlation_id)

      if api_key == 17
        response_encoder.write_int16(0)
        response_encoder.write_array(SUPPORTED_MECHANISMS) { |m| response_encoder.write_string(m) }
        encoder.write_bytes(response.string)
        auth(message, encoder, decoder)
        break
      else
        response_encoder.write_string(message)
        encoder.write_bytes(response.string)
      end
    end
  end

  def auth(auth_mechanism, encoder, decoder)
    case auth_mechanism
    when 'PLAIN'
      message = decoder.bytes
      _authzid, username, password = message.split("\000")
      if username == 'spec_username' && password == '<PASSWORD>'
        encoder.write_bytes('')
      end
    when 'SCRAM-SHA-256', 'SCRAM-SHA-512'
      scram_sasl_authenticate(auth_mechanism[6..-1], encoder, decoder)
    else
      puts "UNKNOWN AUTH MECHANISM"
    end
  end

  def scram_sasl_authenticate(algorithm, encoder, decoder)
{'fraction_non_alphanumeric': 0.08979591836734693, 'fraction_numerical': 0.014405762304921969, 'mean_word_length': 2.811527904849039, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5353467', 'n_tokens_mistral': 1374, 'n_tokens_neox': 1288, 'n_words': 260}