|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe PullRequest, type: :model do |
| 6 | + describe '#labelled_invalid?' do |
| 7 | + context 'Pull request has a label for "invalid"' do |
| 8 | + let(:pr) { pr_helper(INVALID_LABEL_PR) } |
| 9 | + |
| 10 | + it 'is considered labelled invalid' do |
| 11 | + expect(pr.labelled_invalid?).to eq(true) |
| 12 | + end |
| 13 | + |
| 14 | + context "Pull request is merged" do |
| 15 | + let(:pr) { pr_helper(ELIGIBLE_INVALID_MERGED_PR) } |
| 16 | + |
| 17 | + it 'is not considered labelled invalid' do |
| 18 | + expect(pr.labelled_invalid?).to eq(false) |
| 19 | + end |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + context 'Pull request has a label for "❌ Invalid"' do |
| 24 | + let(:pr) { pr_helper(INVALID_EMOJI_LABEL_PR) } |
| 25 | + |
| 26 | + it 'is considered labelled labelled invalid' do |
| 27 | + expect(pr.labelled_invalid?).to eq(true) |
| 28 | + end |
| 29 | + end |
| 30 | + |
| 31 | + context 'Pull request has no labels' do |
| 32 | + let(:pr) { pr_helper(ELIGIBLE_PR) } |
| 33 | + |
| 34 | + it 'is not considered labelled invalid' do |
| 35 | + expect(pr.labelled_invalid?).to eq(false) |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + describe '#spam_repo' do |
| 41 | + # TODO -- need to stub SpamRepositoryService |
| 42 | + # PR is in spam repo initially |
| 43 | + # PR is waiting, then becomes in spam repo |
| 44 | + # PR is eligible, then becomes in spam repo |
| 45 | + end |
| 46 | + |
| 47 | + describe '#invalid_label' do |
| 48 | + context 'Pull request is labelled invalid initially' do |
| 49 | + let(:pr) { pr_helper(INVALID_LABEL_PR) } |
| 50 | + |
| 51 | + it 'is put it in the invalid_label state' do |
| 52 | + expect(pr.state).to eq('invalid_label') |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + context 'Pull request is waiting' do |
| 57 | + let(:pr) { pr_helper(IMMATURE_PR) } |
| 58 | + |
| 59 | + it 'is in the waiting state initially' do |
| 60 | + expect(pr.state).to eq('waiting') |
| 61 | + end |
| 62 | + |
| 63 | + context 'Pull request becomes labelled invalid' do |
| 64 | + before do |
| 65 | + stub_helper(pr, INVALID_LABEL_PR, { 'id' => pr.github_id }) |
| 66 | + pr.check_state |
| 67 | + end |
| 68 | + |
| 69 | + it 'is put it in the invalid_label state' do |
| 70 | + expect(pr.state).to eq('invalid_label') |
| 71 | + end |
| 72 | + |
| 73 | + context 'Seven days pass from pull request creation' do |
| 74 | + before do |
| 75 | + travel_to pr.waiting_since + 7.days |
| 76 | + pr.check_state |
| 77 | + end |
| 78 | + |
| 79 | + it 'remains in the invalid_label state' do |
| 80 | + expect(pr.state).to eq('invalid_label') |
| 81 | + end |
| 82 | + |
| 83 | + after { travel_back } |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + context 'Pull request is eligible' do |
| 89 | + let(:pr) { pr_helper(ELIGIBLE_PR) } |
| 90 | + |
| 91 | + it 'is in the eligible state initially' do |
| 92 | + expect(pr.state).to eq('eligible') |
| 93 | + end |
| 94 | + |
| 95 | + context 'Pull request becomes labelled invalid' do |
| 96 | + before do |
| 97 | + stub_helper(pr, INVALID_LABEL_PR, { 'id' => pr.github_id }) |
| 98 | + pr.check_state |
| 99 | + end |
| 100 | + |
| 101 | + it 'remains in the eligible state' do |
| 102 | + expect(pr.state).to eq('eligible') |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + describe '#waiting' do |
| 109 | + context "Pull request is valid and created less than seven days ago" do |
| 110 | + let(:pr) { pr_helper(IMMATURE_PR) } |
| 111 | + |
| 112 | + it 'is put it in the waiting state' do |
| 113 | + expect(pr.state).to eq('waiting') |
| 114 | + end |
| 115 | + |
| 116 | + it 'has the waiting_since date set to the created date' do |
| 117 | + expect(pr.waiting_since).to eq(pr.created_at) |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + context "Pull request is in an invalid repo initially" do |
| 122 | + # TODO -- need to stub SpamRepositoryService |
| 123 | + end |
| 124 | + |
| 125 | + context "Pull request is labelled invalid initially" do |
| 126 | + let(:pr) { pr_helper(INVALID_LABEL_PR) } |
| 127 | + |
| 128 | + it 'is in the invalid_label state initially' do |
| 129 | + expect(pr.state).to eq('invalid_label') |
| 130 | + end |
| 131 | + |
| 132 | + it 'has no set waiting_since' do |
| 133 | + expect(pr.waiting_since).to be_nil |
| 134 | + end |
| 135 | + |
| 136 | + context 'Pull request has invalid label removed' do |
| 137 | + before do |
| 138 | + freeze_time |
| 139 | + |
| 140 | + stub_helper(pr, IMMATURE_PR, { 'id' => pr.github_id }) |
| 141 | + pr.check_state |
| 142 | + end |
| 143 | + |
| 144 | + it 'is put it in the waiting state' do |
| 145 | + expect(pr.state).to eq('waiting') |
| 146 | + end |
| 147 | + |
| 148 | + it 'has the waiting_since date set to now' do |
| 149 | + expect(pr.waiting_since).to eq(Time.zone.now) |
| 150 | + expect(pr.waiting_since).to_not eq(pr.created_at) |
| 151 | + end |
| 152 | + |
| 153 | + after { travel_back } |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + context "Pull request is labelled invalid initially" do |
| 158 | + let(:pr) { pr_helper(INVALID_LABEL_PR) } |
| 159 | + |
| 160 | + it 'is in the invalid_label state initially' do |
| 161 | + expect(pr.state).to eq('invalid_label') |
| 162 | + end |
| 163 | + |
| 164 | + it 'has no set waiting_since' do |
| 165 | + expect(pr.waiting_since).to be_nil |
| 166 | + end |
| 167 | + |
| 168 | + context 'Pull request is merged' do |
| 169 | + before do |
| 170 | + freeze_time |
| 171 | + |
| 172 | + stub_helper(pr, IMMATURE_INVALID_MERGED_PR, |
| 173 | + { 'id' => pr.github_id }) |
| 174 | + pr.check_state |
| 175 | + end |
| 176 | + |
| 177 | + it 'is put it in the waiting state' do |
| 178 | + expect(pr.state).to eq('waiting') |
| 179 | + end |
| 180 | + |
| 181 | + it 'has the waiting_since date set to now' do |
| 182 | + expect(pr.waiting_since).to eq(Time.zone.now) |
| 183 | + expect(pr.waiting_since).to_not eq(pr.created_at) |
| 184 | + end |
| 185 | + |
| 186 | + after { travel_back } |
| 187 | + end |
| 188 | + end |
| 189 | + end |
| 190 | + |
| 191 | + describe '#eligible' do |
| 192 | + context "Pull request is valid and created over seven days ago" do |
| 193 | + let(:pr) { pr_helper(ELIGIBLE_PR) } |
| 194 | + |
| 195 | + it 'is put it in the eligible state' do |
| 196 | + expect(pr.state).to eq('eligible') |
| 197 | + end |
| 198 | + end |
| 199 | + |
| 200 | + context "Pull request is valid and created less than seven days ago" do |
| 201 | + let(:pr) { pr_helper(IMMATURE_PR) } |
| 202 | + |
| 203 | + it 'is put it in the waiting state' do |
| 204 | + expect(pr.state).to eq('waiting') |
| 205 | + end |
| 206 | + |
| 207 | + it 'has the waiting_since date set to the created date' do |
| 208 | + expect(pr.waiting_since).to eq(pr.created_at) |
| 209 | + end |
| 210 | + |
| 211 | + context "Seven days pass from pull request creation" do |
| 212 | + before do |
| 213 | + travel_to pr.waiting_since + 7.days |
| 214 | + pr.check_state |
| 215 | + end |
| 216 | + |
| 217 | + it 'is put it in the eligible state' do |
| 218 | + expect(pr.state).to eq('eligible') |
| 219 | + end |
| 220 | + |
| 221 | + after { travel_back } |
| 222 | + end |
| 223 | + end |
| 224 | + end |
| 225 | + |
| 226 | + def pr_helper(hash) |
| 227 | + PullRequest.delete_all |
| 228 | + PullRequest.from_github_pull_request(github_pull_request(hash)) |
| 229 | + end |
| 230 | + |
| 231 | + def stub_helper(target, hash, merge = {}) |
| 232 | + allow(target).to receive(:github_pull_request) |
| 233 | + .and_return(github_pull_request(hash.merge(merge))) |
| 234 | + end |
| 235 | +end |
0 commit comments