Skip to content

Commit 2185a33

Browse files
test(gmail): add --from display name fallback to list test
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 1b83601 commit 2185a33

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

internal/cmd/gmail_send_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,82 @@ func TestGmailSendCmd_RunJSON_WithFrom(t *testing.T) {
316316
}
317317
}
318318

319+
func TestGmailSendCmd_RunJSON_WithFromDisplayNameFallbackToList(t *testing.T) {
320+
origNew := newGmailService
321+
t.Cleanup(func() { newGmailService = origNew })
322+
323+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
324+
path := strings.TrimPrefix(r.URL.Path, "/gmail/v1")
325+
switch {
326+
case r.Method == http.MethodGet && path == "/users/me/settings/sendAs/alias@example.com":
327+
// Return send-as settings with empty display name but valid verification.
328+
w.Header().Set("Content-Type", "application/json")
329+
_ = json.NewEncoder(w).Encode(map[string]any{
330+
"sendAsEmail": "alias@example.com",
331+
"displayName": "",
332+
"verificationStatus": "accepted",
333+
})
334+
return
335+
case r.Method == http.MethodGet && path == "/users/me/settings/sendAs":
336+
// Fallback list endpoint returns the alias with a populated display name.
337+
w.Header().Set("Content-Type", "application/json")
338+
_ = json.NewEncoder(w).Encode(map[string]any{
339+
"sendAs": []map[string]any{
340+
{
341+
"sendAsEmail": "alias@example.com",
342+
"displayName": "Alias From List",
343+
"verificationStatus": "accepted",
344+
},
345+
},
346+
})
347+
return
348+
case r.Method == http.MethodPost && path == "/users/me/messages/send":
349+
w.Header().Set("Content-Type", "application/json")
350+
_ = json.NewEncoder(w).Encode(map[string]any{
351+
"id": "m2b",
352+
"threadId": "t2b",
353+
})
354+
return
355+
default:
356+
http.NotFound(w, r)
357+
return
358+
}
359+
}))
360+
defer srv.Close()
361+
362+
svc, err := gmail.NewService(context.Background(),
363+
option.WithoutAuthentication(),
364+
option.WithHTTPClient(srv.Client()),
365+
option.WithEndpoint(srv.URL+"/"),
366+
)
367+
if err != nil {
368+
t.Fatalf("NewService: %v", err)
369+
}
370+
newGmailService = func(context.Context, string) (*gmail.Service, error) { return svc, nil }
371+
372+
u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"})
373+
if err != nil {
374+
t.Fatalf("ui.New: %v", err)
375+
}
376+
ctx := outfmt.WithMode(ui.WithUI(context.Background(), u), outfmt.Mode{JSON: true})
377+
378+
cmd := &GmailSendCmd{
379+
To: "a@example.com",
380+
From: "alias@example.com",
381+
Subject: "Hello",
382+
Body: "Body",
383+
}
384+
385+
out := captureStdout(t, func() {
386+
if err := cmd.Run(ctx, &RootFlags{Account: "a@b.com"}); err != nil {
387+
t.Fatalf("Run: %v", err)
388+
}
389+
})
390+
if !strings.Contains(out, "\"from\"") || !strings.Contains(out, "Alias From List <alias@example.com>") {
391+
t.Fatalf("expected from with display name from list fallback, got: %q", out)
392+
}
393+
}
394+
319395
func TestGmailSendCmd_RunJSON_PrimaryAccountDisplayName(t *testing.T) {
320396
origNew := newGmailService
321397
t.Cleanup(func() { newGmailService = origNew })

0 commit comments

Comments
 (0)