Skip to content

fix: omit zero total from chat search#2035

Open
arnold9672 wants to merge 3 commits into
larksuite:mainfrom
arnold9672:fix/chat-search-total
Open

fix: omit zero total from chat search#2035
arnold9672 wants to merge 3 commits into
larksuite:mainfrom
arnold9672:fix/chat-search-total

Conversation

@arnold9672

@arnold9672 arnold9672 commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix im +chat-search output when the upstream API omits total or returns
zero. The CLI now avoids reporting total: 0 alongside non-empty results and
keeps positive upstream totals unchanged.

Changes

  • Omit data.total when the upstream value is missing, invalid, or zero.
  • Use the number of displayed chats for the pretty-output footer when no
    positive upstream total is available.
  • Add regression coverage for missing, invalid, zero, and positive totals.
  • Add dry-run coverage for the chat-search request contract.

Test Plan

  • Focused shortcuts/im contract tests pass.
  • Chat-search dry-run request contract test passes.
  • go vet ./... passes.
  • gofmt -l and git diff --check pass.
  • make unit-test is fully green. The affected shortcuts/im package
    passes, but the full suite is blocked by unrelated host-environment failures:
    the test Git user config is overridden by the global config, and the system
    Git does not support git init --initial-branch.

Related Issues

  • None

sa: none
doc: none
cfg: none
test: unit test

Summary by CodeRabbit

  • Bug Fixes

    • Improved im +chat-search handling of the API-provided total when it’s missing, invalid, zero, or fractional.
    • total is now omitted from structured output unless it’s a positive value.
    • The “chat(s) found” message continues to display the parsed count from total.
  • Tests

    • Added unit coverage to verify the total field contract across json and pretty modes.
    • Added an end-to-end dry-run CLI test validating the generated request, filters, and pagination parameters.

sa: none
doc: none
cfg: none
test: unit test
@github-actions github-actions Bot added domain/im PR touches the im domain size/M Single-domain feat or fix with limited business impact labels Jul 23, 2026
@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The chat search shortcut conditionally includes the total field only when the truncated API total is positive. Tests cover total-field handling and dry-run request construction.

Changes

Chat Search Total Handling

Layer / File(s) Summary
Conditional total output
shortcuts/im/im_chat_search.go
API totals are parsed as floats, and the structured response includes total only when its truncated value is positive.
Total and request validation
shortcuts/im/im_chat_search_total_test.go, tests/cli_e2e/im/im_chat_search_dryrun_test.go
Tests cover missing, invalid, zero, fractional, and positive totals, and validate dry-run method, URL, filters, and pagination parameters.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested labels: enhancement

Suggested reviewers: shifengjuan-dev

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: omitting zero total from chat search output.
Description check ✅ Passed The description follows the template with Summary, Changes, Test Plan, and Related Issues sections filled in.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ 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.

@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/im/im_chat_search.go`:
- Around line 110-111: The availability predicate in util.ToFloat64 handling
within shortcuts/im/im_chat_search.go lines 110-111 must use int(total) > 0 so
fractional totals such as 0.5 are unavailable after output truncation. In
shortcuts/im/im_chat_search_total_test.go lines 22-25 and 80-82, add coverage
for total: 0.5 in both JSON and pretty output modes, asserting the resulting
zero output.
🪄 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 Plus

Run ID: e4b392d9-7365-48b2-8859-df5246012611

📥 Commits

Reviewing files that changed from the base of the PR and between 1e682bd and a7c59f1.

📒 Files selected for processing (3)
  • shortcuts/im/im_chat_search.go
  • shortcuts/im/im_chat_search_total_test.go
  • tests/cli_e2e/im/im_chat_search_dryrun_test.go

Comment thread shortcuts/im/im_chat_search.go Outdated

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
shortcuts/im/im_chat_search.go (1)

203-203: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Fall back to the displayed-chat count when the upstream total is unavailable.

Line 203 still prints int(total) unconditionally, so missing, invalid, zero, or sub-unit totals produce 0 chat(s) found even when items contains displayed chats. Use len(items) unless hasPositiveTotal is true.

Proposed fix
-			fmt.Fprintf(w, "\n%d chat(s) found%s\n", int(total), moreHint)
+			displayedTotal := len(items)
+			if hasPositiveTotal {
+				displayedTotal = int(total)
+			}
+			fmt.Fprintf(w, "\n%d chat(s) found%s\n", displayedTotal, moreHint)
🤖 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 `@shortcuts/im/im_chat_search.go` at line 203, Update the result-count output
around the Fprintf call to use the upstream total only when hasPositiveTotal is
true; otherwise use len(items). Preserve the existing moreHint formatting and
pluralized message while ensuring displayed chats are counted when the upstream
total is unavailable or invalid.
🤖 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.

Outside diff comments:
In `@shortcuts/im/im_chat_search.go`:
- Line 203: Update the result-count output around the Fprintf call to use the
upstream total only when hasPositiveTotal is true; otherwise use len(items).
Preserve the existing moreHint formatting and pluralized message while ensuring
displayed chats are counted when the upstream total is unavailable or invalid.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: ec49791e-dcff-493c-8f7d-2af4a79c0424

📥 Commits

Reviewing files that changed from the base of the PR and between c2176ed and 59b8851.

📒 Files selected for processing (2)
  • shortcuts/im/im_chat_search.go
  • shortcuts/im/im_chat_search_total_test.go
💤 Files with no reviewable changes (1)
  • shortcuts/im/im_chat_search_total_test.go

"page_token": pageToken,
}
if hasPositiveTotal {
outData["total"] = int(total)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P1] Apply the fallback to pretty output as well. This only omits data.total from structured output. The pretty formatter still converts total unconditionally, so a response containing one chat with a missing or zero total still prints a row followed by 0 chat(s) found. That is the same inconsistency described by this PR. Please use len(items) when hasPositiveTotal is false and restore pretty-output regression coverage for missing and zero totals.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Using len(items) as a fallback is not appropriate here. It represents only the number of items returned on the current page, not the total number of matching chats, so displaying it as the total would be inaccurate (especially with pagination). This PR is intentionally scoped to omitting the zero-valued total field from structured output; it preserves the existing pretty-output behavior rather than substituting a different count with different semantics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

domain/im PR touches the im 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