| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // A removed member's recurring reminder goes quiet rather than waking the |
| 10 | // instance every day for nobody, and their return queues it again (Fable |
| 11 | // #35 N2). |
| 12 | func TestTeamRemovedMembersRemindersGoDormantAndWakeOnReturn(t *testing.T) { |
| 13 | f := newTeamFixture(t) |
| 14 | box := f.mailbox() |
| 15 | subject := teamSubject('z') |
| 16 | alice := f.linkedAlice(subject, "member") |
| 17 | team, _ := f.calendarsOf(alice) |
| 18 | start, end := soon(5) |
| 19 | e, err := f.app.store.CreateEvent(f.withAccess(subject), alice.ID, EventInput{CalendarID: team, Title: "Daily standup", StartLocal: start, EndLocal: end, TZ: "UTC", Kind: KindMeeting, Status: StatusConfirmed, RRule: "FREQ=DAILY"}) |
| 20 | if err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | if err := f.app.store.AddReminder(f.withAccess(subject), alice.ID, e.ID, 10); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | row := func() ReminderQueue { |
| 27 | var q ReminderQueue |
| 28 | f.app.db.G.Joins("JOIN reminders ON reminders.id = reminder_queues.reminder_id").Where("reminders.event_id = ?", e.ID).First(&q) |
| 29 | return q |
| 30 | } |
| 31 | f.dir.Set(subject, "") |
| 32 | f.refreshed() |
| 33 | if sent, _, err := f.app.h.drain(context.Background(), time.Now()); err != nil || sent != 0 || len(box.sent) != 0 { |
| 34 | t.Fatalf("drain for a removed member = %d %v %v", sent, box.sent, err) |
| 35 | } |
| 36 | q := row() |
| 37 | if q.FiredAt == nil || q.ClaimID == nil || *q.ClaimID != dormantClaim { |
| 38 | t.Fatalf("a removed member's daily reminder = fired %v claim %v, want dormant", q.FiredAt, q.ClaimID) |
| 39 | } |
| 40 | // Nothing queued: no wake tomorrow, or the day after, for nobody. |
| 41 | if due := f.app.store.NextDueAt(context.Background()); !due.IsZero() { |
| 42 | t.Fatalf("a dormant reminder still asks for a wake at %v", due) |
| 43 | } |
| 44 | if sent, _, _ := f.app.h.drain(context.Background(), time.Now().Add(72*time.Hour)); sent != 0 { |
| 45 | t.Fatal("a dormant reminder fired days later") |
| 46 | } |
| 47 | // Back on the team: their next request queues it again. |
| 48 | f.dir.Set(subject, "member") |
| 49 | f.refreshed() |
| 50 | f.signInHome(subject) |
| 51 | if w := f.get("/day/2026-11-02"); w.Code != 200 { |
| 52 | t.Fatalf("re-added member's page = %d", w.Code) |
| 53 | } |
| 54 | q = row() |
| 55 | if q.FiredAt != nil || (q.ClaimID != nil && *q.ClaimID == dormantClaim) || q.DueAt <= time.Now().Unix()-3600 { |
| 56 | t.Fatalf("a returning member's reminder = fired %v claim %v due %d, want queued again", q.FiredAt, q.ClaimID, q.DueAt) |
| 57 | } |
| 58 | if due := f.app.store.NextDueAt(context.Background()); due.IsZero() { |
| 59 | t.Fatal("a woken reminder asks for no wake") |
| 60 | } |
| 61 | } |
| 62 | |