Files
larksuite--cli/shortcuts/apps/apps_session_create_test.go
wehub-resource-sync bf9395e022
CI / license-header (push) Has been skipped
CI / e2e-dry-run (push) Has been skipped
CI / fast-gate (push) Failing after 0s
Test PR Label Logic / test-pr-labels (push) Failing after 1s
Skill Format Check / check-format (push) Failing after 2s
CI / security (push) Failing after 5s
CI / unit-test (push) Has been skipped
CI / lint (push) Has been skipped
CI / script-test (push) Has been skipped
CI / deterministic-gate (push) Has been skipped
CI / coverage (push) Has been skipped
CI / results (push) Has been cancelled
CI / deadcode (push) Has been cancelled
CI / e2e-live (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:22:54 +08:00

86 lines
2.9 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package apps
import (
"strings"
"testing"
"github.com/larksuite/cli/internal/httpmock"
)
func TestAppsSessionCreate_Success(t *testing.T) {
factory, stdout, reg := newAppsExecuteFactory(t)
stub := &httpmock.Stub{
Method: "POST",
URL: "/open-apis/spark/v1/apps/app_x/sessions",
Body: map[string]interface{}{
"code": 0,
"data": map[string]interface{}{"session_id": "conv_new"},
},
}
reg.Register(stub)
if err := runAppsShortcut(t, AppsSessionCreate,
[]string{"+session-create", "--app-id", "app_x", "--as", "user"},
factory, stdout); err != nil {
t.Fatalf("execute err=%v", err)
}
if got := stdout.String(); !strings.Contains(got, `"session_id": "conv_new"`) {
t.Fatalf("stdout missing session_id: %s", got)
}
if len(stub.CapturedBody) != 0 {
t.Fatalf("+session-create must POST with no body, got: %s", stub.CapturedBody)
}
}
func TestAppsSessionCreate_Pretty(t *testing.T) {
factory, stdout, reg := newAppsExecuteFactory(t)
reg.Register(&httpmock.Stub{
Method: "POST",
URL: "/open-apis/spark/v1/apps/app_x/sessions",
Body: map[string]interface{}{"code": 0, "data": map[string]interface{}{"session_id": "conv_new"}},
})
if err := runAppsShortcut(t, AppsSessionCreate,
[]string{"+session-create", "--app-id", "app_x", "--format", "pretty", "--as", "user"},
factory, stdout); err != nil {
t.Fatalf("execute err=%v", err)
}
if got := stdout.String(); !strings.Contains(got, "session created: conv_new") {
t.Fatalf("pretty output wrong: %q", got)
}
}
func TestAppsSessionCreate_RequiresAppID(t *testing.T) {
factory, stdout, _ := newAppsExecuteFactory(t)
// present-but-blank --app-id passes cobra MarkFlagRequired, caught by Validate hook.
err := runAppsShortcut(t, AppsSessionCreate, []string{"+session-create", "--app-id", "", "--as", "user"}, factory, stdout)
if err == nil || !strings.Contains(err.Error(), "app-id") {
t.Fatalf("expected --app-id required error, got %v", err)
}
}
func TestAppsSessionCreate_DryRun(t *testing.T) {
factory, stdout, _ := newAppsExecuteFactory(t)
if err := runAppsShortcut(t, AppsSessionCreate,
[]string{"+session-create", "--app-id", "app_x", "--dry-run", "--as", "user"},
factory, stdout); err != nil {
t.Fatalf("dry-run err=%v", err)
}
if got := stdout.String(); !strings.Contains(got, "/open-apis/spark/v1/apps/app_x/sessions") {
t.Fatalf("dry-run missing endpoint: %s", got)
}
}
func TestAppsSessionCreate_EncodesAppID(t *testing.T) {
factory, stdout, _ := newAppsExecuteFactory(t)
if err := runAppsShortcut(t, AppsSessionCreate,
[]string{"+session-create", "--app-id", "a/b", "--dry-run", "--as", "user"},
factory, stdout); err != nil {
t.Fatalf("dry-run err=%v", err)
}
if got := stdout.String(); strings.Contains(got, "apps/a/b/sessions") {
t.Fatalf("app_id must be path-encoded, got raw slash: %s", got)
}
}