<commit_before>require 'yaml'
require 'ostruct'

class Mumukit::Server::TestServer

  include Mumukit::WithContentType

  def runtime
    Mumukit.runtime
  end

  def info(url)
    runtime.info.merge(runtime.metadata_hook.metadata).merge(url: url)
  end

  def start_request!(_request)
  end

  def parse_request(sinatra_request)
    OpenStruct.new parse_request_headers(sinatra_request).merge(parse_request_body(sinatra_request))
  end

  def parse_request_headers(sinatra_request)
    {}
  end

  def parse_request_body(sinatra_request)
    JSON.parse(sinatra_request.body.read).tap do |it|
      I18n.locale = it['locale'] || :en
    end rescue {}
  end

  def test!(request)
    respond_to(request) do |r|
      test_results = run_tests! r
      expectation_results = run_expectations! r

      results = OpenStruct.new(test_results: test_results,
                               expectation_results: expectation_results)

      feedback = run_feedback! r, results

      Mumukit::Server::ResponseBuilder.build do
        add_test_results(test_results)
        add_expectation_results(expectation_results)
        add_feedback(feedback)
      end
    end
  end

  def query!(request)
    respond_to(request) do |r|
      results = run_query!(r)
      Mumukit::Server::ResponseBuilder.build do
        add_query_results(results)
      end
    end
  end

  def run_query!(request)
    compile_and_run runtime.query_hook, request
  end

  def run_tests!(request)
    return ['', :passed] if request.test.blank?

    compile_and_run runtime.test_hook, request
  end

  def run_expectations!(request)
    if request.expectations
      compile_and_run runtime.expectations_hook, request
    else
      []
    end
  end

  def run_feedback!(request, results)
    runtime.feedback_hook.run!(request, results)
  end

  private

  def compile_and_run(hook, request)
    compilation = hook.compile(preprocess request)
    hook.run!(compilation)
  rescue Mumukit::CompilationError => e
    [e.message, :errored]
  end

  def preprocess(request)
    Mumukit.directives_pipeline.transform(request)
  end

  def validate_request!(request)
    runtime.validation_hook.validate! request
  end
<commit_msg>Return empty array on expectation compile errors<commit_after>require 'yaml'
require 'ostruct'

class Mumukit::Server::TestServer

  include Mumukit::WithContentType

  def runtime
    Mumukit.runtime
  end

  def info(url)
    runtime.info.merge(runtime.metadata_hook.metadata).merge(url: url)
  end

  def start_request!(_request)
  end

  def parse_request(sinatra_request)
    OpenStruct.new parse_request_headers(sinatra_request).merge(parse_request_body(sinatra_request))
  end

  def parse_request_headers(sinatra_request)
    {}
  end

  def parse_request_body(sinatra_request)
    JSON.parse(sinatra_request.body.read).tap do |it|
      I18n.locale = it['locale'] || :en
    end rescue {}
  end

  def test!(request)
    respond_to(request) do |r|
      test_results = run_tests! r
      expectation_results = run_expectations! r

      results = OpenStruct.new(test_results: test_results,
                               expectation_results: expectation_results)

      feedback = run_feedback! r, results

      Mumukit::Server::ResponseBuilder.build do
        add_test_results(test_results)
        add_expectation_results(expectation_results)
        add_feedback(feedback)
      end
    end
  end

  def query!(request)
    respond_to(request) do |r|
      results = run_query!(r)
      Mumukit::Server::ResponseBuilder.build do
        add_query_results(results)
      end
    end
  end

  def run_query!(request)
    compile_and_run runtime.query_hook, request
  end

  def run_tests!(request)
    return ['', :passed] if request.test.blank?

    compile_and_run runtime.test_hook, request
  end

  def run_expectations!(request)
    if request.expectations
      compile_and_run(runtime.expectations_hook, request) { [] }
    else
      []
    end
  end

  def run_feedback!(request, results)
    runtime.feedback_hook.run!(request, results)
  end

  private

  def compile_and_run(hook, request)
    compilation = hook.compile(preprocess request)
    hook.run!(compilation)
  rescue Mumukit::CompilationError => e
    block_given? ? yield(e) : [e.message, :errored]
  end

  def preprocess(request)
    Mumukit.directives_pipeline.transform(request)
  end

  def validate_request!(request)
    runtime.validation_hook.validate! request
  end

{'fraction_non_alphanumeric': 0.08470006740058414, 'fraction_numerical': 0.0008986744551786115, 'mean_word_length': 3.483383685800604, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '502017', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1379, 'n_words': 277}