| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "math" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | access "amadan.net/oficina/agent-access.git" |
| 11 | ) |
| 12 | |
| 13 | func TestAgentEventPatchPreservesOmittedTextAndRejectsAllDayZone(t *testing.T) { |
| 14 | start := time.Now().UTC().Add(48 * time.Hour).Truncate(time.Minute) |
| 15 | event := EventView{Event: Event{CalendarID: 1, StartLocal: start.Format(LayoutLocal), EndLocal: start.Add(time.Hour).Format(LayoutLocal), TZ: "UTC", Kind: KindMeeting, Status: StatusConfirmed}, Title: "Original", Location: " imported location ", Notes: "\nimported notes\n"} |
| 16 | next, err := agentEventPatch(event, map[string]json.RawMessage{"title": json.RawMessage(`" Renamed "`)}) |
| 17 | if err != nil || next.Title != "Renamed" || next.Location != event.Location || next.Notes != event.Notes { |
| 18 | t.Fatal("patch changed omitted text", next, err) |
| 19 | } |
| 20 | _, err = agentEventPatch(event, map[string]json.RawMessage{"all_day": json.RawMessage(`true`), "start_local": json.RawMessage(`"2027-01-02"`), "end_local": json.RawMessage(`"2027-01-03"`)}) |
| 21 | if err == nil { |
| 22 | t.Fatal("all-day conversion silently discarded omitted zone") |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | func TestAgentEventUpdateRevisionExhaustion(t *testing.T) { |
| 27 | f, _, person, cal := linkedCalendarAgent(t) |
| 28 | e := createdAgentEvent(t, f.app, person, cal) |
| 29 | if err := f.app.db.G.Model(&Event{}).Where("id=?", e.ID).Update("sequence", math.MaxInt).Error; err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | w := calendarAgentCall(f.app, "calendar_update_event", agentUpdateInput(e.ID, math.MaxInt, map[string]any{"title": "overflow"})) |
| 33 | if w.Code != 409 { |
| 34 | t.Fatal(w.Code, w.Body.String()) |
| 35 | } |
| 36 | var got Event |
| 37 | if err := f.app.db.G.First(&got, e.ID).Error; err != nil || got.Sequence != math.MaxInt { |
| 38 | t.Fatal("revision corrupted", got.Sequence, err) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestAgentEventUpdateRetryFinalGuardWithoutResponse(t *testing.T) { |
| 43 | f, p, person, cal := linkedCalendarAgent(t) |
| 44 | e := createdAgentEvent(t, f.app, person, cal) |
| 45 | input := agentUpdateInput(e.ID, e.Sequence, map[string]any{"title": "updated"}) |
| 46 | if w := calendarAgentCall(f.app, "calendar_update_event", input); w.Code != 200 { |
| 47 | t.Fatal(w.Code) |
| 48 | } |
| 49 | checks := 0 |
| 50 | ctx := context.WithValue(t.Context(), agentBoundaryKey{}, agentRecheck(func(context.Context, access.Principal) error { |
| 51 | checks++ |
| 52 | if checks == 2 { |
| 53 | return access.Fail("unauthenticated", "Revoked.") |
| 54 | } |
| 55 | return nil |
| 56 | })) |
| 57 | raw, _ := json.Marshal(input) |
| 58 | if _, err := f.app.agentUpdateEvent(ctx, *p, person, raw); err == nil || access.SafeFailure(err).Code != "unauthenticated" || checks != 2 { |
| 59 | t.Fatal("retry skipped commit guard", err, checks) |
| 60 | } |
| 61 | } |
| 62 | |