mirror of
https://github.com/larksuite/cli.git
synced 2026-07-08 18:13:01 +08:00
test: cover clipboard Validate/DryRun branches and testing helper
Adds unit tests for the clipboard-related Validate/DryRun paths that Codecov patch-coverage was flagging as uncovered: - Validate error when neither --file nor --from-clipboard is supplied - Validate error when both are supplied (mutual exclusion) - DryRun output contains <clipboard image> placeholder - Self-test for TestNewRuntimeContextForAPI so shortcuts/common sees coverage for the new helper (not just shortcuts/doc)
This commit is contained in:
42
shortcuts/common/testing_test.go
Normal file
42
shortcuts/common/testing_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/larksuite/cli/internal/cmdutil"
|
||||
"github.com/larksuite/cli/internal/core"
|
||||
)
|
||||
|
||||
func TestTestNewRuntimeContextForAPIWiresFields(t *testing.T) {
|
||||
t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir())
|
||||
cfg := &core.CliConfig{AppID: "self-test-app", AppSecret: "secret", Brand: core.BrandFeishu}
|
||||
f, _, _, _ := cmdutil.TestFactory(t, cfg)
|
||||
cmd := &cobra.Command{Use: "testing-helper"}
|
||||
|
||||
ctx := context.Background()
|
||||
rctx := TestNewRuntimeContextForAPI(ctx, cmd, cfg, f)
|
||||
if rctx == nil {
|
||||
t.Fatal("TestNewRuntimeContextForAPI returned nil")
|
||||
}
|
||||
if rctx.Cmd != cmd {
|
||||
t.Errorf("Cmd not wired")
|
||||
}
|
||||
if rctx.Config != cfg {
|
||||
t.Errorf("Config not wired")
|
||||
}
|
||||
if rctx.Factory != f {
|
||||
t.Errorf("Factory not wired")
|
||||
}
|
||||
if !rctx.resolvedAs.IsBot() {
|
||||
t.Errorf("resolvedAs not set to bot, got %q", rctx.resolvedAs)
|
||||
}
|
||||
if rctx.Ctx() != ctx {
|
||||
t.Errorf("ctx not wired")
|
||||
}
|
||||
}
|
||||
@@ -75,6 +75,62 @@ func TestDocMediaInsertRejectsOldDocURL(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocMediaInsertValidateRequiresFileOrClipboard(t *testing.T) {
|
||||
f, _, _, _ := cmdutil.TestFactory(t, docsTestConfigWithAppID("docs-test-app"))
|
||||
|
||||
err := mountAndRunDocs(t, DocMediaInsert, []string{
|
||||
"+media-insert",
|
||||
"--doc", "https://example.larksuite.com/docx/doxcnXXXXXXXXXXXXXXXXXX",
|
||||
"--dry-run",
|
||||
"--as", "bot",
|
||||
}, f, nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected validation error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "one of --file or --from-clipboard is required") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocMediaInsertValidateRejectsFileAndClipboardTogether(t *testing.T) {
|
||||
f, _, _, _ := cmdutil.TestFactory(t, docsTestConfigWithAppID("docs-test-app"))
|
||||
|
||||
err := mountAndRunDocs(t, DocMediaInsert, []string{
|
||||
"+media-insert",
|
||||
"--doc", "https://example.larksuite.com/docx/doxcnXXXXXXXXXXXXXXXXXX",
|
||||
"--file", "dummy.png",
|
||||
"--from-clipboard",
|
||||
"--dry-run",
|
||||
"--as", "bot",
|
||||
}, f, nil)
|
||||
if err == nil {
|
||||
t.Fatal("expected mutual-exclusion error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "mutually exclusive") {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocMediaInsertDryRunWithClipboardUsesPlaceholder(t *testing.T) {
|
||||
f, stdout, _, _ := cmdutil.TestFactory(t, docsTestConfigWithAppID("docs-test-app"))
|
||||
|
||||
err := mountAndRunDocs(t, DocMediaInsert, []string{
|
||||
"+media-insert",
|
||||
"--doc", "https://example.larksuite.com/docx/doxcnXXXXXXXXXXXXXXXXXX",
|
||||
"--from-clipboard",
|
||||
"--dry-run",
|
||||
"--as", "bot",
|
||||
}, f, stdout)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
// JSON output escapes "<" and ">" as \u003c / \u003e by default.
|
||||
out := stdout.String()
|
||||
if !strings.Contains(out, `\u003cclipboard image\u003e`) && !strings.Contains(out, "<clipboard image>") {
|
||||
t.Fatalf("dry-run output missing <clipboard image> placeholder: %s", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocMediaInsertDryRunWikiAddsResolveStep(t *testing.T) {
|
||||
f, stdout, _, _ := cmdutil.TestFactory(t, docsTestConfigWithAppID("docs-test-app"))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user