dyzur-bot/scheduler_test.go

413 lines
12 KiB
Go

package dyzurbot
import (
"context"
"errors"
"math/rand"
"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},
}
t.Run("this week empty -> assign two lowest and announce", func(t *testing.T) {
rota := Rota{
Weeks: []DutyWeek{
{WeekStart: day(t, "2026-06-15"), Row: 5}, // this: empty
{WeekStart: day(t, "2026-06-22"), Person1: "X", Person2: "Y", Row: 6}, // next: 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 != 5 {
t.Errorf("assign = %+v, want Ala/Bartek on row 5", a)
}
want := "Nikt nie zgłosił się na dyżur w tym tygodniu (od 2026-06-15), więc został przydzielony losowo: Ala i Bartek. Z góry dziękujemy za sprzątanie!"
if len(h.sends) != 1 || h.sends[0] != want {
t.Errorf("sends = %q, want [%q]", h.sends, want)
}
})
t.Run("this week filled, next week empty -> reminder + warning", func(t *testing.T) {
rota := Rota{
Weeks: []DutyWeek{
{WeekStart: day(t, "2026-06-15"), Person1: "Jan", Person2: "Anna", Row: 5},
{WeekStart: day(t, "2026-06-22"), Row: 6}, // next: 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 (this week already filled)", len(h.assigns))
}
wantReminder := "Przypomnienie: w tygodniu od 2026-06-15 dyżur sprzątają Jan i Anna. Proszę pamiętać o sprzątaniu."
wantWarning := "Dyżur na przyszły tydzień (od 2026-06-22) jest jeszcze nieobsadzony. Proszę się zgłaszać — w przeciwnym razie zostanie przydzielony losowo."
if len(h.sends) != 2 || h.sends[0] != wantReminder || h.sends[1] != wantWarning {
t.Errorf("sends = %q,\n want [%q, %q]", h.sends, wantReminder, wantWarning)
}
})
t.Run("this week empty but fewer than 2 people -> no assign, no send", func(t *testing.T) {
rota := Rota{
Weeks: []DutyWeek{{WeekStart: day(t, "2026-06-15"), Row: 5}, {WeekStart: day(t, "2026-06-22"), Person1: "X", Person2: "Y", Row: 6}},
People: []Person{{Name: "Ala", DutyCount: 1}},
}
r, h := newFireHarness(t, rota, mon, 1)
r.fire(context.Background())
if len(h.assigns) != 0 || len(h.sends) != 0 {
t.Errorf("assigns=%d sends=%q, want 0 and none", len(h.assigns), h.sends)
}
})
t.Run("this week empty but row unknown -> no assign, no send", func(t *testing.T) {
rota := Rota{
Weeks: []DutyWeek{{WeekStart: day(t, "2026-06-15"), Row: 0}, {WeekStart: day(t, "2026-06-22"), Person1: "X", Person2: "Y", Row: 6}},
People: people,
}
r, h := newFireHarness(t, rota, mon, 1)
r.fire(context.Background())
if len(h.assigns) != 0 || len(h.sends) != 0 {
t.Errorf("assigns=%d sends=%q, want 0 and none (unwritable row)", len(h.assigns), h.sends)
}
})
t.Run("assign write fails -> no announcement", func(t *testing.T) {
rota := Rota{
Weeks: []DutyWeek{{WeekStart: day(t, "2026-06-15"), Row: 5}, {WeekStart: day(t, "2026-06-22"), Person1: "X", Person2: "Y", Row: 6}},
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) != 0 {
t.Errorf("sends = %q, want none (write failed)", 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)
}
}
func TestReminderText(t *testing.T) {
// Week of 2026-06-29; target lands inside it.
target := day(t, "2026-06-30")
rota := func(p1, p2 string) Rota {
return Rota{Weeks: []DutyWeek{week(t, "2026-06-29", p1, p2)}}
}
tests := []struct {
name string
rota Rota
target time.Time
wantOK bool
want string
}{
{
"both people",
rota("Ala", "Bartek"), target, true,
"Przypomnienie: w tygodniu od 2026-06-29 dyżur sprzątają Ala i Bartek. Proszę pamiętać o sprzątaniu.",
},
{
"one person",
rota("Ala", ""), target, true,
"Przypomnienie: w tygodniu od 2026-06-29 dyżur sprząta Ala. Proszę pamiętać o sprzątaniu.",
},
{
"nobody assigned",
rota("", ""), target, true,
"Przypomnienie: w tygodniu od 2026-06-29 dyżur jest nieobsadzony.",
},
{
"no matching week -> skip",
rota("Ala", "Bartek"), day(t, "2026-09-01"), false,
"",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, ok := reminderText(tc.rota, tc.target)
if ok != tc.wantOK {
t.Fatalf("ok = %v, want %v", ok, tc.wantOK)
}
if ok && got != tc.want {
t.Errorf("reminderText = %q, want %q", got, tc.want)
}
})
}
}
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
want string
}{
{
"both have handles",
rota("Ala", "Bartek"),
"Przypomnienie: w tygodniu od 2026-06-29 dyżur sprzątają @ala i @bart. Proszę pamiętać o sprzątaniu.",
},
{
"one handle, one missing -> mixed",
rota("Ala", "Cela"),
"Przypomnienie: w tygodniu od 2026-06-29 dyżur sprzątają @ala i Cela. Proszę pamiętać o sprzątaniu.",
},
{
"name not in people list -> plain name",
rota("Ala", "Zenon"),
"Przypomnienie: w tygodniu od 2026-06-29 dyżur sprzątają @ala i Zenon. Proszę pamiętać o sprzątaniu.",
},
{
"single person with handle",
rota("Bartek", ""),
"Przypomnienie: w tygodniu od 2026-06-29 dyżur sprząta @bart. Proszę pamiętać o sprzątaniu.",
},
}
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")
}
if got != tc.want {
t.Errorf("reminderText = %q, want %q", got, tc.want)
}
})
}
}
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)
}
})
}
}