Files
larksuite-cli/shortcuts/mail/mail_share_to_chat_test.go
chanthuang 2e7a11a8e8 feat(mail): support sharing emails to IM chats (#637)
* feat(mail): add +share-to-chat shortcut to share emails as IM cards

Two-step API (create share token → send card) wrapped in a single
shortcut. Supports message-id/thread-id, five receive-id-type variants
(chat_id, open_id, user_id, union_id, email), and dry-run mode.

Change-Id: Ic7b8c01c0d25fef262f35be92555f1fd019bd679
Co-Authored-By: AI

* fix(mail): regenerate SKILL.md from skill-template instead of manual edit

Add missing safety rule 8 (draft link rule) to skill-template/domains/mail.md
so it survives regeneration. SKILL.md is now produced by `make gen-skills`
in the registry repo rather than hand-edited.

Change-Id: I9cf3605deae8b6de2042e40819fedc304967e78e
Co-Authored-By: AI

* fix(mail): add docstrings and use real validation path in tests

- Add Go doc comments to exported symbols for docstring coverage
- Rewrite tests to exercise MailShareToChat.Validate via RuntimeContext
  instead of duplicating validation logic
- Replace hand-rolled containsStr with strings.Contains
- Add httpmock stubs for execute and error path tests

Change-Id: Ic781494f61e9e844224185844bce7b0c48e8e200
Co-Authored-By: AI

* test(mail): add dry-run E2E test for +share-to-chat

Validate request shape (method, URL, mailbox path) under --dry-run
with fake credentials. Covers message-id, thread-id, and custom
mailbox variants.

Change-Id: Iae87bf141cbe4f312d3e9b1fca4ba175052c5c35
Co-Authored-By: AI

* fix(mail): include request body and params in dry-run output

DryRun now mirrors Execute: the share-token POST shows message_id or
thread_id, and the send POST shows receive_id_type and receive_id.
E2E test updated to assert these fields. Also fix strconv.Itoa usage.

Change-Id: I00f8770fd5a12b7354986c5e5077f97cfe5d6653

* style(mail): gofmt dry-run test file

Change-Id: I47dc6a9a47252dcfb7853737f88dfdaef65a0ae7

* test(mail): assert exact API call count in dry-run test

Change-Id: I9f4a1a183b55d03f5248eb4adddfddb08037ca95
2026-04-24 21:11:48 +08:00

191 lines
5.2 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package mail
import (
"strings"
"testing"
"github.com/larksuite/cli/internal/httpmock"
)
func TestShareToChatValidationErrors(t *testing.T) {
tests := []struct {
name string
args []string
wantErr string
}{
{
name: "missing both message-id and thread-id",
args: []string{"+share-to-chat", "--receive-id", "oc_xxx"},
wantErr: "either --message-id or --thread-id is required",
},
{
name: "both message-id and thread-id",
args: []string{"+share-to-chat", "--message-id", "m1", "--thread-id", "t1", "--receive-id", "oc_xxx"},
wantErr: "--message-id and --thread-id are mutually exclusive",
},
{
name: "invalid receive-id-type",
args: []string{"+share-to-chat", "--message-id", "m1", "--receive-id", "oc_xxx", "--receive-id-type", "invalid"},
wantErr: "--receive-id-type must be one of",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, stdout, _, _ := mailShortcutTestFactory(t)
err := runMountedMailShortcut(t, MailShareToChat, tt.args, f, stdout)
if err == nil {
t.Fatalf("expected error containing %q, got nil", tt.wantErr)
}
if !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("expected error containing %q, got %q", tt.wantErr, err.Error())
}
})
}
}
func TestShareToChatExecuteWithMessageID(t *testing.T) {
f, stdout, _, reg := mailShortcutTestFactory(t)
reg.Register(&httpmock.Stub{
Method: "POST",
URL: "/user_mailboxes/me/messages/share_token",
Body: map[string]interface{}{
"code": 0,
"data": map[string]interface{}{
"card_id": "card_001",
},
},
})
reg.Register(&httpmock.Stub{
Method: "POST",
URL: "/user_mailboxes/me/share_tokens/card_001/send",
Body: map[string]interface{}{
"code": 0,
"data": map[string]interface{}{
"message_id": "om_001",
},
},
})
err := runMountedMailShortcut(t, MailShareToChat, []string{
"+share-to-chat", "--message-id", "m1", "--receive-id", "oc_xxx",
}, f, stdout)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
out := stdout.String()
if !strings.Contains(out, "card_001") {
t.Errorf("expected output to contain card_id, got %s", out)
}
if !strings.Contains(out, "om_001") {
t.Errorf("expected output to contain im_message_id, got %s", out)
}
}
func TestShareToChatExecuteWithThreadID(t *testing.T) {
f, stdout, _, reg := mailShortcutTestFactory(t)
reg.Register(&httpmock.Stub{
Method: "POST",
URL: "/user_mailboxes/me/messages/share_token",
Body: map[string]interface{}{
"code": 0,
"data": map[string]interface{}{
"card_id": "card_002",
},
},
})
reg.Register(&httpmock.Stub{
Method: "POST",
URL: "/user_mailboxes/me/share_tokens/card_002/send",
Body: map[string]interface{}{
"code": 0,
"data": map[string]interface{}{
"message_id": "om_002",
},
},
})
err := runMountedMailShortcut(t, MailShareToChat, []string{
"+share-to-chat", "--thread-id", "t1", "--receive-id", "user@example.com", "--receive-id-type", "email",
}, f, stdout)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
out := stdout.String()
if !strings.Contains(out, "card_002") {
t.Errorf("expected output to contain card_id, got %s", out)
}
}
func TestShareToChatStep1Failure(t *testing.T) {
f, stdout, _, reg := mailShortcutTestFactory(t)
reg.Register(&httpmock.Stub{
Method: "POST",
URL: "/user_mailboxes/me/messages/share_token",
Body: map[string]interface{}{
"code": 4034,
"msg": "message not found",
},
})
err := runMountedMailShortcut(t, MailShareToChat, []string{
"+share-to-chat", "--message-id", "bad_id", "--receive-id", "oc_xxx",
}, f, stdout)
if err == nil {
t.Fatal("expected error for step 1 failure, got nil")
}
if !strings.Contains(err.Error(), "create share token") {
t.Errorf("expected error to mention 'create share token', got %q", err.Error())
}
}
func TestShareToChatStep2Failure(t *testing.T) {
f, stdout, _, reg := mailShortcutTestFactory(t)
reg.Register(&httpmock.Stub{
Method: "POST",
URL: "/user_mailboxes/me/messages/share_token",
Body: map[string]interface{}{
"code": 0,
"data": map[string]interface{}{
"card_id": "card_003",
},
},
})
reg.Register(&httpmock.Stub{
Method: "POST",
URL: "/user_mailboxes/me/share_tokens/card_003/send",
Body: map[string]interface{}{
"code": 4046,
"msg": "user not in chat",
},
})
err := runMountedMailShortcut(t, MailShareToChat, []string{
"+share-to-chat", "--message-id", "m1", "--receive-id", "oc_not_in",
}, f, stdout)
if err == nil {
t.Fatal("expected error for step 2 failure, got nil")
}
if !strings.Contains(err.Error(), "card_003") {
t.Errorf("expected error to contain card_id, got %q", err.Error())
}
if !strings.Contains(err.Error(), "send failed") {
t.Errorf("expected error to mention 'send failed', got %q", err.Error())
}
}
func TestValidReceiveIDTypes(t *testing.T) {
expected := []string{"chat_id", "open_id", "user_id", "union_id", "email"}
for _, typ := range expected {
if !validReceiveIDTypes[typ] {
t.Errorf("expected %q to be a valid receive ID type", typ)
}
}
if validReceiveIDTypes["invalid"] {
t.Error("expected 'invalid' to not be a valid receive ID type")
}
}