Files
larksuite-cli/internal/cmdutil/testing.go
梁硕 83dfb068ad feat: open-source lark-cli — the official CLI for Lark/Feishu
Change-Id: I113d9cdb5403cec347efe4595415e34a18b7decf
2026-03-28 10:36:25 +08:00

67 lines
2.2 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package cmdutil
import (
"bytes"
"net/http"
"testing"
lark "github.com/larksuite/oapi-sdk-go/v3"
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
"github.com/larksuite/cli/internal/core"
"github.com/larksuite/cli/internal/httpmock"
)
// noopKeychain is a no-op KeychainAccess for tests that don't need keychain.
type noopKeychain struct{}
func (n *noopKeychain) Get(service, account string) (string, error) { return "", nil }
func (n *noopKeychain) Set(service, account, value string) error { return nil }
func (n *noopKeychain) Remove(service, account string) error { return nil }
// TestFactory creates a Factory for testing.
// Returns (factory, stdout buffer, stderr buffer, http mock registry).
func TestFactory(t *testing.T, config *core.CliConfig) (*Factory, *bytes.Buffer, *bytes.Buffer, *httpmock.Registry) {
t.Helper()
reg := &httpmock.Registry{}
t.Cleanup(func() { reg.Verify(t) })
stdoutBuf := &bytes.Buffer{}
stderrBuf := &bytes.Buffer{}
mockClient := httpmock.NewClient(reg)
// SDK mock client wraps the mock transport with UserAgentTransport
// so that User-Agent overrides the SDK default (oapi-sdk-go/v3.x.x).
sdkMockClient := &http.Client{
Transport: &UserAgentTransport{Base: reg},
}
// Build a test LarkClient using the config
var testLarkClient *lark.Client
if config != nil && config.AppID != "" {
opts := []lark.ClientOptionFunc{
lark.WithLogLevel(larkcore.LogLevelError),
lark.WithHttpClient(sdkMockClient),
lark.WithHeaders(BaseSecurityHeaders()),
}
if config.Brand != "" {
opts = append(opts, lark.WithOpenBaseUrl(core.ResolveOpenBaseURL(config.Brand)))
}
testLarkClient = lark.NewClient(config.AppID, config.AppSecret, opts...)
}
f := &Factory{
Config: func() (*core.CliConfig, error) { return config, nil },
AuthConfig: func() (*core.CliConfig, error) { return config, nil },
HttpClient: func() (*http.Client, error) { return mockClient, nil },
LarkClient: func() (*lark.Client, error) { return testLarkClient, nil },
IOStreams: &IOStreams{In: nil, Out: stdoutBuf, ErrOut: stderrBuf},
Keychain: &noopKeychain{},
}
return f, stdoutBuf, stderrBuf, reg
}