Files
example-eino/adk/internal/terminal/voice_test.go
T
zhaoyupeng.elqbandTRAE CLI 1512f0516f feat: add weather tracing and unify example system prompts
Move shared prompt constants into pkg/opt and use them across examples.
Add agent, model, and tool tracing to the weather example, with tests,
and update weather argument descriptions and parse-error logging.

Strip timestamp and speaker markers from final ADK voice transcripts,
keeping normal bracketed text and treating marker-only results as empty.

Co-authored-by: TRAE CLI <traecli@bytedance.com>
2026-11-19 10:44:56 +08:00

141 lines
3.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package terminal
import (
"context"
"errors"
"io"
"os"
"os/exec"
"os/signal"
"runtime"
"strings"
"testing"
"time"
)
// 用测试进程模拟 ffmpeg,验证控制流程,不依赖本机麦克风。
func TestVoiceRecorderProcess(t *testing.T) {
if os.Getenv("EINO_TEST_RECORDER") != "1" {
return
}
mode := os.Args[len(os.Args)-1]
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
os.Stdout.Write(make([]byte, 4800))
switch mode {
case "manual":
<-interrupt
os.Exit(255)
case "failure":
os.Stderr.WriteString("麦克风不可用")
os.Exit(1)
default:
os.Exit(0)
}
}
func TestRecordVoice(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("录音控制使用 Unix 信号")
}
for _, mode := range []string{"automatic", "manual", "marked", "markers-only", "brackets", "failure", "asr-failure", "empty", "cancel", "eof"} {
t.Run(mode, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
reader, writer := io.Pipe()
defer reader.Close()
defer writer.Close()
input := newInput(ctx, reader)
processMode := "automatic"
if mode == "manual" || mode == "cancel" || mode == "eof" || mode == "asr-failure" {
processMode = "manual"
} else if mode == "failure" {
processMode = "failure"
}
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestVoiceRecorderProcess$", "--", processMode)
cmd.Env = append(os.Environ(), "EINO_TEST_RECORDER=1")
text, err := recordVoice(ctx, cmd, input, func(ctx context.Context, audio io.Reader, onDelta func(string)) (string, error) {
buffer := make([]byte, 4800)
if _, err := io.ReadFull(audio, buffer); err != nil {
return "", err
}
onDelta("临时识别")
switch mode {
case "manual":
go io.WriteString(writer, "\n下一条聊天\n")
case "cancel":
cancel()
case "eof":
writer.Close()
case "asr-failure":
return "", errors.New("识别服务不可用")
}
if _, err := io.ReadAll(audio); err != nil {
return "", err
}
switch mode {
case "empty":
return "", nil
case "marked":
return " [0.00][S01]嗯,帮我查询一下成都后面[3.00]\n[0.00][S02]两天的天气。[0.98] ", nil
case "markers-only":
return " [0.00][S01][0.98] ", nil
case "brackets":
return "请保留[备注]和[123abc]。", nil
}
return "最终识别", nil
})
if cmd.ProcessState == nil {
t.Fatal("录音进程没有被回收")
}
switch mode {
case "failure", "asr-failure":
if err == nil {
t.Fatal("错误不应被当作成功转写")
}
case "cancel":
if !errors.Is(err, context.Canceled) {
t.Fatalf("预期取消,得到 %v", err)
}
case "eof":
if !errors.Is(err, io.EOF) {
t.Fatalf("预期输入结束,得到 %v", err)
}
case "empty", "markers-only":
if text != "" || err != nil {
t.Fatalf("空转写 = %q,%v", text, err)
}
default:
want := "最终识别"
if mode == "marked" {
want = "嗯,帮我查询一下成都后面\n两天的天气。"
} else if mode == "brackets" {
want = "请保留[备注]和[123abc]。"
}
if text != want || err != nil {
t.Fatalf("转写 = %q,%v", text, err)
}
if mode == "manual" {
line, err := input.next(ctx)
if err != nil || line != "下一条聊天" {
t.Fatalf("录音吞掉了下一条输入: %q,%v", line, err)
}
} else {
go io.WriteString(writer, "y\n下一条聊天\n")
approved, err := confirmImage(ctx, input, "画一只猫")
if err != nil || !approved {
t.Fatalf("自动结束后确认失败: %v", err)
}
line, err := input.next(ctx)
if err != nil || line != "下一条聊天" {
t.Fatal("确认吞掉下一条聊天")
}
}
}
if mode == "failure" && !strings.Contains(err.Error(), "麦克风不可用") {
t.Fatalf("应包含录音错误详情: %v", err)
}
})
}
}