diff --git a/.agents/skills/gog-admin/SKILL.md b/.agents/skills/gog-admin/SKILL.md index 972a4000f..d0855e0d5 100644 --- a/.agents/skills/gog-admin/SKILL.md +++ b/.agents/skills/gog-admin/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-admin -description: "Workspace Admin operations through gog." +description: "Workspace Admin through gog. Use for Workspace Admin Directory users, groups, and organizational units; use gog-groups for Cloud Identity group membership lookup." --- # Workspace Admin diff --git a/.agents/skills/gog-adsense/SKILL.md b/.agents/skills/gog-adsense/SKILL.md index cf8f768ff..557059e69 100644 --- a/.agents/skills/gog-adsense/SKILL.md +++ b/.agents/skills/gog-adsense/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-adsense -description: "Google AdSense operations through gog." +description: "Google AdSense through gog. Use for AdSense accounts, sites, payments, and earnings reports." --- # Google AdSense diff --git a/.agents/skills/gog-analytics/SKILL.md b/.agents/skills/gog-analytics/SKILL.md index 246b34b13..ca60d6785 100644 --- a/.agents/skills/gog-analytics/SKILL.md +++ b/.agents/skills/gog-analytics/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-analytics -description: "Google Analytics operations through gog." +description: "Google Analytics through gog. Use for Google Analytics account summaries and GA4 reports." --- # Google Analytics diff --git a/.agents/skills/gog-appscript/SKILL.md b/.agents/skills/gog-appscript/SKILL.md index 6e03c3886..433821d90 100644 --- a/.agents/skills/gog-appscript/SKILL.md +++ b/.agents/skills/gog-appscript/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-appscript -description: "Apps Script operations through gog." +description: "Apps Script through gog. Use for Apps Script projects, source files, deployments, and executions." --- # Apps Script diff --git a/.agents/skills/gog-calendar/SKILL.md b/.agents/skills/gog-calendar/SKILL.md index 97f58ccc7..57ebd470c 100644 --- a/.agents/skills/gog-calendar/SKILL.md +++ b/.agents/skills/gog-calendar/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-calendar -description: "Google Calendar operations through gog." +description: "Google Calendar through gog. Use for calendars, events, availability, and scheduling; use gog-meeting-prep for meeting briefings or gog-weekly-digest for a cross-service summary." --- # Google Calendar diff --git a/.agents/skills/gog-chat/SKILL.md b/.agents/skills/gog-chat/SKILL.md index dbcf6b049..71d6d29f0 100644 --- a/.agents/skills/gog-chat/SKILL.md +++ b/.agents/skills/gog-chat/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-chat -description: "Google Chat operations through gog." +description: "Google Chat through gog. Use for Google Chat spaces, messages, threads, and members." --- # Google Chat diff --git a/.agents/skills/gog-classroom/SKILL.md b/.agents/skills/gog-classroom/SKILL.md index bf2aa47ac..99c1b858a 100644 --- a/.agents/skills/gog-classroom/SKILL.md +++ b/.agents/skills/gog-classroom/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-classroom -description: "Google Classroom operations through gog." +description: "Google Classroom through gog. Use for Classroom courses, rosters, coursework, submissions, and announcements." --- # Google Classroom diff --git a/.agents/skills/gog-contacts/SKILL.md b/.agents/skills/gog-contacts/SKILL.md index cc9a3bf5a..d98da87d1 100644 --- a/.agents/skills/gog-contacts/SKILL.md +++ b/.agents/skills/gog-contacts/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-contacts -description: "Google Contacts operations through gog." +description: "Google Contacts through gog. Use for personal contacts, contact groups, and other contacts, with directory lookup also available; use gog-people for the caller profile or Workspace directory profiles." --- # Google Contacts diff --git a/.agents/skills/gog-docs/SKILL.md b/.agents/skills/gog-docs/SKILL.md index 88093aff0..6d6b1b17d 100644 --- a/.agents/skills/gog-docs/SKILL.md +++ b/.agents/skills/gog-docs/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-docs -description: "Google Docs operations through gog." +description: "Google Docs through gog. Use for Google Docs text, formatting, tabs, comments, and document exports." --- # Google Docs diff --git a/.agents/skills/gog-drive/SKILL.md b/.agents/skills/gog-drive/SKILL.md index 66757f4a8..6debd3b57 100644 --- a/.agents/skills/gog-drive/SKILL.md +++ b/.agents/skills/gog-drive/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-drive -description: "Google Drive operations through gog." +description: "Google Drive through gog. Use for Drive file search, uploads, downloads, sharing, folders, and metadata; use gog-drive-audit for a sharing audit." --- # Google Drive diff --git a/.agents/skills/gog-forms/SKILL.md b/.agents/skills/gog-forms/SKILL.md index b44581032..db37be428 100644 --- a/.agents/skills/gog-forms/SKILL.md +++ b/.agents/skills/gog-forms/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-forms -description: "Google Forms operations through gog." +description: "Google Forms through gog. Use for Google Forms structure, questions, responses, and watches." --- # Google Forms diff --git a/.agents/skills/gog-gmail/SKILL.md b/.agents/skills/gog-gmail/SKILL.md index 8953bffdd..1139fa820 100644 --- a/.agents/skills/gog-gmail/SKILL.md +++ b/.agents/skills/gog-gmail/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-gmail -description: "Gmail operations through gog." +description: "Gmail through gog. Use for Gmail search, messages, threads, drafts, sending, labels, filters, and settings; use gog-inbox-triage for inbox review or gog-save-attachments for attachment archival." --- # Gmail diff --git a/.agents/skills/gog-groups/SKILL.md b/.agents/skills/gog-groups/SKILL.md index 44d70dbad..fcd123047 100644 --- a/.agents/skills/gog-groups/SKILL.md +++ b/.agents/skills/gog-groups/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-groups -description: "Google Groups operations through gog." +description: "Google Groups through gog. Use for listing Cloud Identity groups and members; use gog-admin to manage Workspace Admin Directory groups." --- # Google Groups diff --git a/.agents/skills/gog-keep/SKILL.md b/.agents/skills/gog-keep/SKILL.md index 5913cd17c..577f2a044 100644 --- a/.agents/skills/gog-keep/SKILL.md +++ b/.agents/skills/gog-keep/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-keep -description: "Google Keep operations through gog." +description: "Google Keep through gog. Use for Google Keep notes and attachments." --- # Google Keep diff --git a/.agents/skills/gog-maps/SKILL.md b/.agents/skills/gog-maps/SKILL.md index 1f5c0d17e..53164bdbe 100644 --- a/.agents/skills/gog-maps/SKILL.md +++ b/.agents/skills/gog-maps/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-maps -description: "Google Maps operations through gog." +description: "Google Maps through gog. Use for Maps place search, place details, directions, and geocoding." --- # Google Maps diff --git a/.agents/skills/gog-meet/SKILL.md b/.agents/skills/gog-meet/SKILL.md index f20bb4000..1c54e15ed 100644 --- a/.agents/skills/gog-meet/SKILL.md +++ b/.agents/skills/gog-meet/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-meet -description: "Google Meet operations through gog." +description: "Google Meet through gog. Use for Google Meet spaces, configuration, call history, and participants." --- # Google Meet diff --git a/.agents/skills/gog-people/SKILL.md b/.agents/skills/gog-people/SKILL.md index 37df010f1..0bd10a2ec 100644 --- a/.agents/skills/gog-people/SKILL.md +++ b/.agents/skills/gog-people/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-people -description: "Google People operations through gog." +description: "Google People through gog. Use for the caller profile, a person profile, or Workspace directory search; use gog-contacts to manage personal contacts and contact groups." --- # Google People diff --git a/.agents/skills/gog-photos/SKILL.md b/.agents/skills/gog-photos/SKILL.md index 41fc1f927..0ce4de68d 100644 --- a/.agents/skills/gog-photos/SKILL.md +++ b/.agents/skills/gog-photos/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-photos -description: "Google Photos operations through gog." +description: "Google Photos through gog. Use for Google Photos media and albums available to the app, including uploads and the picker." --- # Google Photos diff --git a/.agents/skills/gog-searchconsole/SKILL.md b/.agents/skills/gog-searchconsole/SKILL.md index d0cc78b7a..c58a9971f 100644 --- a/.agents/skills/gog-searchconsole/SKILL.md +++ b/.agents/skills/gog-searchconsole/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-searchconsole -description: "Google Search Console operations through gog." +description: "Google Search Console through gog. Use for Search Console sites, search analytics, sitemaps, and URL inspection." --- # Google Search Console diff --git a/.agents/skills/gog-sheets/SKILL.md b/.agents/skills/gog-sheets/SKILL.md index e5837fc1a..31c609668 100644 --- a/.agents/skills/gog-sheets/SKILL.md +++ b/.agents/skills/gog-sheets/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-sheets -description: "Google Sheets operations through gog." +description: "Google Sheets through gog. Use for spreadsheet cells, formulas, formatting, notes, validation, and sheet structure." --- # Google Sheets diff --git a/.agents/skills/gog-sites/SKILL.md b/.agents/skills/gog-sites/SKILL.md index 3214084c6..6dc508db8 100644 --- a/.agents/skills/gog-sites/SKILL.md +++ b/.agents/skills/gog-sites/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-sites -description: "Google Sites operations through gog." +description: "Google Sites through gog. Use for finding Google Sites in Drive, reading site metadata, and printing editor URLs; use gog-drive for file sharing." --- # Google Sites diff --git a/.agents/skills/gog-slides/SKILL.md b/.agents/skills/gog-slides/SKILL.md index 291381886..f6d464c3a 100644 --- a/.agents/skills/gog-slides/SKILL.md +++ b/.agents/skills/gog-slides/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-slides -description: "Google Slides operations through gog." +description: "Google Slides through gog. Use for presentations, slides, text, images, speaker notes, and exports." --- # Google Slides diff --git a/.agents/skills/gog-tasks/SKILL.md b/.agents/skills/gog-tasks/SKILL.md index 2be3ae1a6..a533fc981 100644 --- a/.agents/skills/gog-tasks/SKILL.md +++ b/.agents/skills/gog-tasks/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-tasks -description: "Google Tasks operations through gog." +description: "Google Tasks through gog. Use for Google Tasks lists, task creation, updates, completion, and materialized repeats." --- # Google Tasks diff --git a/.agents/skills/gog-youtube/SKILL.md b/.agents/skills/gog-youtube/SKILL.md index 974dda9eb..530fe5992 100644 --- a/.agents/skills/gog-youtube/SKILL.md +++ b/.agents/skills/gog-youtube/SKILL.md @@ -1,6 +1,6 @@ --- name: gog-youtube -description: "YouTube operations through gog." +description: "YouTube through gog. Use for YouTube search, videos, channels, activities, comments, playlists, and subscriptions." --- # YouTube diff --git a/.agents/skills/gog/SKILL.md b/.agents/skills/gog/SKILL.md index 90cd67b58..109a49972 100644 --- a/.agents/skills/gog/SKILL.md +++ b/.agents/skills/gog/SKILL.md @@ -1,6 +1,6 @@ --- name: gog -description: "gog CLI: safe Google Workspace automation, JSON, auth, scoped reads/writes." +description: "Use the gog CLI when Google connectors lack an operation, shell automation needs stable JSON, or local Google auth needs inspection. Shared account, auth, output, and safety guidance for all gog service skills." --- # gog @@ -142,8 +142,9 @@ the pane width. A truncated consent URL does not fail loudly: Google renders client misconfiguration and sends you debugging the wrong thing. Verify the captured URL contains `response_type` before using it. -Write the URL to a mode-0600 file and hand it to the browser by file reference -(see `$browser-use`); never echo it. Appending `&login_hint=user@example.com` +Write the URL to a mode-0600 file and use a browser automation tool that can +read that file and open the consent URL without displaying it in logs; never +echo it. Appending `&login_hint=user@example.com` skips the account chooser and removes a whole class of wrong-account risk. Expect up to two interstitials when the OAuth client is unverified or in diff --git a/CHANGELOG.md b/CHANGELOG.md index cc8593e54..66288b65f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,19 @@ ## 0.48.1 - Unreleased +- Calendar: reject empty or inverted RFC3339 free/busy windows, resolve calendar names containing `@` consistently, and reject malformed nonblank attendee clears. (#1328, #1329, #1330) — thanks @privatenumber and @pkyIntelligence. +- Calendar: read search and team event pages up to `--max`, truncate the parent series for future-instance updates, and warn about unavailable free/busy calendars with stable row ordering. (#1311, #1317, #1322) — thanks @saariuslystoned. +- Gmail: exclude all send-as aliases from reply-all and default auto-replies; stop default auto-replies when alias discovery fails, and preserve Cc-only reply-all recipients. (#1318, #1325) — thanks @saariuslystoned. +- Gmail: forward HTML-only originals in the plain-text part and read every watch history page before advancing the cursor. (#1312, #1321) — thanks @saariuslystoned. +- Sheets: preserve numeric JSON tokens in append requests and keep multiline cells on one line in text output. (#1316, #1331) — thanks @saariuslystoned and @pkyIntelligence. +- Output: return empty arrays for Drive labels, Drive/Docs comments, and Sheets notes and links, including `--results-only`. (#1314, #1326) — thanks @saariuslystoned. +- Drive: accept commas in label field flags and download or export a shortcut's target. (#1315, #1319) — thanks @saariuslystoned. +- Tasks: send explicit empty titles and notes when clearing them, and reject an empty status. (#1313) — thanks @saariuslystoned. +- Contacts: document and apply Google's 30-result search ceiling, warning when a larger limit is requested. (#1323) — thanks @saariuslystoned. +- AdSense: show report warnings and truncation hints on stderr in table output. (#1324) — thanks @saariuslystoned. +- Sites: preserve requested false booleans in site metadata JSON. (#1308) — thanks @SebTardif. +- Agent skills: describe when to use each service and distinguish overlapping workflows; explain browser consent without requiring an unshipped skill. (#1335) — thanks @henkisdabro. + - Docs: use placeholder accounts in Gmail auto-reply, Drive audit, and live-testing examples. (#1332) — thanks @vyctorbrzezowski. ## 0.48.0 - 2026-11-05 diff --git a/docs/commands/gog-contacts-other-search.md b/docs/commands/gog-contacts-other-search.md index f497569ab..88330da04 100644 --- a/docs/commands/gog-contacts-other-search.md +++ b/docs/commands/gog-contacts-other-search.md @@ -31,7 +31,7 @@ gog contacts (contact) other search ... [flags] | `-h`
`--help` | `kong.helpFlag` | | Show context-sensitive help. | | `--home` | `string` | | Override gogcli config/data/state/cache root (equivalent to GOG_HOME) | | `-j`
`--json`
`--machine` | `bool` | false | Output JSON to stdout (best for scripting) | -| `--max`
`--limit` | `int64` | 50 | Max results | +| `--max`
`--limit` | `int64` | 30 | Max results (Google returns at most 30) | | `--no-input`
`--non-interactive`
`--noninteractive` | `bool` | | Never prompt; fail instead (useful for CI) | | `-p`
`--plain`
`--tsv` | `bool` | false | Output stable, parseable text to stdout (TSV; no colors) | | `--quota-project` | `string` | | Google Cloud project to bill for API usage (sent as X-Goog-User-Project; some APIs require it with --access-token or ADC) | diff --git a/docs/commands/gog-contacts-search.md b/docs/commands/gog-contacts-search.md index 43062069e..2b1c7b568 100644 --- a/docs/commands/gog-contacts-search.md +++ b/docs/commands/gog-contacts-search.md @@ -31,7 +31,7 @@ gog contacts (contact) search ... [flags] | `-h`
`--help` | `kong.helpFlag` | | Show context-sensitive help. | | `--home` | `string` | | Override gogcli config/data/state/cache root (equivalent to GOG_HOME) | | `-j`
`--json`
`--machine` | `bool` | false | Output JSON to stdout (best for scripting) | -| `--max`
`--limit` | `int64` | 50 | Max results | +| `--max`
`--limit` | `int64` | 30 | Max results (Google returns at most 30) | | `--no-input`
`--non-interactive`
`--noninteractive` | `bool` | | Never prompt; fail instead (useful for CI) | | `-p`
`--plain`
`--tsv` | `bool` | false | Output stable, parseable text to stdout (TSV; no colors) | | `--quota-project` | `string` | | Google Cloud project to bill for API usage (sent as X-Goog-User-Project; some APIs require it with --access-token or ADC) | diff --git a/docs/commands/gog-tasks-update.md b/docs/commands/gog-tasks-update.md index c4f543c0a..1bf05b464 100644 --- a/docs/commands/gog-tasks-update.md +++ b/docs/commands/gog-tasks-update.md @@ -39,7 +39,7 @@ gog tasks (task) update (edit,set) [flags] | `--readonly` | `bool` | false | Block mutating API requests at runtime; auth add also requests read-only OAuth scopes | | `--results-only` | `bool` | | In JSON mode, emit only the primary result (drops envelope fields like nextPageToken) | | `--select`
`--pick`
`--project` | `string` | | In JSON mode, select comma-separated fields (best-effort; supports dot paths). Desire path: use --fields for most commands. | -| `--status` | `string` | | New status: needsAction\|completed (set empty to clear) | +| `--status` | `string` | | New status: needsAction\|completed | | `--title` | `string` | | New title (set empty to clear) | | `-v`
`--verbose` | `bool` | | Enable verbose logging | | `--version` | `kong.VersionFlag` | | Print version and exit | diff --git a/docs/examples.md b/docs/examples.md index 056ad8101..46fa958a5 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -27,6 +27,11 @@ permanent deletion is intentional. ## Calendar +`calendar freebusy` rejects empty or inverted windows before querying Google, +including RFC3339 bounds. Calendar names containing `@` resolve as names, +while email-form IDs and `primary` can be queried directly. For event updates, +an empty `--attendees` clears the list; nonblank input must contain an attendee. + See the [`gog calendar`](commands/gog-calendar.md) reference and [Zoom setup](zoom-auth-setup.md). @@ -215,6 +220,11 @@ gog docs raw --pretty ## Sheets +`sheets append --values-json` preserves numeric tokens in its outgoing request; +Google Sheets still applies its own storage precision. Use JSON output to +preserve cell content exactly. Text and `--plain` output keep each row on one +line, displaying embedded newlines as `\n` and tabs as spaces. + See [batch updates](sheets-batch-update.md), [tables](sheets-tables.md), and [formatting](sheets-formatting.md). diff --git a/docs/gmail-autoreply.md b/docs/gmail-autoreply.md index f128bb56b..f5536583a 100644 --- a/docs/gmail-autoreply.md +++ b/docs/gmail-autoreply.md @@ -44,3 +44,5 @@ If you cannot use GitHub or are unsure which repo is affected, reply to this ema - It sends replies in-thread with `In-Reply-To` and `References`. - It adds `Auto-Submitted: auto-replied` and suppresses common auto-response loops. - By default it skips bulk/list/auto-generated mail. +- It also skips mail from your account or any send-as alias, even if the message has an external `Reply-To`. Use `--allow-self` to allow those replies. +- Without `--allow-self`, a failed send-as lookup stops the run before searching or sending, so an unavailable alias list cannot cause replies to your own addresses. diff --git a/docs/watch.md b/docs/watch.md index be0d1b3af..92f5fbbe2 100644 --- a/docs/watch.md +++ b/docs/watch.md @@ -105,6 +105,9 @@ Notes: - `watch renew` reuses stored topic/labels. - Push and pull inspect notifications whose history ID equals the saved cursor; lower IDs remain stale. Duplicate Pub/Sub message IDs are still ignored. +- Push and pull read every page of Gmail history after the saved cursor and + advance the cursor only after the last page, so bursts of more than one page + of changes are delivered instead of skipped. - `watch stop` calls Gmail stop + clears state. - `watch serve` and `watch pull` use stored hook config if `--hook-url` is not provided. diff --git a/internal/cmd/adsense_report_warnings_test.go b/internal/cmd/adsense_report_warnings_test.go new file mode 100644 index 000000000..0a57a2be3 --- /dev/null +++ b/internal/cmd/adsense_report_warnings_test.go @@ -0,0 +1,102 @@ +package cmd + +import ( + "encoding/json" + "net/http" + "strings" + "testing" +) + +// A truncated report (totalMatchedRows above the rows returned) and the API's +// own warnings reach JSON output; table and --plain output must not drop them. +func TestExecute_AdSenseReportsQuery_TableShowsTruncationAndWarnings(t *testing.T) { + svc := newAdSenseTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !(r.Method == http.MethodGet && strings.Contains(r.URL.Path, ":generate")) { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{ + "headers": []map[string]any{ + {"name": "DATE", "type": "DIMENSION"}, + {"name": "ESTIMATED_EARNINGS", "type": "METRIC_CURRENCY"}, + }, + "rows": []map[string]any{ + {"cells": []map[string]any{{"value": "2026-02-01"}, {"value": "1.23"}}}, + {"cells": []map[string]any{{"value": "2026-02-02"}, {"value": "4.56"}}}, + }, + "totalMatchedRows": "250000", + "warnings": []string{"Some requested metrics are not available for this date range."}, + }) + })) + for _, mode := range []string{"--plain", ""} { + args := []string{"--account", "a@b.com", "adsense", "reports", "query", "pub-123", "--metrics", "estimated_earnings"} + if mode != "" { + args = append([]string{mode}, args...) + } + result := executeWithAdSenseTestService(t, args, svc) + if result.err != nil { + t.Fatalf("%q: Execute: %v", mode, result.err) + } + if !strings.Contains(result.stdout, "2026-02-02") { + t.Fatalf("%q: rows missing from stdout: %q", mode, result.stdout) + } + if strings.Contains(result.stdout, "250000") || strings.Contains(result.stdout, "not available") { + t.Fatalf("%q: notes must stay off stdout: %q", mode, result.stdout) + } + if !strings.Contains(result.stderr, "2 of 250000") { + t.Fatalf("%q: want truncation note on stderr, got %q", mode, result.stderr) + } + if !strings.Contains(result.stderr, "Some requested metrics are not available") { + t.Fatalf("%q: want API warning on stderr, got %q", mode, result.stderr) + } + } +} + +// An empty report can still carry API warnings, which explain why no rows came back. +func TestExecute_AdSenseReportsQuery_EmptyTableStillShowsWarnings(t *testing.T) { + svc := newAdSenseTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{ + "headers": []map[string]any{{"name": "ESTIMATED_EARNINGS", "type": "METRIC_CURRENCY"}}, + "rows": []any{}, + "warnings": []string{"Data for this date range is not available yet."}, + }) + })) + for _, mode := range []string{"--plain", ""} { + args := []string{"--account", "a@b.com", "adsense", "reports", "query", "pub-123", "--metrics", "estimated_earnings"} + if mode != "" { + args = append([]string{mode}, args...) + } + result := executeWithAdSenseTestService(t, args, svc) + if result.err != nil { + t.Fatalf("%q: Execute: %v", mode, result.err) + } + if !strings.Contains(result.stderr, "Data for this date range is not available yet.") || !strings.Contains(result.stderr, "No AdSense report rows") { + t.Fatalf("%q: want the warning and the empty-report note on stderr, got %q", mode, result.stderr) + } + } +} + +// Saved reports have no --max flag, so their truncation note must not suggest one. +func TestExecute_AdSenseSavedReportTruncationHintHasNoMaxFlag(t *testing.T) { + svc := newAdSenseTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !strings.Contains(r.URL.Path, "/saved:generate") { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{ + "headers": []map[string]any{{"name": "DATE", "type": "DIMENSION"}}, + "rows": []map[string]any{{"cells": []map[string]any{{"value": "2026-02-01"}}}}, + "totalMatchedRows": "250000", + }) + })) + result := executeWithAdSenseTestService(t, []string{"--plain", "--account", "a@b.com", "adsense", "reports", "saved", "query", "accounts/pub-123/reports/report-1"}, svc) + if result.err != nil { + t.Fatalf("Execute: %v", result.err) + } + if !strings.Contains(result.stderr, "1 of 250000") || strings.Contains(result.stderr, "--max") { + t.Fatalf("want a truncation note without --max, got %q", result.stderr) + } +} diff --git a/internal/cmd/adsense_reports.go b/internal/cmd/adsense_reports.go index cf9acb8f7..a23ed5c1d 100644 --- a/internal/cmd/adsense_reports.go +++ b/internal/cmd/adsense_reports.go @@ -92,7 +92,7 @@ func (c *AdSenseReportsQueryCmd) Run(ctx context.Context, flags *RootFlags) erro return err } - return writeAdSenseReportResult(ctx, u, plan.Account, plan.Dimensions, plan.Metrics, resp, c.FailEmpty) + return writeAdSenseReportResult(ctx, u, plan.Account, plan.Dimensions, plan.Metrics, resp, c.FailEmpty, "raise --max (up to 100000) or narrow the report") } func applyAdSenseReportDateRange(call *adsenseapi.AccountsReportsGenerateCall, plan adSenseReportPlan) *adsenseapi.AccountsReportsGenerateCall { @@ -112,6 +112,7 @@ func writeAdSenseReportResult( metrics []string, resp *adsenseapi.ReportResult, failEmpty bool, + moreRowsHint string, ) error { if outfmt.IsJSON(ctx) { if err := outfmt.WriteJSON(ctx, stdoutWriter(ctx), map[string]any{ @@ -133,6 +134,17 @@ func writeAdSenseReportResult( return nil } + // JSON carries these fields; keep them visible here too, on stderr so + // stdout stays parseable. + for _, warning := range resp.Warnings { + if warning = strings.TrimSpace(warning); warning != "" { + u.Err().Linef("# Warning: %s", warning) + } + } + if resp.TotalMatchedRows > int64(len(resp.Rows)) { + u.Err().Linef("# Showing %d of %d matched rows; %s to see more", len(resp.Rows), resp.TotalMatchedRows, moreRowsHint) + } + if len(resp.Rows) == 0 { u.Err().Println("No AdSense report rows") return failEmptyExit(failEmpty) @@ -307,5 +319,5 @@ func (c *AdSenseReportsSavedQueryCmd) Run(ctx context.Context, flags *RootFlags) return err } - return writeAdSenseReportResult(ctx, u, name, nil, nil, resp, c.FailEmpty) + return writeAdSenseReportResult(ctx, u, name, nil, nil, resp, c.FailEmpty, "narrow the saved report, for example its date range") } diff --git a/internal/cmd/calendar_edit.go b/internal/cmd/calendar_edit.go index 38dcb3a5e..c83463790 100644 --- a/internal/cmd/calendar_edit.go +++ b/internal/cmd/calendar_edit.go @@ -399,10 +399,11 @@ func (c *CalendarUpdateCmd) Run(ctx context.Context, kctx *kong.Context, flags * } } - targetEventID, parentRecurrence, err := applyUpdateScope(ctx, mutation.svc, mutation.calendarID, plan.EventID, plan.Scope, plan.OriginalStartTime, patch) + scoped, err := applyUpdateScope(ctx, mutation.svc, mutation.calendarID, plan.EventID, plan.Scope, plan.OriginalStartTime, patch) if err != nil { return err } + targetEventID := scoped.TargetEventID if patch.ConferenceData != nil && !fields.RegenerateMeet && !fields.zoomMutation() { existing, getErr := mutation.svc.Events.Get(mutation.calendarID, targetEventID).Context(ctx).Do() if getErr != nil { @@ -432,7 +433,7 @@ func (c *CalendarUpdateCmd) Run(ctx context.Context, kctx *kong.Context, flags * return err } if plan.Scope == scopeFuture { - if err := truncateParentRecurrence(ctx, mutation.svc, mutation.calendarID, plan.EventID, parentRecurrence, plan.OriginalStartTime, plan.SendUpdates); err != nil { + if err := truncateParentRecurrence(ctx, mutation.svc, mutation.calendarID, scoped.ParentEventID, scoped.ParentRecurrence, plan.OriginalStartTime, plan.SendUpdates); err != nil { return err } } @@ -545,10 +546,13 @@ func (c *CalendarUpdateCmd) prepareZoomConferencePatch( return patch, changed, nil } -func applyUpdateScope(ctx context.Context, svc *calendar.Service, calendarID, eventID, scope, originalStartTime string, patch *calendar.Event) (string, []string, error) { +// applyUpdateScope resolves which event a scoped update patches and, for +// scope=future, which series it truncates. eventID may be a series ID or an +// instance ID from a listing; ParentEventID is the series either way. +func applyUpdateScope(ctx context.Context, svc *calendar.Service, calendarID, eventID, scope, originalStartTime string, patch *calendar.Event) (recurringScopeResolution, error) { resolution, err := resolveRecurringScopeResolution(ctx, svc, calendarID, eventID, scope, originalStartTime) if err != nil { - return "", nil, err + return recurringScopeResolution{}, err } if scope == scopeFuture { @@ -567,7 +571,7 @@ func applyUpdateScope(ctx context.Context, svc *calendar.Service, calendarID, ev } } - return resolution.TargetEventID, resolution.ParentRecurrence, nil + return resolution, nil } func truncateParentRecurrence(ctx context.Context, svc *calendar.Service, calendarID, eventID string, parentRecurrence []string, originalStartTime, sendUpdates string) error { diff --git a/internal/cmd/calendar_edit_scope_test.go b/internal/cmd/calendar_edit_scope_test.go index a0b3ca37d..08b176bed 100644 --- a/internal/cmd/calendar_edit_scope_test.go +++ b/internal/cmd/calendar_edit_scope_test.go @@ -63,7 +63,8 @@ func TestApplyUpdateScopeFuture_UsesParentRecurrence(t *testing.T) { } patch := &calendar.Event{Summary: "updated"} - targetID, parentRecurrence, err := applyUpdateScope(context.Background(), svc, "cal", "ev", scopeFuture, originalStart, patch) + scoped, err := applyUpdateScope(context.Background(), svc, "cal", "ev", scopeFuture, originalStart, patch) + targetID, parentRecurrence := scoped.TargetEventID, scoped.ParentRecurrence if err != nil { t.Fatalf("applyUpdateScope: %v", err) } @@ -109,7 +110,7 @@ func TestApplyUpdateScopeFuture_NonRecurring(t *testing.T) { } patch := &calendar.Event{Summary: "updated"} - if _, _, err := applyUpdateScope(context.Background(), svc, "cal", "ev", scopeFuture, "2025-01-02T10:00:00Z", patch); err == nil { + if _, err := applyUpdateScope(context.Background(), svc, "cal", "ev", scopeFuture, "2025-01-02T10:00:00Z", patch); err == nil { t.Fatalf("expected error for non-recurring event") } } @@ -160,7 +161,11 @@ func TestApplyUpdateScopeFuture_RecurringInstanceID(t *testing.T) { } patch := &calendar.Event{Summary: "updated"} - targetID, parentRecurrence, err := applyUpdateScope(context.Background(), svc, "cal", "ev_instance", scopeFuture, originalStart, patch) + scoped, err := applyUpdateScope(context.Background(), svc, "cal", "ev_instance", scopeFuture, originalStart, patch) + targetID, parentRecurrence := scoped.TargetEventID, scoped.ParentRecurrence + if scoped.ParentEventID != "ev_master" { + t.Fatalf("ParentEventID = %q, want ev_master", scoped.ParentEventID) + } if err != nil { t.Fatalf("applyUpdateScope: %v", err) } diff --git a/internal/cmd/calendar_events_paging.go b/internal/cmd/calendar_events_paging.go new file mode 100644 index 000000000..2ec5a8201 --- /dev/null +++ b/internal/cmd/calendar_events_paging.go @@ -0,0 +1,39 @@ +package cmd + +import ( + "context" + "strings" + + "google.golang.org/api/calendar/v3" +) + +// listCalendarEventsUpTo reads events.list pages until it has limit events or +// the listing ends. events.list may return fewer events than maxResults and a +// nextPageToken even below its 2500-event page cap, so one call is not +// enough. more reports that the listing had events beyond limit. +func listCalendarEventsUpTo(ctx context.Context, newCall func() *calendar.EventsListCall, limit int64) (events []*calendar.Event, more bool, err error) { + var guard pageTokenGuard + pageToken := "" + for { + if err := guard.check(pageToken); err != nil { + return nil, false, err + } + call := newCall().MaxResults(min(limit-int64(len(events)), 2500)).Context(ctx) + if pageToken != "" { + call = call.PageToken(pageToken) + } + resp, err := call.Do() + if err != nil { + return nil, false, err + } + events = append(events, resp.Items...) + next := strings.TrimSpace(resp.NextPageToken) + if int64(len(events)) >= limit { + return events[:limit], next != "" || int64(len(events)) > limit, nil + } + if next == "" { + return events, false, nil + } + pageToken = next + } +} diff --git a/internal/cmd/calendar_events_paging_test.go b/internal/cmd/calendar_events_paging_test.go new file mode 100644 index 000000000..cd06b2644 --- /dev/null +++ b/internal/cmd/calendar_events_paging_test.go @@ -0,0 +1,139 @@ +package cmd + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "strings" + "sync/atomic" + "testing" + "time" + + "google.golang.org/api/calendar/v3" + "google.golang.org/api/option" + + "github.com/openclaw/gogcli/internal/outfmt" + "github.com/openclaw/gogcli/internal/ui" +) + +// twoPageEventsHandler serves events.list like the real API, which may return +// fewer events than maxResults and a nextPageToken: event1 on the first page, +// event2 on page "p2". +func twoPageEventsHandler(calls *atomic.Int32) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if !strings.HasSuffix(r.URL.Path, "/events") { + http.NotFound(w, r) + return + } + calls.Add(1) + ev := func(id, start string) map[string]any { + return map[string]any{ + "id": id, "iCalUID": id + "@example.com", "summary": "standup " + id, "status": "confirmed", + "start": map[string]any{"dateTime": start}, + "end": map[string]any{"dateTime": strings.Replace(start, ":00:00Z", ":15:00Z", 1)}, + } + } + w.Header().Set("Content-Type", "application/json") + if r.URL.Query().Get("pageToken") == "" { + _ = json.NewEncoder(w).Encode(map[string]any{"items": []any{ev("event1", "2026-11-10T10:00:00Z")}, "nextPageToken": "p2"}) + return + } + _ = json.NewEncoder(w).Encode(map[string]any{"items": []any{ev("event2", "2026-11-11T10:00:00Z")}}) + } +} + +func TestCalendarSearch_ReadsLaterPagesUpToMax(t *testing.T) { + var calls atomic.Int32 + svc, closeServer := newGoogleTestService(t, withPrimaryCalendar(twoPageEventsHandler(&calls)), calendar.NewService) + t.Cleanup(closeServer) + for _, mode := range []string{"--json", "--plain"} { + calls.Store(0) + res := executeWithCalendarTestService(t, []string{mode, "--account", "a@b.com", "calendar", "search", "standup", "--max", "50"}, svc) + if res.err != nil { + t.Fatalf("%s: %v", mode, res.err) + } + if !strings.Contains(res.stdout, "event1") || !strings.Contains(res.stdout, "event2") { + t.Fatalf("%s: want event1 and event2, stdout=%q", mode, res.stdout) + } + if strings.Contains(res.stderr, "--max") { + t.Fatalf("%s: no more results remain, stderr=%q", mode, res.stderr) + } + } +} + +func TestCalendarSearch_StopsAtMaxAndSaysMoreRemain(t *testing.T) { + var calls atomic.Int32 + svc, closeServer := newGoogleTestService(t, withPrimaryCalendar(twoPageEventsHandler(&calls)), calendar.NewService) + t.Cleanup(closeServer) + res := executeWithCalendarTestService(t, []string{"--json", "--account", "a@b.com", "calendar", "search", "standup", "--max", "1"}, svc) + if res.err != nil { + t.Fatalf("search: %v", res.err) + } + if !strings.Contains(res.stdout, "event1") || strings.Contains(res.stdout, "event2") { + t.Fatalf("want only event1, stdout=%q", res.stdout) + } + if calls.Load() != 1 { + t.Fatalf("calls = %d, want 1 (stop once --max is reached)", calls.Load()) + } + if !strings.Contains(res.stderr, "--max") { + t.Fatalf("want a hint that more results remain, stderr=%q", res.stderr) + } +} + +func TestCalendarSearch_ContinuesAfterEmptyPageAndCapsRequestSize(t *testing.T) { + var calls int + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if !strings.HasSuffix(r.URL.Path, "/events") { + _, _ = w.Write([]byte(`{"items":[]}`)) + return + } + calls++ + if got := r.URL.Query().Get("maxResults"); got != "2500" { + t.Errorf("maxResults = %q, want API page limit 2500", got) + } + if r.URL.Query().Get("pageToken") == "" { + _, _ = w.Write([]byte(`{"nextPageToken":"p2"}`)) + return + } + _, _ = w.Write([]byte(`{"items":[{"id":"later","summary":"standup"}]}`)) + }) + svc, closeServer := newGoogleTestService(t, withPrimaryCalendar(handler), calendar.NewService) + t.Cleanup(closeServer) + result := executeWithCalendarTestService(t, []string{"--json", "--account", "a@b.com", "calendar", "search", "standup", "--max", "3000"}, svc) + if result.err != nil || calls != 2 || !strings.Contains(result.stdout, "later") { + t.Fatalf("search: calls=%d, err=%v, stdout=%s", calls, result.err, result.stdout) + } +} + +func TestCalendarTeamEvents_ReadsLaterPagesPerCalendar(t *testing.T) { + var calls atomic.Int32 + srv := httptest.NewServer(twoPageEventsHandler(&calls)) + t.Cleanup(srv.Close) + svc, err := calendar.NewService(context.Background(), + option.WithoutAuthentication(), option.WithHTTPClient(srv.Client()), option.WithEndpoint(srv.URL+"/")) + if err != nil { + t.Fatalf("NewService: %v", err) + } + tr := &TimeRange{ + From: time.Date(2026, 11, 9, 0, 0, 0, 0, time.UTC), + To: time.Date(2026, 11, 13, 0, 0, 0, 0, time.UTC), + Location: time.UTC, + } + cmd := &CalendarTeamCmd{GroupEmail: "group@example.com", Max: 100} + u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) + if err != nil { + t.Fatalf("ui.New: %v", err) + } + ctx := outfmt.WithMode(ui.WithUI(context.Background(), u), outfmt.Mode{JSON: true}) + out := captureStdout(t, func() { + if err := cmd.runEvents(ctx, svc, u, []string{"a@example.com"}, tr); err != nil { + t.Fatalf("runEvents: %v", err) + } + }) + if !strings.Contains(out, "standup event1") || !strings.Contains(out, "standup event2") { + t.Fatalf("want both pages' events, got %q", out) + } +} diff --git a/internal/cmd/calendar_freebusy.go b/internal/cmd/calendar_freebusy.go index ca3429d05..106acea15 100644 --- a/internal/cmd/calendar_freebusy.go +++ b/internal/cmd/calendar_freebusy.go @@ -67,11 +67,53 @@ 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) { if isRFC3339Instant(c.From) && isRFC3339Instant(c.To) { + from, _ := time.Parse(time.RFC3339Nano, strings.TrimSpace(c.From)) + to, _ := time.Parse(time.RFC3339Nano, strings.TrimSpace(c.To)) + if !from.Before(to) { + return "", "", usagef("--from (%s) must be before --to (%s)", c.From, c.To) + } return c.From, c.To, nil } diff --git a/internal/cmd/calendar_freebusy_errors_test.go b/internal/cmd/calendar_freebusy_errors_test.go new file mode 100644 index 000000000..05d82cdaa --- /dev/null +++ b/internal/cmd/calendar_freebusy_errors_test.go @@ -0,0 +1,87 @@ +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) + } +} diff --git a/internal/cmd/calendar_input_validation_test.go b/internal/cmd/calendar_input_validation_test.go new file mode 100644 index 000000000..b3f366db8 --- /dev/null +++ b/internal/cmd/calendar_input_validation_test.go @@ -0,0 +1,80 @@ +package cmd + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestCalendarUpdateRejectsMalformedAttendeeClear(t *testing.T) { + for _, value := range []string{";optional", ";resource", ",,", ";optional,;resource"} { + t.Run(value, func(t *testing.T) { + patch, _, err := buildCalendarUpdatePatch(calendarUpdateInput{Attendees: value}, calendarUpdateFields{Attendees: true}) + if err == nil { + b, _ := json.Marshal(patch) + t.Fatalf("malformed attendees accepted: %s", b) + } + }) + } + for _, value := range []string{"", " "} { + patch, changed, err := buildCalendarUpdatePatch(calendarUpdateInput{Attendees: value}, calendarUpdateFields{Attendees: true}) + if err != nil || !changed { + t.Fatalf("explicit clear: changed=%v err=%v", changed, err) + } + b, _ := json.Marshal(patch) + if string(b) != `{"attendees":[]}` { + t.Fatalf("clear patch=%s", b) + } + } +} + +func TestCalendarFreeBusyRejectsInvalidInstantWindow(t *testing.T) { + for _, to := range []string{"2027-01-01T00:00:00Z", "2027-01-02T01:00:00+01:00"} { + c := CalendarFreeBusyCmd{From: "2027-01-02T00:00:00Z", To: to} + if _, _, err := c.resolveFreeBusyRange(context.Background(), nil); err == nil { + t.Fatalf("accepted invalid window %s to %s", c.From, c.To) + } + } + c := CalendarFreeBusyCmd{From: "2027-01-02T00:00:00.123Z", To: "2027-01-02T01:00:00+00:00"} + from, to, err := c.resolveFreeBusyRange(context.Background(), nil) + if err != nil || from != c.From || to != c.To { + t.Fatalf("valid instants changed: %q %q %v", from, to, err) + } +} + +func TestCalendarFreeBusyResolvesNameContainingAt(t *testing.T) { + var ids []string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if strings.HasSuffix(r.URL.Path, "/calendarList") { + _, _ = io.WriteString(w, `{"items":[{"id":"work@group.calendar.google.com","summary":"Work @ Home"}]}`) + return + } + if strings.HasSuffix(r.URL.Path, "/freeBusy") { + var req struct { + Items []struct { + ID string `json:"id"` + } `json:"items"` + } + _ = json.NewDecoder(r.Body).Decode(&req) + for _, item := range req.Items { + ids = append(ids, item.ID) + } + _, _ = io.WriteString(w, `{"calendars":{}}`) + return + } + http.NotFound(w, r) + })) + defer srv.Close() + res := executeWithCalendarTestService(t, []string{"--home=" + t.TempDir(), "--account=synthetic@example.invalid", "--json", "calendar", "freebusy", "--cal=Work @ Home", "--from=2027-01-01T00:00:00Z", "--to=2027-01-02T00:00:00Z"}, newCalendarServiceFromServer(t, srv)) + if res.err != nil { + t.Fatal(res.err) + } + if len(ids) != 1 || ids[0] != "work@group.calendar.google.com" { + t.Fatalf("calendar IDs=%v", ids) + } +} 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, diff --git a/internal/cmd/calendar_search.go b/internal/cmd/calendar_search.go index 22d37d15f..baa6ed0f1 100644 --- a/internal/cmd/calendar_search.go +++ b/internal/cmd/calendar_search.go @@ -7,6 +7,8 @@ import ( "text/tabwriter" "time" + "google.golang.org/api/calendar/v3" + "github.com/openclaw/gogcli/internal/outfmt" "github.com/openclaw/gogcli/internal/ui" ) @@ -51,35 +53,37 @@ func (c *CalendarSearchCmd) Run(ctx context.Context, flags *RootFlags) error { } from, to := timeRange.FormatRFC3339() - call := svc.Events.List(calendarID). - Q(query). - TimeMin(from). - TimeMax(to). - MaxResults(c.Max). - SingleEvents(true). - OrderBy("startTime") - - resp, err := call.Do() + items, more, err := listCalendarEventsUpTo(ctx, func() *calendar.EventsListCall { + return svc.Events.List(calendarID). + Q(query). + TimeMin(from). + TimeMax(to). + SingleEvents(true). + OrderBy("startTime") + }, c.Max) if err != nil { return err } - redactCalendarEventsForOutput(ctx, resp.Items) + redactCalendarEventsForOutput(ctx, items) + if more { + u.Err().Linef("# More results: raise --max (currently %d) to see more matching events", c.Max) + } if outfmt.IsJSON(ctx) { return outfmt.WriteJSON(ctx, stdoutWriter(ctx), map[string]any{ - "events": wrapEventsWithDays(resp.Items), + "events": wrapEventsWithDays(items), "query": query, }) } - if len(resp.Items) == 0 { + if len(items) == 0 { u.Err().Println("No events found") return nil } tw := tabwriter.NewWriter(stdoutWriter(ctx), 0, 4, 2, ' ', 0) fmt.Fprintln(tw, "ID\tSTART\tEND\tSUMMARY") - for _, e := range resp.Items { + for _, e := range items { fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", e.Id, eventStart(e), eventEnd(e), e.Summary) } _ = tw.Flush() diff --git a/internal/cmd/calendar_team.go b/internal/cmd/calendar_team.go index f6a55661c..fd357fb8a 100644 --- a/internal/cmd/calendar_team.go +++ b/internal/cmd/calendar_team.go @@ -164,15 +164,13 @@ func (c *CalendarTeamCmd) runEvents(ctx context.Context, svc *calendar.Service, sem <- struct{}{} defer func() { <-sem }() - call := svc.Events.List(email). - SingleEvents(true). - TimeMin(from). - TimeMax(to). - MaxResults(c.Max). - OrderBy("startTime"). - Context(ctx) - - resp, err := call.Do() + items, _, err := listCalendarEventsUpTo(ctx, func() *calendar.EventsListCall { + return svc.Events.List(email). + SingleEvents(true). + TimeMin(from). + TimeMax(to). + OrderBy("startTime") + }, c.Max) if err != nil { mu.Lock() errors = append(errors, fmt.Sprintf("%s: %v", email, errfmt.FormatDiagnostic(err))) @@ -180,7 +178,7 @@ func (c *CalendarTeamCmd) runEvents(ctx context.Context, svc *calendar.Service, return } - for _, ev := range resp.Items { + for _, ev := range items { if ev == nil { continue } diff --git a/internal/cmd/calendar_update_future_instance_test.go b/internal/cmd/calendar_update_future_instance_test.go new file mode 100644 index 000000000..64b0ac0fb --- /dev/null +++ b/internal/cmd/calendar_update_future_instance_test.go @@ -0,0 +1,90 @@ +package cmd + +import ( + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +type recordedPatch struct { + eventID string + recurrence []string +} + +// runCalendarUpdateFuture serves a daily series "ser1" whose 2025-01-02 instance +// is "ser1_20250102T100000Z", runs calendar update --scope future with eventID, +// and records every PATCH. +func runCalendarUpdateFuture(t *testing.T, eventID string) []recordedPatch { + t.Helper() + const instanceID = "ser1_20250102T100000Z" + var patches []recordedPatch + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := strings.TrimPrefix(r.URL.Path, "/calendar/v3") + w.Header().Set("Content-Type", "application/json") + switch { + case r.Method == http.MethodGet && path == "/calendars/cal/events/"+instanceID: + _, _ = io.WriteString(w, `{"id":"`+instanceID+`","recurringEventId":"ser1"}`) + case r.Method == http.MethodGet && path == "/calendars/cal/events/ser1": + _, _ = io.WriteString(w, `{"id":"ser1","recurrence":["RRULE:FREQ=DAILY"],"start":{"dateTime":"2025-01-01T10:00:00Z","timeZone":"UTC"},"end":{"dateTime":"2025-01-01T10:30:00Z","timeZone":"UTC"}}`) + case r.Method == http.MethodGet && path == "/calendars/cal/events/ser1/instances": + _, _ = io.WriteString(w, `{"items":[{"id":"`+instanceID+`","originalStartTime":{"dateTime":"2025-01-02T10:00:00Z"}}]}`) + case r.Method == http.MethodPatch && strings.HasPrefix(path, "/calendars/cal/events/"): + var body struct { + Recurrence []string `json:"recurrence"` + } + _ = json.NewDecoder(r.Body).Decode(&body) + id := strings.TrimPrefix(path, "/calendars/cal/events/") + patches = append(patches, recordedPatch{eventID: id, recurrence: body.Recurrence}) + _, _ = io.WriteString(w, `{"id":"`+id+`","summary":"Renamed"}`) + case r.Method == http.MethodGet && path == "/users/me/calendarList": + _, _ = io.WriteString(w, `{"items":[{"id":"cal","summary":"cal","timeZone":"UTC"}]}`) + case r.Method == http.MethodGet && path == "/calendars/cal": + _, _ = io.WriteString(w, `{"id":"cal","timeZone":"UTC"}`) + default: + t.Logf("unexpected %s %s", r.Method, r.URL.String()) + http.NotFound(w, r) + } + })) + t.Cleanup(srv.Close) + + ctx := withCalendarTestService(newCmdRuntimeJSONOutputContext(t, io.Discard, io.Discard), newCalendarServiceFromServer(t, srv)) + if err := runKong(t, &CalendarUpdateCmd{}, []string{ + "cal", eventID, + "--scope", "future", + "--original-start", "2025-01-02T10:00:00Z", + "--summary", "Renamed", + }, ctx, &RootFlags{Account: "a@b.com"}); err != nil { + t.Fatalf("update %s: %v", eventID, err) + } + return patches +} + +func assertFutureScopePatches(t *testing.T, patches []recordedPatch) { + t.Helper() + if len(patches) != 2 { + t.Fatalf("PATCHes = %#v, want instance then series", patches) + } + if patches[0].eventID != "ser1_20250102T100000Z" { + t.Fatalf("first PATCH went to %q, want the instance", patches[0].eventID) + } + if patches[1].eventID != "ser1" { + t.Fatalf("series truncation PATCH went to %q, want ser1", patches[1].eventID) + } + if len(patches[1].recurrence) != 1 || !strings.Contains(patches[1].recurrence[0], "UNTIL=20250102T095959Z") { + t.Fatalf("series recurrence = %q, want UNTIL just before the instance", patches[1].recurrence) + } +} + +func TestCalendarUpdateCmd_ScopeFutureFromSeriesID(t *testing.T) { + assertFutureScopePatches(t, runCalendarUpdateFuture(t, "ser1")) +} + +// calendar events and calendar search list expanded instances, so the ID a +// user has is usually the instance ID. --scope future must still end the old +// series, as calendar delete --scope future already does. +func TestCalendarUpdateCmd_ScopeFutureFromInstanceID(t *testing.T) { + assertFutureScopePatches(t, runCalendarUpdateFuture(t, "ser1_20250102T100000Z")) +} diff --git a/internal/cmd/calendar_update_patch_plan.go b/internal/cmd/calendar_update_patch_plan.go index df7c9b961..7262876a9 100644 --- a/internal/cmd/calendar_update_patch_plan.go +++ b/internal/cmd/calendar_update_patch_plan.go @@ -27,7 +27,11 @@ func buildCalendarUpdatePatch(input calendarUpdateInput, fields calendarUpdateFi changed = true } - if applyUpdateAttendees(input, fields, patch) { + attendeesChanged, err := applyUpdateAttendees(input, fields, patch) + if err != nil { + return nil, false, err + } + if attendeesChanged { changed = true } if applyUpdateAttachments(input, fields, patch) { @@ -171,16 +175,19 @@ func applyUpdateTimeFields(input calendarUpdateInput, fields calendarUpdateField return changed, nil } -func applyUpdateAttendees(input calendarUpdateInput, fields calendarUpdateFields, patch *calendar.Event) bool { +func applyUpdateAttendees(input calendarUpdateInput, fields calendarUpdateFields, patch *calendar.Event) (bool, error) { if !fields.Attendees { - return false + return false, nil } patch.Attendees = buildAttendees(input.Attendees) if len(patch.Attendees) == 0 { + if strings.TrimSpace(input.Attendees) != "" { + return false, usage("--attendees must contain an email address; use an empty value to clear attendees") + } patch.Attendees = []*calendar.EventAttendee{} patch.ForceSendFields = appendForceSendField(patch.ForceSendFields, "Attendees") } - return true + return true, nil } func applyUpdateAttachments(input calendarUpdateInput, fields calendarUpdateFields, patch *calendar.Event) bool { diff --git a/internal/cmd/calendar_util.go b/internal/cmd/calendar_util.go index d1a640d46..5ff75df25 100644 --- a/internal/cmd/calendar_util.go +++ b/internal/cmd/calendar_util.go @@ -3,6 +3,7 @@ package cmd import ( "context" "errors" + "net/mail" "strings" "google.golang.org/api/calendar/v3" @@ -114,7 +115,8 @@ func resolveSelectedCalendarIDs(ctx context.Context, store *config.ConfigStore, if input == "" { continue } - if strings.EqualFold(input, primaryCalendarID) || strings.Contains(input, "@") { + address, parseErr := mail.ParseAddress(input) + if strings.EqualFold(input, primaryCalendarID) || (parseErr == nil && address.Address == input) { continue } if isDigits(input) { diff --git a/internal/cmd/comment_ops.go b/internal/cmd/comment_ops.go index a882d86fc..c3d062976 100644 --- a/internal/cmd/comment_ops.go +++ b/internal/cmd/comment_ops.go @@ -119,6 +119,14 @@ type driveCommentListOptions struct { } func listDriveComments(ctx context.Context, svc *drive.Service, fileID string, opts driveCommentListOptions) ([]*drive.Comment, string, error) { + comments, nextPageToken, err := listDriveCommentPages(ctx, svc, fileID, opts) + if err != nil { + return nil, "", err + } + return nonNilSlice(comments), nextPageToken, nil +} + +func listDriveCommentPages(ctx context.Context, svc *drive.Service, fileID string, opts driveCommentListOptions) ([]*drive.Comment, string, error) { fetch := func(pageToken string) ([]*drive.Comment, string, error) { return fetchDriveCommentsPage(ctx, svc, fileID, opts.max, pageToken, opts.since, driveCommentFieldsForList(opts)) } diff --git a/internal/cmd/contacts.go b/internal/cmd/contacts.go index 034003a7f..ec52847a7 100644 --- a/internal/cmd/contacts.go +++ b/internal/cmd/contacts.go @@ -28,7 +28,7 @@ type ContactsCmd struct { type ContactsSearchCmd struct { Query []string `arg:"" name:"query" help:"Search query"` - Max int64 `name:"max" aliases:"limit" help:"Max results" default:"50"` + Max int64 `name:"max" aliases:"limit" help:"Max results (Google returns at most 30)" default:"30"` } func (c *ContactsSearchCmd) Run(ctx context.Context, flags *RootFlags) error { @@ -50,7 +50,7 @@ func (c *ContactsSearchCmd) Run(ctx context.Context, flags *RootFlags) error { warmSearchContactsCache(ctx, svc) resp, err := svc.People.SearchContacts(). Query(query). - PageSize(c.Max). + PageSize(peopleSearchPageSize(u, c.Max)). ReadMask(contactsReadMask). Do() if err != nil { @@ -267,3 +267,18 @@ func userDefinedFields(p *people.Person) map[string]string { } return fields } + +// peopleSearchMaxPageSize is the most results people.searchContacts and +// otherContacts.search return: larger pageSize values are capped to 30 and +// neither method returns a page token. +const peopleSearchMaxPageSize = 30 + +func peopleSearchPageSize(u *ui.UI, requested int64) int64 { + if requested <= peopleSearchMaxPageSize { + return requested + } + if u != nil { + u.Err().Linef("# Google returns at most %d contact search results; showing up to %d of the requested %d", peopleSearchMaxPageSize, peopleSearchMaxPageSize, requested) + } + return peopleSearchMaxPageSize +} diff --git a/internal/cmd/contacts_directory.go b/internal/cmd/contacts_directory.go index 9fb671810..ae4d785ab 100644 --- a/internal/cmd/contacts_directory.go +++ b/internal/cmd/contacts_directory.go @@ -305,7 +305,7 @@ func (c *ContactsOtherListCmd) Run(ctx context.Context, flags *RootFlags) error type ContactsOtherSearchCmd struct { Query []string `arg:"" name:"query" help:"Search query"` - Max int64 `name:"max" aliases:"limit" help:"Max results" default:"50"` + Max int64 `name:"max" aliases:"limit" help:"Max results (Google returns at most 30)" default:"30"` } func (c *ContactsOtherSearchCmd) Run(ctx context.Context, flags *RootFlags) error { @@ -328,7 +328,7 @@ func (c *ContactsOtherSearchCmd) Run(ctx context.Context, flags *RootFlags) erro resp, err := svc.OtherContacts.Search(). Query(query). ReadMask(contactsOtherReadMask). - PageSize(c.Max). + PageSize(peopleSearchPageSize(u, c.Max)). Do() if err != nil { return err diff --git a/internal/cmd/contacts_search_page_cap_test.go b/internal/cmd/contacts_search_page_cap_test.go new file mode 100644 index 000000000..76cb3e6f8 --- /dev/null +++ b/internal/cmd/contacts_search_page_cap_test.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "net/http" + "strconv" + "strings" + "sync/atomic" + "testing" +) + +// Both people.searchContacts and otherContacts.search cap pageSize at 30 and +// return no page token, so a larger --max can never be met. +func capped30SearchHandler(endpoint string, sent *atomic.Value) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if !strings.Contains(r.URL.Path, endpoint) || r.URL.Query().Get("query") == "" { + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{"results": []any{}}) + return + } + sent.Store(r.URL.Query().Get("pageSize")) + n, _ := strconv.Atoi(r.URL.Query().Get("pageSize")) + if n > 30 { + n = 30 + } + results := make([]map[string]any, 0, n) + for i := range n { + results = append(results, map[string]any{"person": map[string]any{ + "resourceName": fmt.Sprintf("people/c%d", i), + "names": []map[string]any{{"displayName": fmt.Sprintf("Ada %d", i)}}, + }}) + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{"results": results}) + } +} + +func TestContactsSearch_RespectsThe30ResultCap(t *testing.T) { + for _, tc := range []struct { + name string + verb []string + endpoint string + }{ + {name: "contacts search", verb: []string{"contacts", "search", "ada"}, endpoint: "people:searchContacts"}, + {name: "contacts other search", verb: []string{"contacts", "other", "search", "ada"}, endpoint: "otherContacts:search"}, + } { + for _, flags := range []struct { + args []string + wantWarn bool + }{ + {args: nil, wantWarn: false}, + {args: []string{"--max", "50"}, wantWarn: true}, + } { + t.Run(fmt.Sprintf("%s %v", tc.name, flags.args), func(t *testing.T) { + var sent atomic.Value + svc, closeSrv := newPeopleService(t, capped30SearchHandler(tc.endpoint, &sent)) + t.Cleanup(closeSrv) + args := append(append([]string{"--json", "--account", "a@b.com"}, tc.verb...), flags.args...) + res := executeWithPeopleTestServices(t, args, peopleTestServices{ + Contacts: fixedPeopleTestService(svc), + Other: fixedPeopleTestService(svc), + }) + if res.err != nil { + t.Fatalf("Execute: %v", res.err) + } + if got := sent.Load(); got != "30" { + t.Fatalf("pageSize sent = %v, want 30", got) + } + warned := strings.Contains(res.stderr, "30") + if warned != flags.wantWarn { + t.Fatalf("stderr = %q, want warning about the 30-result cap: %v", res.stderr, flags.wantWarn) + } + }) + } + } +} diff --git a/internal/cmd/drive_download.go b/internal/cmd/drive_download.go index ea15f6500..1b9f33a8d 100644 --- a/internal/cmd/drive_download.go +++ b/internal/cmd/drive_download.go @@ -92,12 +92,18 @@ func (c *DriveDownloadCmd) Run(ctx context.Context, flags *RootFlags) error { meta, err := svc.Files.Get(fileID). SupportsAllDrives(true). - Fields("id, name, mimeType"). + Fields("id, name, mimeType, shortcutDetails(targetId)"). Context(ctx). Do() if err != nil { return err } + if meta.MimeType == driveMimeShortcut { + meta, err = driveShortcutTarget(ctx, svc, meta) + if err != nil { + return err + } + } if meta.Name == "" { return errors.New("file has no name") } @@ -130,6 +136,25 @@ func (c *DriveDownloadCmd) Run(ctx context.Context, flags *RootFlags) error { return nil } +// driveShortcutTarget returns the metadata of the file a shortcut points to. +// A shortcut has no content of its own, and Drive exports only Docs Editors +// files, so downloading the shortcut itself cannot succeed. +func driveShortcutTarget(ctx context.Context, svc *drive.Service, shortcut *drive.File) (*drive.File, error) { + if shortcut.ShortcutDetails == nil || strings.TrimSpace(shortcut.ShortcutDetails.TargetId) == "" { + return nil, fmt.Errorf("shortcut %s has no target", shortcut.Id) + } + targetID := strings.TrimSpace(shortcut.ShortcutDetails.TargetId) + target, err := svc.Files.Get(targetID). + SupportsAllDrives(true). + Fields("id, name, mimeType"). + Context(ctx). + Do() + if err != nil { + return nil, fmt.Errorf("shortcut %s points to %s, which could not be read: %w", shortcut.Id, targetID, err) + } + return target, nil +} + func downloadDriveFile(ctx context.Context, svc *drive.Service, meta *drive.File, destPath string, format string, overwrite bool) (string, int64, error) { isGoogleDoc := strings.HasPrefix(meta.MimeType, "application/vnd.google-apps.") normalizedFormat := strings.ToLower(strings.TrimSpace(format)) diff --git a/internal/cmd/drive_download_shortcut_test.go b/internal/cmd/drive_download_shortcut_test.go new file mode 100644 index 000000000..cd8a3f628 --- /dev/null +++ b/internal/cmd/drive_download_shortcut_test.go @@ -0,0 +1,112 @@ +package cmd + +import ( + "context" + "encoding/json" + "errors" + "io" + "net/http" + "os" + "path/filepath" + "strings" + "testing" + + "google.golang.org/api/drive/v3" +) + +type driveShortcutDownloadCalls struct { + downloads []string + exports []string +} + +// runDriveShortcutDownload serves metadata for shortcut sc1, which points at +// target, and records which file IDs gog downloads or exports. +func runDriveShortcutDownload(t *testing.T, target map[string]any, args ...string) (driveShortcutDownloadCalls, string, error) { + t.Helper() + var calls driveShortcutDownloadCalls + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + switch { + case strings.HasSuffix(r.URL.Path, "/files/sc1"): + _ = json.NewEncoder(w).Encode(map[string]any{ + "id": "sc1", + "name": "Shortcut to report", + "mimeType": driveMimeShortcut, + "shortcutDetails": map[string]any{ + "targetId": "t1", + "targetMimeType": target["mimeType"], + }, + }) + case strings.HasSuffix(r.URL.Path, "/files/t1") && target != nil: + _ = json.NewEncoder(w).Encode(target) + default: + w.WriteHeader(http.StatusNotFound) + _ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{"code": 404, "message": "File not found: t1.", "errors": []map[string]any{{"reason": "notFound"}}}}) + } + }) + svc, cleanup := newDriveTestService(t, handler) + t.Cleanup(cleanup) + + download := func(_ context.Context, _ *drive.Service, fileID string) (*http.Response, error) { + calls.downloads = append(calls.downloads, fileID) + return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader("target-bytes"))}, nil + } + export := func(_ context.Context, _ *drive.Service, fileID, _ string) (*http.Response, error) { + calls.exports = append(calls.exports, fileID) + if fileID != "t1" { + // Drive exports only Docs Editors files; a shortcut is not one. + return &http.Response{StatusCode: http.StatusForbidden, Status: "403 Forbidden", Body: io.NopCloser(strings.NewReader(`{"error":{"code":403,"message":"Export only supports Docs Editors files."}}`))}, nil + } + return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader("exported-pdf"))}, nil + } + + dir := t.TempDir() + ctx := withDriveTestOperations(newCmdRuntimeJSONOutputContext(t, io.Discard, io.Discard), svc, download, export) + err := runKong(t, &DriveDownloadCmd{}, append([]string{"sc1", "--out", dir + string(os.PathSeparator)}, args...), ctx, &RootFlags{Account: "a@b.com"}) + return calls, dir, err +} + +func TestDriveDownloadCmd_ShortcutDownloadsTargetFile(t *testing.T) { + calls, dir, err := runDriveShortcutDownload(t, map[string]any{"id": "t1", "name": "report.pdf", "mimeType": "application/pdf"}) + if err != nil { + t.Fatalf("download: %v", err) + } + if len(calls.exports) != 0 || len(calls.downloads) != 1 || calls.downloads[0] != "t1" { + t.Fatalf("calls = %#v, want one download of target t1", calls) + } + data, readErr := os.ReadFile(filepath.Join(dir, "t1_report.pdf")) + if readErr != nil || string(data) != "target-bytes" { + t.Fatalf("t1_report.pdf = %q, %v", data, readErr) + } +} + +func TestDriveDownloadCmd_ShortcutExportsTargetDoc(t *testing.T) { + calls, dir, err := runDriveShortcutDownload(t, map[string]any{"id": "t1", "name": "Plan", "mimeType": driveMimeGoogleDoc}) + if err != nil { + t.Fatalf("download: %v", err) + } + if len(calls.downloads) != 0 || len(calls.exports) != 1 || calls.exports[0] != "t1" { + t.Fatalf("calls = %#v, want one export of target t1", calls) + } + if data, readErr := os.ReadFile(filepath.Join(dir, "t1_Plan.pdf")); readErr != nil || string(data) != "exported-pdf" { + t.Fatalf("t1_Plan.pdf = %q, %v", data, readErr) + } +} + +func TestDriveDownloadCmd_BrokenShortcutIsNotFound(t *testing.T) { + calls, _, err := runDriveShortcutDownload(t, nil) + var exitErr *ExitError + if !errors.As(stableExitCode(err), &exitErr) || exitErr.Code != 5 { + t.Fatalf("err = %v, want not_found exit 5", err) + } + if !strings.Contains(err.Error(), "shortcut") { + t.Fatalf("err = %v, want it to say the shortcut target is missing", err) + } + if len(calls.downloads) != 0 || len(calls.exports) != 0 { + t.Fatalf("calls = %#v, want no content request", calls) + } +} diff --git a/internal/cmd/drive_labels.go b/internal/cmd/drive_labels.go index cda5666f9..52515778a 100644 --- a/internal/cmd/drive_labels.go +++ b/internal/cmd/drive_labels.go @@ -74,7 +74,7 @@ func (c *DriveLabelsFileListCmd) Run(ctx context.Context, flags *RootFlags) erro } if outfmt.IsJSON(ctx) { return outfmt.WriteJSON(ctx, stdoutWriter(ctx), map[string]any{ - "labels": resp.Labels, + "labels": nonNilSlice(resp.Labels), "labelCount": len(resp.Labels), "nextPageToken": resp.NextPageToken, }) @@ -96,11 +96,11 @@ func (c *DriveLabelsFileListCmd) Run(ctx context.Context, flags *RootFlags) erro type DriveLabelsFileApplyCmd struct { FileID string `arg:"" name:"fileId" help:"Drive file ID"` LabelID string `arg:"" name:"labelId" help:"Label ID or labels/{id}"` - Text []string `name:"text" help:"Set text field as field=value (repeatable)"` - Selection []string `name:"selection" help:"Set selection field as field=choiceId[,choiceId] (repeatable)"` - Integer []string `name:"integer" help:"Set integer field as field=123[,456] (repeatable)"` - Date []string `name:"date" help:"Set date field as field=YYYY-MM-DD[,YYYY-MM-DD] (repeatable)"` - User []string `name:"user" help:"Set user field as field=email[,email] (repeatable)"` + Text []string `name:"text" sep:"none" help:"Set text field as field=value (repeatable)"` + Selection []string `name:"selection" sep:"none" help:"Set selection field as field=choiceId[,choiceId] (repeatable)"` + Integer []string `name:"integer" sep:"none" help:"Set integer field as field=123[,456] (repeatable)"` + Date []string `name:"date" sep:"none" help:"Set date field as field=YYYY-MM-DD[,YYYY-MM-DD] (repeatable)"` + User []string `name:"user" sep:"none" help:"Set user field as field=email[,email] (repeatable)"` Unset []string `name:"unset" help:"Unset field ID (repeatable)"` FieldsJSON string `name:"fields-json" help:"Simple JSON object of fieldId to string/number/bool/string-array values (strings become text fields)"` } @@ -227,7 +227,7 @@ func (c *DriveLabelsListCmd) Run(ctx context.Context, flags *RootFlags) error { if outfmt.IsJSON(ctx) { return outfmt.WriteJSON(ctx, stdoutWriter(ctx), map[string]any{ - "labels": resp.Labels, + "labels": nonNilSlice(resp.Labels), "labelCount": len(resp.Labels), "nextPageToken": resp.NextPageToken, }) @@ -339,11 +339,13 @@ func driveLabelFieldMods(c *DriveLabelsFileApplyCmd) ([]*drive.LabelFieldModific } return nil } - if err := add(c.Text, func(m *drive.LabelFieldModification, values []string) error { - m.SetTextValues = values - return nil - }); err != nil { - return nil, err + // A text field holds one value, so a comma in it is part of the text. + for _, item := range c.Text { + field, value, ok := strings.Cut(strings.TrimSpace(item), "=") + if !ok || strings.TrimSpace(field) == "" { + return nil, usage("label field assignments must be field=value") + } + mods = append(mods, &drive.LabelFieldModification{FieldId: strings.TrimSpace(field), SetTextValues: []string{strings.TrimSpace(value)}}) } if err := add(c.Selection, func(m *drive.LabelFieldModification, values []string) error { m.SetSelectionValues = values diff --git a/internal/cmd/drive_labels_comma_test.go b/internal/cmd/drive_labels_comma_test.go new file mode 100644 index 000000000..a47b2c5af --- /dev/null +++ b/internal/cmd/drive_labels_comma_test.go @@ -0,0 +1,60 @@ +package cmd + +import ( + "encoding/json" + "io" + "net/http" + "reflect" + "testing" + + "google.golang.org/api/drive/v3" +) + +// The field flags are parsed by the CLI, not built in Go: a comma in the +// flag value must reach gog's own parser, so text keeps its comma and the +// documented field=a,b forms produce multiple values. +func TestDriveLabelsFileApply_CommaValuesThroughCLI(t *testing.T) { + var got []*drive.LabelFieldModification + svc, closeSvc := newDriveTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost || r.URL.Path != "/files/file1/modifyLabels" { + http.NotFound(w, r) + return + } + var req drive.ModifyLabelsRequest + if err := json.NewDecoder(r.Body).Decode(&req); err != nil && err != io.EOF { + t.Fatalf("decode body: %v", err) + } + got = req.LabelModifications[0].FieldModifications + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{"modifiedLabels": []map[string]any{{"id": "label1"}}}) + })) + defer closeSvc() + + ctx := withDriveTestService(newCmdRuntimeJSONOutputContext(t, io.Discard, io.Discard), svc) + err := runKong(t, &DriveLabelsFileApplyCmd{}, []string{ + "file1", "label1", + "--text", "note=Hello, world", + "--selection", "status=c1,c2", + "--integer", "count=1,2", + "--user", "owners=a@example.com,b@example.com", + }, ctx, &RootFlags{Account: "a@example.com", Force: true}) + if err != nil { + t.Fatalf("apply: %v", err) + } + byField := map[string]*drive.LabelFieldModification{} + for _, mod := range got { + byField[mod.FieldId] = mod + } + if v := byField["note"]; v == nil || !reflect.DeepEqual(v.SetTextValues, []string{"Hello, world"}) { + t.Fatalf("note = %#v, want one text value with its comma", v) + } + if v := byField["status"]; v == nil || !reflect.DeepEqual(v.SetSelectionValues, []string{"c1", "c2"}) { + t.Fatalf("status = %#v, want two selection values", v) + } + if v := byField["count"]; v == nil || !reflect.DeepEqual([]int64(v.SetIntegerValues), []int64{1, 2}) { + t.Fatalf("count = %#v, want two integer values", v) + } + if v := byField["owners"]; v == nil || !reflect.DeepEqual(v.SetUserValues, []string{"a@example.com", "b@example.com"}) { + t.Fatalf("owners = %#v, want two user values", v) + } +} diff --git a/internal/cmd/empty_list_json_test.go b/internal/cmd/empty_list_json_test.go new file mode 100644 index 000000000..506fa4d43 --- /dev/null +++ b/internal/cmd/empty_list_json_test.go @@ -0,0 +1,113 @@ +package cmd + +import ( + "encoding/json" + "net/http" + "strings" + "testing" + + "google.golang.org/api/drivelabels/v2" + + "github.com/openclaw/gogcli/internal/app" +) + +// Google omits empty list fields, so each fake returns {}. +func TestEmptyListJSON(t *testing.T) { + t.Run("drive labels list", func(t *testing.T) { + svc, closeSvc := newGoogleTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet || r.URL.Path != "/v2/labels" { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{}) + }), drivelabels.NewService) + defer closeSvc() + + assertEmptyListJSON(t, "labels", func(args []string) executeTestResult { + return executeWithTestRuntime(t, args, &app.Runtime{Services: app.Services{ + DriveLabels: fixedDriveLabelsTestService(svc), + }}) + }, []string{"--account", "a@example.com", "drive", "labels", "list"}) + }) + + t.Run("drive labels file list", func(t *testing.T) { + svc, closeSvc := newDriveTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet || r.URL.Path != "/files/file1/listLabels" { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{}) + })) + defer closeSvc() + + assertEmptyListJSON(t, "labels", func(args []string) executeTestResult { + return executeWithDriveTestService(t, args, svc) + }, []string{"--account", "a@example.com", "drive", "labels", "file", "list", "file1"}) + }) + + t.Run("docs comments list", func(t *testing.T) { + svc, closeSvc := newDriveTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := strings.TrimPrefix(r.URL.Path, "/drive/v3") + if r.Method != http.MethodGet || path != "/files/empty/comments" { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{}) + })) + defer closeSvc() + + assertEmptyListJSON(t, "comments", func(args []string) executeTestResult { + return executeWithDriveTestService(t, args, svc) + }, []string{"--account", "a@b.com", "docs", "comments", "list", "empty"}) + }) + + t.Run("drive comments list", func(t *testing.T) { + svc, closeSvc := newDriveTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + path := strings.TrimPrefix(r.URL.Path, "/drive/v3") + if r.Method != http.MethodGet || path != "/files/empty/comments" { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{}) + })) + defer closeSvc() + + assertEmptyListJSON(t, "comments", func(args []string) executeTestResult { + return executeWithDriveTestService(t, args, svc) + }, []string{"--account", "a@b.com", "drive", "comments", "list", "empty"}) + }) +} + +func assertEmptyListJSON(t *testing.T, listKey string, run func([]string) executeTestResult, args []string) { + t.Helper() + + jsonArgs := append([]string{"--json"}, args...) + result := run(jsonArgs) + if result.err != nil { + t.Fatalf("Execute: %v\nstderr=%q\nstdout=%q", result.err, result.stderr, result.stdout) + } + var parsed map[string]json.RawMessage + if err := json.Unmarshal([]byte(result.stdout), &parsed); err != nil { + t.Fatalf("json parse: %v\n%s", err, result.stdout) + } + raw, ok := parsed[listKey] + if !ok { + t.Fatalf("missing %s key: %s", listKey, result.stdout) + } + if strings.TrimSpace(string(raw)) != "[]" { + t.Fatalf("%s = %s, want []\nstdout=%s", listKey, raw, result.stdout) + } + + onlyArgs := append([]string{"--json", "--results-only"}, args...) + result = run(onlyArgs) + if result.err != nil { + t.Fatalf("Execute --results-only: %v\nstderr=%q\nstdout=%q", result.err, result.stderr, result.stdout) + } + if got := strings.TrimSpace(result.stdout); got != "[]" { + t.Fatalf("--results-only = %q, want []", result.stdout) + } +} diff --git a/internal/cmd/gmail_autoreply.go b/internal/cmd/gmail_autoreply.go index e95eeeb15..5006c8535 100644 --- a/internal/cmd/gmail_autoreply.go +++ b/internal/cmd/gmail_autoreply.go @@ -164,7 +164,14 @@ func runGmailAutoReply(ctx context.Context, svc *gmail.Service, account string, Label: input.Label, } - from, err := resolveComposeSender(ctx, svc, account, input.From) + sendAs, sendAsErr := listSendAs(ctx, svc) + // Without --allow-self the alias list is what keeps replies away from the + // user's own addresses, so a failed lookup must stop the run. With + // --allow-self those replies are allowed, so keep the previous fallback. + if sendAsErr != nil && !input.AllowSelf { + return summary, fmt.Errorf("list send-as aliases to exclude your own addresses: %w", sendAsErr) + } + from, err := resolveComposeFrom(ctx, svc, account, input.From, sendAs, sendAsErr) if err != nil { return summary, err } @@ -183,10 +190,7 @@ func runGmailAutoReply(ctx context.Context, svc *gmail.Service, account string, return summary, nil } - selfAddrs := []string{account} - if from.sendingEmail != "" && !strings.EqualFold(from.sendingEmail, account) { - selfAddrs = append(selfAddrs, from.sendingEmail) - } + selfAddrs := selfEmailsForReply(account, from.sendingEmail, sendAs) for _, messageID := range messageIDs { msg, err := fetchMessageForAutoReply(ctx, svc, messageID) @@ -225,6 +229,13 @@ func runGmailAutoReply(ctx context.Context, svc *gmail.Service, account string, } replyMeta := replyInfoFromMessage(msg, false) + if !input.AllowSelf && autoReplyFromIsSelf(replyMeta, selfAddrs) { + result.Action = autoReplyActionSkipped + result.Reason = "no_reply_recipient" + summary.Results = append(summary.Results, result) + summary.Skipped++ + continue + } recipients := autoReplyRecipients(replyMeta, selfAddrs) if len(recipients) == 0 && input.AllowSelf { recipients = autoReplyRecipients(replyMeta, nil) @@ -326,6 +337,27 @@ func shouldSkipAutoReplyMessage(msg *gmail.Message) (bool, string) { return false, "" } +// autoReplyFromIsSelf reports whether From is one of the caller's own +// addresses. The comparison matches autoReplyRecipients: case-insensitive on +// the bare address. +func autoReplyFromIsSelf(info *replyInfo, self []string) bool { + if info == nil || len(self) == 0 { + return false + } + selfSet := make(map[string]struct{}, len(self)) + for _, addr := range self { + if trimmed := strings.ToLower(strings.TrimSpace(addr)); trimmed != "" { + selfSet[trimmed] = struct{}{} + } + } + for _, addr := range parseEmailAddresses(info.FromAddr) { + if _, ok := selfSet[strings.ToLower(strings.TrimSpace(addr))]; ok { + return true + } + } + return false +} + func autoReplyRecipients(info *replyInfo, self []string) []string { replyAddress := strings.TrimSpace(info.ReplyToAddr) if replyAddress == "" { diff --git a/internal/cmd/gmail_autoreply_sendas_test.go b/internal/cmd/gmail_autoreply_sendas_test.go new file mode 100644 index 000000000..cc1f26544 --- /dev/null +++ b/internal/cmd/gmail_autoreply_sendas_test.go @@ -0,0 +1,254 @@ +package cmd + +import ( + "encoding/json" + "errors" + "net/http" + "strings" + "testing" + + googleapi "google.golang.org/api/googleapi" +) + +func TestExecuteGmailAutoReplySendAsAlias(t *testing.T) { + t.Run("alias", func(t *testing.T) { + sendCalls, summary, err := executeGmailAutoReplyFrom(t, "alias@example.com", "", false, 0) + if err != nil { + t.Fatalf("Execute: %v", err) + } + if sendCalls != 0 { + t.Fatalf("expected no messages.send calls, got %d", sendCalls) + } + if summary.Replied != 0 || summary.Skipped != 1 || len(summary.Results) != 1 { + t.Fatalf("unexpected summary: %#v", summary) + } + got := summary.Results[0] + if got.Action != autoReplyActionSkipped || got.Reason != "no_reply_recipient" { + t.Fatalf("unexpected result: %#v", got) + } + }) + + t.Run("external", func(t *testing.T) { + sendCalls, summary, err := executeGmailAutoReplyFrom(t, "alice@example.com", "", false, 0) + if err != nil { + t.Fatalf("Execute: %v", err) + } + if sendCalls != 1 { + t.Fatalf("expected one messages.send call, got %d", sendCalls) + } + if summary.Replied != 1 || summary.Skipped != 0 || len(summary.Results) != 1 { + t.Fatalf("unexpected summary: %#v", summary) + } + got := summary.Results[0] + if got.Action != "replied" || got.ReplyTo != "alice@example.com" { + t.Fatalf("unexpected result: %#v", got) + } + }) + + t.Run("display name", func(t *testing.T) { + sendCalls, summary, err := executeGmailAutoReplyFrom(t, `"Alias" `, "", false, 0) + if err != nil { + t.Fatalf("Execute: %v", err) + } + if sendCalls != 0 { + t.Fatalf("expected no messages.send calls, got %d", sendCalls) + } + if summary.Replied != 0 || summary.Skipped != 1 || len(summary.Results) != 1 { + t.Fatalf("unexpected summary: %#v", summary) + } + got := summary.Results[0] + if got.Action != autoReplyActionSkipped || got.Reason != "no_reply_recipient" { + t.Fatalf("unexpected result: %#v", got) + } + }) + + t.Run("reply-to", func(t *testing.T) { + sendCalls, summary, err := executeGmailAutoReplyFrom(t, "alias@example.com", "victim@external.com", false, 0) + if err != nil { + t.Fatalf("Execute: %v", err) + } + if sendCalls != 0 { + t.Fatalf("expected no messages.send calls, got %d", sendCalls) + } + if summary.Replied != 0 || summary.Skipped != 1 || len(summary.Results) != 1 { + t.Fatalf("unexpected summary: %#v", summary) + } + got := summary.Results[0] + if got.Action != autoReplyActionSkipped || got.Reason != "no_reply_recipient" { + t.Fatalf("unexpected result: %#v", got) + } + }) + + t.Run("reply-to allow-self", func(t *testing.T) { + sendCalls, summary, err := executeGmailAutoReplyFrom(t, "alias@example.com", "victim@external.com", true, 0) + if err != nil { + t.Fatalf("Execute: %v", err) + } + if sendCalls != 1 { + t.Fatalf("expected one messages.send call, got %d", sendCalls) + } + if summary.Replied != 1 || summary.Skipped != 0 || len(summary.Results) != 1 { + t.Fatalf("unexpected summary: %#v", summary) + } + got := summary.Results[0] + if got.Action != "replied" || got.ReplyTo != "victim@external.com" { + t.Fatalf("unexpected result: %#v", got) + } + }) + + t.Run("allow-self", func(t *testing.T) { + sendCalls, summary, err := executeGmailAutoReplyFrom(t, "alias@example.com", "", true, 0) + if err != nil { + t.Fatalf("Execute: %v", err) + } + if sendCalls != 1 { + t.Fatalf("expected one messages.send call, got %d", sendCalls) + } + if summary.Replied != 1 || summary.Skipped != 0 || len(summary.Results) != 1 { + t.Fatalf("unexpected summary: %#v", summary) + } + got := summary.Results[0] + if got.Action != "replied" || got.ReplyTo != "alias@example.com" { + t.Fatalf("unexpected result: %#v", got) + } + }) + + t.Run("send-as forbidden", func(t *testing.T) { + sendCalls, _, err := executeGmailAutoReplyFrom(t, "alias@example.com", "", false, http.StatusForbidden) + if err == nil { + t.Fatal("expected error") + } + if sendCalls != 0 { + t.Fatalf("expected no messages.send calls, got %d", sendCalls) + } + if !strings.Contains(err.Error(), "list send-as aliases to exclude your own addresses") { + t.Fatalf("unexpected error: %v", err) + } + var apiErr *googleapi.Error + if !errors.As(err, &apiErr) || apiErr.Code != http.StatusForbidden { + t.Fatalf("expected wrapped 403, got %v", err) + } + if ExitCode(err) != exitCodePermissionDenied { + t.Fatalf("exit code = %d, want %d", ExitCode(err), exitCodePermissionDenied) + } + }) + + // --allow-self already permits replies to the user's own addresses, so a + // failed send-as lookup keeps the previous sender fallback instead of + // stopping the run. + t.Run("send-as forbidden with allow-self", func(t *testing.T) { + sendCalls, summary, err := executeGmailAutoReplyFrom(t, "alice@example.com", "", true, http.StatusForbidden) + if err != nil { + t.Fatalf("Execute: %v", err) + } + if sendCalls != 1 || summary.Replied != 1 { + t.Fatalf("sendCalls = %d, summary = %#v, want one reply", sendCalls, summary) + } + }) +} + +// executeGmailAutoReplyFrom runs `gmail autoreply` through Execute against an +// httptest Gmail server whose send-as list is the primary account plus +// alias@example.com. from and replyTo are the matching message's From and +// Reply-To headers. allowSelf sets --allow-self. sendAsStatus 0 serves the +// send-as list; any other status is returned by GET settings/sendAs. +// The helper fails the test unless that GET happens exactly once. +func executeGmailAutoReplyFrom(t *testing.T, from, replyTo string, allowSelf bool, sendAsStatus int) (int, gmailAutoReplySummary, error) { + t.Helper() + setTestConfigHome(t) + + var sendCalls, sendAsGets, followupCalls int + svc, cleanup := newGmailServiceForTest(t, func(w http.ResponseWriter, r *http.Request) { + path := strings.TrimPrefix(r.URL.Path, "/gmail/v1") + w.Header().Set("Content-Type", "application/json") + sendAsList := r.Method == http.MethodGet && path == "/users/me/settings/sendAs" + if !sendAsList { + followupCalls++ + } + switch { + case sendAsList: + sendAsGets++ + if sendAsStatus != 0 { + w.WriteHeader(sendAsStatus) + _ = json.NewEncoder(w).Encode(map[string]any{ + "error": map[string]any{ + "code": sendAsStatus, + "message": http.StatusText(sendAsStatus), + "errors": []map[string]any{{ + "message": http.StatusText(sendAsStatus), + "domain": "global", + "reason": "forbidden", + }}, + }, + }) + return + } + sendAsListHandler(w) + case r.Method == http.MethodGet && strings.HasPrefix(path, "/users/me/labels"): + _ = json.NewEncoder(w).Encode(map[string]any{ + "labels": []map[string]any{ + {"id": "Label_1", "name": "AutoReplied", "type": "user"}, + }, + }) + case r.Method == http.MethodGet && strings.HasPrefix(path, "/users/me/messages") && r.URL.Query().Get("q") != "": + _ = json.NewEncoder(w).Encode(map[string]any{ + "messages": []map[string]any{{"id": "m1", "threadId": "t1"}}, + }) + case r.Method == http.MethodGet && path == "/users/me/messages/m1": + headers := []map[string]any{ + {"name": "From", "value": from}, + {"name": "To", "value": "me@example.com"}, + {"name": "Subject", "value": "hello"}, + {"name": "Message-ID", "value": ""}, + } + if replyTo != "" { + headers = append(headers, map[string]any{"name": "Reply-To", "value": replyTo}) + } + _ = json.NewEncoder(w).Encode(map[string]any{ + "id": "m1", + "threadId": "t1", + "labelIds": []string{"INBOX"}, + "payload": map[string]any{ + "headers": headers, + }, + }) + case r.Method == http.MethodPost && path == "/users/me/messages/send": + sendCalls++ + _ = json.NewEncoder(w).Encode(map[string]any{"id": "reply1", "threadId": "t1"}) + case r.Method == http.MethodPost && path == "/users/me/threads/t1/modify": + _ = json.NewEncoder(w).Encode(map[string]any{"id": "t1"}) + default: + http.Error(w, "unexpected "+r.Method+" "+r.URL.RequestURI(), http.StatusNotFound) + } + }) + defer cleanup() + + args := []string{ + "--json", "--no-input", "--account", "me@example.com", + "gmail", "autoreply", "in:inbox", "--body", "thanks", + } + if allowSelf { + args = append(args, "--allow-self") + } + result := executeWithGmailTestService(t, args, svc) + if sendAsGets != 1 { + t.Fatalf("GET settings/sendAs calls = %d, want 1", sendAsGets) + } + if sendAsStatus != 0 && !allowSelf && (sendCalls != 0 || followupCalls != 0) { + t.Fatalf("send-as failure continued: send=%d followup=%d err=%v", sendCalls, followupCalls, result.err) + } + if result.err != nil { + if sendAsStatus == 0 { + t.Fatalf("Execute(%v): %v\nstderr:\n%s\nstdout:\n%s", args, result.err, result.stderr, result.stdout) + } + return sendCalls, gmailAutoReplySummary{}, result.err + } + + var payload struct { + AutoReply gmailAutoReplySummary `json:"autoReply"` + } + if err := json.Unmarshal([]byte(result.stdout), &payload); err != nil { + t.Fatalf("decode stdout: %v\n%s", err, result.stdout) + } + return sendCalls, payload.AutoReply, nil +} diff --git a/internal/cmd/gmail_forward.go b/internal/cmd/gmail_forward.go index b063bb2a0..c0c24f252 100644 --- a/internal/cmd/gmail_forward.go +++ b/internal/cmd/gmail_forward.go @@ -189,6 +189,11 @@ func (c *GmailForwardOptions) buildForwardComposeMessage(ctx context.Context, sv origSubject := headerValue(origMsg.Payload, "Subject") origPlain := gmailcontent.FindPartBody(origMsg.Payload, "text/plain") origHTML := gmailcontent.FindPartBody(origMsg.Payload, "text/html") + // An original with no usable plain text still forwards its body in the + // plain part, as a reply's plain quote does. + if strings.TrimSpace(origPlain) == "" && strings.TrimSpace(origHTML) != "" { + origPlain = htmlToPlainText(origHTML) + } // Build forward subject (avoid stacking prefixes). fwdSubject := buildForwardSubject(origSubject) diff --git a/internal/cmd/gmail_forward_html_original_test.go b/internal/cmd/gmail_forward_html_original_test.go new file mode 100644 index 000000000..41212e3eb --- /dev/null +++ b/internal/cmd/gmail_forward_html_original_test.go @@ -0,0 +1,74 @@ +package cmd + +import ( + "encoding/base64" + "strings" + "testing" +) + +func encodeForwardPart(s string) map[string]any { + data := base64.RawURLEncoding.EncodeToString([]byte(s)) + return map[string]any{"data": data, "size": len(data)} +} + +func forwardOriginalHeaders(subject string) []map[string]any { + return []map[string]any{ + {"name": "From", "value": "Alice "}, + {"name": "Date", "value": "Mon, 10 Mar 2026 09:00:00 -0400"}, + {"name": "Subject", "value": subject}, + } +} + +func mockHTMLOnlyForwardSource() map[string]any { + return map[string]any{ + "id": "orig-msg-1", "threadId": "thread-1", + "payload": map[string]any{ + "mimeType": "text/html", + "headers": forwardOriginalHeaders("HTML original"), + "body": encodeForwardPart("

Quarterly numbers attached.

"), + }, + } +} + +func mockBlankPlainForwardSource() map[string]any { + return map[string]any{ + "id": "orig-msg-1", "threadId": "thread-1", + "payload": map[string]any{ + "mimeType": "multipart/alternative", + "headers": forwardOriginalHeaders("Blank plain original"), + "parts": []map[string]any{ + {"mimeType": "text/plain", "body": encodeForwardPart(" \r\n")}, + {"mimeType": "text/html", "body": encodeForwardPart("

Quarterly numbers attached.

")}, + }, + }, + } +} + +// When the original has no usable plain text, the forward's plain part carries +// the HTML body as text, as a reply's plain quote already does. +func TestGmailForward_PlainPartCarriesHTMLOnlyOriginal(t *testing.T) { + t.Setenv("GOG_TIMEZONE", "UTC") + for _, cmd := range forwardCommands { + for _, src := range []struct { + name string + source func() map[string]any + }{ + {name: "HTML-only original", source: mockHTMLOnlyForwardSource}, + {name: "blank plain part", source: mockBlankPlainForwardSource}, + } { + t.Run(cmd.name+"/"+src.name, func(t *testing.T) { + plain, html := forwardTextParts(t, forwardArgs(cmd.verb), cmd.finalizePath, src.source) + _, body, _ := strings.Cut(plain, forwardedSeparator) + if !strings.Contains(body, "Quarterly numbers attached.") { + t.Errorf("plain part lacks the original body:\n%s", plain) + } + if strings.Contains(body, "") { + t.Errorf("plain part carries raw HTML:\n%s", plain) + } + if !strings.Contains(html, "

Quarterly numbers attached.

") { + t.Errorf("HTML part lacks the original:\n%s", html) + } + }) + } + } +} diff --git a/internal/cmd/gmail_reply.go b/internal/cmd/gmail_reply.go index 228b392d7..ed4632ea2 100644 --- a/internal/cmd/gmail_reply.go +++ b/internal/cmd/gmail_reply.go @@ -13,10 +13,11 @@ import ( "github.com/openclaw/gogcli/internal/mailmime" ) -// buildReplyAllRecipients constructs To and Cc lists for a reply-all. +// buildReplyAllRecipients constructs To and Cc lists for a reply-all, +// leaving out every one of selfEmails. // Per RFC 5322: if Reply-To header is present, use it instead of From. -func buildReplyAllRecipients(info *replyInfo, selfEmail string) (to, cc []string) { - recipients, err := buildReplyRecipients(info, []string{selfEmail}, true, nil, nil, nil, nil) +func buildReplyAllRecipients(info *replyInfo, selfEmails ...string) (to, cc []string) { + recipients, err := buildReplyRecipients(info, selfEmails, true, nil, nil, nil, nil) if err != nil { return nil, nil } diff --git a/internal/cmd/gmail_send.go b/internal/cmd/gmail_send.go index c59bbefc2..b2637eb2b 100644 --- a/internal/cmd/gmail_send.go +++ b/internal/cmd/gmail_send.go @@ -76,6 +76,8 @@ type sendMessageOptions struct { Attachments []mailmime.Attachment Track bool TrackingCfg *tracking.Config + // AllowMissingTo lets a message go out with only Cc or Bcc recipients. + AllowMissingTo bool } func (c *GmailSendCmd) Run(ctx context.Context, flags *RootFlags) error { @@ -173,7 +175,8 @@ func (c *GmailSendCmd) Run(ctx context.Context, flags *RootFlags) error { return err } - from, err := resolveComposeSender(ctx, svc, account, c.From) + sendAs, sendAsErr := listSendAs(ctx, svc) + from, err := resolveComposeFrom(ctx, svc, account, c.From, sendAs, sendAsErr) if err != nil { return err } @@ -194,7 +197,8 @@ func (c *GmailSendCmd) Run(ctx context.Context, flags *RootFlags) error { var toRecipients, ccRecipients []string if c.ReplyAll { // Auto-populate recipients from original message - toRecipients, ccRecipients = buildReplyAllRecipients(replyInfo, from.sendingEmail) + // Leave out the account and every send-as alias, as gmail reply-all does. + toRecipients, ccRecipients = buildReplyAllRecipients(replyInfo, selfEmailsForReply(account, from.sendingEmail, sendAs)...) } // Explicit --to and --cc override (not merge with) auto-populated @@ -209,8 +213,11 @@ func (c *GmailSendCmd) Run(ctx context.Context, flags *RootFlags) error { ccRecipients = explicitCc } - // Final validation: we must have at least one recipient - if len(toRecipients) == 0 { + // Final validation: we must have at least one recipient. A reply-all may + // leave only Cc recipients once the user's own addresses are dropped, which + // gmail reply-all also sends. + allowMissingTo := c.ReplyAll && strings.TrimSpace(c.To) == "" && len(ccRecipients)+len(explicitBcc) > 0 + if len(toRecipients) == 0 && !allowMissingTo { return usage("no recipients: specify --to or use --reply-all with a message that has recipients") } @@ -239,6 +246,8 @@ func (c *GmailSendCmd) Run(ctx context.Context, flags *RootFlags) error { Attachments: atts, Track: c.Track, TrackingCfg: trackingCfg, + + AllowMissingTo: allowMissingTo, }, batches) if err != nil { return err @@ -442,7 +451,7 @@ func sendGmailBatches(ctx context.Context, svc *gmail.Service, opts sendMessageO messageOpts := opts messageOpts.BodyHTML = htmlBody - msg, err := buildGmailMessage(ctx, messageOpts, batch, false) + msg, err := buildGmailMessage(ctx, messageOpts, batch, opts.AllowMissingTo) if err != nil { return nil, err } diff --git a/internal/cmd/gmail_send_reply_all_aliases_test.go b/internal/cmd/gmail_send_reply_all_aliases_test.go new file mode 100644 index 000000000..1565d68ae --- /dev/null +++ b/internal/cmd/gmail_send_reply_all_aliases_test.go @@ -0,0 +1,118 @@ +package cmd + +import ( + "encoding/base64" + "encoding/json" + "io" + "net/http" + "strings" + "testing" + + "google.golang.org/api/gmail/v1" +) + +// headerLine returns the first raw header line that starts with name. +func headerLine(raw, name string) string { + for _, line := range strings.Split(raw, "\n") { + if strings.HasPrefix(line, name+":") { + return strings.TrimSpace(line) + } + } + return "" +} + +// The original is addressed to the user's send-as alias and to bob. A +// reply-all must leave every address of the user out of the recipients, +// whichever command sends it. +func sendReplyAllViaCommand(t *testing.T, args ...string) string { + t.Helper() + return sendReplyAllWithHeaders(t, "alice@example.com", "me@example.com, alias@example.com, bob@example.com", "", args...) +} + +func sendReplyAllWithHeaders(t *testing.T, from, to, cc string, args ...string) string { + t.Helper() + var sentRaw string + svc, cleanup := newGmailServiceForTest(t, func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch { + case r.Method == http.MethodGet && r.URL.Path == "/gmail/v1/users/me/settings/sendAs": + _ = json.NewEncoder(w).Encode(map[string]any{"sendAs": []map[string]any{ + {"sendAsEmail": "me@example.com", "isPrimary": true, "verificationStatus": "accepted"}, + {"sendAsEmail": "alias@example.com", "verificationStatus": "accepted"}, + }}) + case r.Method == http.MethodGet && r.URL.Path == "/gmail/v1/users/me/messages/msg-1": + _ = json.NewEncoder(w).Encode(map[string]any{ + "id": "msg-1", "threadId": "thread-1", + "payload": map[string]any{"headers": []map[string]any{ + {"name": "Message-ID", "value": ""}, + {"name": "From", "value": from}, + {"name": "To", "value": to}, + {"name": "Cc", "value": cc}, + {"name": "Subject", "value": "Plans"}, + }}, + }) + case r.Method == http.MethodPost && r.URL.Path == "/gmail/v1/users/me/messages/send": + body, _ := io.ReadAll(r.Body) + var msg gmail.Message + _ = json.Unmarshal(body, &msg) + raw, _ := base64.RawURLEncoding.DecodeString(msg.Raw) + sentRaw = string(raw) + _ = json.NewEncoder(w).Encode(map[string]any{"id": "sent-1", "threadId": "thread-1"}) + default: + http.NotFound(w, r) + } + }) + defer cleanup() + + result := executeWithGmailTestService(t, append([]string{"--json", "--account", "me@example.com"}, args...), svc) + if result.err != nil { + t.Fatalf("Execute %v: %v", args, result.err) + } + return sentRaw +} + +func TestGmailSendReplyAllExcludesSendAsAliases(t *testing.T) { + for _, tc := range []struct { + name string + args []string + }{ + {name: "gmail reply-all (control)", args: []string{"gmail", "reply-all", "msg-1", "--body", "ok", "--no-quote"}}, + {name: "gmail send --reply-all", args: []string{"gmail", "send", "--reply-to-message-id", "msg-1", "--reply-all", "--body", "ok"}}, + {name: "gmail send --reply-all --from alias", args: []string{"gmail", "send", "--reply-to-message-id", "msg-1", "--reply-all", "--from", "alias@example.com", "--body", "ok"}}, + } { + t.Run(tc.name, func(t *testing.T) { + raw := sendReplyAllViaCommand(t, tc.args...) + recipients := headerLine(raw, "To") + " " + headerLine(raw, "Cc") + if !strings.Contains(recipients, "alice@example.com") || !strings.Contains(recipients, "bob@example.com") { + t.Fatalf("recipients %q, want alice and bob", recipients) + } + if strings.Contains(recipients, "alias@example.com") || strings.Contains(recipients, "me@example.com") { + t.Fatalf("recipients %q include the sender's own address", recipients) + } + }) + } +} + +// A message the user sent from an alias to their own primary address, copying +// bob: once both self addresses are left out, bob remains only in Cc. That is +// still a valid reply, as gmail reply-all already treats it. +func TestGmailSendReplyAllKeepsCcOnlyRecipients(t *testing.T) { + for _, tc := range []struct { + name string + args []string + }{ + {name: "gmail reply-all (control)", args: []string{"gmail", "reply-all", "msg-1", "--body", "ok", "--no-quote"}}, + {name: "gmail send --reply-all", args: []string{"gmail", "send", "--reply-to-message-id", "msg-1", "--reply-all", "--body", "ok"}}, + } { + t.Run(tc.name, func(t *testing.T) { + raw := sendReplyAllWithHeaders(t, "alias@example.com", "me@example.com", "bob@example.com", tc.args...) + if got := headerLine(raw, "Cc"); !strings.Contains(got, "bob@example.com") { + t.Fatalf("Cc %q, want bob", got) + } + recipients := headerLine(raw, "To") + " " + headerLine(raw, "Cc") + if strings.Contains(recipients, "alias@example.com") || strings.Contains(recipients, "me@example.com") { + t.Fatalf("recipients %q include the sender's own address", recipients) + } + }) + } +} diff --git a/internal/cmd/gmail_watch_history_pages_test.go b/internal/cmd/gmail_watch_history_pages_test.go new file mode 100644 index 000000000..fd3a27ef0 --- /dev/null +++ b/internal/cmd/gmail_watch_history_pages_test.go @@ -0,0 +1,62 @@ +package cmd + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" +) + +// A history listing that spans two pages: every record must be returned, and +// the stored cursor may only be the historyId of the page without a +// nextPageToken ("If you receive no nextPageToken in the response, there are no +// updates to retrieve and you can store the returned historyId"). +func TestGmailWatchSourceListHistoryReadsEveryPage(t *testing.T) { + var calls []string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/gmail/v1/users/me/history" { + http.NotFound(w, r) + return + } + token := r.URL.Query().Get("pageToken") + calls = append(calls, token) + w.Header().Set("Content-Type", "application/json") + switch token { + case "": + _, _ = w.Write([]byte(`{"history":[{"id":"101","messagesAdded":[{"message":{"id":"m1"}}]}],"nextPageToken":"p2","historyId":"300"}`)) + case "p2": + _, _ = w.Write([]byte(`{"history":[{"id":"250","messagesAdded":[{"message":{"id":"m2"}}]}],"historyId":"300"}`)) + default: + http.Error(w, "unexpected page token", http.StatusBadRequest) + } + })) + t.Cleanup(srv.Close) + + source := &gmailWatchSource{service: newGmailServiceFromServer(t, srv)} + page, err := source.ListHistory(context.Background(), 100, 1, nil) + if err != nil { + t.Fatalf("ListHistory: %v", err) + } + if len(calls) != 2 || calls[1] != "p2" { + t.Fatalf("history.list calls = %q, want first page then p2", calls) + } + if len(page.Records) != 2 || page.Records[1].Added[0] != "m2" { + t.Fatalf("records = %#v, want both pages", page.Records) + } + if page.HistoryID != "300" { + t.Fatalf("historyId = %q, want 300", page.HistoryID) + } +} + +func TestGmailWatchSourceListHistoryRejectsRepeatedPageToken(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"history":[{"id":"101"}],"nextPageToken":"same","historyId":"300"}`)) + })) + t.Cleanup(srv.Close) + + source := &gmailWatchSource{service: newGmailServiceFromServer(t, srv)} + if _, err := source.ListHistory(context.Background(), 100, 1, nil); err == nil { + t.Fatal("ListHistory accepted a repeating page token; the cursor would advance past unread history") + } +} diff --git a/internal/cmd/gmail_watch_server_history_pages_test.go b/internal/cmd/gmail_watch_server_history_pages_test.go new file mode 100644 index 000000000..dd19abe5a --- /dev/null +++ b/internal/cmd/gmail_watch_server_history_pages_test.go @@ -0,0 +1,88 @@ +package cmd + +import ( + "bytes" + "context" + "encoding/base64" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "google.golang.org/api/gmail/v1" + + "github.com/openclaw/gogcli/internal/gmailwatch" +) + +// A push whose history spans two pages must deliver the messages from both +// pages to the hook and only then store the mailbox's historyId. +func TestGmailWatchServer_DeliversHistoryBeyondFirstPage(t *testing.T) { + setWatchTestConfigHome(t) + store := newGmailWatchTestStore(t, "a@b.com") + _ = store.Update(func(s *gmailWatchState) error { + s.Account = "a@b.com" + s.HistoryID = "100" + return nil + }) + + api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + switch { + case r.URL.Path == "/gmail/v1/users/me/history" && r.URL.Query().Get("pageToken") == "": + _, _ = io.WriteString(w, `{"history":[{"id":"150","messagesAdded":[{"message":{"id":"m1"}}]}],"nextPageToken":"p2","historyId":"300"}`) + case r.URL.Path == "/gmail/v1/users/me/history" && r.URL.Query().Get("pageToken") == "p2": + _, _ = io.WriteString(w, `{"history":[{"id":"250","messagesAdded":[{"message":{"id":"m2"}}]}],"historyId":"300"}`) + case strings.HasPrefix(r.URL.Path, "/gmail/v1/users/me/messages/"): + id := strings.TrimPrefix(r.URL.Path, "/gmail/v1/users/me/messages/") + _, _ = io.WriteString(w, `{"id":"`+id+`","threadId":"t-`+id+`","payload":{"headers":[{"name":"Subject","value":"s"}]}}`) + default: + http.NotFound(w, r) + } + })) + t.Cleanup(api.Close) + gsvc := newGmailServiceFromServer(t, api) + + var delivered gmailwatch.Payload + hook := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = json.NewDecoder(r.Body).Decode(&delivered) + w.WriteHeader(http.StatusNoContent) + })) + t.Cleanup(hook.Close) + + server := &gmailWatchServer{ + cfg: gmailWatchServeConfig{ + Account: "a@b.com", + Path: "/hook", + SharedToken: "tok", + HookURL: hook.URL, + HistoryMax: 1, + }, + store: store, + newService: func(context.Context, string) (*gmail.Service, error) { return gsvc, nil }, + hookClient: hook.Client(), + logf: func(string, ...any) {}, + warnf: func(string, ...any) {}, + } + + push := pubsubPushEnvelope{} + push.Message.Data = base64.StdEncoding.EncodeToString([]byte(`{"emailAddress":"a@b.com","historyId":"300"}`)) + body, _ := json.Marshal(push) + rr := httptest.NewRecorder() + server.ServeHTTP(rr, httptest.NewRequestWithContext(context.Background(), http.MethodPost, "/hook?token=tok", bytes.NewReader(body))) + if rr.Code != http.StatusOK { + t.Fatalf("status: %d %s", rr.Code, rr.Body.String()) + } + + ids := make([]string, 0, len(delivered.Messages)) + for _, m := range delivered.Messages { + ids = append(ids, m.ID) + } + if strings.Join(ids, ",") != "m1,m2" { + t.Fatalf("hook got messages %v, want m1,m2", ids) + } + if got := store.Get().HistoryID; got != "300" { + t.Fatalf("stored historyId = %q, want 300", got) + } +} diff --git a/internal/cmd/gmail_watch_source.go b/internal/cmd/gmail_watch_source.go index 1606c122f..9f979ba62 100644 --- a/internal/cmd/gmail_watch_source.go +++ b/internal/cmd/gmail_watch_source.go @@ -35,17 +35,35 @@ func newGmailWatchSource(service *gmail.Service, cfg gmailWatchServeConfig, excl } } +// ListHistory reads every page of history after startID. The returned +// historyId is the mailbox's current one, which is only a safe cursor once no +// nextPageToken remains; storing it after the first page would skip the rest. func (s *gmailWatchSource) ListHistory(ctx context.Context, startID uint64, maxResults int64, historyTypes []string) (gmailwatch.HistoryPage, error) { - call := s.service.Users.History.List("me").StartHistoryId(startID).MaxResults(maxResults) - if len(historyTypes) > 0 { - call.HistoryTypes(historyTypes...) - } - response, err := call.Context(ctx).Do() + historyID := "" + records, err := collectAllPages("", func(pageToken string) ([]gmailwatch.HistoryRecord, string, error) { + call := s.service.Users.History.List("me").StartHistoryId(startID).MaxResults(maxResults) + if len(historyTypes) > 0 { + call.HistoryTypes(historyTypes...) + } + if pageToken != "" { + call.PageToken(pageToken) + } + response, callErr := call.Context(ctx).Do() + if callErr != nil { + return nil, "", callErr + } + page := historyPageFromGmail(response) + historyID = page.HistoryID + if response == nil { + return page.Records, "", nil + } + return page.Records, response.NextPageToken, nil + }) if err != nil { return gmailwatch.HistoryPage{}, err } - return historyPageFromGmail(response), nil + return gmailwatch.HistoryPage{HistoryID: historyID, Records: records}, nil } func (s *gmailWatchSource) ListRecentMessageIDs(ctx context.Context, maxResults int64) ([]string, error) { diff --git a/internal/cmd/output_helpers.go b/internal/cmd/output_helpers.go index 741b4587a..d3cff9d18 100644 --- a/internal/cmd/output_helpers.go +++ b/internal/cmd/output_helpers.go @@ -94,3 +94,12 @@ func escapeTerminalControls(value string) string { } return output.String() } + +// nonNilSlice returns s, or an empty slice when s is nil. Google omits empty +// list fields, and JSON output should still encode an empty list as []. +func nonNilSlice[T any](s []T) []T { + if s == nil { + return []T{} + } + return s +} diff --git a/internal/cmd/sheets.go b/internal/cmd/sheets.go index 3f5915541..d6cf3a57b 100644 --- a/internal/cmd/sheets.go +++ b/internal/cmd/sheets.go @@ -166,15 +166,22 @@ func (c *SheetsGetCmd) Run(ctx context.Context, flags *RootFlags) error { w, flush := tableWriter(ctx) defer flush() for _, row := range resp.Values { - cells := make([]string, len(row)) - for i, cell := range row { - cells[i] = fmt.Sprintf("%v", cell) - } - fmt.Fprintln(w, strings.Join(cells, "\t")) + fmt.Fprintln(w, sheetsValueRowLine(row)) } return nil } +// sheetsValueRowLine joins one row of cell values with tabs. Line breaks and +// tabs inside a cell are escaped with oneLine so each sheet row stays one +// output row, as in the other Sheets cell tables. +func sheetsValueRowLine(row []interface{}) string { + cells := make([]string, len(row)) + for i, cell := range row { + cells[i] = oneLine(fmt.Sprintf("%v", cell)) + } + return strings.Join(cells, "\t") +} + type SheetsUpdateCmd struct { SpreadsheetID string `arg:"" name:"spreadsheetId" help:"Spreadsheet ID"` Range string `arg:"" name:"range" help:"Range (A1 notation or named range name; e.g. Sheet1!A1:B2 or MyNamedRange)"` @@ -559,7 +566,7 @@ func (c *SheetsAppendCmd) Run(ctx context.Context, flags *RootFlags) error { return usagef("read --values-json: %v", err) } - values, err = sheetsvalues.Decode(b) + values, err = sheetsvalues.DecodeStrict(b) if err != nil { return sheetsValuesPlannerError(err) } diff --git a/internal/cmd/sheets_append_precision_test.go b/internal/cmd/sheets_append_precision_test.go new file mode 100644 index 000000000..9c01b2e91 --- /dev/null +++ b/internal/cmd/sheets_append_precision_test.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestSheetsAppendPreservesJSONNumbers(t *testing.T) { + var body string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + data, _ := io.ReadAll(r.Body) + body = string(data) + w.Header().Set("Content-Type", "application/json") + _, _ = io.WriteString(w, `{"updates":{"updatedRows":1}}`) + })) + defer srv.Close() + result := executeWithSheetsTestService(t, []string{ + "--home=" + t.TempDir(), "--account=synthetic@example.invalid", "--json", + "sheets", "append", "--input=RAW", "--values-json=[[9007199254740993,1.234567890123456789]]", + "--", "synthetic-sheet", "A:B", + }, newSheetsServiceFromServer(t, srv)) + if result.err != nil { + t.Fatal(result.err) + } + if !strings.Contains(body, "9007199254740993") || !strings.Contains(body, "1.234567890123456789") { + t.Fatalf("input numbers changed in request: %s", body) + } +} diff --git a/internal/cmd/sheets_datasource.go b/internal/cmd/sheets_datasource.go index 804288d89..943bb3291 100644 --- a/internal/cmd/sheets_datasource.go +++ b/internal/cmd/sheets_datasource.go @@ -347,11 +347,7 @@ func (c *SheetsDataSourceTableReadCmd) Run(ctx context.Context, flags *RootFlags w, flush := tableWriter(ctx) defer flush() for _, row := range rowsOut { - cells := make([]string, len(row)) - for i, cell := range row { - cells[i] = fmt.Sprintf("%v", cell) - } - fmt.Fprintln(w, strings.Join(cells, "\t")) + fmt.Fprintln(w, sheetsValueRowLine(row)) } return nil } diff --git a/internal/cmd/sheets_links.go b/internal/cmd/sheets_links.go index 6186ac77b..2f3b919b9 100644 --- a/internal/cmd/sheets_links.go +++ b/internal/cmd/sheets_links.go @@ -50,7 +50,7 @@ func (c *SheetsLinksGetCmd) Run(ctx context.Context, flags *RootFlags) error { return err } - var links []sheetsCellLink + links := []sheetsCellLink{} for _, sheet := range resp.Sheets { if sheet == nil { diff --git a/internal/cmd/sheets_notes.go b/internal/cmd/sheets_notes.go index c3ea31f20..281346d8b 100644 --- a/internal/cmd/sheets_notes.go +++ b/internal/cmd/sheets_notes.go @@ -40,7 +40,7 @@ func (c *SheetsNotesCmd) Run(ctx context.Context, flags *RootFlags) error { return err } - var notes []sheetsCellNote + notes := []sheetsCellNote{} for _, sheet := range resp.Sheets { if sheet == nil { diff --git a/internal/cmd/sheets_notes_links_empty_test.go b/internal/cmd/sheets_notes_links_empty_test.go new file mode 100644 index 000000000..a8d6b01dd --- /dev/null +++ b/internal/cmd/sheets_notes_links_empty_test.go @@ -0,0 +1,55 @@ +package cmd + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +// A range with no notes or links must still be a JSON array, so scripts can +// use `jq '.[]'` or `length` on it, with and without --results-only. +func TestSheetsNotesAndLinks_EmptyRangeJSONIsEmptyArray(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{ + "sheets": []map[string]any{{ + "properties": map[string]any{"title": "Sheet1"}, + "data": []map[string]any{{ + "rowData": []map[string]any{{"values": []map[string]any{{"formattedValue": "plain"}}}}, + }}, + }}, + }) + })) + defer srv.Close() + svc := newSheetsServiceFromServer(t, srv) + + for _, tc := range []struct { + command string + key string + }{ + {command: "notes", key: "notes"}, + {command: "links", key: "links"}, + } { + envelope := executeWithSheetsTestService(t, []string{"--json", "--account", "a@b.com", "sheets", tc.command, "s1", "Sheet1!A1:A1"}, svc) + if envelope.err != nil { + t.Fatalf("%s: %v", tc.command, envelope.err) + } + var got map[string]json.RawMessage + if err := json.Unmarshal([]byte(envelope.stdout), &got); err != nil { + t.Fatalf("%s json: %v (%q)", tc.command, err, envelope.stdout) + } + if string(got[tc.key]) != "[]" { + t.Fatalf("%s: %q = %s, want []", tc.command, tc.key, got[tc.key]) + } + + unwrapped := executeWithSheetsTestService(t, []string{"--json", "--results-only", "--account", "a@b.com", "sheets", tc.command, "s1", "Sheet1!A1:A1"}, svc) + if unwrapped.err != nil { + t.Fatalf("%s --results-only: %v", tc.command, unwrapped.err) + } + if strings.TrimSpace(unwrapped.stdout) != "[]" { + t.Fatalf("%s --results-only = %s, want []", tc.command, strings.TrimSpace(unwrapped.stdout)) + } + } +} diff --git a/internal/cmd/sheets_plain_test.go b/internal/cmd/sheets_plain_test.go index 9ae870884..8e9a8dd87 100644 --- a/internal/cmd/sheets_plain_test.go +++ b/internal/cmd/sheets_plain_test.go @@ -80,3 +80,31 @@ func TestSheetsMetadataCmd_PlainOutputTSV(t *testing.T) { t.Errorf("metadata table header missing tab delimiters in plain mode: %q", got) } } + +// TestSheetsGetCmd_PlainKeepsMultilineCellsOnOneRow verifies that a cell with +// line breaks or tabs stays in its own row and column, as the other Sheets +// cell tables (links, notes, format) already do through oneLine. +func TestSheetsGetCmd_PlainKeepsMultilineCellsOnOneRow(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(map[string]any{ + "range": "Sheet1!A1:D3", + "values": [][]any{ + {"Name", "Address", "Note"}, + {"Alice", "1 Main St\nBerlin", "a\tb"}, + {"Bob", "2 High St\r\nParis", 3.5, true}, + }, + }) + })) + defer srv.Close() + + svc := newSheetsServiceFromServer(t, srv) + result := executeWithSheetsTestService(t, []string{"--plain", "--account", "a@b.com", "sheets", "get", "spreadsheet1", "Sheet1!A1:D3"}, svc) + if result.err != nil { + t.Fatalf("execute: %v", result.err) + } + want := "Name\tAddress\tNote\nAlice\t1 Main St\\nBerlin\ta b\nBob\t2 High St\\nParis\t3.5\ttrue\n" + if result.stdout != want { + t.Fatalf("stdout = %q, want %q", result.stdout, want) + } +} diff --git a/internal/cmd/sites.go b/internal/cmd/sites.go index a271f1a41..fd696847c 100644 --- a/internal/cmd/sites.go +++ b/internal/cmd/sites.go @@ -129,6 +129,7 @@ func (c *SitesGetCmd) Run(ctx context.Context, flags *RootFlags) error { } if outfmt.IsJSON(ctx) { + forceRequestedDriveFileBools(f, mask) return outfmt.WriteJSON(ctx, stdoutWriter(ctx), map[string]any{"site": f}) } diff --git a/internal/cmd/sites_test.go b/internal/cmd/sites_test.go index bc4cf047b..24ebce24e 100644 --- a/internal/cmd/sites_test.go +++ b/internal/cmd/sites_test.go @@ -181,3 +181,92 @@ func TestSitesGetRejectsNonSite(t *testing.T) { t.Fatalf("expected url not-site error, got %v", err) } } + +// TestSitesGet_JSONKeepsRequestedFalseBools proves sites get JSON keeps +// requested boolean fields when they are false. drive.File tags them +// omitempty, so starred and hasThumbnail disappear unless ForceSendFields +// is set. Drive get already does that; sites get must too. +func TestSitesGet_JSONKeepsRequestedFalseBools(t *testing.T) { + cases := []struct { + name string + args []string + body map[string]any + want map[string]bool + notWant []string + }{ + { + name: "default fields", + args: []string{"site1"}, + body: map[string]any{ + "id": "site1", + "name": "Team Site", + "mimeType": driveMimeGoogleSite, + "starred": false, + "hasThumbnail": false, + "trashed": false, + }, + want: map[string]bool{"starred": false, "hasThumbnail": false}, + notWant: []string{"trashed"}, + }, + { + name: "custom trashed mask", + args: []string{"site1", "--fields", "id,name,trashed"}, + body: map[string]any{ + "id": "site1", + "name": "Team Site", + "mimeType": driveMimeGoogleSite, + "starred": false, + "hasThumbnail": false, + "trashed": false, + }, + want: map[string]bool{"trashed": false}, + notWant: []string{"starred", "hasThumbnail"}, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + svc, closeSrv := newDriveTestService(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !strings.Contains(r.URL.Path, "/files/site1") { + http.NotFound(w, r) + return + } + + w.Header().Set("Content-Type", "application/json") + _ = json.NewEncoder(w).Encode(tc.body) + })) + defer closeSrv() + + var stdout bytes.Buffer + ctx := withSitesDriveTestService(newCmdRuntimeJSONOutputContext(t, &stdout, io.Discard), svc) + flags := &RootFlags{Account: "a@b.com"} + + if err := runKong(t, &SitesGetCmd{}, tc.args, ctx, flags); err != nil { + t.Fatalf("run: %v", err) + } + + var envelope struct { + Site map[string]any `json:"site"` + } + if err := json.Unmarshal(stdout.Bytes(), &envelope); err != nil { + t.Fatalf("invalid JSON: %v\nraw: %s", err, stdout.String()) + } + + for key, want := range tc.want { + got, ok := envelope.Site[key] + if !ok { + t.Fatalf("missing %q in output: %s", key, stdout.String()) + } + + if got != want { + t.Fatalf("%q = %v, want %v", key, got, want) + } + } + + for _, key := range tc.notWant { + if _, ok := envelope.Site[key]; ok { + t.Fatalf("unexpected %q in output: %s", key, stdout.String()) + } + } + }) + } +} diff --git a/internal/cmd/tasks_items.go b/internal/cmd/tasks_items.go index 10f094fc8..2aab09ff2 100644 --- a/internal/cmd/tasks_items.go +++ b/internal/cmd/tasks_items.go @@ -349,7 +349,7 @@ type TasksUpdateCmd struct { Title string `name:"title" help:"New title (set empty to clear)"` Notes string `name:"notes" help:"New notes (set empty to clear)"` Due string `name:"due" help:"New due date (RFC3339 or YYYY-MM-DD; time may be ignored; set empty to clear)"` - Status string `name:"status" help:"New status: needsAction|completed (set empty to clear)"` + Status string `name:"status" help:"New status: needsAction|completed"` } func (c *TasksUpdateCmd) Run(ctx context.Context, kctx *kong.Context, flags *RootFlags) error { diff --git a/internal/cmd/tasks_update_clear_test.go b/internal/cmd/tasks_update_clear_test.go new file mode 100644 index 000000000..d299d9f4c --- /dev/null +++ b/internal/cmd/tasks_update_clear_test.go @@ -0,0 +1,67 @@ +package cmd + +import ( + "errors" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +// runTasksUpdatePatch runs tasks update against a fake Tasks server and +// returns the PATCH body it sent ("" when no PATCH was sent). +func runTasksUpdatePatch(t *testing.T, args ...string) (string, error) { + t.Helper() + var patched string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodGet && r.URL.Path == "/tasks/v1/users/@me/lists": + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"items":[{"id":"l1","title":"One"}]}`)) + case r.Method == http.MethodPatch && r.URL.Path == "/tasks/v1/lists/l1/tasks/t1": + body, _ := io.ReadAll(r.Body) + patched = strings.TrimSpace(string(body)) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"id":"t1","title":"Task","status":"needsAction"}`)) + default: + http.NotFound(w, r) + } + })) + t.Cleanup(srv.Close) + + ctx := withTasksTestService(newCmdRuntimeJSONOutputContext(t, io.Discard, io.Discard), newTasksServiceFromServer(t, srv)) + err := runKong(t, &TasksUpdateCmd{}, append([]string{"l1", "t1"}, args...), ctx, &RootFlags{Account: "a@b.com"}) + return patched, err +} + +func TestTasksUpdateCmd_EmptyNotesAndTitleAreSent(t *testing.T) { + for _, tc := range []struct { + args []string + want string + }{ + {args: []string{"--notes", ""}, want: `{"notes":""}`}, + {args: []string{"--title", ""}, want: `{"title":""}`}, + {args: []string{"--due", ""}, want: `{"due":null}`}, + {args: []string{"--notes", "keep"}, want: `{"notes":"keep"}`}, + } { + got, err := runTasksUpdatePatch(t, tc.args...) + if err != nil { + t.Fatalf("%v: %v", tc.args, err) + } + if got != tc.want { + t.Fatalf("%v sent PATCH %s, want %s", tc.args, got, tc.want) + } + } +} + +func TestTasksUpdateCmd_EmptyStatusIsUsageError(t *testing.T) { + got, err := runTasksUpdatePatch(t, "--status", "") + var exitErr *ExitError + if !errors.As(err, &exitErr) || exitErr.Code != 2 { + t.Fatalf("err = %v, want usage error", err) + } + if got != "" { + t.Fatalf("sent PATCH %s before rejecting", got) + } +} diff --git a/internal/cmd/tasks_update_plan.go b/internal/cmd/tasks_update_plan.go index 4c2e4b271..4dddcc718 100644 --- a/internal/cmd/tasks_update_plan.go +++ b/internal/cmd/tasks_update_plan.go @@ -53,12 +53,20 @@ func newTasksUpdatePlan(input tasksUpdateInput, fields tasksUpdateFields) (tasks } changed := false + // Title and Notes are omitempty in the API struct; force an explicit empty + // value into the PATCH so "set empty to clear" clears instead of sending {}. if fields.Title { plan.Patch.Title = strings.TrimSpace(input.Title) + if plan.Patch.Title == "" { + plan.Patch.ForceSendFields = append(plan.Patch.ForceSendFields, "Title") + } changed = true } if fields.Notes { plan.Patch.Notes = strings.TrimSpace(input.Notes) + if plan.Patch.Notes == "" { + plan.Patch.ForceSendFields = append(plan.Patch.ForceSendFields, "Notes") + } changed = true } if fields.Due { @@ -81,7 +89,8 @@ func newTasksUpdatePlan(input tasksUpdateInput, fields tasksUpdateFields) (tasks if !changed { return tasksUpdatePlan{}, usage("no fields to update (set at least one of: --title, --notes, --due, --status)") } - if fields.Status && plan.Patch.Status != "" && plan.Patch.Status != taskStatusNeedsAction && plan.Patch.Status != taskStatusCompleted { + // A task is always needsAction or completed; there is no empty status to set. + if fields.Status && plan.Patch.Status != taskStatusNeedsAction && plan.Patch.Status != taskStatusCompleted { return tasksUpdatePlan{}, usage("invalid --status (expected needsAction or completed)") } return plan, nil diff --git a/scripts/gen-agent-skills.mjs b/scripts/gen-agent-skills.mjs index f88e67254..ba531300e 100644 --- a/scripts/gen-agent-skills.mjs +++ b/scripts/gen-agent-skills.mjs @@ -27,6 +27,33 @@ const titles = { slides: "Google Slides", tasks: "Google Tasks", youtube: "YouTube", }; +const uses = { + admin: "Use for Workspace Admin Directory users, groups, and organizational units; use gog-groups for Cloud Identity group membership lookup.", + adsense: "Use for AdSense accounts, sites, payments, and earnings reports.", + analytics: "Use for Google Analytics account summaries and GA4 reports.", + appscript: "Use for Apps Script projects, source files, deployments, and executions.", + calendar: "Use for calendars, events, availability, and scheduling; use gog-meeting-prep for meeting briefings or gog-weekly-digest for a cross-service summary.", + chat: "Use for Google Chat spaces, messages, threads, and members.", + classroom: "Use for Classroom courses, rosters, coursework, submissions, and announcements.", + contacts: "Use for personal contacts, contact groups, and other contacts, with directory lookup also available; use gog-people for the caller profile or Workspace directory profiles.", + docs: "Use for Google Docs text, formatting, tabs, comments, and document exports.", + drive: "Use for Drive file search, uploads, downloads, sharing, folders, and metadata; use gog-drive-audit for a sharing audit.", + forms: "Use for Google Forms structure, questions, responses, and watches.", + gmail: "Use for Gmail search, messages, threads, drafts, sending, labels, filters, and settings; use gog-inbox-triage for inbox review or gog-save-attachments for attachment archival.", + groups: "Use for listing Cloud Identity groups and members; use gog-admin to manage Workspace Admin Directory groups.", + keep: "Use for Google Keep notes and attachments.", + maps: "Use for Maps place search, place details, directions, and geocoding.", + meet: "Use for Google Meet spaces, configuration, call history, and participants.", + people: "Use for the caller profile, a person profile, or Workspace directory search; use gog-contacts to manage personal contacts and contact groups.", + photos: "Use for Google Photos media and albums available to the app, including uploads and the picker.", + searchconsole: "Use for Search Console sites, search analytics, sitemaps, and URL inspection.", + sheets: "Use for spreadsheet cells, formulas, formatting, notes, validation, and sheet structure.", + sites: "Use for finding Google Sites in Drive, reading site metadata, and printing editor URLs; use gog-drive for file sharing.", + slides: "Use for presentations, slides, text, images, speaker notes, and exports.", + tasks: "Use for Google Tasks lists, task creation, updates, completion, and materialized repeats.", + youtube: "Use for YouTube search, videos, channels, activities, comments, playlists, and subscriptions.", +}; + const examples = { calendar: "gog --readonly --account user@example.com calendar events --today --json --wrap-untrusted", contacts: "gog --readonly --account user@example.com contacts search alice --json --wrap-untrusted", @@ -68,7 +95,7 @@ function skillMarkdown(service, command) { const example = examples[service] || `gog --readonly --account user@example.com ${service} --help`; return `--- name: gog-${service} -description: "${title} operations through gog." +description: ${JSON.stringify(`${title} through gog. ${uses[service]}`)} --- # ${title}