aboutsummaryrefslogtreecommitdiff
path: root/spec/chotto_spec.rb
blob: 019c0e48f73f1ebbd7716bdfab0b58ed5f3ed06e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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(: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(: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