mirror of
https://github.com/larksuite/cli.git
synced 2026-07-06 00:06:28 +08:00
Add `lark-cli mail +draft-send` shortcut that takes one or more existing
draft IDs and sends each via POST /drafts/:draft_id/send sequentially.
Per-draft failures are isolated and aggregated into a structured output;
fatal failures (auth, permission, network, mailbox quota) abort the
entire batch immediately while recoverable failures honor --stop-on-error.
Also extend internal/output with six mail-send-specific errno constants
(LarkErrMailboxNotFound=4013, LarkErrMailSendQuota{User,UserExt,TenantExt},
LarkErrMailQuota, LarkErrTenantStorageLimit) consumed by isFatalSendErr.
Risk is "high-risk-write" so the framework's --yes gate applies; the
shortcut declares only the minimal mail:user_mailbox.message:send scope
to avoid asking users for permissions it does not need.
140 lines
4.1 KiB
Go
140 lines
4.1 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package output
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestClassifyLarkError_DriveCreateShortcutConstraints verifies known Drive shortcut errors map to actionable hints.
|
|
func TestClassifyLarkError_DriveCreateShortcutConstraints(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
code int
|
|
wantExitCode int
|
|
wantType string
|
|
wantHint string
|
|
}{
|
|
{
|
|
name: "resource contention",
|
|
code: LarkErrDriveResourceContention,
|
|
wantExitCode: ExitAPI,
|
|
wantType: "conflict",
|
|
wantHint: "avoid concurrent duplicate requests",
|
|
},
|
|
{
|
|
name: "cross tenant unit",
|
|
code: LarkErrDriveCrossTenantUnit,
|
|
wantExitCode: ExitAPI,
|
|
wantType: "cross_tenant",
|
|
wantHint: "same tenant and region/unit",
|
|
},
|
|
{
|
|
name: "cross brand",
|
|
code: LarkErrDriveCrossBrand,
|
|
wantExitCode: ExitAPI,
|
|
wantType: "cross_brand",
|
|
wantHint: "same brand environment",
|
|
},
|
|
{
|
|
name: "sheets float image invalid dims",
|
|
code: LarkErrSheetsFloatImageInvalidDims,
|
|
wantExitCode: ExitAPI,
|
|
wantType: "invalid_parameters",
|
|
wantHint: "--width / --height / --offset-x / --offset-y",
|
|
},
|
|
{
|
|
name: "drive permission apply rate limit",
|
|
code: LarkErrDrivePermApplyRateLimit,
|
|
wantExitCode: ExitAPI,
|
|
wantType: "rate_limit",
|
|
wantHint: "5 times per day",
|
|
},
|
|
{
|
|
name: "drive permission apply not applicable",
|
|
code: LarkErrDrivePermApplyNotApplicable,
|
|
wantExitCode: ExitAPI,
|
|
wantType: "invalid_parameters",
|
|
wantHint: "does not accept a permission-apply request",
|
|
},
|
|
{
|
|
name: "ownership mismatch",
|
|
code: LarkErrOwnershipMismatch,
|
|
wantExitCode: ExitAPI,
|
|
wantType: "ownership_mismatch",
|
|
wantHint: "messages-resources-download",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
gotExitCode, gotType, gotHint := ClassifyLarkError(tt.code, "raw msg")
|
|
if gotExitCode != tt.wantExitCode {
|
|
t.Fatalf("exitCode=%d, want %d", gotExitCode, tt.wantExitCode)
|
|
}
|
|
if gotType != tt.wantType {
|
|
t.Fatalf("type=%q, want %q", gotType, tt.wantType)
|
|
}
|
|
if gotHint == "" {
|
|
t.Fatal("expected non-empty hint")
|
|
}
|
|
if !strings.Contains(gotHint, tt.wantHint) {
|
|
t.Fatalf("hint=%q, want substring %q", gotHint, tt.wantHint)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMailSendErrorConstantsUseServiceScopedCodes(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
got int
|
|
want int
|
|
}{
|
|
{name: "mailbox not found", got: LarkErrMailboxNotFound, want: 1234013},
|
|
{name: "user daily send quota", got: LarkErrMailSendQuotaUser, want: 1236007},
|
|
{name: "user external recipient quota", got: LarkErrMailSendQuotaUserExt, want: 1236008},
|
|
{name: "tenant external recipient quota", got: LarkErrMailSendQuotaTenantExt, want: 1236009},
|
|
{name: "mail quota", got: LarkErrMailQuota, want: 1236010},
|
|
{name: "tenant storage limit", got: LarkErrTenantStorageLimit, want: 1236013},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
if tt.got != tt.want {
|
|
t.Fatalf("code=%d, want %d", tt.got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestClassifyLarkError_WikiLockContention verifies the wiki write-lock
|
|
// contention error (131009) maps to an actionable retry hint instead of
|
|
// a generic "api_error". Surfaces during concurrent wiki +node-create
|
|
// against the same parent (see larksuite/cli#1012).
|
|
func TestClassifyLarkError_WikiLockContention(t *testing.T) {
|
|
t.Parallel()
|
|
gotExitCode, gotType, gotHint := ClassifyLarkError(LarkErrWikiLockContention, "raw msg")
|
|
if gotExitCode != ExitAPI {
|
|
t.Fatalf("exitCode=%d, want %d", gotExitCode, ExitAPI)
|
|
}
|
|
if gotType != "conflict" {
|
|
t.Fatalf("type=%q, want %q", gotType, "conflict")
|
|
}
|
|
if !strings.Contains(gotHint, "wiki write lock") {
|
|
t.Fatalf("hint=%q, want substring %q", gotHint, "wiki write lock")
|
|
}
|
|
if !strings.Contains(gotHint, "backoff") {
|
|
t.Fatalf("hint=%q, want substring %q", gotHint, "backoff")
|
|
}
|
|
}
|