diff --git a/shortcuts/common/testing_test.go b/shortcuts/common/testing_test.go new file mode 100644 index 000000000..1361efa8d --- /dev/null +++ b/shortcuts/common/testing_test.go @@ -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") + } +} diff --git a/shortcuts/doc/doc_media_test.go b/shortcuts/doc/doc_media_test.go index 3f75445dd..14b7c206a 100644 --- a/shortcuts/doc/doc_media_test.go +++ b/shortcuts/doc/doc_media_test.go @@ -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, "") { + t.Fatalf("dry-run output missing placeholder: %s", out) + } +} + func TestDocMediaInsertDryRunWikiAddsResolveStep(t *testing.T) { f, stdout, _, _ := cmdutil.TestFactory(t, docsTestConfigWithAppID("docs-test-app"))