Files
shop-crm-agent/internal/command/commands_test.go
T
Shiny 3466e19d13 feat: 初始化shop-crm-agent项目,实现基础CLI与CRM操作能力
本提交初始化完整的shop-crm-agent项目:
1. 创建基础项目结构与配置文件(.gitignore、go.mod/go.sum)
2. 添加项目许可协议与文档(README.md、AGENTS.md、SKILL.md)
3. 实现核心CLI入口与命令框架
4. 封装Protobuf API客户端与完整的命令实现
5. 添加单元测试覆盖核心功能与边界情况
6. 提供构建脚本与版本管理能力
2026-08-01 01:36:37 +08:00

95 lines
2.9 KiB
Go

package command
import (
"bytes"
"errors"
"testing"
dto "code.zhecent.com/open/shop-crm-agent/internal/api"
"google.golang.org/protobuf/encoding/protojson"
)
func TestRootCommandExposesOnlyCrmOperations(t *testing.T) {
cmd := New("test-version")
want := map[string]bool{
"customer-list": true, "project": true,
"quotation": true, "catalog-search": true, "edit": true,
}
for _, child := range cmd.Commands() {
if child.Name() == "credential" {
t.Fatal("external client exposes credential administration")
}
if child.Name() == "progress-template-list" {
t.Fatal("external client exposes retired progress template query")
}
delete(want, child.Name())
}
if len(want) != 0 {
t.Fatalf("missing root commands = %v", want)
}
if cmd.Version != "test-version" {
t.Fatalf("version = %q", cmd.Version)
}
}
func TestProjectCommandsExcludeProgressAndTemplateFlag(t *testing.T) {
root := New("test-version")
project, _, err := root.Find([]string{"project"})
if err != nil {
t.Fatal(err)
}
for _, child := range project.Commands() {
if child.Name() == "progress-complete" {
t.Fatal("project command exposes retired progress completion")
}
}
for _, name := range []string{"create", "update"} {
command, _, findErr := root.Find([]string{"project", name})
if findErr != nil {
t.Fatal(findErr)
}
if command.Flags().Lookup("progress-template-key") != nil {
t.Fatalf("project %s exposes retired progress template flag", name)
}
}
}
func TestRemoteCommandRequiresEnvironmentConfiguration(t *testing.T) {
t.Setenv("LIGHTCORE_API_BASE_URL", "")
t.Setenv("LIGHTCORE_SHOP_CRM_AGENT_TOKEN", "")
cmd := New("test")
cmd.SetArgs([]string{"customer-list"})
if err := cmd.Execute(); err == nil {
t.Fatal("missing environment configuration was accepted")
}
}
func TestWriteCrmAgentResultPrintsStructuredBusinessError(t *testing.T) {
commandErr := errors.New("CRM Agent API 错误 400: 报价已锁定")
response := &dto.ProjectResponse{Header: &dto.ResponseHeader{
Code: 400, Message: "报价已锁定", RequestId: "request-123", Replayed: true,
}}
var output bytes.Buffer
err := writeCrmAgentResult(&output, response, commandErr)
if !errors.Is(err, commandErr) {
t.Fatalf("command error = %v", err)
}
printed := &dto.ProjectResponse{}
if err := protojson.Unmarshal(output.Bytes(), printed); err != nil {
t.Fatalf("decode output: %v", err)
}
if printed.GetHeader() == nil || printed.GetHeader().Code != 400 || printed.GetHeader().RequestId != "request-123" || !printed.GetHeader().Replayed {
t.Fatalf("printed header = %#v", printed.GetHeader())
}
}
func TestWriteCrmAgentResultSkipsMissingTransportResponse(t *testing.T) {
commandErr := errors.New("connection refused")
var output bytes.Buffer
err := writeCrmAgentResult(&output, &dto.ProjectResponse{}, commandErr)
if !errors.Is(err, commandErr) || output.Len() != 0 {
t.Fatalf("error=%v output=%q", err, output.String())
}
}