gogcli/internal/cmd/gmail_thread_validation_more_test.go
2026-01-09 09:18:16 +01:00

93 lines
2.6 KiB
Go

package cmd
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"google.golang.org/api/gmail/v1"
"google.golang.org/api/option"
"github.com/steipete/gogcli/internal/outfmt"
"github.com/steipete/gogcli/internal/ui"
)
func TestGmailThreadGet_ValidationErrors(t *testing.T) {
u, uiErr := ui.New(ui.Options{Stdout: io.Discard, Stderr: io.Discard, Color: "never"})
if uiErr != nil {
t.Fatalf("ui.New: %v", uiErr)
}
ctx := ui.WithUI(context.Background(), u)
if err := (&GmailThreadGetCmd{}).Run(ctx, &RootFlags{}); err == nil {
t.Fatalf("expected missing account error")
}
if err := (&GmailThreadGetCmd{}).Run(ctx, &RootFlags{Account: "a@b.com"}); err == nil {
t.Fatalf("expected missing threadId error")
}
}
func TestGmailThreadModify_ValidationErrors(t *testing.T) {
u, uiErr := ui.New(ui.Options{Stdout: io.Discard, Stderr: io.Discard, Color: "never"})
if uiErr != nil {
t.Fatalf("ui.New: %v", uiErr)
}
ctx := ui.WithUI(context.Background(), u)
flags := &RootFlags{Account: "a@b.com"}
if err := (&GmailThreadModifyCmd{}).Run(ctx, flags); err == nil {
t.Fatalf("expected missing threadId error")
}
if err := (&GmailThreadModifyCmd{ThreadID: "t1"}).Run(ctx, flags); err == nil {
t.Fatalf("expected missing labels error")
}
}
func TestGmailThreadAttachments_EmptyThread_JSON(t *testing.T) {
origNew := newGmailService
t.Cleanup(func() { newGmailService = origNew })
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, "/gmail/v1/users/me/threads/t1") && r.Method == http.MethodGet {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"id": "t1",
"messages": []map[string]any{},
})
return
}
http.NotFound(w, r)
}))
defer srv.Close()
svc, err := gmail.NewService(context.Background(),
option.WithoutAuthentication(),
option.WithHTTPClient(srv.Client()),
option.WithEndpoint(srv.URL+"/"),
)
if err != nil {
t.Fatalf("NewService: %v", err)
}
newGmailService = func(context.Context, string) (*gmail.Service, error) { return svc, nil }
u, uiErr := ui.New(ui.Options{Stdout: io.Discard, Stderr: io.Discard, Color: "never"})
if uiErr != nil {
t.Fatalf("ui.New: %v", uiErr)
}
ctx := outfmt.WithMode(ui.WithUI(context.Background(), u), outfmt.Mode{JSON: true})
flags := &RootFlags{Account: "a@b.com"}
out := captureStdout(t, func() {
if err := (&GmailThreadAttachmentsCmd{ThreadID: "t1"}).Run(ctx, flags); err != nil {
t.Fatalf("attachments: %v", err)
}
})
if !strings.Contains(out, "\"attachments\"") {
t.Fatalf("unexpected output: %q", out)
}
}