1# frozen_string_literal: true
2
3require 'spec_helper'
4
5RSpec.describe Gitlab::DiscussionsDiff::FileCollection do
6  let(:merge_request) { create(:merge_request) }
7  let!(:diff_note_a) { create(:diff_note_on_merge_request, project: merge_request.project, noteable: merge_request) }
8  let!(:diff_note_b) { create(:diff_note_on_merge_request, project: merge_request.project, noteable: merge_request) }
9  let(:note_diff_file_a) { diff_note_a.note_diff_file }
10  let(:note_diff_file_b) { diff_note_b.note_diff_file }
11
12  subject { described_class.new([note_diff_file_a, note_diff_file_b]) }
13
14  describe '#load_highlight', :clean_gitlab_redis_shared_state do
15    it 'writes uncached diffs highlight' do
16      file_a_caching_content = diff_note_a.diff_file.highlighted_diff_lines.map(&:to_hash)
17      file_b_caching_content = diff_note_b.diff_file.highlighted_diff_lines.map(&:to_hash)
18
19      expect(Gitlab::DiscussionsDiff::HighlightCache)
20        .to receive(:write_multiple)
21        .with({ note_diff_file_a.id => file_a_caching_content,
22                note_diff_file_b.id => file_b_caching_content })
23        .and_call_original
24
25      subject.load_highlight
26    end
27
28    it 'does not write cache for already cached file' do
29      file_a_caching_content = diff_note_a.diff_file.highlighted_diff_lines.map(&:to_hash)
30      Gitlab::DiscussionsDiff::HighlightCache
31        .write_multiple({ note_diff_file_a.id => file_a_caching_content })
32
33      file_b_caching_content = diff_note_b.diff_file.highlighted_diff_lines.map(&:to_hash)
34
35      expect(Gitlab::DiscussionsDiff::HighlightCache)
36        .to receive(:write_multiple)
37        .with({ note_diff_file_b.id => file_b_caching_content })
38        .and_call_original
39
40      subject.load_highlight
41    end
42
43    it 'does not write cache for empty mapping' do
44      allow(subject).to receive(:highlighted_lines_by_ids).and_return([])
45
46      expect(Gitlab::DiscussionsDiff::HighlightCache).not_to receive(:write_multiple)
47
48      subject.load_highlight
49    end
50
51    it 'does not write cache for resolved notes' do
52      diff_note_a.update_column(:resolved_at, Time.now)
53
54      file_b_caching_content = diff_note_b.diff_file.highlighted_diff_lines.map(&:to_hash)
55
56      expect(Gitlab::DiscussionsDiff::HighlightCache)
57        .to receive(:write_multiple)
58        .with({ note_diff_file_b.id => file_b_caching_content })
59        .and_call_original
60
61      subject.load_highlight
62    end
63
64    it 'loaded diff files have highlighted lines loaded' do
65      subject.load_highlight
66
67      diff_file_a = subject.find_by_id(note_diff_file_a.id)
68      diff_file_b = subject.find_by_id(note_diff_file_b.id)
69
70      expect(diff_file_a).to be_highlight_loaded
71      expect(diff_file_b).to be_highlight_loaded
72    end
73
74    it 'not loaded diff files does not have highlighted lines loaded' do
75      diff_note_a.update_column(:resolved_at, Time.now)
76
77      subject.load_highlight
78
79      diff_file_a = subject.find_by_id(note_diff_file_a.id)
80      diff_file_b = subject.find_by_id(note_diff_file_b.id)
81
82      expect(diff_file_a).not_to be_highlight_loaded
83      expect(diff_file_b).to be_highlight_loaded
84    end
85  end
86end
87