| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestAgentLongAllDayBusyWorkIsBounded(t *testing.T) { |
| 14 | f, _, person, cal := linkedCalendarAgent(t) |
| 15 | if _, err := f.app.store.CreateEvent(t.Context(), person.ID, EventInput{CalendarID: cal, Title: "millennial all-day commitment", AllDay: true, StartLocal: "0001-01-01", EndLocal: "9999-12-31", Kind: KindCommitment}); err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | from := time.Date(2026, 10, 24, 0, 0, 0, 0, time.UTC) |
| 19 | to := from.Add(24 * time.Hour) |
| 20 | spans, err := f.app.store.BusyFor(t.Context(), person, from.Unix(), to.Unix(), nil) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | // The extended one-day window occupies at most five civil dates, not |
| 25 | // every date in the stored event. This asserts work size, not wall time. |
| 26 | if len(spans) == 0 || len(spans) > 5 { |
| 27 | t.Fatal("unbounded materialization", len(spans)) |
| 28 | } |
| 29 | response := calendarAgentCall(f.app, "calendar_free_busy", map[string]any{"team": "acme", "start": from.Format(time.RFC3339), "end": to.Format(time.RFC3339)}) |
| 30 | if response.Code != 200 || !strings.Contains(response.Body.String(), from.Format(time.RFC3339)) { |
| 31 | t.Fatal("bounded response incomplete", response.Code, response.Body.String()) |
| 32 | } |
| 33 | input := eventAgentInput(cal) |
| 34 | input["tz"] = "Local" |
| 35 | if w := calendarAgentCall(f.app, "calendar_create_event", input); w.Code != 422 { |
| 36 | t.Fatal("machine-local time accepted", w.Code) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestAgentEventTransactionRollsBackReminderFailure(t *testing.T) { |
| 41 | f, _, _, cal := linkedCalendarAgent(t) |
| 42 | _, err := f.app.db.Writer().Exec(`CREATE TRIGGER reject_agent_reminder BEFORE INSERT ON reminder_queues BEGIN SELECT RAISE(ABORT,'fixture'); END`) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | if w := calendarAgentCall(f.app, "calendar_create_event", eventAgentInput(cal)); w.Code != 503 { |
| 47 | t.Fatal("partial effect reported success", w.Code) |
| 48 | } |
| 49 | for _, table := range []string{"events", "reminders", "reminder_queues", "calendar_agent_events"} { |
| 50 | var count int |
| 51 | if err = f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM " + table).Scan(&count); err != nil || count != 0 { |
| 52 | t.Fatal("partial event transaction", table, count, err) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestAgentMountedRouteUsesBearerAndRearms(t *testing.T) { |
| 58 | f, p, _, cal := linkedCalendarAgent(t) |
| 59 | // The production route constructor must mount the same checked handler |
| 60 | // outside browser CSRF and inside the scheduling response hook. |
| 61 | scheduler := newScheduledWork(f.app.store.nextDueAt, false, nil) |
| 62 | recorder := &scheduleRecorder{} |
| 63 | scheduler.capability = func(context.Context) error { return nil } |
| 64 | scheduler.armed = func(context.Context) (remoteSchedule, error) { return remoteSchedule{}, nil } |
| 65 | scheduler.put = func(ctx context.Context, _ string, at time.Time, _ string) error { return recorder.write(ctx, at) } |
| 66 | scheduler.remove = func(context.Context, string) error { return nil } |
| 67 | if _, err := scheduler.sync(true); err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | t.Cleanup(scheduler.close) |
| 71 | f.app.scheduled.close() |
| 72 | f.app.scheduled = scheduler |
| 73 | f.app.mux = f.app.routes(f.app.h, f.app.auth, f.app.home.admitted, f.app.cfg.Origin) |
| 74 | raw, _ := json.Marshal(eventAgentInput(cal)) |
| 75 | r := httptest.NewRequest(http.MethodPost, homeTestOrigin+"/api/v1/operations/calendar_create_event", strings.NewReader(string(raw))) |
| 76 | r.Header.Set("Content-Type", "application/json") |
| 77 | r.Header.Set("Authorization", "Bearer fixture") |
| 78 | w := httptest.NewRecorder() |
| 79 | f.app.mux.ServeHTTP(responseRecorder{w}, r) |
| 80 | if w.Code != 200 { |
| 81 | t.Fatal("mounted bearer operation", w.Code, w.Body.String()) |
| 82 | } |
| 83 | // The loop is paused: the POST must queue a wake before the test drains it. |
| 84 | if len(scheduler.wake) != 1 { |
| 85 | t.Fatal("agent route omitted scheduler wake") |
| 86 | } |
| 87 | if _, err := scheduler.sync(false); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | if len(recorder.snapshot()) == 0 { |
| 91 | t.Fatal("agent-created reminder did not arm a wake") |
| 92 | } |
| 93 | p.Teams = []string{"other"} |
| 94 | if w := calendarAgentCall(f.app, "calendar_list", map[string]any{"team": "other"}); w.Code != 403 { |
| 95 | t.Fatal("configured team lost") |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestAgentBusyCrossesSkippedCivilDate(t *testing.T) { |
| 100 | loc, err := Zone("Pacific/Apia") |
| 101 | if err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | first := time.Date(2011, 12, 29, 0, 0, 0, 0, loc) |
| 105 | end := time.Date(2012, 1, 1, 0, 0, 0, 0, loc) |
| 106 | from := time.Date(2011, 12, 28, 0, 0, 0, 0, time.UTC) |
| 107 | to := from.AddDate(0, 0, 6) |
| 108 | e := EventView{Event: Event{AllDay: true, TZ: "Pacific/Apia"}, OccStartUTC: first.Unix(), OccEndUTC: end.Unix()} |
| 109 | spans, err := allDayBusy(e, "UTC", time.UTC, from.Unix(), to.Unix()) |
| 110 | if err != nil || len(spans) != 3 { |
| 111 | t.Fatal("skipped local date expansion", len(spans), err) |
| 112 | } |
| 113 | for i, span := range spans { |
| 114 | want := time.Date(2011, 12, 29+i, 0, 0, 0, 0, time.UTC) |
| 115 | if span.StartUTC != want.Unix() || span.EndUTC != want.AddDate(0, 0, 1).Unix() { |
| 116 | t.Fatal("civil date moved", span) |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func TestAgentRechecksLinkAndLocalAdmission(t *testing.T) { |
| 122 | f, _, person, cal := linkedCalendarAgent(t) |
| 123 | input := eventAgentInput(cal) |
| 124 | if w := calendarAgentCall(f.app, "calendar_create_event", input); w.Code != 200 { |
| 125 | t.Fatal(w.Code) |
| 126 | } |
| 127 | if err := f.app.db.G.Model(&Person{}).Where("id=?", person.ID).Update("address", "removed@example.test").Error; err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | if w := calendarAgentCall(f.app, "calendar_list", map[string]any{"team": "acme"}); w.Code != 403 { |
| 131 | t.Fatal("changed unadmitted identity", w.Code) |
| 132 | } |
| 133 | if err := f.app.db.G.Model(&Person{}).Where("id=?", person.ID).Update("address", person.Address).Error; err != nil { |
| 134 | t.Fatal(err) |
| 135 | } |
| 136 | if w := calendarAgentCall(f.app, "calendar_list", map[string]any{"team": "acme"}); w.Code != 200 { |
| 137 | t.Fatal("restored identity", w.Code) |
| 138 | } |
| 139 | if _, err := f.app.db.Writer().Exec("DELETE FROM home_identity_links"); err != nil { |
| 140 | t.Fatal(err) |
| 141 | } |
| 142 | for _, name := range []string{"calendar_list", "calendar_create_event"} { |
| 143 | args := input |
| 144 | if name == "calendar_list" { |
| 145 | args = map[string]any{"team": "acme"} |
| 146 | } |
| 147 | if w := calendarAgentCall(f.app, name, args); w.Code != 403 { |
| 148 | t.Fatal("unlinked request", name, w.Code) |
| 149 | } |
| 150 | } |
| 151 | var count int |
| 152 | if err := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM home_identity_links").Scan(&count); err != nil || count != 0 { |
| 153 | t.Fatal("link recreated", count, err) |
| 154 | } |
| 155 | if err := f.app.db.Writer().QueryRow("SELECT COUNT(*) FROM events").Scan(&count); err != nil || count != 1 { |
| 156 | t.Fatal("unlinked request changed events", count, err) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | func TestAgentBusyMidnightGapFailsClosed(t *testing.T) { |
| 161 | f, _, person, cal := linkedCalendarAgent(t) |
| 162 | if err := f.app.db.G.Model(&Person{}).Where("id=?", person.ID).Update("zone", "America/Santiago").Error; err != nil { |
| 163 | t.Fatal(err) |
| 164 | } |
| 165 | if _, err := f.app.store.CreateEvent(t.Context(), person.ID, EventInput{CalendarID: cal, Title: "all-day commitment", AllDay: true, StartLocal: "2026-09-06", EndLocal: "2026-09-07", Kind: KindCommitment}); err != nil { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | w := calendarAgentCall(f.app, "calendar_free_busy", map[string]any{"team": "acme", "start": "2026-09-06T00:00:00-04:00", "end": "2026-09-07T00:00:00-03:00"}) |
| 169 | if w.Code != 503 { |
| 170 | t.Fatal("midnight gap was not a closed failure", w.Code, w.Body.String()) |
| 171 | } |
| 172 | } |
| 173 | |