<commit_before>require_relative '../../spec_helper'

def mock_fixture(fixture)
  mock_xml = File.open("spec/fixtures/#{fixture}.xml").read
  allow(RestClient).to receive(:get) { mock_xml }
end

describe Lita::Handlers::OnewheelWolframAlpha, :lita_handler => true do

  before(:each) do
    # mock_geocoder = ::Geocoder::Result::Google.new({'formatted_address' => 'Portland, OR', 'geometry' => { 'location' => { 'lat' => 45.523452, 'lng' => -122.676207 }}})
    # allow(::Geocoder).to receive(:search) { [mock_geocoder] }  # It expects an array of geocoder objects.

    registry.configure do |config|
      config.handlers.onewheel_wolfram_alpha.api_uri = 'http://api.wolframalpha.com/v2/query?input=[query]&appid=[appid]'
      config.handlers.onewheel_wolfram_alpha.app_id = 'app-id-here'
    end
  end

  it { is_expected.to route_command('alpha') }

  it 'will work' do
    mock_fixture('pi')
    send_command 'alpha pi'
    expect(replies.last).to eq('3.141592653589793238462643383279502884197169399375105820974...')
  end

  it 'will fail gracefully' do
    mock_fixture('boopadoop')
    send_command 'alpha boopadoop'
    expect(replies.last).to satisfy { |v|
      ['Nope, no boopadoop to see here.',
       'What\'s that, now?',
       'boopadoop?'].include? v
    }
  end

  it 'will print plot for mathy things' do
    mock_fixture('x^2')
    send_command 'alpha x^2'
    expect(replies.last).to eq('http://www2.wolframalpha.com/Calculate/MSP/MSP8620hdi8ba18h005cd0000268f85ce36b8h331?MSPStoreType=image/gif&s=15')
  end

  it 'will error missing config' do
    registry.configure do |config|
      config.handlers.onewheel_wolfram_alpha.api_uri = 'http://api.wolframalpha.com/v2/query?input=[query]&appid=[appid]'
      config.handlers.onewheel_wolfram_alpha.app_id = nil
    end
    send_command 'alpha pi'
    expect(replies.last).to eq(nil)
  end

  it 'will single-line a multi-line plaintext' do
    mock_fixture('multiline-plaintext')
    send_command 'alpha light years'
    expect(replies.last).to eq('1 ly | 0.3066 pc  (parsecs) | 63241 au  (astronomical units) | 9.461&#xD7;10^12 km  (kilometers) | 9.461&#xD7;10^15 meters | 5.879 trillion miles')
  end

  it 'will add desired text to the end' do
    mock_fixture('x^2')
    send_command 'alpha x^2 <as a service>'
    expect(replies.last).to eq('http://www2.wolframalpha.com/Calculate/MSP/MSP8620hdi8ba18h005cd0000268f85ce36b8h331?MSPStoreType=image/gif&s=15 as a service')
  end

  # it 'will add .0 to a fraction' do
  #   mock_fixture('onedividedby2')
  #   send_command 'alpha 1/2'
  #   expect(replies.last).to eq('0.5')
  # end
end<commit_msg>Added test for decimal adds.<commit_after>require_relative '../../spec_helper'

def mock_fixture(fixture)
  mock_xml = File.open("spec/fixtures/#{fixture}.xml").read
  allow(RestClient).to receive(:get) { mock_xml }
end

describe Lita::Handlers::OnewheelWolframAlpha, :lita_handler => true do

  before(:each) do
    # mock_geocoder = ::Geocoder::Result::Google.new({'formatted_address' => 'Portland, OR', 'geometry' => { 'location' => { 'lat' => 45.523452, 'lng' => -122.676207 }}})
    # allow(::Geocoder).to receive(:search) { [mock_geocoder] }  # It expects an array of geocoder objects.

    registry.configure do |config|
      config.handlers.onewheel_wolfram_alpha.api_uri = 'http://api.wolframalpha.com/v2/query?input=[query]&appid=[appid]'
      config.handlers.onewheel_wolfram_alpha.app_id = 'app-id-here'
    end
  end

  it { is_expected.to route_command('alpha') }

  it 'will work' do
    mock_fixture('pi')
    send_command 'alpha pi'
    expect(replies.last).to eq('3.141592653589793238462643383279502884197169399375105820974...')
  end

  it 'will fail gracefully' do
    mock_fixture('boopadoop')
    send_command 'alpha boopadoop'
    expect(replies.last).to satisfy { |v|
      ['Nope, no boopadoop to see here.',
       'What\'s that, now?',
       'boopadoop?'].include? v
    }
  end

  it 'will print plot for mathy things' do
    mock_fixture('x^2')
    send_command 'alpha x^2'
    expect(replies.last).to eq('http://www2.wolframalpha.com/Calculate/MSP/MSP8620hdi8ba18h005cd0000268f85ce36b8h331?MSPStoreType=image/gif&s=15')
  end

  it 'will error missing config' do
    registry.configure do |config|
      config.handlers.onewheel_wolfram_alpha.api_uri = 'http://api.wolframalpha.com/v2/query?input=[query]&appid=[appid]'
      config.handlers.onewheel_wolfram_alpha.app_id = nil
    end
    send_command 'alpha pi'
    expect(replies.last).to eq(nil)
  end

  it 'will single-line a multi-line plaintext' do
    mock_fixture('multiline-plaintext')
    send_command 'alpha light years'
    expect(replies.last).to eq('1 ly | 0.3066 pc  (parsecs) | 63241 au  (astronomical units) | 9.461&#xD7;10^12 km  (kilometers) | 9.461&#xD7;10^15 meters | 5.879 trillion miles')
  end

  it 'will add desired text to the end' do
    mock_fixture('x^2')
    send_command 'alpha x^2 <as a service>'
    expect(replies.last).to eq('http://www2.wolframalpha.com/Calculate/MSP/MSP8620hdi8ba18h005cd0000268f85ce36b8h331?MSPStoreType=image/gif&s=15 as a service')
  end

  it 'will add .0 to a fraction' do
    mock_fixture('oneovertwo')
    send_command 'alpha 1/2'
    expect(replies.last).to eq('0.5')
  end
end
{'fraction_non_alphanumeric': 0.13910060975609756, 'fraction_numerical': 0.06688262195121951, 'mean_word_length': 4.013371537726838, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 6}, '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': '7583108', 'n_tokens_mistral': 2199, 'n_tokens_neox': 1984, 'n_words': 379}