From 97987fe03874ffd74d863f928ed7e3bdcccb4df7 Mon Sep 17 00:00:00 2001 From: Bobby Bones <159389674+saariuslystoned@users.noreply.github.com> Date: Sun, 8 Nov 2026 08:03:10 -0500 Subject: [PATCH 1/3] fix(calendar): report freebusy calendars Google could not read freeBusy.query returns errors[] and no busy periods for a calendar it could not compute, such as one the caller cannot see (notFound). The text and --plain views read only busy, so such a calendar printed nothing and looked free. Warn on stderr for each calendar with errors, as calendar team already reports them, and list rows in the requested calendar order instead of map order. Stdout columns, exit status and JSON output are unchanged. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 + internal/cmd/calendar_freebusy.go | 25 +++++- internal/cmd/calendar_freebusy_errors_test.go | 84 +++++++++++++++++++ internal/cmd/calendar_presentation.go | 27 +++++- internal/cmd/calendar_presentation_test.go | 2 +- 5 files changed, 135 insertions(+), 5 deletions(-) create mode 100644 internal/cmd/calendar_freebusy_errors_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 861521fad..d1b55a216 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. + ## 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..5d735c06c 100644 --- a/internal/cmd/calendar_freebusy.go +++ b/internal/cmd/calendar_freebusy.go @@ -67,7 +67,30 @@ 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", calendarID, strings.Join(reasons, ", ")) + } + } + + return outfmt.WriteTable(ctx, stdoutWriter(ctx), calendarFreeBusyRows(resp.Calendars, calendarIDs), calendarFreeBusyColumns()) +} + +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..7e168f505 --- /dev/null +++ b/internal/cmd/calendar_freebusy_errors_test.go @@ -0,0 +1,84 @@ +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, "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) + } +} 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, From 21a6caabd3de6710bb0586d4886952fb8e3614bd Mon Sep 17 00:00:00 2001 From: Bobby Bones <159389674+saariuslystoned@users.noreply.github.com> Date: Sun, 8 Nov 2026 08:03:22 -0500 Subject: [PATCH 2/3] docs(changelog): reference #1311 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d1b55a216..10a358796 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 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. +- 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 From 268dbaf9eaa7789ddb9ecb14582c2da1450bacc6 Mon Sep 17 00:00:00 2001 From: Bobby Bones <159389674+saariuslystoned@users.noreply.github.com> Date: Sun, 8 Nov 2026 08:36:36 -0500 Subject: [PATCH 3/3] fix(calendar): give freebusy warnings a next step The warning for a calendar Google could not read now ends with a next step for the documented reasons: check the calendar ID and sharing for notFound, query fewer calendars for tooManyCalendarsRequested and groupTooBig, and otherwise retry before relying on its availability. Co-Authored-By: Claude Opus 5.5 --- internal/cmd/calendar_freebusy.go | 16 +++++++++++++++- internal/cmd/calendar_freebusy_errors_test.go | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/internal/cmd/calendar_freebusy.go b/internal/cmd/calendar_freebusy.go index 5d735c06c..0c2aaefb2 100644 --- a/internal/cmd/calendar_freebusy.go +++ b/internal/cmd/calendar_freebusy.go @@ -71,13 +71,27 @@ func (c *CalendarFreeBusyCmd) Run(ctx context.Context, flags *RootFlags) error { // 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", calendarID, strings.Join(reasons, ", ")) + 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 { diff --git a/internal/cmd/calendar_freebusy_errors_test.go b/internal/cmd/calendar_freebusy_errors_test.go index 7e168f505..2badb279f 100644 --- a/internal/cmd/calendar_freebusy_errors_test.go +++ b/internal/cmd/calendar_freebusy_errors_test.go @@ -47,6 +47,9 @@ func TestCalendarFreeBusyCmd_WarnsWhenACalendarHasErrors(t *testing.T) { 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) } @@ -82,3 +85,17 @@ func TestCalendarFreeBusyCmd_JSONKeepsErrors(t *testing.T) { 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) + } + } +}