<commit_before>
require 'test_helper'

class Bixby::Hello < Bixby::API
  def hi(name)
    return "hello #{name}"
  end

  def msg(foo, bar)
    return "msg: #{foo} #{bar}"
  end

  def err(foo)
    raise Exception.new("ahhh")
  end
end

module Bixby
module Test
module Controllers

class API < ActionController::TestCase

  def setup
    @controller = ApiController.new
    SimpleCov.command_name 'test:controllers:api'
  end

  def test_post_invalid
    post :handle
    assert @response.body =~ /invalid request/

    @request.recycle!
    @request.env['RAW_POST_DATA'] = "aslkdjafsdf"
    post :handle
    assert @response.body =~ /invalid request/

    @request.recycle!
    @request.env['RAW_POST_DATA'] = JsonRequest.new(nil, nil).to_json
    post :handle

    res = JsonResponse.from_json(@response.body)
    assert res
    refute res.success?
    assert @response.body =~ /invalid request/
    assert res.message =~ /invalid request/
  end

  def test_unsupported_op
    unsupported_op("hello", "joe")
    unsupported_op("foo:bar", "joe")
    unsupported_op("hello:world", "joe")
  end

  def test_valid_request
    agent = FactoryGirl.create(:agent)
    @request.env['RAW_POST_DATA'] = JsonRequest.new("hello:hi", "joe").to_json
    post :handle

    res = JsonResponse.from_json(@response.body)
    assert res
    assert res.success?
    assert "hello joe", res.data
  end

  def test_params_array
    agent = FactoryGirl.create(:agent)
    @request.env['RAW_POST_DATA'] = JsonRequest.new("hello:msg", %w(hi there)).to_json
    post :handle

    res = JsonResponse.from_json(@response.body)
    assert res
    assert res.success?
    assert "msg: hi there", res.data
  end

  def test_catch_exception
    agent = FactoryGirl.create(:agent)
    @request.env['RAW_POST_DATA'] = JsonRequest.new("hello:err", "joe").to_json
    post :handle<commit_msg>added a test for params hash<commit_after>
require 'test_helper'

class Bixby::Hello < Bixby::API
  def hi(name)
    return "hello #{name}"
  end

  def msg(foo, bar)
    return "msg: #{foo} #{bar}"
  end

  def err(foo)
    raise Exception.new("ahhh")
  end

  def hash(hash)
    return hash[:msg]
  end
end

module Bixby
module Test
module Controllers

class API < ActionController::TestCase

  def setup
    @controller = ApiController.new
    SimpleCov.command_name 'test:controllers:api'
  end

  def test_post_invalid
    post :handle
    assert @response.body =~ /invalid request/

    @request.recycle!
    @request.env['RAW_POST_DATA'] = "aslkdjafsdf"
    post :handle
    assert @response.body =~ /invalid request/

    @request.recycle!
    @request.env['RAW_POST_DATA'] = JsonRequest.new(nil, nil).to_json
    post :handle

    res = JsonResponse.from_json(@response.body)
    assert res
    refute res.success?
    assert @response.body =~ /invalid request/
    assert res.message =~ /invalid request/
  end

  def test_unsupported_op
    unsupported_op("hello", "joe")
    unsupported_op("foo:bar", "joe")
    unsupported_op("hello:world", "joe")
  end

  def test_valid_request
    agent = FactoryGirl.create(:agent)
    @request.env['RAW_POST_DATA'] = JsonRequest.new("hello:hi", "joe").to_json
    post :handle

    res = JsonResponse.from_json(@response.body)
    assert res
    assert res.success?
    assert "hello joe", res.data
  end

  def test_params_array
    agent = FactoryGirl.create(:agent)
    @request.env['RAW_POST_DATA'] = JsonRequest.new("hello:msg", %w(hi there)).to_json
    post :handle

    res = JsonResponse.from_json(@response.body)
    assert res
    assert res.success?
    assert "msg: hi there", res.data
  end

  def test_params_hash
    agent = FactoryGirl.create(:agent)
    @request.env['RAW_POST_DATA'] = JsonRequest.new("hello:hash", {:msg => "yo boss"}).to_json
    post :handle

    res = JsonResponse.from_json(@response.body)
    assert res
    assert res.success?
    assert "yo boss", res.data
  end

  def test_catch_exception
    agent = FactoryGirl.create(:agent)
    @request.env['RAW_POST_DATA'] = JsonRequest.new("hello:err", "joe").to_json
    post :handle
{'fraction_non_alphanumeric': 0.12187346738597352, 'fraction_numerical': 0.0, 'mean_word_length': 3.522172949002217, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, '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': '4550215', 'n_tokens_mistral': 1404, 'n_tokens_neox': 1354, 'n_words': 311}