summaryrefslogtreecommitdiff
path: root/spec/services/clusters/agents/delete_expired_events_service_spec.rb
blob: 3dc166f54eb9299bb31474b4e236f1b5f74cd07a (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Clusters::Agents::DeleteExpiredEventsService do
  let_it_be(:agent) { create(:cluster_agent) }

  describe '#execute' do
    let_it_be(:event1) { create(:agent_activity_event, agent: agent, recorded_at: 1.hour.ago) }
    let_it_be(:event2) { create(:agent_activity_event, agent: agent, recorded_at: 2.hours.ago) }
    let_it_be(:event3) { create(:agent_activity_event, agent: agent, recorded_at: 3.hours.ago) }
    let_it_be(:event4) { create(:agent_activity_event, agent: agent, recorded_at: 4.hours.ago) }
    let_it_be(:event5) { create(:agent_activity_event, agent: agent, recorded_at: 5.hours.ago) }

    let(:deletion_cutoff) { 1.day.ago }

    subject { described_class.new(agent).execute }

    before do
      allow(agent).to receive(:activity_event_deletion_cutoff).and_return(deletion_cutoff)
    end

    it 'does not delete events if the limit has not been reached' do
      expect { subject }.not_to change(agent.activity_events, :count)
    end

    context 'there are more events than the limit' do
      let(:deletion_cutoff) { event3.recorded_at }

      it 'removes events to remain at the limit, keeping the most recent' do
        expect { subject }.to change(agent.activity_events, :count).from(5).to(3)
        expect(agent.activity_events).to contain_exactly(event1, event2, event3)
      end
    end
  end
end