| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "slices" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | "unicode/utf8" |
| 11 | |
| 12 | "amadan.net/oficina/calendar/internal/calendar/seal" |
| 13 | ) |
| 14 | |
| 15 | func TestAgentListEventsPagesStayUnderTheFrameBudget(t *testing.T) { |
| 16 | f, _, person, cal := linkedCalendarAgent(t) |
| 17 | day := time.Date(2027, 3, 10, 0, 0, 0, 0, time.UTC) |
| 18 | // Valid at the limits: 200-byte title and location, with characters |
| 19 | // JSON escapes, and attendees on every event. |
| 20 | long := func(i int, tag string) string { |
| 21 | s := fmt.Sprintf("%s%03d<&>", tag, i) |
| 22 | return s + strings.Repeat("x", MaxTitleLen-len(s)) |
| 23 | } |
| 24 | for i := range 200 { |
| 25 | start := day.Add(time.Duration(i) * time.Minute) |
| 26 | e, err := f.app.store.CreateEvent(t.Context(), person.ID, EventInput{CalendarID: cal, Title: long(i, "t"), Location: long(i, "l"), StartLocal: start.Format(LayoutLocal), EndLocal: start.Add(time.Minute).Format(LayoutLocal), TZ: "UTC"}) |
| 27 | if err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if err = f.app.store.SetAttendees(t.Context(), e.ID, []string{fmt.Sprintf("first.attendee.%03d@example.com", i), fmt.Sprintf("second.attendee.%03d@example.com", i)}); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | } |
| 34 | window := map[string]any{"team": "acme", "from": day.Format(time.RFC3339), "to": day.Add(24 * time.Hour).Format(time.RFC3339)} |
| 35 | seen, pages := []string{}, 0 |
| 36 | for { |
| 37 | w := calendarAgentCall(f.app, "calendar_list_events", window) |
| 38 | if w.Code != 200 { |
| 39 | t.Fatal(w.Code, w.Body.String()) |
| 40 | } |
| 41 | if w.Body.Len() > 48*1024 { |
| 42 | t.Fatal("page over budget", pages, w.Body.Len()) |
| 43 | } |
| 44 | var out agentListed |
| 45 | if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil || len(out.Events) == 0 { |
| 46 | t.Fatal("empty page", err) |
| 47 | } |
| 48 | for _, e := range out.Events { |
| 49 | if len(e.Attendees) != 2 { |
| 50 | t.Fatal("attendees", e.Attendees) |
| 51 | } |
| 52 | seen = append(seen, e.Title) |
| 53 | } |
| 54 | pages++ |
| 55 | if out.Next == "" { |
| 56 | break |
| 57 | } |
| 58 | window["cursor"] = out.Next |
| 59 | } |
| 60 | if pages < 2 || len(seen) != 200 { |
| 61 | t.Fatal("pages", pages, len(seen)) |
| 62 | } |
| 63 | for i, title := range seen { |
| 64 | if title != long(i, "t") { |
| 65 | t.Fatal("skipped or repeated event", i) |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestAgentListEventsKeepsSubSecondBoundaries(t *testing.T) { |
| 71 | f, _, person, cal := linkedCalendarAgent(t) |
| 72 | if err := f.app.db.G.Model(&Person{}).Where("id=?", person.ID).Update("zone", "UTC").Error; err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | for _, in := range []EventInput{ |
| 76 | {CalendarID: cal, Title: "Eleven", StartLocal: "2027-03-10T11:00", EndLocal: "2027-03-10T12:00", TZ: "UTC"}, |
| 77 | {CalendarID: cal, Title: "Thursday", AllDay: true, StartLocal: "2027-03-11", EndLocal: "2027-03-12"}, |
| 78 | } { |
| 79 | if _, err := f.app.store.CreateEvent(t.Context(), person.ID, in); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | } |
| 83 | for _, tc := range []struct { |
| 84 | from, to string |
| 85 | want []string |
| 86 | }{ |
| 87 | {"2027-03-10T10:00:00Z", "2027-03-10T11:00:00.5Z", []string{"Eleven"}}, |
| 88 | {"2027-03-10T10:00:00Z", "2027-03-10T11:00:00Z", []string{}}, |
| 89 | {"2027-03-10T12:30:00Z", "2027-03-11T00:00:00.5Z", []string{"Thursday"}}, |
| 90 | {"2027-03-10T12:30:00Z", "2027-03-11T00:00:00Z", []string{}}, |
| 91 | } { |
| 92 | if got := listedTitles(listAgentEvents(t, f.app, map[string]any{"from": tc.from, "to": tc.to})); !slices.Equal(got, tc.want) { |
| 93 | t.Fatal(tc.from, tc.to, got) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestAgentListEventsContract(t *testing.T) { |
| 99 | ops, err := AgentContracts() |
| 100 | if err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | found := false |
| 104 | for _, op := range ops { |
| 105 | if op.Name == "calendar_list_events" { |
| 106 | found = true |
| 107 | if !op.ReadOnly || !op.Idempotent || op.Destructive || op.Scope != "calendar:read" { |
| 108 | t.Fatal("list flags", op) |
| 109 | } |
| 110 | } |
| 111 | if op.Name == "calendar_create_event" || op.Name == "calendar_update_event" { |
| 112 | if !strings.Contains(op.Description, "attendees") || !strings.Contains(op.Description, "never invited") || !strings.Contains(string(op.Input), `"attendees"`) { |
| 113 | t.Fatal("attendee contract", op.Name, op.Description) |
| 114 | } |
| 115 | if strings.Contains(string(op.Input), "send_invitations") || !strings.Contains(string(op.Output), `"invitations_sent":{"const":false}`) { |
| 116 | t.Fatal("invitation contract", op.Name) |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | if !found { |
| 121 | t.Fatal("calendar_list_events missing") |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func TestAgentListEventsFitsOneOversizedEvent(t *testing.T) { |
| 126 | f, _, person, cal := linkedCalendarAgent(t) |
| 127 | e, err := f.app.store.CreateEvent(t.Context(), person.ID, EventInput{CalendarID: cal, Title: "Town hall", StartLocal: "2027-03-10T10:00", EndLocal: "2027-03-10T11:00", TZ: "UTC"}) |
| 128 | if err != nil { |
| 129 | t.Fatal(err) |
| 130 | } |
| 131 | // Quick add and import store whatever they find: an over-long |
| 132 | // "address", thousands of attendees, a title past the form's limit. |
| 133 | addresses := []string{strings.Repeat("a", 1000) + "@example.com"} |
| 134 | for i := range 2000 { |
| 135 | addresses = append(addresses, fmt.Sprintf("attendee.number.%04d@example.com", i)) |
| 136 | } |
| 137 | if err = f.app.store.SetAttendees(t.Context(), e.ID, addresses); err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | title, err := f.app.store.seal.SealString(seal.PurposeEventTitle, strings.Repeat("é<", 20000)) |
| 141 | if err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | if err = f.app.db.G.Model(&Event{}).Where("id=?", e.ID).Update("title_sealed", title).Error; err != nil { |
| 145 | t.Fatal(err) |
| 146 | } |
| 147 | check := func(op string, input map[string]any) { |
| 148 | t.Helper() |
| 149 | w := calendarAgentCall(f.app, op, input) |
| 150 | if w.Code != 200 || w.Body.Len() > 48*1024 { |
| 151 | t.Fatal(op, w.Code, w.Body.Len()) |
| 152 | } |
| 153 | var out struct { |
| 154 | Events []struct { |
| 155 | Title string `json:"title"` |
| 156 | Attendees []string `json:"attendees"` |
| 157 | More int `json:"attendees_more"` |
| 158 | } `json:"events"` |
| 159 | Title string `json:"title"` |
| 160 | Attendees []string `json:"attendees"` |
| 161 | More int `json:"attendees_more"` |
| 162 | } |
| 163 | if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil { |
| 164 | t.Fatal(err) |
| 165 | } |
| 166 | if len(out.Events) == 1 { |
| 167 | out.Title, out.Attendees, out.More = out.Events[0].Title, out.Events[0].Attendees, out.Events[0].More |
| 168 | } |
| 169 | if len(out.Attendees) == 0 || len(out.Attendees) > 50 || out.Attendees[0] != "attendee.number.0000@example.com" || len(out.Attendees)+out.More != 2001 { |
| 170 | t.Fatal(op, "attendees", len(out.Attendees), out.More) |
| 171 | } |
| 172 | if n := utf8.RuneCountInString(out.Title); n == 0 || n > MaxTitleLen { |
| 173 | t.Fatal(op, "title", n) |
| 174 | } |
| 175 | } |
| 176 | check("calendar_list_events", map[string]any{"team": "acme", "from": "2027-03-10T00:00:00Z", "to": "2027-03-11T00:00:00Z"}) |
| 177 | check("calendar_get_event", map[string]any{"team": "acme", "id": e.ID}) |
| 178 | } |
| 179 | |