require 'pokeplot/miner' require 'pokeplot/database' require 'pokeplot/helpers/math' describe Pokeplot::Miner do subject(:miner) { db = double() allow(db).to receive(:find).and_return(db) allow(db).to receive(:insert_one) allow(db).to receive(:count).and_return(0,0,1,1) #test unique and non unique allow(db).to receive(:[]).and_return(db) accounts = [{'username' => '', 'password' => '', 'provider' => 'ptc'}] expect(Pokeplot::Database).to receive(:mongo).at_least(:once).and_return(db) m = described_class.new(accounts, 74.3, 43.2, 1, 0, true, true, false, '', false, false) return m } describe ".new" do it "should create an instance of Pokeplot::Miner" do expect(miner).to be_a(Pokeplot::Miner) end end describe "#start" do data2 = {:GET_MAP_OBJECTS => {:status => :SUCCESS, :map_cells => [{:s2_cell_id => 10, :current_timestamp_ms => 400, :forts => [{:id => 1, :enabled => false, :latitude => 0, :longitude => 0, :type => :GYM}], :wild_pokemons => [{:time_till_hidden_ms => 0, :encounter_id => 1, :latitude => 3, :longitude => 4, :spawn_point_id => 5, :pokemon_data => {:pokemon_id => :PIDGEY}}]}]}} data1 = {:GET_MAP_OBJECTS => {:status => :SUCCESS, :map_cells => [{:forts => [], :wild_pokemons => [{:time_till_hidden_ms => -1, :pokemon_data => {:pokemon_id => :WEEDLE}}]}]}} context "single thread" do it "should create a miner thread" do #To test normal and negative api = double() allow(api).to receive(:set_location).and_return(api) allow(api).to receive(:map_heartbeat).and_return(data1, data2) allow(api).to receive(:login).with(instance_of(String), instance_of(String), /(google|ptc)/) expect(Thread).to receive(:new).and_yield expect(miner).to receive(:loop).and_yield expect(Pokeplot::API).to receive(:new).and_return(api) miner.start miner.stop end end context "multiple threads" do it "should create more than 1 thread" do miner api = double() allow(api).to receive(:set_location).and_return(api) allow(api).to receive(:map_heartbeat).and_return(data1, data2) allow(api).to receive(:login).with(instance_of(String), instance_of(String), /(google|ptc)/).at_least(:once) accounts = [{'username' => '', 'password' => '', 'provider' => 'ptc'}] m = described_class.new(accounts, 74.3, 43.2, 1, 0, true, true, true, '', false, false) expect(Thread).to receive(:new).at_least(:twice).and_yield allow(m).to receive(:loop).and_yield expect(Pokeplot::API).to receive(:new).and_return(api) m.start m.stop end end end describe "#stop" do it "should delete the miner thread" do miner.start expect(miner.miner).not_to be(nil) miner.stop expect(miner.miner).to be(nil) end end describe "#get_all_coords" do it "should create an array of coords" do expect(miner.send('get_all_coords')).to all(have_key(:lat) & have_key(:lng)) end end describe "#parse_map_objects" do context "successful status" do let(:data) { {:GET_MAP_OBJECTS => {:status => :SUCCESS, :map_cells => [{:s2_cell_id => 10, :current_timestamp_ms => 400, :forts => [{:id => 1, :enabled => false, :latitude => 0, :longitude => 0, :type => :GYM}], :wild_pokemons => [{:time_till_hidden_ms => 0, :encounter_id => 1, :latitude => 3, :longitude => 4, :spawn_point_id => 5, :pokemon_data => {:pokemon_id => :PIDGEY}}]}]}} } it "should save encounters and forts" do expect(miner.db).to receive(:find).with(instance_of(Hash)).twice expect(miner.db).to receive(:insert_one).with(instance_of(Hash)).twice expect(miner.db).to receive(:count).twice expect(miner.db).to receive(:[]).with(instance_of(Symbol)).exactly(4).times miner.send('parse_map_objects', data) end end context "unsuccessful status" do let(:data) { {:GET_MAP_OBJECTS => {:status => :ERROR}} } it "should log an error" do expect( miner.send('parse_map_objects', data) ).to eq(nil) end end end end