|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "google.golang.org/api/gmail/v1" |
| 12 | + "google.golang.org/api/option" |
| 13 | +) |
| 14 | + |
| 15 | +func TestExecute_GmailThreadAliases(t *testing.T) { |
| 16 | + origNew := newGmailService |
| 17 | + t.Cleanup(func() { newGmailService = origNew }) |
| 18 | + |
| 19 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 20 | + path := strings.TrimPrefix(r.URL.Path, "/gmail/v1") |
| 21 | + switch { |
| 22 | + case r.Method == http.MethodGet && path == "/users/me/threads/t1": |
| 23 | + w.Header().Set("Content-Type", "application/json") |
| 24 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 25 | + "id": "t1", |
| 26 | + "messages": []map[string]any{ |
| 27 | + { |
| 28 | + "id": "m1", |
| 29 | + "payload": map[string]any{ |
| 30 | + "headers": []map[string]any{ |
| 31 | + {"name": "From", "value": "me@example.com"}, |
| 32 | + {"name": "To", "value": "you@example.com"}, |
| 33 | + {"name": "Subject", "value": "Hello"}, |
| 34 | + {"name": "Date", "value": "Wed, 21 Jan 2026 12:00:00 +0000"}, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }) |
| 40 | + return |
| 41 | + default: |
| 42 | + http.NotFound(w, r) |
| 43 | + return |
| 44 | + } |
| 45 | + })) |
| 46 | + defer srv.Close() |
| 47 | + |
| 48 | + svc, err := gmail.NewService(context.Background(), |
| 49 | + option.WithoutAuthentication(), |
| 50 | + option.WithHTTPClient(srv.Client()), |
| 51 | + option.WithEndpoint(srv.URL+"/"), |
| 52 | + ) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("NewService: %v", err) |
| 55 | + } |
| 56 | + newGmailService = func(context.Context, string) (*gmail.Service, error) { return svc, nil } |
| 57 | + |
| 58 | + cases := [][]string{ |
| 59 | + {"--plain", "--account", "a@b.com", "gmail", "read", "t1"}, |
| 60 | + {"--plain", "--account", "a@b.com", "gmail", "thread", "t1"}, |
| 61 | + } |
| 62 | + for _, args := range cases { |
| 63 | + out := captureStdout(t, func() { |
| 64 | + _ = captureStderr(t, func() { |
| 65 | + if execErr := Execute(args); execErr != nil { |
| 66 | + t.Fatalf("Execute %v: %v", args, execErr) |
| 67 | + } |
| 68 | + }) |
| 69 | + }) |
| 70 | + if !strings.Contains(out, "Thread contains 1 message(s)") { |
| 71 | + t.Fatalf("unexpected output for %v: %q", args, out) |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments