From 906263ee20928ab2557fa562cb0c516193ec82f3 Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma Date: Mon, 20 Jul 2026 11:49:03 +0530 Subject: [PATCH] test(automations): anchor delayed due_at to the triggering event time --- spec/models/automation_rule_pending_execution_spec.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/spec/models/automation_rule_pending_execution_spec.rb b/spec/models/automation_rule_pending_execution_spec.rb index 7398174ae..4826ab6d0 100644 --- a/spec/models/automation_rule_pending_execution_spec.rb +++ b/spec/models/automation_rule_pending_execution_spec.rb @@ -72,6 +72,14 @@ RSpec.describe AutomationRulePendingExecution do expect(row.due_at).to be_within(5.seconds).of(60.minutes.from_now) end + it 'anchors due_at to the event time, not when a backlogged listener runs' do + conversation.update!(status_changed_at: 30.minutes.ago) + described_class.schedule(rule: rule, conversation: conversation) + + # A 60-minute rule on a status that changed 30 minutes ago is already 30 minutes into its wait. + expect(described_class.last.due_at).to be_within(5.seconds).of(30.minutes.from_now) + end + it 'does not reset the clock for a repeated status episode' do described_class.schedule(rule: rule, conversation: conversation) original_due_at = described_class.last.due_at @@ -86,10 +94,11 @@ RSpec.describe AutomationRulePendingExecution do first_reply = create(:message, conversation: conversation, account: account, message_type: :outgoing) described_class.schedule(rule: rule, conversation: conversation, message: first_reply) - second_reply = create(:message, conversation: conversation, account: account, message_type: :outgoing) travel_to(30.minutes.from_now) do + second_reply = create(:message, conversation: conversation, account: account, message_type: :outgoing) described_class.schedule(rule: rule, conversation: conversation, message: second_reply) + # due_at is re-anchored to the new reply's created_at, not the original schedule time. expect(described_class.count).to eq(1) expect(described_class.last.message_id).to eq(second_reply.id) expect(described_class.last.due_at).to be_within(5.seconds).of(60.minutes.from_now)