<commit_before>require 'spider-gazelle'
require 'spider-gazelle/gazelle/http1'


# TODO:: Mock logger

class MockSocket
    def initialize
        @stopped = false
        @write_cb = proc {}
    end

    attr_reader   :closed, :stopped
    attr_accessor :storage, :write_cb, :shutdown_cb, :close_cb

    def peername; ['127.0.0.1']; end
    def finally(method = nil, &block); @on_close = method || block; end
    def close; @close_cb.call; end
    def shutdown; @shutdown_cb.call; end
    def progress(_); end
    def stop_read; @stopped = true; end

    def write(data); @write_cb.call(data); end
end

class MockLogger
    def initialize
        @logged = []
    end

    attr_reader :logged

    def method_missing(methId, *args)
        @logged << methId
        p "#{methId}: #{args}"
    end

    def print_error(e, msg = '', trace = nil)
        @logged << msg << ': ' << e
        msg << ":\n" unless msg.empty?
        msg << "#{e.message}\n"
        backtrace = e.backtrace if e.respond_to?(:backtrace)
        if backtrace
            msg << "#{backtrace.join("\n")}\n"
        elsif trace.nil?
            trace = caller
        end
        msg << "Caller backtrace:\n#{trace.join("\n")}\n" if trace
        puts msg
    end
end

describe ::SpiderGazelle::Gazelle::Http1 do
    before :each do
        @shutdown_called = 0
        @close_called = 0

        @loop = ::Libuv::Reactor.default
        @loop.notifier do |error, context|
            begin
                p "Log called: #{context}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
            rescue Exception
                p 'error in logger'
            end
        end
        @timeout = @loop.timer {
            @loop.stop
            @general_failure << "test timed out"
        }.start(1000)


        @return ||= proc {|http1|
            @returned = http1
        }
        @logger = MockLogger.new
        @http1_callbacks ||= ::SpiderGazelle::Gazelle::Http1::Callbacks.new
        @http1 = ::SpiderGazelle::Gazelle::Http1.new(@return, @http1_callbacks, @loop, @logger)

        @socket = MockSocket.new
        @socket.shutdown_cb = proc {
            @shutdown_called += 1
            @loop.stop
        }
        @socket.close_cb = proc {
            @close_called += 1
            @loop.stop
        }

        @port = 80
        @tls = false<commit_msg>(http1_spec) update class names<commit_after>require 'spider-gazelle'
require 'spider-gazelle/spider/http1'


# TODO:: Mock logger

class MockSocket
    def initialize
        @stopped = false
        @write_cb = proc {}
    end

    attr_reader   :closed, :stopped
    attr_accessor :storage, :write_cb, :shutdown_cb, :close_cb

    def peername; ['127.0.0.1']; end
    def finally(method = nil, &block); @on_close = method || block; end
    def close; @close_cb.call; end
    def shutdown; @shutdown_cb.call; end
    def progress(_); end
    def stop_read; @stopped = true; end

    def write(data); @write_cb.call(data); end
end

class MockLogger
    def initialize
        @logged = []
    end

    attr_reader :logged

    def method_missing(methId, *args)
        @logged << methId
        p "#{methId}: #{args}"
    end

    def print_error(e, msg = '', trace = nil)
        @logged << msg << ': ' << e
        msg << ":\n" unless msg.empty?
        msg << "#{e.message}\n"
        backtrace = e.backtrace if e.respond_to?(:backtrace)
        if backtrace
            msg << "#{backtrace.join("\n")}\n"
        elsif trace.nil?
            trace = caller
        end
        msg << "Caller backtrace:\n#{trace.join("\n")}\n" if trace
        puts msg
    end
end

describe ::SpiderGazelle::Spider::Http1 do
    before :each do
        @shutdown_called = 0
        @close_called = 0

        @loop = ::Libuv::Reactor.default
        @loop.notifier do |error, context|
            begin
                p "Log called: #{context}\n#{error.message}\n#{error.backtrace.join("\n")}\n"
            rescue Exception
                p 'error in logger'
            end
        end
        @timeout = @loop.timer {
            @loop.stop
            @general_failure << "test timed out"
        }.start(1000)


        @return ||= proc {|http1|
            @returned = http1
        }
        @logger = MockLogger.new
        @http1_callbacks ||= ::SpiderGazelle::Spider::Http1::Callbacks.new
        @http1 = ::SpiderGazelle::Spider::Http1.new(@return, @http1_callbacks, @loop, @logger, [@loop].cycle)

        @socket = MockSocket.new
        @socket.shutdown_cb = proc {
            @shutdown_called += 1
            @loop.stop
        }
        @socket.close_cb = proc {
            @close_called += 1
            @loop.stop
        }

        @port = 80
        @tls = false
{'fraction_non_alphanumeric': 0.13021384713106077, 'fraction_numerical': 0.01079822146940504, 'mean_word_length': 1.9341614906832298, 'pattern_counts': {'":': 2, '<': 39, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4762933', 'n_tokens_mistral': 1558, 'n_tokens_neox': 1502, 'n_words': 357}