diff --git a/CHANGELOG.md b/CHANGELOG.md index 861521fad..10a358796 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.48.1 - Unreleased +- Calendar: warn on stderr when `calendar freebusy` gets no data for a calendar (for example `notFound` for a calendar you cannot see), instead of printing nothing for it as if it were free, and list text and `--plain` rows in the requested calendar order instead of a random order. Stdout columns and JSON output are unchanged. (#1311) — thanks @saariuslystoned. + ## 0.48.0 - 2026-11-05 **Highlights:** New backups stay local until you configure a remote; existing configurations keep working. Store service-account keys in the keyring, search Gmail IDs efficiently, and use environment-backed watch tokens, with Calendar fixes and security dependency updates. diff --git a/internal/cmd/calendar_freebusy.go b/internal/cmd/calendar_freebusy.go index ca3429d05..0c2aaefb2 100644 --- a/internal/cmd/calendar_freebusy.go +++ b/internal/cmd/calendar_freebusy.go @@ -67,7 +67,44 @@ func (c *CalendarFreeBusyCmd) Run(ctx context.Context, flags *RootFlags) error { return nil } - return outfmt.WriteTable(ctx, stdoutWriter(ctx), calendarFreeBusyRows(resp.Calendars), calendarFreeBusyColumns()) + // A calendar Google could not read comes back with errors and no busy + // periods, which a table of busy rows would show as free. + for _, calendarID := range calendarFreeBusyOrder(resp.Calendars, calendarIDs) { + if reasons := calendarFreeBusyErrorReasons(resp.Calendars[calendarID]); len(reasons) > 0 { + u.Err().Linef("Warning: no free/busy data for %s (%s); its busy times are unknown, not free. %s", calendarID, strings.Join(reasons, ", "), calendarFreeBusyErrorHint(reasons)) + } + } + + return outfmt.WriteTable(ctx, stdoutWriter(ctx), calendarFreeBusyRows(resp.Calendars, calendarIDs), calendarFreeBusyColumns()) +} + +// calendarFreeBusyErrorHint suggests a next step for the reasons Google +// documents for freeBusy calendar errors. +func calendarFreeBusyErrorHint(reasons []string) string { + for _, reason := range reasons { + switch reason { + case "notFound": + return "Check the calendar ID and that the calendar is shared with this account." + case "tooManyCalendarsRequested", "groupTooBig": + return "Query fewer calendars at a time." + } + } + return "Retry before relying on its availability." +} + +func calendarFreeBusyErrorReasons(data calendar.FreeBusyCalendar) []string { + reasons := make([]string, 0, len(data.Errors)) + for _, e := range data.Errors { + if e == nil { + continue + } + reason := strings.TrimSpace(e.Reason) + if reason == "" { + reason = "error" + } + reasons = append(reasons, reason) + } + return reasons } func (c *CalendarFreeBusyCmd) resolveFreeBusyRange(ctx context.Context, svc *calendar.Service) (string, string, error) { diff --git a/internal/cmd/calendar_freebusy_errors_test.go b/internal/cmd/calendar_freebusy_errors_test.go new file mode 100644 index 000000000..2badb279f --- /dev/null +++ b/internal/cmd/calendar_freebusy_errors_test.go @@ -0,0 +1,101 @@ +package cmd + +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/openclaw/gogcli/internal/outfmt" +) + +// freeBusyErrorsServer answers freeBusy.query the way Google does when one of +// the requested calendars cannot be read: that calendar has errors and no busy +// periods. The response lists calendars in a different order than requested. +func runFreeBusyWithErrors(t *testing.T, mode outfmt.Mode, args ...string) (string, string) { + t.Helper() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost || !strings.HasSuffix(r.URL.Path, "/freeBusy") { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{ + "kind": "calendar#freeBusy", + "calendars": { + "a@example.com": {"busy": [{"start": "2026-11-10T17:00:00Z", "end": "2026-11-10T18:00:00Z"}]}, + "b@example.com": {"busy": [{"start": "2026-11-10T19:00:00Z", "end": "2026-11-10T20:00:00Z"}]}, + "c@example.com": {"errors": [{"domain": "global", "reason": "notFound"}], "busy": []} + } + }`)) + })) + t.Cleanup(srv.Close) + + var stdout, stderr bytes.Buffer + ctx := withCalendarTestService(outfmt.WithMode(newCmdRuntimeOutputContext(t, &stdout, &stderr), mode), newCalendarServiceFromServer(t, srv)) + if err := runKong(t, &CalendarFreeBusyCmd{}, args, ctx, &RootFlags{Account: "a@b.com"}); err != nil { + t.Fatalf("freebusy: %v", err) + } + return stdout.String(), stderr.String() +} + +func TestCalendarFreeBusyCmd_WarnsWhenACalendarHasErrors(t *testing.T) { + _, stderr := runFreeBusyWithErrors(t, outfmt.Mode{}, "a@example.com,c@example.com,b@example.com", + "--from", "2026-11-10T16:00:00Z", "--to", "2026-11-10T22:00:00Z") + if !strings.Contains(stderr, "c@example.com") || !strings.Contains(stderr, "notFound") { + t.Fatalf("stderr = %q, want a warning naming c@example.com and notFound", stderr) + } + if !strings.Contains(stderr, "shared with this account") { + t.Fatalf("stderr = %q, want a next step for notFound", stderr) + } + if strings.Contains(stderr, "a@example.com") || strings.Contains(stderr, "b@example.com") { + t.Fatalf("stderr = %q, warned about calendars without errors", stderr) + } +} + +func TestCalendarFreeBusyCmd_PlainRowsFollowRequestedOrder(t *testing.T) { + for range 20 { + stdout, _ := runFreeBusyWithErrors(t, outfmt.Mode{Plain: true}, "a@example.com,c@example.com,b@example.com", + "--from", "2026-11-10T16:00:00Z", "--to", "2026-11-10T22:00:00Z") + want := "CALENDAR\tSTART\tEND\n" + + "a@example.com\t2026-11-10T17:00:00Z\t2026-11-10T18:00:00Z\n" + + "b@example.com\t2026-11-10T19:00:00Z\t2026-11-10T20:00:00Z\n" + if stdout != want { + t.Fatalf("stdout = %q, want %q", stdout, want) + } + } +} + +func TestCalendarFreeBusyCmd_JSONKeepsErrors(t *testing.T) { + stdout, _ := runFreeBusyWithErrors(t, outfmt.Mode{JSON: true}, "c@example.com", + "--from", "2026-11-10T16:00:00Z", "--to", "2026-11-10T22:00:00Z") + var parsed struct { + Calendars map[string]struct { + Errors []struct { + Reason string `json:"reason"` + } `json:"errors"` + } `json:"calendars"` + } + if err := json.Unmarshal([]byte(stdout), &parsed); err != nil { + t.Fatalf("json: %v (%q)", err, stdout) + } + if got := parsed.Calendars["c@example.com"].Errors; len(got) != 1 || got[0].Reason != "notFound" { + t.Fatalf("errors = %#v", got) + } +} + +func TestCalendarFreeBusyErrorHint(t *testing.T) { + t.Parallel() + for reasons, want := range map[string]string{ + "notFound": "shared with this account", + "tooManyCalendarsRequested": "fewer calendars", + "groupTooBig": "fewer calendars", + "internalError": "Retry", + } { + if got := calendarFreeBusyErrorHint([]string{reasons}); !strings.Contains(got, want) { + t.Fatalf("hint(%s) = %q, want %q", reasons, got, want) + } + } +} diff --git a/internal/cmd/calendar_presentation.go b/internal/cmd/calendar_presentation.go index 0ef4d9ea4..0619413c6 100644 --- a/internal/cmd/calendar_presentation.go +++ b/internal/cmd/calendar_presentation.go @@ -87,10 +87,10 @@ func calendarFreeBusyColumns() []outfmt.Column[calendarFreeBusyRow] { } } -func calendarFreeBusyRows(calendars map[string]calendar.FreeBusyCalendar) []calendarFreeBusyRow { +func calendarFreeBusyRows(calendars map[string]calendar.FreeBusyCalendar, requested []string) []calendarFreeBusyRow { rows := make([]calendarFreeBusyRow, 0) - for calendarID, data := range calendars { - for _, busy := range data.Busy { + for _, calendarID := range calendarFreeBusyOrder(calendars, requested) { + for _, busy := range calendars[calendarID].Busy { if busy != nil { rows = append(rows, calendarFreeBusyRow{ CalendarID: calendarID, @@ -103,6 +103,27 @@ func calendarFreeBusyRows(calendars map[string]calendar.FreeBusyCalendar) []cale return rows } +// calendarFreeBusyOrder lists the response's calendars in the order they were +// requested, then any others sorted by ID, so output does not follow map order. +func calendarFreeBusyOrder(calendars map[string]calendar.FreeBusyCalendar, requested []string) []string { + order := make([]string, 0, len(calendars)) + seen := make(map[string]bool, len(calendars)) + for _, calendarID := range requested { + if _, ok := calendars[calendarID]; ok && !seen[calendarID] { + seen[calendarID] = true + order = append(order, calendarID) + } + } + rest := make([]string, 0, len(calendars)-len(order)) + for calendarID := range calendars { + if !seen[calendarID] { + rest = append(rest, calendarID) + } + } + sort.Strings(rest) + return append(order, rest...) +} + func calendarUserColumns() []outfmt.Column[calendarUserRow] { return []outfmt.Column[calendarUserRow]{ {Header: "EMAIL", Value: func(row calendarUserRow) string { return sanitizeTab(row.Email) }}, diff --git a/internal/cmd/calendar_presentation_test.go b/internal/cmd/calendar_presentation_test.go index 31d027f43..41c638a0a 100644 --- a/internal/cmd/calendar_presentation_test.go +++ b/internal/cmd/calendar_presentation_test.go @@ -64,7 +64,7 @@ func TestCalendarPresentationSchemas(t *testing.T) { {Start: "2026-06-12T10:00:00Z", End: "2026-06-12T11:00:00Z"}, }, }, - }) + }, nil) got := renderPlainTable(t, rows, calendarFreeBusyColumns()) assertTableOutput( t,