<commit_before>require 'spec_helper'
require 'helpmeout/formatter'
require 'rails'

describe "Service" do
  before(:each) do
    @service = Helpmeout::Service.new
    @failed_test = Helpmeout::FailedTest.new( 
                    :exception_message => 'Something very bad happened',
                    :backtrace => 
                    "/home/user/file.rb\n/home/user/other_file.rb\n/lib/rails/whatever.rb"
                                 )
  end

  describe "generate_fix_xml" do
    it "should generate xml that Rails understands if there are no fix_files" do
      @service.send(:generate_fix_xml, @failed_test).should == File.read("#{File.dirname(__FILE__)}/fix_no_files.xml")
    end
  end

  describe "generate_fix_xml" do
    it "should generate xml that Rails understands if there is one fix_files" do
      expected_xml = File.read("#{File.dirname(__FILE__)}/fix_one_file.xml")
      @failed_test_file = Helpmeout::FailedTestFile.new(:path => 'path/file.rb', :content => "Broken\nFile\nContent")
      File.should_receive(:read).with('path/file.rb').and_return("Fixed\nFile\nContent")
      @failed_test.stub(:failed_test_files => [@failed_test_file])
      @service.send(:generate_fix_xml, @failed_test).should == expected_xml
    end
  end

  describe "query_fix" do
    it 'should query the server for fixes with a cleaned backtrace' do
      backtrace = [
        "/home/user/ruby/whatever.rb:48", 
        "/home/user/project/some_model.rb:12", 
        "/home/user/project/some_controller.rb:23",
        "/home/user/ruby/palim.rb:300"
      ]
      expected_backtrace = 
        "/home/user/ruby/whatever.rb:48\n%\n%\n/home/user/ruby/palim.rb:300"
      Hash.should_receive(:from_xml).with(:response).and_return(:fixes_hash)
      Rails.stub(:root => "/home/user/project")
      RestClient.should_receive(:get).with('http://localhost:3000/fixes', :params => {:backtrace => expected_backtrace}).and_return(:response)
      @service.query_fix(backtrace).should == :fixes_hash
    end
  end


  describe 'clean_backtrace' do
    it 'removes files in the project directory from the backtrace' do
      Helpmeout::Config.stub(:exclude_prefixes => [])
      Rails.stub(:root => '/projects/this')
      backtrace = ['/projects/this/fail.rb', '/lib/something/else.rb']<commit_msg>config, clean backtrace for service<commit_after>require 'spec_helper'
require 'helpmeout/formatter'
require 'rails'

describe "Service" do
  before(:each) do
    @service = Helpmeout::Service.new
    @failed_test = Helpmeout::FailedTest.new( 
                    :exception_message => 'Something very bad happened',
                    :backtrace => 
                    "/home/user/file.rb\n/home/user/other_file.rb\n/lib/rails/whatever.rb"
                                 )
  end

  describe "generate_fix_xml" do
    it "should generate xml that Rails understands if there are no fix_files" do
      @service.send(:generate_fix_xml, @failed_test).should == File.read("#{File.dirname(__FILE__)}/fix_no_files.xml")
    end
  end

  describe "generate_fix_xml" do
    it "should generate xml that Rails understands if there is one fix_files" do
      expected_xml = File.read("#{File.dirname(__FILE__)}/fix_one_file.xml")
      @failed_test_file = Helpmeout::FailedTestFile.new(:path => 'path/file.rb', :content => "Broken\nFile\nContent")
      File.should_receive(:read).with('path/file.rb').and_return("Fixed\nFile\nContent")
      @failed_test.stub(:failed_test_files => [@failed_test_file])
      @service.send(:generate_fix_xml, @failed_test).should == expected_xml
    end
  end

  describe "query_fix" do
    it 'should query the server for fixes with a cleaned backtrace' do
      backtrace = "/home/user/ruby/whatever.rb:48\n%\n%\n/home/user/ruby/palim.rb:300"
      @service.should_receive(:clean_backtrace).with(backtrace).and_return(:cleaned_backtrace)
      Hash.should_receive(:from_xml).with(:response).and_return(:fixes_hash)
      RestClient.should_receive(:get).with('http://localhost:3000/fixes', :params => {:backtrace => :cleaned_backtrace}).and_return(:response)
      @service.query_fix(backtrace).should == :fixes_hash
    end
  end


  describe 'clean_backtrace' do
    it 'removes files in the project directory from the backtrace' do
      Helpmeout::Config.stub(:exclude_prefixes => [])
      Rails.stub(:root => '/projects/this')
      backtrace = ['/projects/this/fail.rb', '/lib/something/else.rb']
{'fraction_non_alphanumeric': 0.13542844223377798, 'fraction_numerical': 0.006104453990504182, 'mean_word_length': 3.3672260612043434, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 22}, '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': '4415839', 'n_tokens_mistral': 1454, 'n_tokens_neox': 1378, 'n_words': 253}