mirror of
https://github.com/larksuite/cli.git
synced 2026-08-03 08:32:46 +08:00
* fix: unify dry-run output contract
* fix: address dry-run review feedback
* fix(dryrun): tighten preview contract and unify data shape
- transcribe HTTP method verbatim in previews (HEAD/OPTIONS were
reported as GET); reject an empty method in api with a typed error
- unify the dry-run data payload across api/service/shortcut paths:
{api, context?: {app_id, user_open_id}}; drop data.as — the envelope
top-level identity is the single identity source
- mark pretty dry-run stdout with '# dry-run: request not sent' so logs
that drop stderr still show it was a preview
- extract the shared preview builder, collapse PrintDryRunWithFile's
loose params into FileUploadMeta, and fail loudly on nil previews
- revert description-marker identity parsing: stale prose must not
override corrected accessTokens (blocks legal user calls on
images.create); identity gating keys off accessTokens only
- pin the new contracts with tests: verbatim method, three-way context
parity, nil-preview error, empty-context omission, marker line
* docs(agents): add typed-data, faithful-transcription, and contract-test conventions
- typed struct at the boundary over map[string]interface{} threading;
distinct types where values could swap silently (internal/meta.Token)
- transcribe input verbatim in previews/transformations; reject
unhonorable flag combinations with typed errors instead of silently
substituting behavior
- contract tests must fail when the implementation is reverted
* test: migrate dry-run tests grown on main to the envelope format
main gained raw-format dry-run readers while the PR was in flight
(wiki drive export #1802, drive list comments #1845, slash commands,
sheets history, docs fetch, mail draft-send/triage, vc meeting events).
Migrate them to the envelope accessors (clie2e.DryRunGet / data-wrapped
decoders) and drop the now-redundant DryRunData extractions in files
unified on DryRunGet.
---------
Co-authored-by: guokexin.02 <264159873+Tantanz20020918@users.noreply.github.com>
225 lines
6.3 KiB
Go
225 lines
6.3 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package clie2e
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCLIStdinRegression_SuccessCases(t *testing.T) {
|
|
setDryRunConfigEnv(t)
|
|
|
|
tests := []struct {
|
|
name string
|
|
req Request
|
|
assertions func(*testing.T, *Result)
|
|
}{
|
|
{
|
|
name: "api reads params from stdin",
|
|
req: Request{
|
|
Args: []string{"api", "GET", "/open-apis/test", "--params", "-", "--dry-run"},
|
|
Stdin: []byte(`{"a":"1","b":"2"}` + "\n"),
|
|
},
|
|
assertions: func(t *testing.T, result *Result) {
|
|
entry := firstDryRunRequest(t, result.Stdout)
|
|
assert.Equal(t, "GET", entry["method"])
|
|
assert.Equal(t, "/open-apis/test", entry["url"])
|
|
assert.Equal(t, map[string]any{"a": "1", "b": "2"}, entry["params"])
|
|
},
|
|
},
|
|
{
|
|
name: "api reads data from stdin",
|
|
req: Request{
|
|
Args: []string{"api", "POST", "/open-apis/test", "--data", "-", "--dry-run"},
|
|
Stdin: []byte(`{"text":"hello"}` + "\n"),
|
|
},
|
|
assertions: func(t *testing.T, result *Result) {
|
|
entry := firstDryRunRequest(t, result.Stdout)
|
|
assert.Equal(t, "POST", entry["method"])
|
|
assert.Equal(t, map[string]any{"text": "hello"}, entry["body"])
|
|
},
|
|
},
|
|
{
|
|
name: "api strips single quoted json",
|
|
req: Request{
|
|
Args: []string{"api", "GET", "/open-apis/test", "--params", `'{"a":"1"}'`, "--dry-run"},
|
|
},
|
|
assertions: func(t *testing.T, result *Result) {
|
|
entry := firstDryRunRequest(t, result.Stdout)
|
|
assert.Equal(t, map[string]any{"a": "1"}, entry["params"])
|
|
},
|
|
},
|
|
{
|
|
name: "service reads params from stdin",
|
|
req: Request{
|
|
Args: []string{
|
|
"calendar", "events", "instance_view",
|
|
"--as", "bot",
|
|
"--params", "-",
|
|
"--dry-run",
|
|
},
|
|
Stdin: []byte(`{"calendar_id":"primary","start_time":"1700000000","end_time":"1700003600"}` + "\n"),
|
|
},
|
|
assertions: func(t *testing.T, result *Result) {
|
|
entry := firstDryRunRequest(t, result.Stdout)
|
|
assert.Equal(t, "GET", entry["method"])
|
|
assert.Equal(t, "/open-apis/calendar/v4/calendars/primary/events/instance_view", entry["url"])
|
|
assert.Equal(t, map[string]any{
|
|
"start_time": "1700000000",
|
|
"end_time": "1700003600",
|
|
}, entry["params"])
|
|
},
|
|
},
|
|
{
|
|
name: "service reads data from stdin",
|
|
req: Request{
|
|
Args: []string{
|
|
"task", "tasks", "create",
|
|
"--as", "bot",
|
|
"--data", "-",
|
|
"--dry-run",
|
|
},
|
|
Stdin: []byte(`{"summary":"stdin regression"}` + "\n"),
|
|
},
|
|
assertions: func(t *testing.T, result *Result) {
|
|
entry := firstDryRunRequest(t, result.Stdout)
|
|
assert.Equal(t, "POST", entry["method"])
|
|
assert.Equal(t, "/open-apis/task/v2/tasks", entry["url"])
|
|
assert.Equal(t, map[string]any{"summary": "stdin regression"}, entry["body"])
|
|
},
|
|
},
|
|
{
|
|
name: "service strips single quoted json",
|
|
req: Request{
|
|
Args: []string{
|
|
"task", "tasks", "create",
|
|
"--as", "bot",
|
|
"--data", `'{"summary":"single quote"}'`,
|
|
"--dry-run",
|
|
},
|
|
},
|
|
assertions: func(t *testing.T, result *Result) {
|
|
entry := firstDryRunRequest(t, result.Stdout)
|
|
assert.Equal(t, map[string]any{"summary": "single quote"}, entry["body"])
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := RunCmd(context.Background(), tt.req)
|
|
require.NoError(t, err)
|
|
require.NoError(t, result.RunErr, "stderr:\n%s", result.Stderr)
|
|
result.AssertExitCode(t, 0)
|
|
tt.assertions(t, result)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCLIStdinRegression_ErrorCases(t *testing.T) {
|
|
setDryRunConfigEnv(t)
|
|
|
|
tests := []struct {
|
|
name string
|
|
req Request
|
|
wantMessage string
|
|
}{
|
|
{
|
|
name: "api rejects empty stdin",
|
|
req: Request{
|
|
Args: []string{"api", "GET", "/open-apis/test", "--params", "-", "--dry-run"},
|
|
Stdin: []byte{},
|
|
},
|
|
wantMessage: "--params: stdin is empty (did you forget to pipe input?)",
|
|
},
|
|
{
|
|
name: "api rejects double stdin",
|
|
req: Request{
|
|
Args: []string{"api", "POST", "/open-apis/test", "--params", "-", "--data", "-", "--dry-run"},
|
|
Stdin: []byte(`{"x":1}` + "\n"),
|
|
},
|
|
wantMessage: "--params and --data cannot both read from stdin (-)",
|
|
},
|
|
{
|
|
name: "service rejects empty stdin",
|
|
req: Request{
|
|
Args: []string{
|
|
"calendar", "events", "instance_view",
|
|
"--as", "bot",
|
|
"--params", "-",
|
|
"--dry-run",
|
|
},
|
|
Stdin: []byte{},
|
|
},
|
|
wantMessage: "--params: stdin is empty (did you forget to pipe input?)",
|
|
},
|
|
{
|
|
name: "service rejects double stdin",
|
|
req: Request{
|
|
Args: []string{
|
|
"task", "tasks", "create",
|
|
"--as", "bot",
|
|
"--params", "-",
|
|
"--data", "-",
|
|
"--dry-run",
|
|
},
|
|
Stdin: []byte(`{"summary":"stdin regression"}` + "\n"),
|
|
},
|
|
wantMessage: "--params and --data cannot both read from stdin (-)",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := RunCmd(context.Background(), tt.req)
|
|
require.NoError(t, err)
|
|
assert.Error(t, result.RunErr)
|
|
result.AssertExitCode(t, 2)
|
|
|
|
envelope, ok := result.StderrJSON(t).(map[string]any)
|
|
require.True(t, ok)
|
|
assert.Equal(t, false, envelope["ok"])
|
|
|
|
errDetail, ok := envelope["error"].(map[string]any)
|
|
require.True(t, ok)
|
|
assert.Equal(t, "validation", errDetail["type"])
|
|
assert.Equal(t, tt.wantMessage, errDetail["message"])
|
|
})
|
|
}
|
|
}
|
|
|
|
func setDryRunConfigEnv(t *testing.T) {
|
|
t.Helper()
|
|
t.Setenv("LARKSUITE_CLI_APP_ID", "app")
|
|
t.Setenv("LARKSUITE_CLI_APP_SECRET", "secret")
|
|
t.Setenv("LARKSUITE_CLI_BRAND", "feishu")
|
|
}
|
|
|
|
func firstDryRunRequest(t *testing.T, stdout string) map[string]any {
|
|
t.Helper()
|
|
|
|
var payload map[string]any
|
|
if err := json.Unmarshal([]byte(stdout), &payload); err != nil {
|
|
t.Fatalf("parse dry-run payload: %v\nstdout:\n%s", err, stdout)
|
|
}
|
|
require.Equal(t, true, payload["ok"], "payload missing ok envelope: %#v", payload)
|
|
require.Equal(t, true, payload["dry_run"], "payload missing dry_run marker: %#v", payload)
|
|
|
|
data, ok := payload["data"].(map[string]any)
|
|
require.True(t, ok, "payload missing data object: %#v", payload)
|
|
|
|
apiEntries, ok := data["api"].([]any)
|
|
require.True(t, ok, "payload data missing api array: %#v", payload)
|
|
require.Len(t, apiEntries, 1)
|
|
|
|
entry, ok := apiEntries[0].(map[string]any)
|
|
require.True(t, ok, "api entry is not an object: %#v", apiEntries[0])
|
|
return entry
|
|
}
|