Include chat, weather, image, travel, streaming ASR and text examples, shared model initialization, documentation, and existing tests. Co-authored-by: TRAE CLI <traecli@bytedance.com>
70 lines
2.1 KiB
Go
70 lines
2.1 KiB
Go
package cli
|
||
|
||
import (
|
||
"bytes"
|
||
"context"
|
||
"strings"
|
||
"testing"
|
||
|
||
"example.com/ueino/adk/internal/config"
|
||
)
|
||
|
||
func TestCommands(t *testing.T) {
|
||
for _, tt := range []struct {
|
||
name string
|
||
args []string
|
||
voice bool
|
||
}{
|
||
{name: "默认文字"},
|
||
{name: "语音", args: []string{"voice"}, voice: true},
|
||
{name: "子命令后参数", args: []string{"voice", "--model", "test-chat", "--asr-model", "test-asr", "--api-key-env", "CLI_TEST_KEY"}, voice: true},
|
||
{name: "子命令前参数", args: []string{"--model", "test-chat", "--asr-model", "test-asr", "--api-key-env", "CLI_TEST_KEY", "voice"}, voice: true},
|
||
} {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
t.Setenv("CLI_TEST_KEY", "shared-key")
|
||
called := 0
|
||
ctx := context.Background()
|
||
command := newCommand(func(gotCtx context.Context, cfg config.Config, voice bool) error {
|
||
called++
|
||
if voice != tt.voice || gotCtx != ctx {
|
||
t.Fatal("模式或上下文传递错误")
|
||
}
|
||
if len(tt.args) > 1 && (cfg.Chat.Model != "test-chat" || cfg.ASR.Model != "test-asr" || cfg.Chat.APIKey != "shared-key" || cfg.ASR.APIKey != "shared-key") {
|
||
t.Fatal("参数或密钥继承错误")
|
||
}
|
||
return nil
|
||
})
|
||
command.SetArgs(tt.args)
|
||
if err := command.ExecuteContext(ctx); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if called != 1 {
|
||
t.Fatalf("终端调用次数 = %d", called)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestHelpAndInvalidArgs(t *testing.T) {
|
||
for _, args := range [][]string{{"--help"}, {"voice", "--help"}, {"unknown"}, {"voice", "extra"}, {"--unknown"}} {
|
||
t.Run(strings.Join(args, " "), func(t *testing.T) {
|
||
command := newCommand(func(context.Context, config.Config, bool) error {
|
||
t.Fatal("帮助或非法参数不应启动终端")
|
||
return nil
|
||
})
|
||
var output bytes.Buffer
|
||
command.SetOut(&output)
|
||
command.SetErr(&output)
|
||
command.SetArgs(args)
|
||
err := command.Execute()
|
||
if args[len(args)-1] == "--help" {
|
||
if err != nil || !strings.Contains(output.String(), "--asr-model") {
|
||
t.Fatalf("帮助输出错误: %v,%s", err, output.String())
|
||
}
|
||
} else if err == nil {
|
||
t.Fatal("非法参数应返回错误")
|
||
}
|
||
})
|
||
}
|
||
}
|