Skip to content

feat(mail): bot+mailbox=me validation and dynamic --as help tests#895

Merged
xzcong0820 merged 7 commits into
larksuite:mainfrom
xzcong0820:feat/831a0c0
May 19, 2026
Merged

feat(mail): bot+mailbox=me validation and dynamic --as help tests#895
xzcong0820 merged 7 commits into
larksuite:mainfrom
xzcong0820:feat/831a0c0

Conversation

@xzcong0820

@xzcong0820 xzcong0820 commented May 14, 2026

Copy link
Copy Markdown
Collaborator

Summary

Add cross-field validation that rejects the combination of --as bot with the default --mailbox me, and add tests verifying the --as flag usage text reflects the supported identity types dynamically.

Changes

  • shortcuts/mail/helpers.go: add validateBotMailboxNotMe helper — returns a structured validation error when bot identity is used with mailbox=me, which cannot be resolved under a tenant access token
  • shortcuts/mail/mail_message.go, mail_messages.go, mail_thread.go, mail_triage.go: wire validateBotMailboxNotMe into the Validate hook of each shortcut
  • shortcuts/mail/mail_shortcut_validation_test.go: 9 unit tests covering bot+me rejection and explicit-mailbox / user-identity pass-through for all four shortcuts
  • internal/cmdutil/identity_flag_test.go: 2 unit tests verifying --as usage text renders identity type: user or identity type: user | bot according to the declared AuthTypes

Test Plan

  • Unit tests pass (go test ./shortcuts/mail/... ./internal/cmdutil/...)
  • lark-cli mail +message --as bot (no explicit mailbox) exits 2 with validation error
  • lark-cli mail +message --as bot --mailbox alice@example.com passes validation
  • lark-cli mail +watch --help shows identity type: user only
  • lark-cli mail +message --help shows identity type: user | bot

Related Issues

  • None

Summary by CodeRabbit

  • Bug Fixes

    • Mail commands (+message, +messages, +thread, +triage, template create/update) now reject using the default "me" mailbox when running as a bot; an explicit mailbox email is required to proceed.
  • Tests

    • Added comprehensive validation tests for mail shortcuts to verify bot/mailbox constraints and correct error reporting for allowed combinations.

Review Change Stack

Add validateBotMailboxNotMe helper to shortcuts/mail/helpers.go and
wire it as a Validate callback into +message, +messages, +thread and
+triage, so bot identity combined with the default --mailbox me is
rejected early with a clear fixup hint instead of a late opaque API
error.

The --as help text was already dynamic via AddShortcutIdentityFlag;
add TC-10/TC-11 tests in internal/cmdutil/identity_flag_test.go to
pin that behaviour, and TC-1 through TC-9 in
shortcuts/mail/mail_shortcut_validation_test.go to cover the new
Validate callbacks.

+watch is excluded: its AuthTypes is ["user"], so bot is never valid.

sprint: S2
@coderabbitai

coderabbitai Bot commented May 14, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c5384229-bdce-4739-b40d-455ee97e9aa5

📥 Commits

Reviewing files that changed from the base of the PR and between 44fa23e and 677f793.

📒 Files selected for processing (2)
  • shortcuts/mail/mail_template_create.go
  • shortcuts/mail/mail_template_update.go
🚧 Files skipped from review as they are similar to previous changes (2)
  • shortcuts/mail/mail_template_create.go
  • shortcuts/mail/mail_template_update.go

📝 Walkthrough

Walkthrough

Adds tests for --as flag usage text, a helper that rejects bots using mailbox me, wires that validation into mail shortcuts and template create/update validations, and adds tests covering the new validation behavior.

Changes

Bot identity and mailbox validation

Layer / File(s) Summary
Identity flag usage formatting
internal/cmdutil/identity_flag_test.go
Tests verify the --as flag Usage text correctly formats allowed identity types: identity type: user for a single type and identity type: user | bot for multiple types.
Bot mailbox validation helper
shortcuts/mail/helpers.go
validateBotMailboxNotMe checks if the runtime is a bot and the resolved mailbox equals me, returning a structured validation error requiring an explicit email address.
Mailbox validation wiring into shortcuts and templates
shortcuts/mail/mail_message.go, shortcuts/mail/mail_messages.go, shortcuts/mail/mail_thread.go, shortcuts/mail/mail_triage.go, shortcuts/mail/mail_template_create.go, shortcuts/mail/mail_template_update.go
Each mail shortcut adds a Validate hook calling validateBotMailboxNotMe(runtime) before execution; template create/update validation now call the same helper early in their validation flow.
Validation test coverage
shortcuts/mail/mail_shortcut_validation_test.go
Adds assertValidationError and assertValidatePasses helpers plus tests for MailMessage, MailMessages, MailThread, and MailTriage ensuring --as bot with default me fails validation and explicit --mailbox (or --as user) passes.

Sequence Diagram

sequenceDiagram
  participant CLI as CLI Command
  participant Shortcut as Shortcut (e.g., MailMessage)
  participant Validate as validateBotMailboxNotMe
  participant Runtime as RuntimeContext
  participant Output as ExitError

  CLI->>Shortcut: invoke with flags (`--as`, `--mailbox`)
  Shortcut->>Validate: call validateBotMailboxNotMe(runtime)
  Validate->>Runtime: inspect runtime.IsBot() and resolved mailbox
  alt mailbox == "me" and runtime.IsBot()
    Validate->>Output: return ExitValidation error (message about explicit email)
    Output->>CLI: validation error returned
  else valid
    Validate->>Shortcut: return nil
    Shortcut->>CLI: continue to DryRun/Execute
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • chanthuang
  • infeng

Poem

"I'm a rabbit with a tiny key,
I sniff code paths where bots can't be.
'Me' is for users — bots must show mail,
Tests hop the checks so flows won't fail,
🐰 — a little cheer for safer mail."

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically summarizes the main changes: adding bot+mailbox validation and dynamic --as flag help tests for mail shortcuts.
Description check ✅ Passed The description covers all required template sections with detailed information about motivation, specific file changes, comprehensive test plan with checkmarks, and related issues.
Docstring Coverage ✅ Passed Docstring coverage is 94.44% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot added domain/mail PR touches the mail domain size/M Single-domain feat or fix with limited business impact labels May 14, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
internal/cmdutil/identity_flag_test.go (2)

71-89: 💤 Low value

Consider adding visibility check for consistency.

The test properly validates the usage text, but existing tests in this file (lines 63-65) also verify that flag.Hidden is false. Adding this check would improve consistency with the established test pattern.

🔍 Optional consistency enhancement
 	if flag == nil {
 		t.Fatal("expected --as flag to be registered")
 	}
+	if flag.Hidden {
+		t.Fatal("expected --as flag to be visible")
+	}
 	wantUsage := "identity type: user"
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/cmdutil/identity_flag_test.go` around lines 71 - 89, The test
TestAddShortcutIdentityFlag_UserOnlyAuthTypes should also assert the flag's
visibility like the other tests: after retrieving the flag via
cmd.Flags().Lookup("as") add a check that flag.Hidden is false (or call
t.Fatalf/t.Errorf with a clear message if it is true) to ensure the --as flag is
not hidden; update this in the test alongside the existing Usage assertions and
reference AddShortcutIdentityFlag and the flag variable for locating the change.

91-106: 💤 Low value

Consider adding visibility and default value checks for consistency.

The test properly validates the usage text, but existing tests (lines 63-68) also verify flag.Hidden is false and the default value is empty. Adding these checks would improve completeness and consistency with the established test pattern.

🔍 Optional consistency enhancement
 	if flag == nil {
 		t.Fatal("expected --as flag to be registered")
 	}
+	if flag.Hidden {
+		t.Fatal("expected --as flag to be visible")
+	}
+	if got := flag.DefValue; got != "" {
+		t.Fatalf("default value = %q, want empty string", got)
+	}
 	wantUsage := "identity type: user | bot"
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/cmdutil/identity_flag_test.go` around lines 91 - 106, The test
TestAddShortcutIdentityFlag_UserBotAuthTypes currently only checks the --as flag
Usage; update it to also assert the flag's visibility and default value by
verifying flag.Hidden is false and flag.Value.String() is empty. Locate the test
function TestAddShortcutIdentityFlag_UserBotAuthTypes and after retrieving flag
:= cmd.Flags().Lookup("as"), add assertions similar to the existing pattern (as
in the other tests around lines 63-68) to check flag.Hidden == false and
flag.Value.String() == "" to ensure consistent coverage for
AddShortcutIdentityFlag.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@internal/cmdutil/identity_flag_test.go`:
- Around line 71-89: The test TestAddShortcutIdentityFlag_UserOnlyAuthTypes
should also assert the flag's visibility like the other tests: after retrieving
the flag via cmd.Flags().Lookup("as") add a check that flag.Hidden is false (or
call t.Fatalf/t.Errorf with a clear message if it is true) to ensure the --as
flag is not hidden; update this in the test alongside the existing Usage
assertions and reference AddShortcutIdentityFlag and the flag variable for
locating the change.
- Around line 91-106: The test TestAddShortcutIdentityFlag_UserBotAuthTypes
currently only checks the --as flag Usage; update it to also assert the flag's
visibility and default value by verifying flag.Hidden is false and
flag.Value.String() is empty. Locate the test function
TestAddShortcutIdentityFlag_UserBotAuthTypes and after retrieving flag :=
cmd.Flags().Lookup("as"), add assertions similar to the existing pattern (as in
the other tests around lines 63-68) to check flag.Hidden == false and
flag.Value.String() == "" to ensure consistent coverage for
AddShortcutIdentityFlag.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 34c46c79-140e-4b19-89de-318bc743ed12

📥 Commits

Reviewing files that changed from the base of the PR and between 7acf64c and 74234de.

📒 Files selected for processing (7)
  • internal/cmdutil/identity_flag_test.go
  • shortcuts/mail/helpers.go
  • shortcuts/mail/mail_message.go
  • shortcuts/mail/mail_messages.go
  • shortcuts/mail/mail_shortcut_validation_test.go
  • shortcuts/mail/mail_thread.go
  • shortcuts/mail/mail_triage.go

@codecov

codecov Bot commented May 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.81818% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.19%. Comparing base (7acf64c) to head (677f793).
⚠️ Report is 31 commits behind head on main.

Files with missing lines Patch % Lines
shortcuts/mail/mail_template_create.go 0.00% 1 Missing and 1 partial ⚠️
shortcuts/mail/mail_template_update.go 0.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #895      +/-   ##
==========================================
+ Coverage   65.90%   67.19%   +1.29%     
==========================================
  Files         520      569      +49     
  Lines       49274    53530    +4256     
==========================================
+ Hits        32474    35970    +3496     
- Misses      14026    14544     +518     
- Partials     2774     3016     +242     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

github-actions Bot commented May 14, 2026

Copy link
Copy Markdown

🚀 PR Preview Install Guide

🧰 CLI update

npm i -g https://pkg.pr.new/larksuite/cli/@larksuite/cli@677f79333a70e4aefeb064359bf10fe092bca3a9

🧩 Skill update

npx skills add xzcong0820/larksuite-cli#feat/831a0c0 -y -g

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@shortcuts/mail/mail_template_update.go`:
- Around line 85-90: In the Validate function, skip mailbox validation when the
local-only flag is set by checking runtime.Bool("print-patch-template") before
calling validateBotMailboxNotMe; either move the existing if
runtime.Bool("print-patch-template") early or add a guard so
validateBotMailboxNotMe(runtime) is not invoked when
runtime.Bool("print-patch-template") is true, referencing the Validate function
and validateBotMailboxNotMe and the "print-patch-template" runtime flag.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e537865a-6870-4d3c-b358-cfdafbe4ca02

📥 Commits

Reviewing files that changed from the base of the PR and between ebf3e3a and 44fa23e.

📒 Files selected for processing (2)
  • shortcuts/mail/mail_template_create.go
  • shortcuts/mail/mail_template_update.go

Comment thread shortcuts/mail/mail_template_update.go
@xzcong0820
xzcong0820 merged commit 0e70b05 into larksuite:main May 19, 2026
18 checks passed
@liangshuo-1 liangshuo-1 mentioned this pull request May 19, 2026
2 tasks
tuxedomm pushed a commit to zhumiaoxin/cli that referenced this pull request Jun 6, 2026
…rksuite#895)

* feat(mail): bot+mailbox=me validation and dynamic --as help tests

Add validateBotMailboxNotMe helper to shortcuts/mail/helpers.go and
wire it as a Validate callback into +message, +messages, +thread and
+triage, so bot identity combined with the default --mailbox me is
rejected early with a clear fixup hint instead of a late opaque API
error.

The --as help text was already dynamic via AddShortcutIdentityFlag;
add TC-10/TC-11 tests in internal/cmdutil/identity_flag_test.go to
pin that behaviour, and TC-1 through TC-9 in
shortcuts/mail/mail_shortcut_validation_test.go to cover the new
Validate callbacks.

+watch is excluded: its AuthTypes is ["user"], so bot is never valid.

sprint: S2

* test(cmdutil): add Hidden and DefValue assertions to identity flag tests

* fix(mail): add bot+mailbox=me validation to +template-create and +template-update

* fix(mail): add bot+mailbox=me validation to +template-update

* fix(mail): gofmt mail_template_create.go

* fix(mail): gofmt mail_template_update.go

* fix(mail): skip bot+mailbox=me check for print-patch-template local path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

domain/mail PR touches the mail domain size/M Single-domain feat or fix with limited business impact

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants