mirror of
https://github.com/larksuite/cli.git
synced 2026-07-04 23:15:25 +08:00
* fix(apps): read app object from data.app for +create and +update
The Miaoda OpenAPI returns the application object nested under
data.app for both POST /apps and PATCH /apps/{appId}. The CLI text
helper was reading common.GetString(data, "app_id"), which yields an
empty string against the wire format -- so `lark-cli apps +create
--format pretty` printed `created: ` with no ID.
Navigate the new nested path via GetString(data, "app", "app_id") for
both create and update. Update unit-test mocks to wrap the response
under `app`. Refresh the lark-apps skill references (example response
shape + jq paths) so agents reading them follow the right path.
Wire format is passed through to the user's JSON envelope untouched
-- no unwrapping in CLI. Consumers reading the response should use
.data.app.app_id.
The GET /apps list endpoint is unchanged: per the design doc its
items[] are flat objects, no wrapper.
* docs(apps): add required --app-type HTML to scenario 2 snippet
The "用户没有 app_id" snippet in lark-apps-html-publish.md was missing
the required --app-type flag, so copy-pasting it triggered Validate
("--app-type is required") and left $APP empty -- the following
+html-publish then failed with --app-id "". Bring the snippet in line
with every other apps +create example in the skill.
* docs(apps): simplify auth-recovery rule to error.type == missing_scope
Every apps shortcut declares Scopes, so the precheck path in
shortcuts/common/runner.go:825 is always the one that fires on scope
violations and the envelope's error.type is the stable discriminator.
Drop the keyword-sniffing of error.hint, the chain explanation, and the
bot caveat — they all reduce to one boolean: error.type == "missing_scope"
→ run `lark-cli auth login --domain apps`.
Also collapse the corresponding bullet in 快速决策 to point at this rule.
190 lines
5.9 KiB
Go
190 lines
5.9 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package apps
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/larksuite/cli/internal/cmdutil"
|
|
"github.com/larksuite/cli/internal/core"
|
|
"github.com/larksuite/cli/internal/httpmock"
|
|
"github.com/larksuite/cli/shortcuts/common"
|
|
)
|
|
|
|
// 测试基础设施 —— 后续 Task 2.2-2.4 / Task 3.4 复用
|
|
|
|
func newAppsExecuteFactory(t *testing.T) (*cmdutil.Factory, *bytes.Buffer, *httpmock.Registry) {
|
|
t.Helper()
|
|
t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir())
|
|
cfg := &core.CliConfig{
|
|
AppID: "test-app-" + strings.ToLower(t.Name()),
|
|
AppSecret: "test-secret",
|
|
Brand: core.BrandFeishu,
|
|
UserOpenId: "ou_test",
|
|
}
|
|
factory, stdout, _, reg := cmdutil.TestFactory(t, cfg)
|
|
return factory, stdout, reg
|
|
}
|
|
|
|
func runAppsShortcut(t *testing.T, sc common.Shortcut, args []string, factory *cmdutil.Factory, stdout *bytes.Buffer) error {
|
|
t.Helper()
|
|
parent := &cobra.Command{Use: "apps"}
|
|
sc.Mount(parent, factory)
|
|
parent.SetArgs(args)
|
|
parent.SilenceErrors = true
|
|
parent.SilenceUsage = true
|
|
if stdout != nil {
|
|
stdout.Reset()
|
|
}
|
|
return parent.ExecuteContext(context.Background())
|
|
}
|
|
|
|
// +create 测试
|
|
|
|
func TestAppsCreate_Success(t *testing.T) {
|
|
factory, stdout, reg := newAppsExecuteFactory(t)
|
|
stub := &httpmock.Stub{
|
|
Method: "POST",
|
|
URL: "/open-apis/spark/v1/apps",
|
|
Body: map[string]interface{}{
|
|
"code": 0,
|
|
"data": map[string]interface{}{
|
|
"app": map[string]interface{}{
|
|
"app_id": "app_x",
|
|
"name": "Demo",
|
|
"icon_url": "https://lf3-static.bytednsdoc.com/.../default.svg",
|
|
"created_at": "2026-05-18T10:00:00Z",
|
|
},
|
|
},
|
|
},
|
|
}
|
|
reg.Register(stub)
|
|
|
|
if err := runAppsShortcut(t, AppsCreate,
|
|
[]string{"+create", "--name", "Demo", "--app-type", "HTML", "--description", "d", "--as", "user"},
|
|
factory, stdout); err != nil {
|
|
t.Fatalf("execute err=%v", err)
|
|
}
|
|
if got := stdout.String(); !strings.Contains(got, `"app_id": "app_x"`) {
|
|
t.Fatalf("stdout missing app_id: %s", got)
|
|
}
|
|
|
|
var sent map[string]interface{}
|
|
if err := json.Unmarshal(stub.CapturedBody, &sent); err != nil {
|
|
t.Fatalf("decode body: %v", err)
|
|
}
|
|
if sent["name"] != "Demo" {
|
|
t.Fatalf("body.name = %v", sent["name"])
|
|
}
|
|
if sent["app_type"] != "HTML" {
|
|
t.Fatalf("body.app_type = %v (want HTML)", sent["app_type"])
|
|
}
|
|
if sent["description"] != "d" {
|
|
t.Fatalf("body.description = %v", sent["description"])
|
|
}
|
|
if _, present := sent["icon_url"]; present {
|
|
t.Fatalf("icon_url should be omitted when not provided: %v", sent)
|
|
}
|
|
}
|
|
|
|
func TestAppsCreate_WithIconURL(t *testing.T) {
|
|
factory, stdout, reg := newAppsExecuteFactory(t)
|
|
reg.Register(&httpmock.Stub{
|
|
Method: "POST",
|
|
URL: "/open-apis/spark/v1/apps",
|
|
Body: map[string]interface{}{
|
|
"code": 0,
|
|
"data": map[string]interface{}{
|
|
"app": map[string]interface{}{"app_id": "app_x", "name": "Demo"},
|
|
},
|
|
},
|
|
})
|
|
|
|
if err := runAppsShortcut(t, AppsCreate,
|
|
[]string{"+create", "--name", "Demo", "--app-type", "HTML", "--icon-url", "https://example.com/icon.svg", "--as", "user"},
|
|
factory, stdout); err != nil {
|
|
t.Fatalf("execute err=%v", err)
|
|
}
|
|
}
|
|
|
|
// TestAppsCreate_PrettyOutputReadsNestedAppID exercises the prettyFn callback
|
|
// passed to OutFormat (only invoked under --format pretty) so the new
|
|
// data.app.app_id nesting is actually read by the text writer. Without this,
|
|
// default --format json dumps the whole envelope and the substring assertion
|
|
// in TestAppsCreate_Success would pass even if the GetString path were wrong.
|
|
func TestAppsCreate_PrettyOutputReadsNestedAppID(t *testing.T) {
|
|
factory, stdout, reg := newAppsExecuteFactory(t)
|
|
reg.Register(&httpmock.Stub{
|
|
Method: "POST",
|
|
URL: "/open-apis/spark/v1/apps",
|
|
Body: map[string]interface{}{
|
|
"code": 0,
|
|
"data": map[string]interface{}{
|
|
"app": map[string]interface{}{"app_id": "app_x", "name": "Demo"},
|
|
},
|
|
},
|
|
})
|
|
|
|
if err := runAppsShortcut(t, AppsCreate,
|
|
[]string{"+create", "--name", "Demo", "--app-type", "HTML", "--format", "pretty", "--as", "user"},
|
|
factory, stdout); err != nil {
|
|
t.Fatalf("execute err=%v", err)
|
|
}
|
|
if got := stdout.String(); !strings.Contains(got, "created: app_x") {
|
|
t.Fatalf("pretty output should read app_id from data.app.app_id, got: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestAppsCreate_RequiresName(t *testing.T) {
|
|
factory, stdout, _ := newAppsExecuteFactory(t)
|
|
err := runAppsShortcut(t, AppsCreate, []string{"+create", "--app-type", "HTML", "--as", "user"}, factory, stdout)
|
|
if err == nil || !strings.Contains(err.Error(), "name") {
|
|
t.Fatalf("expected name required error, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestAppsCreate_RequiresAppType(t *testing.T) {
|
|
factory, stdout, _ := newAppsExecuteFactory(t)
|
|
err := runAppsShortcut(t, AppsCreate,
|
|
[]string{"+create", "--name", "Demo", "--as", "user"}, factory, stdout)
|
|
if err == nil || !strings.Contains(err.Error(), "app-type") {
|
|
t.Fatalf("expected --app-type required error, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestAppsCreate_RejectsInvalidAppType(t *testing.T) {
|
|
factory, stdout, _ := newAppsExecuteFactory(t)
|
|
err := runAppsShortcut(t, AppsCreate,
|
|
[]string{"+create", "--name", "Demo", "--app-type", "spa", "--as", "user"},
|
|
factory, stdout)
|
|
if err == nil || !strings.Contains(err.Error(), "not supported") {
|
|
t.Fatalf("expected unsupported app-type error, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestAppsCreate_DryRun(t *testing.T) {
|
|
factory, stdout, _ := newAppsExecuteFactory(t)
|
|
if err := runAppsShortcut(t, AppsCreate,
|
|
[]string{"+create", "--name", "Demo", "--app-type", "HTML", "--dry-run", "--as", "user"},
|
|
factory, stdout); err != nil {
|
|
t.Fatalf("dry-run err=%v", err)
|
|
}
|
|
got := stdout.String()
|
|
if !strings.Contains(got, "/open-apis/spark/v1/apps") {
|
|
t.Fatalf("dry-run missing endpoint: %s", got)
|
|
}
|
|
if !strings.Contains(got, `"name": "Demo"`) {
|
|
t.Fatalf("dry-run missing body: %s", got)
|
|
}
|
|
if !strings.Contains(got, `"app_type": "HTML"`) {
|
|
t.Fatalf("dry-run missing app_type: %s", got)
|
|
}
|
|
}
|