Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions mailcmd/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,58 @@ func TestReadCommand_Success(t *testing.T) {
})
}

func syntheticQuotedReplyMessage(id string) *gmailapi.Message {
return &gmailapi.Message{
ID: id,
Subject: "Synthetic subject",
Body: "Authored reply.\n\nOn [date], [author] wrote:\n> quoted history",
}
}

func TestReadCommand_ElidesQuotedReplyByDefault(t *testing.T) {
mock := &MockGmailClient{
GetMessageFunc: func(_ context.Context, messageID string, includeBody bool) (*gmailapi.Message, error) {
testutil.Equal(t, messageID, "synthetic-message")
testutil.True(t, includeBody)
return syntheticQuotedReplyMessage(messageID), nil
},
}

cmd := newReadCommand()
cmd.SetArgs([]string{"synthetic-message"})

withMockClient(mock, func() {
output := testutil.CaptureStdout(t, func() {
testutil.NoError(t, cmd.Execute())
})

testutil.Contains(t, output, "Authored reply.")
testutil.Contains(t, output, quotedReplyMarkerForTest)
testutil.NotContains(t, output, "quoted history")
})
}

func TestReadCommand_IncludesQuotedReplyBodies(t *testing.T) {
mock := &MockGmailClient{
GetMessageFunc: func(_ context.Context, messageID string, includeBody bool) (*gmailapi.Message, error) {
testutil.True(t, includeBody)
return syntheticQuotedReplyMessage(messageID), nil
},
}

cmd := newReadCommand()
cmd.SetArgs([]string{"synthetic-message", "--include-quoted-reply-bodies"})

withMockClient(mock, func() {
output := testutil.CaptureStdout(t, func() {
testutil.NoError(t, cmd.Execute())
})

testutil.Contains(t, output, "Authored reply.\n\nOn [date], [author] wrote:\n> quoted history")
testutil.NotContains(t, output, quotedReplyMarkerForTest)
})
}

func TestReadCommand_NotFound(t *testing.T) {
mock := &MockGmailClient{
GetMessageFunc: func(_ context.Context, _ string, _ bool) (*gmailapi.Message, error) {
Expand Down Expand Up @@ -206,6 +258,48 @@ func TestThreadCommand_Success(t *testing.T) {
})
}

func TestThreadCommand_ElidesQuotedReplyByDefault(t *testing.T) {
mock := &MockGmailClient{
GetThreadFunc: func(_ context.Context, id string) ([]*gmailapi.Message, error) {
testutil.Equal(t, id, "synthetic-thread")
return []*gmailapi.Message{syntheticQuotedReplyMessage("synthetic-message")}, nil
},
}

cmd := newThreadCommand()
cmd.SetArgs([]string{"synthetic-thread"})

withMockClient(mock, func() {
output := testutil.CaptureStdout(t, func() {
testutil.NoError(t, cmd.Execute())
})

testutil.Contains(t, output, "Authored reply.")
testutil.Contains(t, output, quotedReplyMarkerForTest)
testutil.NotContains(t, output, "quoted history")
})
}

func TestThreadCommand_IncludesQuotedReplyBodies(t *testing.T) {
mock := &MockGmailClient{
GetThreadFunc: func(_ context.Context, _ string) ([]*gmailapi.Message, error) {
return []*gmailapi.Message{syntheticQuotedReplyMessage("synthetic-message")}, nil
},
}

cmd := newThreadCommand()
cmd.SetArgs([]string{"synthetic-thread", "--include-quoted-reply-bodies"})

withMockClient(mock, func() {
output := testutil.CaptureStdout(t, func() {
testutil.NoError(t, cmd.Execute())
})

testutil.Contains(t, output, "Authored reply.\n\nOn [date], [author] wrote:\n> quoted history")
testutil.NotContains(t, output, quotedReplyMarkerForTest)
})
}

func TestLabelsCommand_Success(t *testing.T) {
mock := &MockGmailClient{
FetchLabelsFunc: func(_ context.Context) error {
Expand Down
15 changes: 10 additions & 5 deletions mailcmd/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ func newGmailClient(ctx context.Context) (MailClient, error) {

// MessagePrintOptions controls which fields to include in message output
type MessagePrintOptions struct {
IncludeThreadID bool
IncludeTo bool
IncludeSnippet bool
IncludeBody bool
IncludeThreadID bool
IncludeTo bool
IncludeSnippet bool
IncludeBody bool
IncludeQuotedReplyBodies bool
}

// printMessageHeader prints the common header fields of a message
Expand All @@ -70,7 +71,11 @@ func printMessageHeader(msg *gmail.Message, opts MessagePrintOptions) {
fmt.Printf("Snippet: %s\n", SanitizeOutput(msg.Snippet))
}
if opts.IncludeBody {
body := msg.Body
if !opts.IncludeQuotedReplyBodies {
body, _ = elideQuotedReplyBody(body, msg.BodyIsHTML)
}
fmt.Print("\n--- Body ---\n\n")
fmt.Println(SanitizeOutput(msg.Body))
fmt.Println(SanitizeOutput(body))
}
}
25 changes: 25 additions & 0 deletions mailcmd/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mailcmd
import (
"testing"

"github.com/open-cli-collective/google-cli-common/gmail"
"github.com/open-cli-collective/google-cli-common/testutil"
)

Expand All @@ -15,6 +16,7 @@ func TestMessagePrintOptions(t *testing.T) {
testutil.False(t, opts.IncludeTo)
testutil.False(t, opts.IncludeSnippet)
testutil.False(t, opts.IncludeBody)
testutil.False(t, opts.IncludeQuotedReplyBodies)
})

t.Run("options can be set individually", func(t *testing.T) {
Expand All @@ -27,5 +29,28 @@ func TestMessagePrintOptions(t *testing.T) {
testutil.False(t, opts.IncludeTo)
testutil.False(t, opts.IncludeSnippet)
testutil.True(t, opts.IncludeBody)
opts.IncludeQuotedReplyBodies = true
testutil.True(t, opts.IncludeQuotedReplyBodies)
})
}

func TestPrintMessageHeader_HTMLQuoteModes(t *testing.T) {
body := `<p>Authored reply.</p><div class="gmail_quote"><p>Older message</p></div>`
msg := &gmail.Message{ID: "synthetic-message", Body: body, BodyIsHTML: true}

defaultOutput := testutil.CaptureStdout(t, func() {
printMessageHeader(msg, MessagePrintOptions{IncludeBody: true})
})
testutil.Contains(t, defaultOutput, "Authored reply.")
testutil.Contains(t, defaultOutput, quotedReplyMarkerForTest)
testutil.NotContains(t, defaultOutput, "Older message")

includedOutput := testutil.CaptureStdout(t, func() {
printMessageHeader(msg, MessagePrintOptions{
IncludeBody: true,
IncludeQuotedReplyBodies: true,
})
})
testutil.Contains(t, includedOutput, body)
testutil.NotContains(t, includedOutput, quotedReplyMarkerForTest)
}
Loading
Loading