-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: omit zero total from chat search #2035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arnold9672
wants to merge
3
commits into
larksuite:main
Choose a base branch
from
arnold9672:fix/chat-search-total
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package im | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestImChatSearchExecuteTotalContract(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| total interface{} | ||
| wantTotal bool | ||
| wantValue int | ||
| }{ | ||
| {name: "missing"}, | ||
| {name: "invalid", total: "unknown"}, | ||
| {name: "zero", total: 0}, | ||
| {name: "fractional truncates to zero", total: 0.5}, | ||
| {name: "positive", total: 7, wantTotal: true, wantValue: 7}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| data := map[string]interface{}{ | ||
| "items": []interface{}{ | ||
| map[string]interface{}{ | ||
| "meta_data": map[string]interface{}{ | ||
| "chat_id": "oc_example", | ||
| "name": "Example chat", | ||
| }, | ||
| }, | ||
| }, | ||
| "has_more": false, | ||
| "page_token": "", | ||
| } | ||
| if tt.total != nil { | ||
| data["total"] = tt.total | ||
| } | ||
|
|
||
| runtime := newBotShortcutRuntime(t, shortcutRoundTripFunc(func(req *http.Request) (*http.Response, error) { | ||
| if req.URL.Path != "/open-apis/im/v2/chats/search" { | ||
| return nil, fmt.Errorf("unexpected request: %s", req.URL.String()) | ||
| } | ||
| return shortcutJSONResponse(http.StatusOK, map[string]interface{}{ | ||
| "code": 0, | ||
| "data": data, | ||
| }), nil | ||
| })) | ||
| runtime.Cmd = newChatSearchNoticeTestCommand(t, "example") | ||
| runtime.Format = "json" | ||
|
|
||
| if err := ImChatSearch.Execute(context.Background(), runtime); err != nil { | ||
| t.Fatalf("ImChatSearch.Execute() error = %v", err) | ||
| } | ||
|
|
||
| got := decodeShortcutData(t, runtime) | ||
| total, exists := got["total"] | ||
| if exists != tt.wantTotal { | ||
| t.Fatalf("data total presence = %v, want %v; data=%#v", exists, tt.wantTotal, got) | ||
| } | ||
| if tt.wantTotal && int(total.(float64)) != tt.wantValue { | ||
| t.Fatalf("data.total = %v, want %d", total, tt.wantValue) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package im | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| clie2e "github.com/larksuite/cli/tests/cli_e2e" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestIM_ChatSearchDryRun(t *testing.T) { | ||
| t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir()) | ||
| t.Setenv("LARKSUITE_CLI_APP_ID", "app") | ||
| t.Setenv("LARKSUITE_CLI_APP_SECRET", "secret") | ||
| t.Setenv("LARKSUITE_CLI_BRAND", "feishu") | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| t.Cleanup(cancel) | ||
|
|
||
| result, err := clie2e.RunCmd(ctx, clie2e.Request{ | ||
| Args: []string{ | ||
| "im", "+chat-search", | ||
| "--query", "example-chat", | ||
| "--search-types", "private,public_joined", | ||
| "--chat-modes", "group", | ||
| "--page-size", "25", | ||
| "--page-token", "next_page", | ||
| "--dry-run", | ||
| }, | ||
| DefaultAs: "bot", | ||
| }) | ||
| require.NoError(t, err) | ||
| result.AssertExitCode(t, 0) | ||
|
|
||
| require.Equal(t, "POST", clie2e.DryRunGet(result.Stdout, "api.0.method").String(), "stdout:\n%s", result.Stdout) | ||
| require.Equal(t, "/open-apis/im/v2/chats/search", clie2e.DryRunGet(result.Stdout, "api.0.url").String(), "stdout:\n%s", result.Stdout) | ||
| require.Equal(t, `"example-chat"`, clie2e.DryRunGet(result.Stdout, "api.0.body.query").String(), "stdout:\n%s", result.Stdout) | ||
| require.Equal(t, "private", clie2e.DryRunGet(result.Stdout, "api.0.body.filter.search_types.0").String(), "stdout:\n%s", result.Stdout) | ||
| require.Equal(t, "public_joined", clie2e.DryRunGet(result.Stdout, "api.0.body.filter.search_types.1").String(), "stdout:\n%s", result.Stdout) | ||
| require.Equal(t, "default", clie2e.DryRunGet(result.Stdout, "api.0.body.filter.chat_modes.0").String(), "stdout:\n%s", result.Stdout) | ||
| require.Equal(t, int64(25), clie2e.DryRunGet(result.Stdout, "api.0.params.page_size").Int(), "stdout:\n%s", result.Stdout) | ||
| require.Equal(t, "next_page", clie2e.DryRunGet(result.Stdout, "api.0.params.page_token").String(), "stdout:\n%s", result.Stdout) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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-valuedtotalfield from structured output; it preserves the existing pretty-output behavior rather than substituting a different count with different semantics.