diff --git a/app/models/card/entropy.rb b/app/models/card/entropy.rb index 50a624a45..4bfdba611 100644 --- a/app/models/card/entropy.rb +++ b/app/models/card/entropy.rb @@ -8,7 +8,7 @@ module Card::Entropy scope :entropic_by, ->(period_name) do left_outer_joins(collection: :entropy_configuration) .where("last_active_at <= DATETIME('now', '-' || COALESCE(entropy_configurations.#{period_name}, (?)) || ' seconds')", - Entropy::Configuration.where(id: Account.sole.default_entropy_configuration).select("id").limit(1)) + Entropy::Configuration.where(id: Account.sole.default_entropy_configuration).select(period_name).limit(1)) end scope :stagnated, -> do diff --git a/test/fixtures/entropy/configurations.yml b/test/fixtures/entropy/configurations.yml index cab952c69..2ead35c67 100644 --- a/test/fixtures/entropy/configurations.yml +++ b/test/fixtures/entropy/configurations.yml @@ -5,8 +5,8 @@ writebook_collection: container: writebook (Collection) - auto_reconsider_period: <%= 30.days.to_i %> - auto_close_period: <%= 30.days.to_i %> + auto_reconsider_period: <%= 90.days.to_i %> + auto_close_period: <%= 90.days.to_i %> private_collection: container: private (Collection) diff --git a/test/models/card/entropy_test.rb b/test/models/card/entropy_test.rb index 682c6dc0a..d9c569515 100644 --- a/test/models/card/entropy_test.rb +++ b/test/models/card/entropy_test.rb @@ -5,15 +5,58 @@ class Card::EntropyTest < ActiveSupport::TestCase Current.session = sessions(:david) end - test "auto_close_at infers the period from the collection" do + test "auto_close_at uses the period defined in the account by default" do + freeze_time + + entropy_configurations(:writebook_collection).destroy + entropy_configurations("37s_account").reload.update! auto_close_period: 456.days + cards(:layout).update! last_active_at: 2.day.ago + assert_equal (456 - 2).days.from_now, cards(:layout).auto_close_at + end + + test "auto_close_at infers the period from the collection when present" do freeze_time entropy_configurations(:writebook_collection).update! auto_close_period: 123.days cards(:layout).update! last_active_at: 2.day.ago - assert_equal (123-2).days.from_now, cards(:layout).auto_close_at + assert_equal (123 - 2).days.from_now, cards(:layout).auto_close_at end - test "auto close all due" do + test "auto_reconsider_at uses the period defined in the account by default" do + freeze_time + + cards(:layout).engage + entropy_configurations(:writebook_collection).destroy + entropy_configurations("37s_account").reload.update! auto_reconsider_period: 456.days + cards(:layout).update! last_active_at: 2.day.ago + assert_equal (456 - 2).days.from_now, cards(:layout).auto_reconsider_at + end + + test "auto_reconsider_at infers the period from the collection when present" do + freeze_time + + cards(:layout).engage + entropy_configurations(:writebook_collection).update! auto_reconsider_period: 123.days + cards(:layout).update! last_active_at: 2.day.ago + assert_equal (123 - 2).days.from_now, cards(:layout).auto_reconsider_at + end + + test "auto close all due using the default account entropy configuration" do + cards(:logo, :shipping).each(&:reconsider) + entropy_configurations(:writebook_collection).destroy + + cards(:logo).update!(last_active_at: 1.day.ago - entropy_configurations("37s_account").auto_close_period) + cards(:shipping).update!(last_active_at: 1.day.from_now - entropy_configurations("37s_account").auto_close_period) + + assert_difference -> { Card.closed.count }, +1 do + Card.auto_close_all_due + end + + assert cards(:logo).reload.closed? + assert_not cards(:shipping).reload.closed? + end + + test "auto close all due using entropy configuration defined at the collection level" do cards(:logo, :shipping).each(&:reconsider) cards(:logo).update!(last_active_at: 1.day.ago - entropy_configurations(:writebook_collection).auto_close_period) @@ -27,16 +70,22 @@ class Card::EntropyTest < ActiveSupport::TestCase assert_not cards(:shipping).reload.closed? end - test "don't auto close those cards where the collection has no auto close period" do - cards(:logo, :shipping).each(&:reconsider) + test "auto consider all stagnated using the default account entropy configuration" do + travel_to Time.current - collections(:writebook).entropy_configuration.destroy + cards(:logo, :shipping).each(&:engage) + entropy_configurations(:writebook_collection).destroy - assert_no_difference -> { Card.closed.count } do - Card.auto_close_all_due + cards(:logo).update!(last_active_at: 1.day.ago - entropy_configurations("37s_account").auto_close_period) + cards(:shipping).update!(last_active_at: 1.day.from_now - entropy_configurations("37s_account").auto_close_period) + + assert_difference -> { Card.considering.count }, +1 do + Card.auto_reconsider_all_stagnated end - assert_not cards(:logo).reload.closed? + assert cards(:shipping).reload.doing? + assert cards(:logo).reload.considering? + assert_equal Time.current, cards(:logo).last_active_at end test "auto_reconsider_all_stagnated" do @@ -44,8 +93,8 @@ class Card::EntropyTest < ActiveSupport::TestCase cards(:logo, :shipping).each(&:engage) - cards(:logo).update!(last_active_at: 1.day.ago - Card::AUTO_RECONSIDER_PERIOD) - cards(:shipping).update!(last_active_at: 1.day.from_now - Card::AUTO_RECONSIDER_PERIOD) + cards(:logo).update!(last_active_at: 1.day.ago - entropy_configurations("writebook_collection").auto_close_period) + cards(:shipping).update!(last_active_at: 1.day.from_now - entropy_configurations("writebook_collection").auto_close_period) assert_difference -> { Card.considering.count }, +1 do Card.auto_reconsider_all_stagnated