Files
warmbly/internal/scheduler/parse_time_test.go
T
Matthew Meszaros b83a1a3ce5 feat: make send-time optimization actually schedule sends (#226)
* feat: make send-time optimization actually schedule sends, instead of being a documented setting with no caller: advanced.OptimizeSendTime had zero call sites anywhere in the codebase, so an org that enabled send_time_optimization through PATCH /outreach/settings changed nothing about when its campaign mail left, and the API reference said so in a callout; the campaign scheduler now resolves the recipient's timezone from the contact's timezone custom field, then the country-code suffix of its email domain, then the org fallback, and holds the slot until that clock reaches a preferred hour, raising hardFloor so the task handler reschedules rather than sending immediately, the snap is forward-only and never crosses the campaign end date, the default flips to off so no existing workspace silently re-times its sends, and Settings > Sending gives the block its first UI

* feat: stop the recipient-hour gate deferring a send forever, and fix the window parser that silently disabled every campaign schedule: recipientSlot now searches for a moment BOTH calendars accept and yields when they never meet, because raising a hard floor at an hour the sender cannot serve made every tick re-derive it, defer, wake in the sender's window and defer again; separately parseTimeOfDay accepted only the 15:04 layout while start_time, end_time, warmup_start_time and warmup_end_time are Postgres time columns pgx renders as 09:00:00.000000, so every read parsed to 0 and effectiveWindows read that as unconstrained, leaving both the campaign sending window and its day-of-week gate off for every campaign on the legacy fields and pinning warmup to its 08:00-20:00 fallbacks; Settings > Sending also gains the MANAGE_SETTINGS gate a direct visit needs
2026-08-28 09:46:39 -07:00

64 lines
2.0 KiB
Go

package scheduler
import (
"testing"
"github.com/warmbly/warmbly/internal/models"
)
// The Postgres renderings are the whole point: start_time, end_time,
// warmup_start_time and warmup_end_time are `time` columns, so every read
// arrives with seconds and a fraction attached.
func TestParseTimeOfDay(t *testing.T) {
tests := []struct {
in string
want int
}{
{"09:00", 9 * 60},
{"09:00:00", 9 * 60},
{"09:00:00.000000", 9 * 60}, // what pgx hands back
{"17:30:00.000000", 17*60 + 30},
{"23:59:59.999999", 23*60 + 59},
{"00:00:00.000000", 0},
{" 08:15 ", 8*60 + 15},
{"", 0},
{"nonsense", 0},
{"25:00", 0}, // out of range, not a wrapped hour
{"12:60", 0},
{"12", 0},
{"-1:00", 0},
}
for _, tt := range tests {
if got := parseTimeOfDay(tt.in); got != tt.want {
t.Errorf("parseTimeOfDay(%q) = %d, want %d", tt.in, got, tt.want)
}
}
}
// effectiveWindows read an unparseable time as 0 for both ends, took
// "end <= start" as "no window", and returned an empty schedule — which
// nextScheduleSlot treats as "any time is allowed". A campaign restricted to
// weekday mornings was therefore free to send at 3am on a Sunday.
func TestEffectiveWindowsHonorsPostgresRenderedTimes(t *testing.T) {
c := campaignWithWindow("09:00:00.000000", "17:00:00.000000", 0b0011111) // Mon-Fri
sw := effectiveWindows(c)
if sw.IsEmpty() {
t.Fatal("windows are empty: the campaign schedule is not being enforced at all")
}
for _, wd := range []int{1, 2, 3, 4, 5} { // Monday..Friday
if len(sw[wd]) != 1 {
t.Fatalf("weekday %d has %d intervals, want 1", wd, len(sw[wd]))
}
if sw[wd][0].Start != 9*60 || sw[wd][0].End != 17*60 {
t.Errorf("weekday %d = %d-%d, want 540-1020", wd, sw[wd][0].Start, sw[wd][0].End)
}
}
if len(sw[0]) != 0 || len(sw[6]) != 0 {
t.Error("weekend has intervals; the day-of-week gate is not being applied")
}
}
func campaignWithWindow(start, end string, days uint8) *models.Campaign {
return &models.Campaign{StartTime: start, EndTime: end, Days: days}
}