Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions shortcuts/im/im_chat_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ var ImChatSearch = common.Shortcut{
}

rawItems, _ := resData["items"].([]interface{})
totalF, _ := util.ToFloat64(resData["total"])
total := totalF
total, hasPositiveTotal := util.ToFloat64(resData["total"])
hasPositiveTotal = hasPositiveTotal && int(total) > 0
hasMore, pageToken := common.PaginationMeta(resData)

// Extract MetaData from each item
Expand Down Expand Up @@ -144,10 +144,12 @@ var ImChatSearch = common.Shortcut{

outData := map[string]interface{}{
"chats": items,
"total": int(total),
"has_more": hasMore,
"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.

}
if notice, _ := resData["notice"].(string); notice != "" {
outData["notice"] = notice
}
Expand Down
71 changes: 71 additions & 0 deletions shortcuts/im/im_chat_search_total_test.go
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)
}
})
}
}
47 changes: 47 additions & 0 deletions tests/cli_e2e/im/im_chat_search_dryrun_test.go
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)
}
Loading