Files
warmbly/internal/app/consumer/warmup_reply_back_test.go
T
Matthew Meszaros d47d31b7c4 feat: let a warmup recipient answer the mailbox that wrote to it (#230)
* feat: let a warmup recipient answer the mailbox that just wrote to it, so a thread reads as a conversation rather than two mailboxes monologuing on their own ramps: warmup_tasks.target_account_id was written as nil and never read by anything, so a reply only happened when the recipient's own ramp fired AND the draw happened to land on that partner; a verified receipt now sometimes re-points the recipient's pending warmup task at the sender 25 minutes to 5 hours later inside its own warmup hours, which is a re-pointing rather than new work because only one warmup task may be pending per mailbox, it can never delay a send the mailbox had planned sooner, and it stops before the thread cap so replies cannot answer replies forever; the clock parser also moves into models.ClockMinutes so a second copy of the HH:MM parsing that silently disabled every sending window cannot drift back in

* feat: stop the reply-back drawing the reply rate twice, and stop its jitter escaping a short warmup window: the scheduler drew the recipient's reply rate to decide whether to answer at all, then the task handler drew it again to decide reply-versus-new, so a 30 percent reply rate produced a 9 percent answer rate and a directed task could send a fresh message to the mailbox it was meant to be answering; a directed task now IS the reply, and the opening-time jitter is capped to the window width so a mailbox warming 09:00 to 09:20 is not scheduled past its own close
2026-08-28 10:40:25 -07:00

77 lines
2.5 KiB
Go

package jobs
import (
"testing"
"time"
"github.com/warmbly/warmbly/internal/models"
)
func acct(tz, start, end string) *models.Email {
return &models.Email{Timezone: tz, WarmupStartTime: start, WarmupEndTime: end}
}
func TestWithinWarmupHours(t *testing.T) {
a := acct("UTC", "09:00:00.000000", "17:00:00.000000")
// Inside the window: untouched, so the drawn delay survives.
inside := time.Date(2026, 3, 4, 11, 20, 0, 0, time.UTC)
if got := withinWarmupHours(inside, a); !got.Equal(inside) {
t.Errorf("in-window time moved: got %s want %s", got, inside)
}
// Before it opens: forward to the opening, same day.
early := time.Date(2026, 3, 4, 6, 0, 0, 0, time.UTC)
got := withinWarmupHours(early, a)
if got.Day() != 4 || got.Hour() < 9 || got.Hour() > 9 {
t.Errorf("early = %s, want just after 09:00 the same day", got)
}
if got.Before(early) == false && got.Hour() != 9 {
t.Errorf("early = %s, want the 09:00 opening", got)
}
// After it closes: the next day's opening.
late := time.Date(2026, 3, 4, 22, 0, 0, 0, time.UTC)
got = withinWarmupHours(late, a)
if got.Day() != 5 || got.Hour() != 9 {
t.Errorf("late = %s, want 09:xx the next day", got)
}
}
// A window shorter than the jitter must not push the reply past its own close.
func TestWithinWarmupHoursClampsJitterToAShortWindow(t *testing.T) {
a := acct("UTC", "09:00", "09:20")
closeAt := 9*60 + 20
for i := 0; i < 200; i++ {
for _, at := range []time.Time{
time.Date(2026, 3, 4, 6, 0, 0, 0, time.UTC), // before open
time.Date(2026, 3, 4, 22, 0, 0, 0, time.UTC), // after close
} {
got := withinWarmupHours(at, a)
if mins := got.Hour()*60 + got.Minute(); mins > closeAt {
t.Fatalf("scheduled %s, past the 09:20 close", got)
}
}
}
}
// An inverted window (close before open) has no reachable slot, so the time is
// left alone rather than moved somewhere arbitrary.
func TestWithinWarmupHoursWithAnInvertedWindowIsANoop(t *testing.T) {
at := time.Date(2026, 3, 4, 3, 0, 0, 0, time.UTC)
if got := withinWarmupHours(at, acct("UTC", "20:00", "08:00")); !got.Equal(at) {
t.Errorf("inverted window moved the time to %s", got)
}
}
// Unreadable times fall back to the documented 08:00-20:00 defaults rather than
// leaving the reply wherever it landed, which could be 03:00.
func TestWithinWarmupHoursFallsBackToBusinessHours(t *testing.T) {
at := time.Date(2026, 3, 4, 3, 0, 0, 0, time.UTC)
got := withinWarmupHours(at, acct("UTC", "", ""))
if got.Hour() < 8 || got.Hour() > 8 {
t.Errorf("got %s, want the 08:00 default opening", got)
}
}