# frozen_string_literal: true require 'rspec' require_relative '../lib/chotto' describe Chotto do let(:db_double) { double('::Notmuch::Database') } let(:db_instance_double) { double('::Notmuch::Database') } let(:db_query_double) { double('::Notmuch::Query') } let(:message_double) { double('::Notmuch::Message') } before do $db_double = db_double # we need to access this double within block evaled elsewhere allow(db_double).to receive(:new).with('/path', mode: Notmuch::MODE_READ_WRITE).and_return(db_instance_double) allow(db_instance_double).to receive(:query).with(' (from:baltar@galactica.com)').and_return(db_query_double) allow(db_query_double).to receive(:search_messages).and_return([message_double]) Chotto.configure do config.database_path = '/path' config.db_class = $db_double end Chotto.rule_set 'sample' do messages.or.filter(from: 'baltar@galactica.com').each do |msg| msg.tags = msg.subject == 'Number Six' ? [:todo] : [:spam] msg.save! end end end after do Chotto.force_fresh_config end it 'sends the rules to Notmuch - scenario 1' do expect(message_double).to receive(:tags) expect(message_double).to receive(:header).with('Subject').and_return('Number Six') expect(message_double).to receive(:remove_all_tags) expect(message_double).to receive(:add_tag).with('todo') expect(db_instance_double).to receive(:close) eval_rules end it 'sends the rules to Notmuch - scenario 2' do expect(message_double).to receive(:tags) expect(message_double).to receive(:header).with('Subject').and_return('Boomer') expect(message_double).to receive(:remove_all_tags) expect(message_double).to receive(:add_tag).with('spam') expect(db_instance_double).to receive(:close) eval_rules end end