<commit_before>defmodule SlackCoder.Github.RepositoryTest do
  use Pavlov.Case, async: true
  import Pavlov.Syntax.Expect
  alias SlackCoder.Models.PR
  import SlackCoder.Support.Github
  alias SlackCoder.Github.Watchers.Repository, as: Watcher
  use Timex

  describe "stale notifications" do
    let :now, do: Timex.Date.now
    let :two_hours_ago, do: Timex.Date.now |> Timex.Date.subtract(Timex.Time.to_timestamp(2, :hours))
    let :three_hours_ago, do: Timex.Date.now |> Timex.Date.subtract(Timex.Time.to_timestamp(3, :hours))
    let :ten_hours_ago, do: Timex.Date.now |> Timex.Date.subtract(Timex.Time.to_timestamp(10, :hours))
    describe "sends a notification" do
      it "equal to the backoff hours" do
        cs = PR.changeset pr_with(%{backoff: 2, latest_comment: two_hours_ago, opened_at: three_hours_ago}), %{latest_comment: two_hours_ago}
        {pr, send_notification} = Watcher.stale_pr cs
        assert 4 = pr.backoff # Bumps to the next
        assert true = send_notification
      end
      it "more than the backoff hours" do
        cs = PR.changeset pr_with(%{backoff: 2, latest_comment: three_hours_ago, opened_at: three_hours_ago}), %{latest_comment: three_hours_ago}
        {pr, send_notification} = Watcher.stale_pr cs
        assert 4 = pr.backoff # Bumps to the next
        assert true = send_notification
      end
      it "more than one backoff step" do
        cs = PR.changeset pr_with(%{backoff: 2, latest_comment: ten_hours_ago, opened_at: ten_hours_ago}), %{latest_comment: ten_hours_ago}
        {pr, send_notification} = Watcher.stale_pr cs
        assert 16 = pr.backoff
        assert true = send_notification
      end
      it "handles a new PR" do
        cs = PR.changeset pr_with(%{backoff: 2, opened_at: ten_hours_ago}), %{latest_comment: ten_hours_ago}
        {pr, send_notification} = Watcher.stale_pr cs
        assert 16 = pr.backoff
        assert true = send_notification
      end
    end

    describe "does not send a notification" do
      it "below the backoff" do
        cs = PR.changeset pr_with(%{backoff: 4, latest_comment: two_hours_ago, opened_at: three_hours_ago}), %{latest_comment: two_hours_ago}
        {pr, send_notification} = Watcher.stale_pr cs
        assert 4 = pr.backoff # Keeps the same
        assert send_notification == nil
      end
      it "handles a new PR" do
        cs = PR.changeset pr_with(%{backoff: 4, opened_at: three_hours_ago}), %{latest_comment: two_hours_ago}
        {pr, send_notification} = Watcher.stale_pr cs
        assert 4 = pr.backoff # Keeps the same
        assert send_notification == nil
      end
      it "1 backoff" do
        cs = PR.changeset pr_with(%{backoff: 1, latest_comment: now, opened_at: three_hours_ago}), %{latest_comment: now}
        {pr, send_notification} = Watcher.stale_pr cs
        assert 1 = pr.backoff # Keeps the same
        assert send_notification == nil
      end
    end
  end

end<commit_msg>Ensure date diff is correct.<commit_after>defmodule SlackCoder.Github.RepositoryTest do
  use Pavlov.Case, async: true
  import Pavlov.Syntax.Expect
  alias SlackCoder.Models.PR
  import SlackCoder.Support.Github
  alias SlackCoder.Github.Helper
  alias SlackCoder.Github.Watchers.Repository, as: Watcher
  use Timex

  describe "stale notifications" do
    let :now, do: Timex.Date.now
    let :two_hours_ago, do: Timex.Date.now |> Timex.Date.subtract(Timex.Time.to_timestamp(2, :hours))
    let :three_hours_ago, do: Timex.Date.now |> Timex.Date.subtract(Timex.Time.to_timestamp(3, :hours))
    let :ten_hours_ago, do: Timex.Date.now |> Timex.Date.subtract(Timex.Time.to_timestamp(10, :hours))
    describe "sends a notification" do
      it "equal to the backoff hours" do
        cs = PR.changeset pr_with(%{backoff: 2, latest_comment: two_hours_ago, opened_at: three_hours_ago}), %{latest_comment: two_hours_ago}
        {pr, send_notification} = Watcher.stale_pr cs
        assert 4 = pr.backoff # Bumps to the next
        assert true = send_notification
      end
      it "more than the backoff hours" do
        cs = PR.changeset pr_with(%{backoff: 2, latest_comment: three_hours_ago, opened_at: three_hours_ago}), %{latest_comment: three_hours_ago}
        {pr, send_notification} = Watcher.stale_pr cs
        assert 4 = pr.backoff # Bumps to the next
        assert true = send_notification
      end
      it "more than one backoff step" do
        cs = PR.changeset pr_with(%{backoff: 2, latest_comment: ten_hours_ago, opened_at: ten_hours_ago}), %{latest_comment: ten_hours_ago}
        {pr, send_notification} = Watcher.stale_pr cs
        assert 16 = pr.backoff
        assert true = send_notification
      end
      it "handles a new PR" do
        cs = PR.changeset pr_with(%{backoff: 2, opened_at: ten_hours_ago}), %{latest_comment: ten_hours_ago}
        {pr, send_notification} = Watcher.stale_pr cs
        assert 16 = pr.backoff
        assert 10 = Timex.Date.diff(pr.latest_comment, Helper.now, :hours)
        assert true = send_notification
      end
    end

    describe "does not send a notification" do
      it "below the backoff" do
        cs = PR.changeset pr_with(%{backoff: 4, latest_comment: two_hours_ago, opened_at: three_hours_ago}), %{latest_comment: two_hours_ago}
        {pr, send_notification} = Watcher.stale_pr cs
        assert 4 = pr.backoff # Keeps the same
        assert 2 = Timex.Date.diff(pr.latest_comment, Helper.now, :hours)
        assert send_notification == nil
      end
      it "handles a new PR" do
        cs = PR.changeset pr_with(%{backoff: 4, opened_at: three_hours_ago}), %{latest_comment: two_hours_ago}
        {pr, send_notification} = Watcher.stale_pr cs
        assert 4 = pr.backoff # Keeps the same
        assert 2 = Timex.Date.diff(pr.latest_comment, Helper.now, :hours)
        assert send_notification == nil
      end
      it "1 backoff" do
        cs = PR.changeset pr_with(%{backoff: 1, latest_comment: now, opened_at: three_hours_ago}), %{latest_comment: now}
        {pr, send_notification} = Watcher.stale_pr cs
        assert 1 = pr.backoff # Keeps the same
        assert 0 = Timex.Date.diff(pr.latest_comment, Helper.now, :hours)
        assert send_notification == nil
      end
    end
  end

end
{'fraction_non_alphanumeric': 0.09475709475709476, 'fraction_numerical': 0.007535674202340869, 'mean_word_length': 3.1476063829787235, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5654676', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2034, 'n_words': 551}