Skip to content

LG-11441 - Fix Flakey In-person Enrollment test#9477

Merged
JackRyan1989 merged 5 commits intomainfrom
jryan/lg-11441-fix-flakey-enrollment-test
Nov 1, 2023
Merged

LG-11441 - Fix Flakey In-person Enrollment test#9477
JackRyan1989 merged 5 commits intomainfrom
jryan/lg-11441-fix-flakey-enrollment-test

Conversation

@JackRyan1989
Copy link
Contributor

🎫 Ticket

🛠 Summary of changes

The issue appears not to be with the spec, but rather with how we were calculating the difference between the start date and the current date. Now using the Time object to perform simple subtraction between the start date and current date.

📜 Testing Plan

Make sure that /spec/models/in_person_enrollment_spec.rb doesn't fail intermittently.

@JackRyan1989 JackRyan1989 self-assigned this Oct 28, 2023
@JackRyan1989 JackRyan1989 marked this pull request as ready for review October 28, 2023 02:34
Copy link
Contributor

@aduth aduth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the issue is with the underlying logic, can we update or add spec coverage to reliably check against regressions?

@JackRyan1989
Copy link
Contributor Author

@aduth I think the tests I added are sufficient, let me know your thoughts.

freeze_time do
enrollment = create(
:in_person_enrollment,
enrollment_established_at: Time.zone.now - 9.days,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a config this is based on that could be used? Ditto for the other places where the tests use 9.5, 10, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could set the values in let blocks like above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JackRyan1989 I think @mitchellhenke's point is that the timespans depend on the in_person_enrollment_validity_in_days configuration, so we should control that either by basing the timespans on the configuration, or stubbing the configuration to a fixed value for the duration of the test.

The risk is that if we change the default configuration in the future or if a developer has their own overrides for the configuration value, the tests may suddenly start breaking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah whoops, I got confused. I thought you were specifically talking about the calculation for the enrollment_established_at value, and that can be any date and is not necessarily dependent on the configuration.

I've updated the expectations to match the previous pattern, e.g. expect(enrollment.days_to_due_date).to eq(validity_in_days - days_ago_established_at)

So for example:

expect(enrollment.days_to_due_date).to eq((validity_in_days - nine).to_i)
  expect(enrollment.due_date).to(
            eq((validity_in_days - nine).days.from_now),
            )

I think I understand now, but please let me know if I'm still missing something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, yeah, I didn't explain it well. The goal would be to avoid a change in validity_in_days changing and breaking the tests. fa67fbb was I think the preferred approach.

My suggestion is something along these lines:

    it 'due_date returns the enrollment expiration date based on when it was established' do
      freeze_time do
        enrollment = create(
          :in_person_enrollment,
          enrollment_established_at: 3.days.ago,
        )
        expect(enrollment.due_date).to eq((validity_in_days - 3).days.from_now)
      end
    end

    it 'days_to_due_date returns the number of days left until the due date' do
      freeze_time do
        enrollment = create(
          :in_person_enrollment,
          enrollment_established_at: 3.days.ago,
        )
        expect(enrollment.days_to_due_date).to eq(validity_in_days - 3)
      end
    end

    context 'check edges to confirm date calculation is correct' do
      it 'returns the correct due date and days to due date with 1 day left' do
        freeze_time do
          enrollment = create(
            :in_person_enrollment,
            enrollment_established_at: (validity_in_days - 1).days.ago,
          )
          expect(enrollment.days_to_due_date).to eq(1)
          expect(enrollment.due_date).to eq(Time.zone.now + 1.day)
        end
      end

      it 'returns the correct due date and days to due date with 0.5 days left' do
        freeze_time do
          enrollment = create(
            :in_person_enrollment,
            enrollment_established_at: (validity_in_days - 0.5).days.ago,
          )
          expect(enrollment.days_to_due_date).to eq(0)
          expect(enrollment.due_date).to eq(Time.zone.now + 0.5.days)
        end
      end

      it 'returns the correct due date and days to due date with 0 days left' do
        freeze_time do
          enrollment = create(
            :in_person_enrollment,
            enrollment_established_at: validity_in_days.days.ago,
          )
          expect(enrollment.days_to_due_date).to eq(0)
          expect(enrollment.due_date).to eq(Time.zone.now)
        end
      end
    end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for breaking this down @mitchellhenke and @aduth. Appropriate changes should be up

Copy link
Contributor

@aduth aduth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clarifying question, but otherwise LGTM 👍

Comment on lines +438 to +440
enrollment_established_at: (validity_in_days - 0.5).days.ago,
)
expect(enrollment.days_to_due_date).to eq(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just want to double-check that rounding down to zero is the expected result here? I see some related subtraction math on this number that might worry me if we tell the user that they "have -1 days remaining" or some such.

# Reminder is exclusive of the day the email is sent (1 less than days_to_due_date)
def days_remaining
enrollment.days_to_due_date - 1
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduth great callout. I think we're ok. I'm currently digging through the ready_to_verify_presenter_spec: I just added two tests locally for 1 day remaining and 0 days remaining. 1 day remaining works, 0 days remaining fails because we get a -1, as expected by your observation.

The other test already in that file passes, so I think the behavior is expected. We do have a in_person_deadline_passed email that goes out, and I'm just digging around now for the logic that controls when that is sent so I can confirm that on 0 days remaining that email is sent and the in_person_ready_to_verify_reminder email is not. I'll confirm in another message once I'm done digging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the email_reminder_job the latest we send the reminder is 1 day before expiration. In that case we'd say '0 days left' or something similar, and we would not send a negative value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok 👍 Thanks for checking

@JackRyan1989 JackRyan1989 merged commit 09711c5 into main Nov 1, 2023
@JackRyan1989 JackRyan1989 deleted the jryan/lg-11441-fix-flakey-enrollment-test branch November 1, 2023 16:07
@amirbey amirbey mentioned this pull request Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants