<commit_before>require "helper"
require "rack/test"

class PinglishTest < MiniTest::Unit::TestCase
  FakeApp = lambda { |env| [404, {}, []] }

  def build_app(*args, &block)
    Rack::Builder.new do |builder|
      builder.use Pinglish, *args, &block
      builder.run FakeApp
    end
  end

  def test_default_path_and_status
    app = build_app

    session = Rack::Test::Session.new(app)
    session.get '/_ping'
    assert_equal 200, session.last_response.status
  end

  def test_with_good_check
    app = build_app do |ping|
      ping.check(:db) { :up_and_at_em }
      ping.check(:queue) { :pushin_and_poppin }
    end

    session = Rack::Test::Session.new(app)
    session.get '/_ping'

    json = JSON.load(session.last_response.body)

    assert_in_delta Time.now.to_i, json['now'].to_i, 2
    assert_equal 'ok', json['status']
    assert_equal 'up_and_at_em', json['db']
    assert_equal 'pushin_and_poppin', json['queue']
  end

  def test_with_check_that_raises
    app = build_app do |ping|
      ping.check(:db) { :ok }
      ping.check(:raise) { raise 'nooooope' }
    end

    session = Rack::Test::Session.new(app)
    session.get '/_ping'
    assert_equal 503, session.last_response.status
  end

  def test_with_check_that_returns_false
    app = build_app do |ping|
      ping.check(:db) { :ok }
      ping.check(:fail) { false }
    end

    session = Rack::Test::Session.new(app)
    session.get '/_ping'
    assert_equal 503, session.last_response.status
  end

  def test_customizing_path
    app = build_app("/_piiiiing")

    session = Rack::Test::Session.new(app)
    session.get '/_piiiiing'
    assert_equal 200, session.last_response.status
  end

  def test_check_without_name
    pinglish = Pinglish.new(FakeApp)
    check = pinglish.check { :ok }
    assert_instance_of Pinglish::Check, check
  end

  def test_check_with_name
    pinglish = Pinglish.new(FakeApp)
    check = pinglish.check(:db) { :ok }
    assert_instance_of Pinglish::Check, check
    assert_equal :db, check.name
  end

  def test_failure_boolean
    pinglish = Pinglish.new(FakeApp)

    assert pinglish.failure?(Exception.new),
      "Expected failure with exception to be true"

    assert pinglish.failure?(false),
      "Expected failure with false to be true"

    assert !pinglish.failure?(true),
      "Expected failure with true value to be false"

    assert !pinglish.failure?(:ok),
      "Expected failure with non-false and non-exception to be false"<commit_msg>Assert content type in tests.<commit_after>require "helper"
require "rack/test"

class PinglishTest < MiniTest::Unit::TestCase
  FakeApp = lambda { |env| [404, {}, []] }

  def build_app(*args, &block)
    Rack::Builder.new do |builder|
      builder.use Pinglish, *args, &block
      builder.run FakeApp
    end
  end

  def test_default_path_and_status
    app = build_app

    session = Rack::Test::Session.new(app)
    session.get '/_ping'
    assert_equal 200, session.last_response.status
    assert_equal 'application/json; charset=UTF-8',
      session.last_response.content_type
  end

  def test_with_good_check
    app = build_app do |ping|
      ping.check(:db) { :up_and_at_em }
      ping.check(:queue) { :pushin_and_poppin }
    end

    session = Rack::Test::Session.new(app)
    session.get '/_ping'

    assert_equal 'application/json; charset=UTF-8',
      session.last_response.content_type

    json = JSON.load(session.last_response.body)

    assert_in_delta Time.now.to_i, json['now'].to_i, 2
    assert_equal 'ok', json['status']
    assert_equal 'up_and_at_em', json['db']
    assert_equal 'pushin_and_poppin', json['queue']
  end

  def test_with_check_that_raises
    app = build_app do |ping|
      ping.check(:db) { :ok }
      ping.check(:raise) { raise 'nooooope' }
    end

    session = Rack::Test::Session.new(app)
    session.get '/_ping'
    assert_equal 503, session.last_response.status
    assert_equal 'application/json; charset=UTF-8',
      session.last_response.content_type
  end

  def test_with_check_that_returns_false
    app = build_app do |ping|
      ping.check(:db) { :ok }
      ping.check(:fail) { false }
    end

    session = Rack::Test::Session.new(app)
    session.get '/_ping'
    assert_equal 503, session.last_response.status
    assert_equal 'application/json; charset=UTF-8',
      session.last_response.content_type
  end

  def test_with_custom_path
    app = build_app("/_piiiiing")

    session = Rack::Test::Session.new(app)
    session.get '/_piiiiing'
    assert_equal 200, session.last_response.status
    assert_equal 'application/json; charset=UTF-8',
      session.last_response.content_type
  end

  def test_check_without_name
    pinglish = Pinglish.new(FakeApp)
    check = pinglish.check { :ok }
    assert_instance_of Pinglish::Check, check
  end

  def test_check_with_name
    pinglish = Pinglish.new(FakeApp)
    check = pinglish.check(:db) { :ok }
    assert_instance_of Pinglish::Check, check
    assert_equal :db, check.name
  end

  def test_failure_boolean
    pinglish = Pinglish.new(FakeApp)

    assert pinglish.failure?(Exception.new),
      "Expected failure with exception to be true"

    assert pinglish.failure?(false),
      "Expected failure with false to be true"

    assert !pinglish.failure?(true),
      "Expected failure with true value to be false"

    assert !pinglish.failure?(:ok),
      "Expected failure with non-false and non-exception to be false"
{'fraction_non_alphanumeric': 0.10984570168993388, 'fraction_numerical': 0.006796473181484203, 'mean_word_length': 3.4017784963621667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '2810996', 'n_tokens_mistral': 1905, 'n_tokens_neox': 1806, 'n_words': 397}