<commit_before>require 'spec_helper'
require 'rails_autoscale_agent/reporter'
require 'rails_autoscale_agent/config'
require 'rails_autoscale_agent/store'
require 'webmock/rspec'

module RailsAutoscaleAgent
  describe Reporter do

    describe "#report!" do
      let(:config) { Config.new('RAILS_AUTOSCALE_URL' => 'http://example.com/api') }
      let(:store) { Store.instance }
      let!(:stub) { stub_request(:post, "http://example.com/api/reports") }

      before { store.instance_variable_set '@measurements', [] }

      it "reports stored metrics to the API" do
        store.push 123, Time.now - 60

        Reporter.instance.report!(config, store)

        expect(stub).to have_been_requested.once
      end

      it "does not report if there are only stored requests for the current minute" do
        store.push 123, Time.now

        Reporter.instance.report!(config, store)

        expect(stub).to_not have_been_requested
      end

      it "reports for each minute up to the current" do
        store.push 123, Time.now - 600
        store.push 456, Time.now - 60

        Reporter.instance.report!(config, store)

        expect(stub).to have_been_requested.twice
      end
    end

    describe "#register!" do
      let(:config) { Config.new('RAILS_AUTOSCALE_URL' => 'http://example.com/api') }
      let!(:stub) { stub_request(:post, "http://example.com/api/registrations") }

      it "registers the reporter with contextual info" do
        Reporter.instance.register!(config)

        expected_payload = {
          registration: {
            pid: Process.pid,
            ruby_version: '2.3.1',
            rails_version: '5.0.fake',
            gem_version: '0.1.0',
          }<commit_msg>Fix reporter spec<commit_after>require 'spec_helper'
require 'rails_autoscale_agent/reporter'
require 'rails_autoscale_agent/config'
require 'rails_autoscale_agent/store'
require 'webmock/rspec'

module RailsAutoscaleAgent
  describe Reporter do

    describe "#report!" do
      let(:config) { Config.new('RAILS_AUTOSCALE_URL' => 'http://example.com/api') }
      let(:store) { Store.instance }
      let!(:stub) { stub_request(:post, "http://example.com/api/reports") }

      before { store.instance_variable_set '@measurements', [] }

      it "reports stored metrics to the API" do
        store.push 123, Time.now - 60

        Reporter.instance.report!(config, store)

        expect(stub).to have_been_requested.once
      end

      it "does not report if there are only stored requests for the current minute" do
        store.push 123, Time.now

        Reporter.instance.report!(config, store)

        expect(stub).to_not have_been_requested
      end

      it "reports for each minute up to the current" do
        store.push 123, Time.now - 600
        store.push 456, Time.now - 60

        Reporter.instance.report!(config, store)

        expect(stub).to have_been_requested.twice
      end
    end

    describe "#register!" do
      let(:config) { Config.new('DYNO' => 'web.0', 'RAILS_AUTOSCALE_URL' => 'http://example.com/api') }
      let!(:stub) { stub_request(:post, "http://example.com/api/registrations") }

      it "registers the reporter with contextual info" do
        Reporter.instance.register!(config)

        expected_payload = {
          registration: {
            dyno: 'web.0',
            pid: Process.pid,
            ruby_version: '2.3.1',
            rails_version: '5.0.fake',
            gem_version: '0.1.0',
          }
{'fraction_non_alphanumeric': 0.11668577981651376, 'fraction_numerical': 0.016055045871559634, 'mean_word_length': 2.626819126819127, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '3703879', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1057, 'n_words': 248}