summaryrefslogtreecommitdiff
path: root/spec/experiments/new_project_readme_experiment_spec.rb
blob: 17e28cf6e7f773a1350e733b8166b33a313bbdb3 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe NewProjectReadmeExperiment, :experiment do
  subject { described_class.new(actor: actor) }

  let(:actor) { User.new(id: 42, created_at: Time.current) }

  before do
    stub_experiments(new_project_readme: :control)
  end

  describe "exclusions" do
    let(:threshold) { described_class::MAX_ACCOUNT_AGE }

    it { is_expected.to exclude(actor: User.new(created_at: (threshold + 1.minute).ago)) }
    it { is_expected.not_to exclude(actor: User.new(created_at: (threshold - 1.minute).ago)) }
  end

  describe "the control behavior" do
    subject { described_class.new(actor: actor).run(:control) }

    it { is_expected.to be false }
  end

  describe "the candidate behavior" do
    subject { described_class.new(actor: actor).run(:candidate) }

    it { is_expected.to be true }
  end

  context "when tracking initial writes" do
    let!(:project) { create(:project, :repository) }

    def stub_gitaly_count(count = 1)
      allow(Gitlab::GitalyClient).to receive(:call).and_call_original
      allow(Gitlab::GitalyClient).to receive(:call).with(anything, :commit_service, :count_commits, anything, anything)
        .and_return(double(count: count))
    end

    before do
      stub_gitaly_count
    end

    it "tracks an event for the first commit on a project with a repository" do
      expect(subject).to receive(:track).with(:write, property: project.created_at.to_s, value: 1).and_call_original

      subject.track_initial_writes(project)
    end

    it "tracks an event for the second commit on a project with a repository" do
      stub_gitaly_count(2)

      expect(subject).to receive(:track).with(:write, property: project.created_at.to_s, value: 2).and_call_original

      subject.track_initial_writes(project)
    end

    it "doesn't track if the repository has more then 2 commits" do
      stub_gitaly_count(3)

      expect(subject).not_to receive(:track)

      subject.track_initial_writes(project)
    end

    it "doesn't track when we generally shouldn't" do
      allow(subject).to receive(:should_track?).and_return(false)

      expect(subject).not_to receive(:track)

      subject.track_initial_writes(project)
    end

    it "doesn't track if the project is older" do
      expect(project).to receive(:created_at).and_return(described_class::EXPERIMENT_START_DATE - 1.minute)

      expect(subject).not_to receive(:track)

      subject.track_initial_writes(project)
    end

    it "handles exceptions by logging them" do
      allow(Gitlab::GitalyClient).to receive(:call).with(anything, :commit_service, :count_commits, anything, anything)
        .and_raise(e = StandardError.new('_message_'))

      expect(Gitlab::ErrorTracking).to receive(:track_exception).with(e, experiment: 'new_project_readme')

      subject.track_initial_writes(project)
    end
  end
end