432 lines
12 KiB
Go
432 lines
12 KiB
Go
package dyzurbot
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"math/rand"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type assignCall struct {
|
|
week DutyWeek
|
|
p1, p2 string
|
|
}
|
|
|
|
// fireHarness builds a Reminder over a fixed rota/now with capturing send and
|
|
// assign funcs, so fire()'s branches can be asserted without Telegram or Google.
|
|
type fireHarness struct {
|
|
sends []string
|
|
assigns []assignCall
|
|
assignFn AssignFunc
|
|
}
|
|
|
|
func newFireHarness(t *testing.T, rota Rota, now time.Time, seed int64) (*Reminder, *fireHarness) {
|
|
t.Helper()
|
|
h := &fireHarness{}
|
|
send := func(_ context.Context, text string) error {
|
|
h.sends = append(h.sends, text)
|
|
return nil
|
|
}
|
|
assign := func(ctx context.Context, week DutyWeek, p1, p2 string) error {
|
|
h.assigns = append(h.assigns, assignCall{week, p1, p2})
|
|
if h.assignFn != nil {
|
|
return h.assignFn(ctx, week, p1, p2)
|
|
}
|
|
return nil
|
|
}
|
|
r := &Reminder{
|
|
store: &Store{rota: rota},
|
|
send: send,
|
|
assign: assign,
|
|
cfg: ReminderConfig{Loc: warsaw(t)},
|
|
now: func() time.Time { return now },
|
|
rng: rand.New(rand.NewSource(seed)),
|
|
}
|
|
return r, h
|
|
}
|
|
|
|
func TestFire(t *testing.T) {
|
|
loc := warsaw(t)
|
|
mon := time.Date(2026, 6, 15, 9, 0, 0, 0, loc) // Mon; this week starts 2026-06-15
|
|
people := []Person{
|
|
{Name: "Ala", DutyCount: 1},
|
|
{Name: "Bartek", DutyCount: 2},
|
|
{Name: "Cela", DutyCount: 3},
|
|
}
|
|
// filled is a fully-assigned week reused wherever a horizon must be left alone.
|
|
filled := func(start string, row int) DutyWeek {
|
|
return DutyWeek{WeekStart: day(t, start), Person1: "X", Person2: "Y", Row: row}
|
|
}
|
|
thisWeekFilled := DutyWeek{WeekStart: day(t, "2026-06-15"), Person1: "Jan", Person2: "Anna", Row: 5}
|
|
|
|
t.Run("this week empty -> reminder only, never assigns", func(t *testing.T) {
|
|
rota := Rota{
|
|
Weeks: []DutyWeek{
|
|
{WeekStart: day(t, "2026-06-15"), Row: 5}, // this: empty
|
|
filled("2026-06-22", 6), // +1: filled
|
|
filled("2026-06-29", 7), // +2: filled
|
|
},
|
|
People: people,
|
|
}
|
|
r, h := newFireHarness(t, rota, mon, 1)
|
|
r.fire(context.Background())
|
|
|
|
if len(h.assigns) != 0 {
|
|
t.Errorf("assigns = %d, want 0 (this week is never auto-assigned)", len(h.assigns))
|
|
}
|
|
if len(h.sends) != 1 {
|
|
t.Errorf("sends = %q, want exactly the this-week reminder", h.sends)
|
|
}
|
|
})
|
|
|
|
t.Run("week ahead empty -> assign two lowest, back-fill row 6, announce", func(t *testing.T) {
|
|
rota := Rota{
|
|
Weeks: []DutyWeek{
|
|
thisWeekFilled,
|
|
{WeekStart: day(t, "2026-06-22"), Row: 6}, // +1: empty
|
|
filled("2026-06-29", 7), // +2: filled
|
|
},
|
|
People: people,
|
|
}
|
|
r, h := newFireHarness(t, rota, mon, 1)
|
|
r.fire(context.Background())
|
|
|
|
if len(h.assigns) != 1 {
|
|
t.Fatalf("assigns = %d, want 1", len(h.assigns))
|
|
}
|
|
a := h.assigns[0]
|
|
if a.p1 != "Ala" || a.p2 != "Bartek" || a.week.Row != 6 {
|
|
t.Errorf("assign = %+v, want Ala/Bartek on row 6", a)
|
|
}
|
|
if len(h.sends) != 2 ||
|
|
!strings.Contains(h.sends[1], "Ala") || !strings.Contains(h.sends[1], "Bartek") {
|
|
t.Errorf("sends = %q, want reminder + announcement naming Ala and Bartek", h.sends)
|
|
}
|
|
})
|
|
|
|
t.Run("two weeks ahead empty -> first warning", func(t *testing.T) {
|
|
rota := Rota{
|
|
Weeks: []DutyWeek{
|
|
thisWeekFilled,
|
|
filled("2026-06-22", 6), // +1: filled
|
|
{WeekStart: day(t, "2026-06-29"), Row: 7}, // +2: empty
|
|
},
|
|
People: people,
|
|
}
|
|
r, h := newFireHarness(t, rota, mon, 1)
|
|
r.fire(context.Background())
|
|
|
|
if len(h.assigns) != 0 {
|
|
t.Errorf("assigns = %d, want 0 (warning only, no assign at +2)", len(h.assigns))
|
|
}
|
|
if len(h.sends) != 2 {
|
|
t.Errorf("sends = %q, want reminder + warning", h.sends)
|
|
}
|
|
})
|
|
|
|
t.Run("week ahead empty but fewer than 2 people -> no assign", func(t *testing.T) {
|
|
rota := Rota{
|
|
Weeks: []DutyWeek{
|
|
thisWeekFilled,
|
|
{WeekStart: day(t, "2026-06-22"), Row: 6},
|
|
filled("2026-06-29", 7),
|
|
},
|
|
People: []Person{{Name: "Ala", DutyCount: 1}},
|
|
}
|
|
r, h := newFireHarness(t, rota, mon, 1)
|
|
r.fire(context.Background())
|
|
|
|
if len(h.assigns) != 0 {
|
|
t.Errorf("assigns = %d, want 0", len(h.assigns))
|
|
}
|
|
if len(h.sends) != 1 {
|
|
t.Errorf("sends = %q, want only the this-week reminder", h.sends)
|
|
}
|
|
})
|
|
|
|
t.Run("week ahead empty but row unknown -> no assign", func(t *testing.T) {
|
|
rota := Rota{
|
|
Weeks: []DutyWeek{
|
|
thisWeekFilled,
|
|
{WeekStart: day(t, "2026-06-22"), Row: 0}, // unwritable row
|
|
filled("2026-06-29", 7),
|
|
},
|
|
People: people,
|
|
}
|
|
r, h := newFireHarness(t, rota, mon, 1)
|
|
r.fire(context.Background())
|
|
|
|
if len(h.assigns) != 0 {
|
|
t.Errorf("assigns = %d, want 0 (unwritable row)", len(h.assigns))
|
|
}
|
|
if len(h.sends) != 1 {
|
|
t.Errorf("sends = %q, want only the this-week reminder", h.sends)
|
|
}
|
|
})
|
|
|
|
t.Run("assign write fails -> attempted, no announcement", func(t *testing.T) {
|
|
rota := Rota{
|
|
Weeks: []DutyWeek{
|
|
thisWeekFilled,
|
|
{WeekStart: day(t, "2026-06-22"), Row: 6},
|
|
filled("2026-06-29", 7),
|
|
},
|
|
People: people,
|
|
}
|
|
r, h := newFireHarness(t, rota, mon, 1)
|
|
h.assignFn = func(context.Context, DutyWeek, string, string) error { return errors.New("boom") }
|
|
r.fire(context.Background())
|
|
|
|
if len(h.assigns) != 1 {
|
|
t.Errorf("assigns = %d, want 1 (attempted)", len(h.assigns))
|
|
}
|
|
if len(h.sends) != 1 {
|
|
t.Errorf("sends = %q, want only the this-week reminder (no announce on write failure)", h.sends)
|
|
}
|
|
})
|
|
|
|
t.Run("partially-filled week ahead -> left alone", func(t *testing.T) {
|
|
rota := Rota{
|
|
Weeks: []DutyWeek{
|
|
thisWeekFilled,
|
|
{WeekStart: day(t, "2026-06-22"), Person1: "Ola", Row: 6}, // +1: one slot filled
|
|
filled("2026-06-29", 7),
|
|
},
|
|
People: people,
|
|
}
|
|
r, h := newFireHarness(t, rota, mon, 1)
|
|
r.fire(context.Background())
|
|
|
|
if len(h.assigns) != 0 {
|
|
t.Errorf("assigns = %d, want 0 (partial week is not empty)", len(h.assigns))
|
|
}
|
|
if len(h.sends) != 1 {
|
|
t.Errorf("sends = %q, want only the this-week reminder", h.sends)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestChooseTwo(t *testing.T) {
|
|
rng := rand.New(rand.NewSource(1))
|
|
|
|
t.Run("picks two lowest counts, lowest first", func(t *testing.T) {
|
|
people := []Person{
|
|
{Name: "Ala", DutyCount: 3},
|
|
{Name: "Bartek", DutyCount: 1},
|
|
{Name: "Cela", DutyCount: 2},
|
|
}
|
|
p1, p2, ok := chooseTwo(people, rng)
|
|
if !ok {
|
|
t.Fatal("ok = false, want true")
|
|
}
|
|
if p1 != "Bartek" || p2 != "Cela" {
|
|
t.Errorf("got (%q, %q), want (Bartek, Cela)", p1, p2)
|
|
}
|
|
})
|
|
|
|
t.Run("ignores blank-name entries", func(t *testing.T) {
|
|
people := []Person{
|
|
{Name: "", DutyCount: 0},
|
|
{Name: "Ala", DutyCount: 5},
|
|
{Name: "Bartek", DutyCount: 6},
|
|
}
|
|
p1, p2, ok := chooseTwo(people, rng)
|
|
if !ok || p1 != "Ala" || p2 != "Bartek" {
|
|
t.Errorf("got (%q, %q, %v), want (Ala, Bartek, true)", p1, p2, ok)
|
|
}
|
|
})
|
|
|
|
t.Run("fewer than two eligible -> ok=false", func(t *testing.T) {
|
|
if _, _, ok := chooseTwo([]Person{{Name: "Ala"}}, rng); ok {
|
|
t.Error("one person: ok = true, want false")
|
|
}
|
|
if _, _, ok := chooseTwo(nil, rng); ok {
|
|
t.Error("no people: ok = true, want false")
|
|
}
|
|
if _, _, ok := chooseTwo([]Person{{Name: ""}, {Name: ""}}, rng); ok {
|
|
t.Error("only blank names: ok = true, want false")
|
|
}
|
|
})
|
|
|
|
t.Run("equal counts: random tie-break, distinct and deterministic per seed", func(t *testing.T) {
|
|
people := []Person{
|
|
{Name: "Ala", DutyCount: 1},
|
|
{Name: "Bartek", DutyCount: 1},
|
|
{Name: "Cela", DutyCount: 1},
|
|
}
|
|
p1, p2, ok := chooseTwo(people, rand.New(rand.NewSource(42)))
|
|
if !ok {
|
|
t.Fatal("ok = false, want true")
|
|
}
|
|
if p1 == p2 {
|
|
t.Errorf("chose the same person twice: %q", p1)
|
|
}
|
|
names := map[string]bool{"Ala": true, "Bartek": true, "Cela": true}
|
|
if !names[p1] || !names[p2] {
|
|
t.Errorf("got (%q, %q), both must be from the roster", p1, p2)
|
|
}
|
|
// Same seed reproduces the same choice.
|
|
q1, q2, _ := chooseTwo(people, rand.New(rand.NewSource(42)))
|
|
if q1 != p1 || q2 != p2 {
|
|
t.Errorf("not deterministic for a seed: (%q,%q) vs (%q,%q)", p1, p2, q1, q2)
|
|
}
|
|
})
|
|
}
|
|
|
|
func warsaw(t *testing.T) *time.Location {
|
|
t.Helper()
|
|
loc, err := time.LoadLocation("Europe/Warsaw")
|
|
if err != nil {
|
|
t.Skipf("Europe/Warsaw tzdata unavailable: %v", err)
|
|
}
|
|
return loc
|
|
}
|
|
|
|
func TestNextFire(t *testing.T) {
|
|
loc := warsaw(t)
|
|
at := func(y int, mo time.Month, d, h, mi int) time.Time {
|
|
return time.Date(y, mo, d, h, mi, 0, 0, loc)
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
now time.Time
|
|
weekday time.Weekday
|
|
hour int
|
|
want time.Time
|
|
}{
|
|
{
|
|
"same weekday before hour -> today",
|
|
at(2026, 6, 15, 8, 0), time.Monday, 9, // Mon 08:00
|
|
at(2026, 6, 15, 9, 0),
|
|
},
|
|
{
|
|
"same weekday exactly at hour -> next week (strictly after)",
|
|
at(2026, 6, 15, 9, 0), time.Monday, 9,
|
|
at(2026, 6, 22, 9, 0),
|
|
},
|
|
{
|
|
"same weekday after hour -> next week",
|
|
at(2026, 6, 15, 10, 0), time.Monday, 9,
|
|
at(2026, 6, 22, 9, 0),
|
|
},
|
|
{
|
|
"later weekday -> upcoming target weekday",
|
|
at(2026, 6, 17, 12, 0), time.Monday, 9, // Wed -> next Mon
|
|
at(2026, 6, 22, 9, 0),
|
|
},
|
|
{
|
|
"day before -> next day",
|
|
at(2026, 6, 21, 23, 0), time.Monday, 9, // Sun 23:00 -> Mon 09:00
|
|
at(2026, 6, 22, 9, 0),
|
|
},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := nextFire(tc.now, tc.weekday, tc.hour, loc)
|
|
if !got.Equal(tc.want) {
|
|
t.Errorf("nextFire = %s, want %s", got.In(loc), tc.want.In(loc))
|
|
}
|
|
if !got.After(tc.now) {
|
|
t.Errorf("nextFire %s not strictly after now %s", got.In(loc), tc.now.In(loc))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Across the spring-forward (2026-03-29 in Warsaw, clocks jump 02:00->03:00),
|
|
// the next Monday 09:00 must still be 09:00 wall-clock in Warsaw, proving the
|
|
// fire time is recomputed in the zone rather than by +7d UTC arithmetic.
|
|
func TestNextFireAcrossDST(t *testing.T) {
|
|
loc := warsaw(t)
|
|
now := time.Date(2026, 3, 28, 12, 0, 0, 0, loc) // Sat before DST
|
|
got := nextFire(now, time.Monday, 9, loc).In(loc)
|
|
|
|
if got.Weekday() != time.Monday || got.Hour() != 9 || got.Minute() != 0 {
|
|
t.Fatalf("got %s, want Monday 09:00 Warsaw", got)
|
|
}
|
|
if y, m, d := got.Date(); y != 2026 || m != time.March || d != 30 {
|
|
t.Errorf("got date %04d-%02d-%02d, want 2026-03-30", y, m, d)
|
|
}
|
|
}
|
|
|
|
// TestReminderText checks only the match/skip behavior — a target inside a
|
|
// rota week yields a reminder, one outside skips. The wording itself is not
|
|
// asserted, so copy tweaks don't break the test.
|
|
func TestReminderText(t *testing.T) {
|
|
rota := Rota{Weeks: []DutyWeek{week(t, "2026-06-29", "Ala", "Bartek")}}
|
|
|
|
if _, ok := reminderText(rota, day(t, "2026-06-30")); !ok {
|
|
t.Error("target inside a rota week should yield a reminder")
|
|
}
|
|
if _, ok := reminderText(rota, day(t, "2026-09-01")); ok {
|
|
t.Error("target outside all rota weeks should skip")
|
|
}
|
|
}
|
|
|
|
// TestReminderTextMentions checks the handle substitution — names resolve to
|
|
// @handles when known (normalizing a missing leading @) and stay plain names
|
|
// otherwise. Only the mention tokens are asserted, not the full wording.
|
|
func TestReminderTextMentions(t *testing.T) {
|
|
target := day(t, "2026-06-30")
|
|
people := []Person{
|
|
{Name: "Ala", Handle: "@ala"}, // stored with leading @
|
|
{Name: "Bartek", Handle: "bart"}, // stored without @ -> normalized
|
|
{Name: "Cela", Handle: ""}, // present but no handle -> plain name
|
|
}
|
|
rota := func(p1, p2 string) Rota {
|
|
return Rota{Weeks: []DutyWeek{week(t, "2026-06-29", p1, p2)}, People: people}
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
rota Rota
|
|
wantTokens []string // each must appear in the reminder text
|
|
}{
|
|
{"both have handles", rota("Ala", "Bartek"), []string{"@ala", "@bart"}},
|
|
{"one handle, one missing -> mixed", rota("Ala", "Cela"), []string{"@ala", "Cela"}},
|
|
{"name not in people list -> plain name", rota("Ala", "Zenon"), []string{"@ala", "Zenon"}},
|
|
{"single person with handle", rota("Bartek", ""), []string{"@bart"}},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got, ok := reminderText(tc.rota, target)
|
|
if !ok {
|
|
t.Fatalf("ok = false, want true")
|
|
}
|
|
for _, token := range tc.wantTokens {
|
|
if !strings.Contains(got, token) {
|
|
t.Errorf("reminderText = %q, want it to contain %q", got, token)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseWeekday(t *testing.T) {
|
|
tests := []struct {
|
|
in string
|
|
want time.Weekday
|
|
wantErr bool
|
|
}{
|
|
{"Monday", time.Monday, false},
|
|
{"Sunday", time.Sunday, false},
|
|
{"monday", time.Monday, false}, // case-insensitive
|
|
{"Funday", 0, true},
|
|
{"", 0, true},
|
|
}
|
|
for _, tc := range tests {
|
|
t.Run(tc.in, func(t *testing.T) {
|
|
got, err := parseWeekday(tc.in)
|
|
if (err != nil) != tc.wantErr {
|
|
t.Fatalf("err = %v, wantErr %v", err, tc.wantErr)
|
|
}
|
|
if err == nil && got != tc.want {
|
|
t.Errorf("parseWeekday(%q) = %v, want %v", tc.in, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|