package calendartest import ( "bytes" "context" "encoding/json" "net/http" "net/http/httptest" "testing" "time" calendar "amadan.net/oficina/calendar/internal/calendar" ) // A native create is a JSON POST, not a page load. Main's scheduledWork // middleware re-arms the wake after EVERY request, so the reminder the // native create schedules is armed before anyone opens a page. The // merge of native/testflight kept that behaviour; this stands in front // of it. func TestNativeCreateArmsTheWakeForItsReminder(t *testing.T) { control := controlForTest(t) app, session := signedIn(t, "someone@example.com") rec := getAs(t, app.Handler(), session, "/api/native/session") var sess struct { DefaultCalendarID int64 `json:"default_calendar_id"` } if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &sess) != nil || sess.DefaultCalendarID == 0 { t.Fatalf("native session = %d: %s", rec.Code, rec.Body.String()) } if at, _ := control.snapshot(); !at.IsZero() { t.Fatalf("timer armed before any work existed: %v", at) } start := time.Now().UTC().Add(2 * time.Minute) body, _ := json.Marshal(map[string]any{ "calendar_id": sess.DefaultCalendarID, "title": "Native standup", "tz": "UTC", "start_local": start.Format("2006-01-02T15:04"), "end_local": start.Add(15 * time.Minute).Format("2006-01-02T15:04"), }) req := httptest.NewRequest(http.MethodPost, "/api/native/events", bytes.NewReader(body)) req.Header.Set("Origin", calendar.TestOrigin) req.Header.Set("Content-Type", "application/json") req.AddCookie(session) rec = httptest.NewRecorder() app.Handler().ServeHTTP(rec, req) var saved struct { ID int64 `json:"id"` Warning string `json:"warning"` } if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &saved) != nil || saved.ID == 0 || saved.Warning != "" { t.Fatalf("native create = %d: %s", rec.Code, rec.Body.String()) } expected := app.StoreForTest().NextDueAt(context.Background()) if expected.IsZero() { t.Fatal("native create scheduled no reminder, so this test proves nothing") } control.waitArmed(t) // The default reminder is already overdue, so the wake is "now": // no earlier than the due moment and no later than the check. if at, _ := control.snapshot(); at.Before(expected) || at.After(time.Now()) { t.Fatalf("native POST armed %v, want between %v and now", at, expected) } }