| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | access "amadan.net/oficina/agent-access.git" |
| 13 | ) |
| 14 | |
| 15 | type agentListedEvent struct { |
| 16 | ID int64 `json:"id"` |
| 17 | CalendarID int64 `json:"calendar_id"` |
| 18 | Calendar string `json:"calendar"` |
| 19 | Title string `json:"title"` |
| 20 | Location string `json:"location"` |
| 21 | AllDay bool `json:"all_day"` |
| 22 | Start string `json:"start"` |
| 23 | End string `json:"end"` |
| 24 | TZ string `json:"tz"` |
| 25 | Status string `json:"status"` |
| 26 | Recurring bool `json:"recurring"` |
| 27 | Attendees []string `json:"attendees"` |
| 28 | URL string `json:"url"` |
| 29 | } |
| 30 | type agentListed struct { |
| 31 | Events []agentListedEvent `json:"events"` |
| 32 | Next string `json:"next"` |
| 33 | } |
| 34 | |
| 35 | func listAgentEvents(t *testing.T, a *App, input map[string]any) agentListed { |
| 36 | t.Helper() |
| 37 | input["team"] = "acme" |
| 38 | w := calendarAgentCall(a, "calendar_list_events", input) |
| 39 | if w.Code != 200 { |
| 40 | t.Fatal(w.Code, w.Body.String()) |
| 41 | } |
| 42 | var out agentListed |
| 43 | if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | return out |
| 47 | } |
| 48 | func listedTitles(out agentListed) []string { |
| 49 | titles := []string{} |
| 50 | for _, e := range out.Events { |
| 51 | titles = append(titles, e.Title) |
| 52 | } |
| 53 | return titles |
| 54 | } |
| 55 | |
| 56 | func TestAgentListEventsReadsADay(t *testing.T) { |
| 57 | f, _, person, cal := linkedCalendarAgent(t) |
| 58 | if err := f.app.db.G.Model(&Person{}).Where("id=?", person.ID).Update("zone", "America/New_York").Error; err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | create := eventAgentInput(cal) |
| 62 | create["title"], create["location"] = "Planning with Doc", "Room 4" |
| 63 | create["start_local"], create["end_local"], create["tz"] = "2027-03-10T14:00", "2027-03-10T15:00", "America/New_York" |
| 64 | create["attendees"] = []string{"doc@tito.io", "Ana@Example.com"} |
| 65 | if w := calendarAgentCall(f.app, "calendar_create_event", create); w.Code != 200 { |
| 66 | t.Fatal(w.Code, w.Body.String()) |
| 67 | } |
| 68 | standup, err := f.app.store.CreateEvent(t.Context(), person.ID, EventInput{CalendarID: cal, Title: "Standup", StartLocal: "2027-03-01T09:00", EndLocal: "2027-03-01T09:15", TZ: "America/New_York", RRule: "FREQ=DAILY"}) |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | // Tomorrow's standup moves to the afternoon: an override row, listed |
| 73 | // under its series. |
| 74 | if err = f.app.store.UpdateOccurrence(t.Context(), person.ID, standup.ID, "2027-03-11T09:00", EventInput{CalendarID: cal, Title: "Standup", StartLocal: "2027-03-11T16:00", EndLocal: "2027-03-11T16:15", TZ: "America/New_York", Kind: KindMeeting, Status: StatusConfirmed}); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | for _, in := range []EventInput{ |
| 78 | {CalendarID: cal, Title: "Day before", StartLocal: "2027-03-09T14:00", EndLocal: "2027-03-09T15:00", TZ: "America/New_York"}, |
| 79 | {CalendarID: cal, Title: "Offsite", AllDay: true, StartLocal: "2027-03-10", EndLocal: "2027-03-11"}, |
| 80 | // Indexed at UTC midnight on the 11th, which is 19:00 on the 10th in |
| 81 | // New York: a naive overlap would put tomorrow's all-day event today. |
| 82 | {CalendarID: cal, Title: "Tomorrow all day", AllDay: true, StartLocal: "2027-03-11", EndLocal: "2027-03-12"}, |
| 83 | } { |
| 84 | if _, err := f.app.store.CreateEvent(t.Context(), person.ID, in); err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | } |
| 88 | out := listAgentEvents(t, f.app, map[string]any{"from": "2027-03-10T00:00:00-05:00", "to": "2027-03-11T00:00:00-05:00"}) |
| 89 | if titles := listedTitles(out); !slices.Equal(titles, []string{"Offsite", "Standup", "Planning with Doc"}) || out.Next != "" { |
| 90 | t.Fatal("wrong day", titles, out.Next) |
| 91 | } |
| 92 | offsite, daily, planning := out.Events[0], out.Events[1], out.Events[2] |
| 93 | if !offsite.AllDay || offsite.Start != "2027-03-10" || offsite.End != "2027-03-11" { |
| 94 | t.Fatal("all-day dates", offsite) |
| 95 | } |
| 96 | if daily.ID != standup.ID || !daily.Recurring || daily.Start != "2027-03-10T09:00:00-05:00" || daily.End != "2027-03-10T09:15:00-05:00" { |
| 97 | t.Fatal("occurrence", daily) |
| 98 | } |
| 99 | cals, err := f.app.store.Calendars(t.Context(), person.ID) |
| 100 | if err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | name := "" |
| 104 | for _, c := range cals { |
| 105 | if c.ID == cal { |
| 106 | name = c.Name |
| 107 | } |
| 108 | } |
| 109 | if planning.ID == 0 || planning.CalendarID != cal || planning.Calendar != name || name == "" || planning.Location != "Room 4" || planning.AllDay || planning.Start != "2027-03-10T14:00:00-05:00" || planning.End != "2027-03-10T15:00:00-05:00" || planning.TZ != "America/New_York" || planning.Status != StatusConfirmed || planning.URL != homeTestOrigin+fmt.Sprintf("/events/%d", planning.ID) { |
| 110 | t.Fatal("event fields", planning) |
| 111 | } |
| 112 | if !slices.Equal(planning.Attendees, []string{"doc@tito.io", "ana@example.com"}) || offsite.Attendees == nil || len(offsite.Attendees) != 0 { |
| 113 | t.Fatal("attendees", planning.Attendees, offsite.Attendees) |
| 114 | } |
| 115 | moved := listAgentEvents(t, f.app, map[string]any{"from": "2027-03-11T12:00:00-05:00", "to": "2027-03-11T18:00:00-05:00", "query": "standup"}) |
| 116 | if len(moved.Events) != 1 || moved.Events[0].ID != standup.ID || !moved.Events[0].Recurring || moved.Events[0].Start != "2027-03-11T16:00:00-05:00" { |
| 117 | t.Fatal("moved occurrence", moved.Events) |
| 118 | } |
| 119 | if out := listAgentEvents(t, f.app, map[string]any{"from": "2027-03-10T00:00:00-05:00", "to": "2027-03-11T00:00:00-05:00", "query": "DOC"}); !slices.Equal(listedTitles(out), []string{"Planning with Doc"}) { |
| 120 | t.Fatal("query", listedTitles(out)) |
| 121 | } |
| 122 | if out := listAgentEvents(t, f.app, map[string]any{"from": "2027-03-10T00:00:00-05:00", "to": "2027-03-11T00:00:00-05:00", "query": "room 4"}); !slices.Equal(listedTitles(out), []string{"Planning with Doc"}) { |
| 123 | t.Fatal("query matches location", listedTitles(out)) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | func TestAgentListEventsRefusesUnboundedInput(t *testing.T) { |
| 128 | f, _, _, _ := linkedCalendarAgent(t) |
| 129 | for _, in := range []map[string]any{ |
| 130 | {"from": "2027-03-01T00:00:00Z", "to": "2027-04-01T00:00:01Z"}, |
| 131 | {"from": "2027-03-02T00:00:00Z", "to": "2027-03-01T00:00:00Z"}, |
| 132 | {"from": "2027-03-01T00:00:00Z", "to": "2027-03-01T00:00:00Z"}, |
| 133 | {"from": "2027-03-01T00:00:00", "to": "2027-03-02T00:00:00"}, |
| 134 | {"from": "2027-03-01T00:00:00Z", "to": "2027-03-02T00:00:00Z", "cursor": "-1"}, |
| 135 | {"from": "2027-03-01T00:00:00Z", "to": "2027-03-02T00:00:00Z", "cursor": "x"}, |
| 136 | {"from": "2027-03-01T00:00:00Z", "to": "2027-03-02T00:00:00Z", "limit": 201}, |
| 137 | {"from": "2027-03-01T00:00:00Z", "to": "2027-03-02T00:00:00Z", "limit": 0}, |
| 138 | {"from": "2027-03-01T00:00:00Z", "to": "2027-03-02T00:00:00Z", "send_invitations": true}, |
| 139 | } { |
| 140 | in["team"] = "acme" |
| 141 | if w := calendarAgentCall(f.app, "calendar_list_events", in); w.Code != 422 && w.Code != 400 { |
| 142 | t.Fatal("accepted", in, w.Code, w.Body.String()) |
| 143 | } |
| 144 | } |
| 145 | if out := listAgentEvents(t, f.app, map[string]any{"from": "2027-03-01T00:00:00Z", "to": "2027-04-01T00:00:00Z"}); out.Events == nil || len(out.Events) != 0 { |
| 146 | t.Fatal("31 days refused or nil list", out) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | func TestAgentListEventsPagesThroughEverything(t *testing.T) { |
| 151 | f, _, person, cal := linkedCalendarAgent(t) |
| 152 | day := time.Date(2027, 3, 10, 0, 0, 0, 0, time.UTC) |
| 153 | for i := range 205 { |
| 154 | start := day.Add(time.Duration(i) * time.Minute) |
| 155 | if _, err := f.app.store.CreateEvent(t.Context(), person.ID, EventInput{CalendarID: cal, Title: fmt.Sprintf("e%03d", i), StartLocal: start.Format(LayoutLocal), EndLocal: start.Add(time.Minute).Format(LayoutLocal), TZ: "UTC"}); err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | } |
| 159 | window := map[string]any{"from": day.Format(time.RFC3339), "to": day.Add(24 * time.Hour).Format(time.RFC3339)} |
| 160 | // Whichever bound ends a page — 200 events or the byte budget — the |
| 161 | // cursor chain covers every event once, in order. |
| 162 | all, page := []string{}, listAgentEvents(t, f.app, window) |
| 163 | if len(page.Events) > 200 || page.Next == "" || page.Events[0].Title != "e000" { |
| 164 | t.Fatal("first page", len(page.Events), page.Next) |
| 165 | } |
| 166 | for { |
| 167 | all = append(all, listedTitles(page)...) |
| 168 | if page.Next == "" { |
| 169 | break |
| 170 | } |
| 171 | window["cursor"] = page.Next |
| 172 | page = listAgentEvents(t, f.app, window) |
| 173 | } |
| 174 | if len(all) != 205 { |
| 175 | t.Fatal("chain", len(all)) |
| 176 | } |
| 177 | for i, title := range all { |
| 178 | if title != fmt.Sprintf("e%03d", i) { |
| 179 | t.Fatal("skipped or repeated", i, title) |
| 180 | } |
| 181 | } |
| 182 | last := listAgentEvents(t, f.app, map[string]any{"from": window["from"], "to": window["to"], "limit": 5, "cursor": "200"}) |
| 183 | if len(last.Events) != 5 || last.Next != "" { |
| 184 | t.Fatal("exact last page", len(last.Events), last.Next) |
| 185 | } |
| 186 | if past := listAgentEvents(t, f.app, map[string]any{"from": window["from"], "to": window["to"], "cursor": "900"}); len(past.Events) != 0 || past.Next != "" { |
| 187 | t.Fatal("past the end", past) |
| 188 | } |
| 189 | small := listAgentEvents(t, f.app, map[string]any{"from": window["from"], "to": window["to"], "limit": 2}) |
| 190 | if !slices.Equal(listedTitles(small), []string{"e000", "e001"}) || small.Next == "" { |
| 191 | t.Fatal("limit", listedTitles(small)) |
| 192 | } |
| 193 | small = listAgentEvents(t, f.app, map[string]any{"from": window["from"], "to": window["to"], "limit": 2, "cursor": small.Next}) |
| 194 | if !slices.Equal(listedTitles(small), []string{"e002", "e003"}) { |
| 195 | t.Fatal("limit cursor", listedTitles(small)) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | func TestAgentListEventsFollowsCalendarGrants(t *testing.T) { |
| 200 | f, p, person, cal := linkedCalendarAgent(t) |
| 201 | other, err := f.app.store.EnsurePersonSetup(t.Context(), "replacement@example.test") |
| 202 | if err != nil { |
| 203 | t.Fatal(err) |
| 204 | } |
| 205 | otherCal, err := f.app.store.DefaultCalendarID(t.Context(), other.ID) |
| 206 | if err != nil { |
| 207 | t.Fatal(err) |
| 208 | } |
| 209 | cals, err := f.app.store.Calendars(t.Context(), person.ID) |
| 210 | if err != nil { |
| 211 | t.Fatal(err) |
| 212 | } |
| 213 | var shared int64 |
| 214 | for _, c := range cals { |
| 215 | if c.ID != cal { |
| 216 | shared = c.ID |
| 217 | } |
| 218 | } |
| 219 | if shared == 0 { |
| 220 | t.Fatal("no team calendar") |
| 221 | } |
| 222 | for _, e := range []struct { |
| 223 | who Person |
| 224 | cal int64 |
| 225 | title string |
| 226 | }{{person, cal, "Mine"}, {person, shared, "Team"}, {other, otherCal, "Someone else's"}} { |
| 227 | if _, err := f.app.store.CreateEvent(t.Context(), e.who.ID, EventInput{CalendarID: e.cal, Title: e.title, StartLocal: "2027-03-10T10:00", EndLocal: "2027-03-10T11:00", TZ: "UTC"}); err != nil { |
| 228 | t.Fatal(err) |
| 229 | } |
| 230 | } |
| 231 | window := func(extra map[string]any) map[string]any { |
| 232 | in := map[string]any{"from": "2027-03-10T00:00:00Z", "to": "2027-03-11T00:00:00Z"} |
| 233 | for k, v := range extra { |
| 234 | in[k] = v |
| 235 | } |
| 236 | return in |
| 237 | } |
| 238 | if titles := listedTitles(listAgentEvents(t, f.app, window(nil))); !slices.Equal(titles, []string{"Mine", "Team"}) && !slices.Equal(titles, []string{"Team", "Mine"}) { |
| 239 | t.Fatal("visible events", titles) |
| 240 | } |
| 241 | if titles := listedTitles(listAgentEvents(t, f.app, window(map[string]any{"calendar_id": shared}))); !slices.Equal(titles, []string{"Team"}) { |
| 242 | t.Fatal("calendar filter", titles) |
| 243 | } |
| 244 | in := window(map[string]any{"calendar_id": otherCal, "team": "acme"}) |
| 245 | if w := calendarAgentCall(f.app, "calendar_list_events", in); w.Code != 404 || strings.Contains(w.Body.String(), "Someone") { |
| 246 | t.Fatal("foreign calendar", w.Code, w.Body.String()) |
| 247 | } |
| 248 | // A viewer reads as the UI does. |
| 249 | if err = f.app.db.G.Model(&Grant{}).Where("person_id=? AND calendar_id=?", person.ID, shared).Update("role", RoleViewer).Error; err != nil { |
| 250 | t.Fatal(err) |
| 251 | } |
| 252 | if titles := listedTitles(listAgentEvents(t, f.app, window(map[string]any{"calendar_id": shared}))); !slices.Equal(titles, []string{"Team"}) { |
| 253 | t.Fatal("viewer", titles) |
| 254 | } |
| 255 | // An archived calendar leaves every view, and so leaves this one. |
| 256 | if err = f.app.db.G.Model(&Calendar{}).Where("id=?", shared).Update("archived_at", time.Now().UTC()).Error; err != nil { |
| 257 | t.Fatal(err) |
| 258 | } |
| 259 | if titles := listedTitles(listAgentEvents(t, f.app, window(nil))); !slices.Equal(titles, []string{"Mine"}) { |
| 260 | t.Fatal("archived", titles) |
| 261 | } |
| 262 | in = window(map[string]any{"calendar_id": shared, "team": "acme"}) |
| 263 | if w := calendarAgentCall(f.app, "calendar_list_events", in); w.Code != 404 { |
| 264 | t.Fatal("archived filter", w.Code) |
| 265 | } |
| 266 | // The operation refuses these itself, not only through the response's |
| 267 | // provenance recheck. |
| 268 | for _, id := range []int64{otherCal, shared} { |
| 269 | raw, _ := json.Marshal(window(map[string]any{"calendar_id": id, "team": "acme"})) |
| 270 | if _, err := f.app.agentListEvents(f.app.agentContext(t.Context()), access.Principal{}, person, raw); !errors.Is(err, ErrNotFound) { |
| 271 | t.Fatal("operation disclosed calendar", id, err) |
| 272 | } |
| 273 | } |
| 274 | if err = f.app.db.G.Where("person_id=? AND calendar_id=?", person.ID, cal).Delete(&Grant{}).Error; err != nil { |
| 275 | t.Fatal(err) |
| 276 | } |
| 277 | if titles := listedTitles(listAgentEvents(t, f.app, window(nil))); len(titles) != 0 { |
| 278 | t.Fatal("lost grant", titles) |
| 279 | } |
| 280 | p.Scopes = []string{"calendar:write"} |
| 281 | in = window(map[string]any{"team": "acme"}) |
| 282 | if w := calendarAgentCall(f.app, "calendar_list_events", in); w.Code != 403 { |
| 283 | t.Fatal("read scope", w.Code) |
| 284 | } |
| 285 | } |
| 286 | |