summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/updated_notes_paginator_spec.rb
blob: eedc11777d42aca4384a1eae9c8eac4d48bb55aa (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
55
56
57
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::UpdatedNotesPaginator do
  let(:issue) { create(:issue) }

  let(:project) { issue.project }
  let(:finder) { NotesFinder.new(user, target: issue, last_fetched_at: last_fetched_at) }
  let(:user) { issue.author }

  let!(:page_1) { create_list(:note, 2, noteable: issue, project: project, updated_at: 2.days.ago) }
  let!(:page_2) { [create(:note, noteable: issue, project: project, updated_at: 1.day.ago)] }

  let(:page_1_boundary) { page_1.last.updated_at + NotesFinder::FETCH_OVERLAP }

  around do |example|
    Timecop.freeze do
      example.run
    end
  end

  before do
    stub_const("Gitlab::UpdatedNotesPaginator::LIMIT", 2)
  end

  subject(:paginator) { described_class.new(finder.execute, last_fetched_at: last_fetched_at) }

  describe 'last_fetched_at: start of time' do
    let(:last_fetched_at) { Time.at(0) }

    it 'calculates the first page of notes', :aggregate_failures do
      expect(paginator.notes).to match_array(page_1)
      expect(paginator.metadata).to match(
        more: true,
        last_fetched_at: microseconds(page_1_boundary)
      )
    end
  end

  describe 'last_fetched_at: start of final page' do
    let(:last_fetched_at) { page_1_boundary }

    it 'calculates a final page', :aggregate_failures do
      expect(paginator.notes).to match_array(page_2)
      expect(paginator.metadata).to match(
        more: false,
        last_fetched_at: microseconds(Time.zone.now)
      )
    end
  end

  # Convert a time to an integer number of microseconds
  def microseconds(time)
    (time.to_i * 1_000_000) + time.usec
  end
end