From 96cf00922878bebad12fe01205a4bcfd1d30289f Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Sun, 14 Jun 2026 11:55:29 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E9=80=82=E9=85=8D=E6=9E=84=E9=80=A0=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E7=AD=BE=E5=90=8D=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/ai/llm/openai_test.go | 12 ++-- backend/internal/ai/stt/deepgram_test.go | 12 ++-- backend/internal/ai/stt/mimo_test.go | 2 +- backend/internal/ai/tts/mimo_test.go | 18 +++--- backend/internal/ai/tts/openai_test.go | 14 ++--- .../internal/orchestrator/pipeline_test.go | 57 ++++++++++++++++--- backend/internal/ws/handler_test.go | 10 +++- 7 files changed, 86 insertions(+), 39 deletions(-) diff --git a/backend/internal/ai/llm/openai_test.go b/backend/internal/ai/llm/openai_test.go index 9f2eee2..240e523 100644 --- a/backend/internal/ai/llm/openai_test.go +++ b/backend/internal/ai/llm/openai_test.go @@ -53,7 +53,7 @@ func TestOpenAIService_ChatStream_Success(t *testing.T) { }) defer srv.Close() - svc := NewOpenAIService("test-key", "gpt-4o", srv.URL, 10, zap.NewNop().Sugar()) + svc := NewOpenAIService("test-key", "gpt-4o", srv.URL, 10, 60, zap.NewNop().Sugar()) ch, err := svc.ChatStream(context.Background(), Request{ Text: "这是什么?", @@ -99,7 +99,7 @@ func TestOpenAIService_ChatStream_WithImage(t *testing.T) { }) defer srv.Close() - svc := NewOpenAIService("test-key", "gpt-4o", srv.URL, 10, zap.NewNop().Sugar()) + svc := NewOpenAIService("test-key", "gpt-4o", srv.URL, 10, 60, zap.NewNop().Sugar()) ch, err := svc.ChatStream(context.Background(), Request{ Image: []byte("fake-jpeg-data"), @@ -123,7 +123,7 @@ func TestOpenAIService_ChatStream_WithHistory(t *testing.T) { }) defer srv.Close() - svc := NewOpenAIService("test-key", "gpt-4o", srv.URL, 10, zap.NewNop().Sugar()) + svc := NewOpenAIService("test-key", "gpt-4o", srv.URL, 10, 60, zap.NewNop().Sugar()) ch, err := svc.ChatStream(context.Background(), Request{ Text: "继续", @@ -149,7 +149,7 @@ func TestOpenAIService_ChatStream_APIError(t *testing.T) { }) defer srv.Close() - svc := NewOpenAIService("bad-key", "gpt-4o", srv.URL, 10, zap.NewNop().Sugar()) + svc := NewOpenAIService("bad-key", "gpt-4o", srv.URL, 10, 60, zap.NewNop().Sugar()) _, err := svc.ChatStream(context.Background(), Request{ Text: "test", @@ -172,7 +172,7 @@ func TestOpenAIService_ChatStream_Timeout(t *testing.T) { }) defer srv.Close() - svc := NewOpenAIService("test-key", "gpt-4o", srv.URL, 1, zap.NewNop().Sugar()) // 1s timeout + svc := NewOpenAIService("test-key", "gpt-4o", srv.URL, 1, 60, zap.NewNop().Sugar()) // 1s timeout ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() @@ -204,7 +204,7 @@ func TestOpenAIService_ChatStream_UsageInResponse(t *testing.T) { }) defer srv.Close() - svc := NewOpenAIService("test-key", "gpt-4o", srv.URL, 10, zap.NewNop().Sugar()) + svc := NewOpenAIService("test-key", "gpt-4o", srv.URL, 10, 60, zap.NewNop().Sugar()) ch, err := svc.ChatStream(context.Background(), Request{Text: "test"}) if err != nil { diff --git a/backend/internal/ai/stt/deepgram_test.go b/backend/internal/ai/stt/deepgram_test.go index 2d00e53..70cc52e 100644 --- a/backend/internal/ai/stt/deepgram_test.go +++ b/backend/internal/ai/stt/deepgram_test.go @@ -74,7 +74,7 @@ func TestDeepgramService_Recognize_Success(t *testing.T) { }) defer srv.Close() - svc := NewDeepgramService("test-key", "", wsToWss(srv.URL)+"/v1/listen", zap.NewNop().Sugar()) + svc := NewDeepgramService("test-key", "", wsToWss(srv.URL)+"/v1/listen", 0, zap.NewNop().Sugar()) text, err := svc.Recognize(context.Background(), []byte("fake-pcm-audio"), Options{ Encoding: "pcm_s16le", @@ -90,7 +90,7 @@ func TestDeepgramService_Recognize_Success(t *testing.T) { } func TestDeepgramService_Recognize_EmptyAudio(t *testing.T) { - svc := NewDeepgramService("test-key", "", "ws://localhost", zap.NewNop().Sugar()) + svc := NewDeepgramService("test-key", "", "ws://localhost", 0, zap.NewNop().Sugar()) _, err := svc.Recognize(context.Background(), nil, Options{}) if err == nil { t.Fatal("Recognize() with empty audio should return error") @@ -98,7 +98,7 @@ func TestDeepgramService_Recognize_EmptyAudio(t *testing.T) { } func TestDeepgramService_Recognize_ConnectError(t *testing.T) { - svc := NewDeepgramService("test-key", "", "ws://localhost:1", zap.NewNop().Sugar()) + svc := NewDeepgramService("test-key", "", "ws://localhost:1", 0, zap.NewNop().Sugar()) ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() @@ -117,7 +117,7 @@ func TestDeepgramService_Recognize_Timeout(t *testing.T) { }) defer srv.Close() - svc := NewDeepgramService("test-key", "", wsToWss(srv.URL)+"/v1/listen", zap.NewNop().Sugar()) + svc := NewDeepgramService("test-key", "", wsToWss(srv.URL)+"/v1/listen", 0, zap.NewNop().Sugar()) ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) defer cancel() @@ -147,7 +147,7 @@ func TestDeepgramService_Recognize_MultipleFinals(t *testing.T) { }) defer srv.Close() - svc := NewDeepgramService("test-key", "", wsToWss(srv.URL)+"/v1/listen", zap.NewNop().Sugar()) + svc := NewDeepgramService("test-key", "", wsToWss(srv.URL)+"/v1/listen", 0, zap.NewNop().Sugar()) text, err := svc.Recognize(context.Background(), []byte("audio"), Options{}) if err != nil { @@ -159,7 +159,7 @@ func TestDeepgramService_Recognize_MultipleFinals(t *testing.T) { } func TestDeepgramService_buildURL(t *testing.T) { - svc := NewDeepgramService("key", "", "wss://api.deepgram.com/v1/listen", zap.NewNop().Sugar()) + svc := NewDeepgramService("key", "", "wss://api.deepgram.com/v1/listen", 0, zap.NewNop().Sugar()) tests := []struct { name string diff --git a/backend/internal/ai/stt/mimo_test.go b/backend/internal/ai/stt/mimo_test.go index 435a926..78ab038 100644 --- a/backend/internal/ai/stt/mimo_test.go +++ b/backend/internal/ai/stt/mimo_test.go @@ -14,7 +14,7 @@ import ( func newTestMiMoService(handler http.HandlerFunc) (*MiMoService, *httptest.Server) { srv := httptest.NewServer(handler) - s := NewMiMoService("test-key", "mimo-v2.5-asr", srv.URL, zap.NewNop().Sugar()) + s := NewMiMoService("test-key", "mimo-v2.5-asr", srv.URL, 0, zap.NewNop().Sugar()) return s, srv } diff --git a/backend/internal/ai/tts/mimo_test.go b/backend/internal/ai/tts/mimo_test.go index 0e419bf..0d6b1de 100644 --- a/backend/internal/ai/tts/mimo_test.go +++ b/backend/internal/ai/tts/mimo_test.go @@ -93,7 +93,7 @@ func TestMiMoService_SynthesizeStream_Success(t *testing.T) { }) defer srv.Close() - svc := NewMiMoService("test-key", "", "冰糖", srv.URL, 5, zap.NewNop().Sugar()) + svc := NewMiMoService("test-key", "mimo-v2.5-tts", "冰糖", srv.URL, 5, 30, zap.NewNop().Sugar()) textStream := sendSentences("你好", "世界", "!") @@ -145,7 +145,7 @@ func TestMiMoService_SynthesizeStream_APIError(t *testing.T) { }) defer srv.Close() - svc := NewMiMoService("test-key", "", "冰糖", srv.URL, 5, zap.NewNop().Sugar()) + svc := NewMiMoService("test-key", "mimo-v2.5-tts", "冰糖", srv.URL, 5, 30, zap.NewNop().Sugar()) textStream := sendSentences("你好") @@ -178,7 +178,7 @@ func TestMiMoService_SynthesizeStream_Timeout(t *testing.T) { defer srv.Close() // 1 秒超时 - svc := NewMiMoService("test-key", "", "冰糖", srv.URL, 1, zap.NewNop().Sugar()) + svc := NewMiMoService("test-key", "mimo-v2.5-tts", "冰糖", srv.URL, 1, 30, zap.NewNop().Sugar()) textStream := sendSentences("很长的句子") @@ -214,7 +214,7 @@ func TestMiMoService_SynthesizeStream_EmptyText(t *testing.T) { }) defer srv.Close() - svc := NewMiMoService("test-key", "", "冰糖", srv.URL, 5, zap.NewNop().Sugar()) + svc := NewMiMoService("test-key", "mimo-v2.5-tts", "冰糖", srv.URL, 5, 30, zap.NewNop().Sugar()) // 空句子应该被跳过 textStream := sendSentences("", "你好", "") @@ -248,7 +248,7 @@ func TestMiMoService_SynthesizeStream_ContextCancelled(t *testing.T) { }) defer srv.Close() - svc := NewMiMoService("test-key", "", "冰糖", srv.URL, 5, zap.NewNop().Sugar()) + svc := NewMiMoService("test-key", "mimo-v2.5-tts", "冰糖", srv.URL, 5, 30, zap.NewNop().Sugar()) textStream := make(chan string, 3) textStream <- "第一句" @@ -290,7 +290,7 @@ func TestMiMoService_SynthesizeStream_PartialFailure(t *testing.T) { }) defer srv.Close() - svc := NewMiMoService("test-key", "", "冰糖", srv.URL, 5, zap.NewNop().Sugar()) + svc := NewMiMoService("test-key", "mimo-v2.5-tts", "冰糖", srv.URL, 5, 30, zap.NewNop().Sugar()) textStream := sendSentences("第一句", "第二句", "第三句") @@ -329,7 +329,7 @@ func TestMiMoService_SynthesizeStream_CustomVoice(t *testing.T) { }) defer srv.Close() - svc := NewMiMoService("test-key", "", "冰糖", srv.URL, 5, zap.NewNop().Sugar()) + svc := NewMiMoService("test-key", "mimo-v2.5-tts", "冰糖", srv.URL, 5, 30, zap.NewNop().Sugar()) textStream := sendSentences("你好") @@ -360,7 +360,7 @@ func TestMiMoService_SynthesizeStream_DefaultVoice(t *testing.T) { defer srv.Close() // 不指定 voice - svc := NewMiMoService("test-key", "", "", srv.URL, 5, zap.NewNop().Sugar()) + svc := NewMiMoService("test-key", "mimo-v2.5-tts", "冰糖", srv.URL, 5, 30, zap.NewNop().Sugar()) textStream := sendSentences("你好") @@ -381,7 +381,7 @@ func TestMiMoService_SynthesizeStream_EmptyAudioData(t *testing.T) { }) defer srv.Close() - svc := NewMiMoService("test-key", "", "冰糖", srv.URL, 5, zap.NewNop().Sugar()) + svc := NewMiMoService("test-key", "mimo-v2.5-tts", "冰糖", srv.URL, 5, 30, zap.NewNop().Sugar()) textStream := sendSentences("你好") diff --git a/backend/internal/ai/tts/openai_test.go b/backend/internal/ai/tts/openai_test.go index 36afd31..7a0d7f5 100644 --- a/backend/internal/ai/tts/openai_test.go +++ b/backend/internal/ai/tts/openai_test.go @@ -58,7 +58,7 @@ func TestOpenAIService_SynthesizeStream_Success(t *testing.T) { }) defer srv.Close() - svc := NewOpenAIService("test-key", "", "alloy", srv.URL, 1.0, 5, zap.NewNop().Sugar()) + svc := NewOpenAIService("test-key", "tts-1", "alloy", srv.URL, 1.0, 5, 30, zap.NewNop().Sugar()) textStream := sendSentences("你好", "世界", "!") @@ -110,7 +110,7 @@ func TestOpenAIService_SynthesizeStream_APIError(t *testing.T) { }) defer srv.Close() - svc := NewOpenAIService("test-key", "", "alloy", srv.URL, 1.0, 5, zap.NewNop().Sugar()) + svc := NewOpenAIService("test-key", "tts-1", "alloy", srv.URL, 1.0, 5, 30, zap.NewNop().Sugar()) textStream := sendSentences("你好") @@ -142,7 +142,7 @@ func TestOpenAIService_SynthesizeStream_Timeout(t *testing.T) { defer srv.Close() // 1 秒超时 - svc := NewOpenAIService("test-key", "", "alloy", srv.URL, 1.0, 1, zap.NewNop().Sugar()) + svc := NewOpenAIService("test-key", "tts-1", "alloy", srv.URL, 1.0, 1, 30, zap.NewNop().Sugar()) textStream := sendSentences("很长的句子") @@ -177,7 +177,7 @@ func TestOpenAIService_SynthesizeStream_EmptyText(t *testing.T) { }) defer srv.Close() - svc := NewOpenAIService("test-key", "", "alloy", srv.URL, 1.0, 5, zap.NewNop().Sugar()) + svc := NewOpenAIService("test-key", "tts-1", "alloy", srv.URL, 1.0, 5, 30, zap.NewNop().Sugar()) // 空句子应该被跳过 textStream := sendSentences("", "你好", "") @@ -210,7 +210,7 @@ func TestOpenAIService_SynthesizeStream_ContextCancelled(t *testing.T) { }) defer srv.Close() - svc := NewOpenAIService("test-key", "", "alloy", srv.URL, 1.0, 5, zap.NewNop().Sugar()) + svc := NewOpenAIService("test-key", "tts-1", "alloy", srv.URL, 1.0, 5, 30, zap.NewNop().Sugar()) // 发送多个句子,但在第一个后取消 textStream := make(chan string, 3) @@ -252,7 +252,7 @@ func TestOpenAIService_SynthesizeStream_PartialFailure(t *testing.T) { }) defer srv.Close() - svc := NewOpenAIService("test-key", "", "alloy", srv.URL, 1.0, 5, zap.NewNop().Sugar()) + svc := NewOpenAIService("test-key", "tts-1", "alloy", srv.URL, 1.0, 5, 30, zap.NewNop().Sugar()) textStream := sendSentences("第一句", "第二句", "第三句") @@ -286,7 +286,7 @@ func TestOpenAIService_SynthesizeStream_CustomVoice(t *testing.T) { }) defer srv.Close() - svc := NewOpenAIService("test-key", "", "alloy", srv.URL, 1.0, 5, zap.NewNop().Sugar()) + svc := NewOpenAIService("test-key", "tts-1", "alloy", srv.URL, 1.0, 5, 30, zap.NewNop().Sugar()) textStream := sendSentences("你好") diff --git a/backend/internal/orchestrator/pipeline_test.go b/backend/internal/orchestrator/pipeline_test.go index 2a36a29..ad9b847 100644 --- a/backend/internal/orchestrator/pipeline_test.go +++ b/backend/internal/orchestrator/pipeline_test.go @@ -13,6 +13,7 @@ import ( "github.com/hhs/camtalk/internal/ai/llm" "github.com/hhs/camtalk/internal/ai/stt" "github.com/hhs/camtalk/internal/ai/tts" + "github.com/hhs/camtalk/internal/config" "github.com/hhs/camtalk/internal/logger" "github.com/hhs/camtalk/internal/models" ) @@ -254,7 +255,12 @@ func TestProcessQuery_Success(t *testing.T) { mockSender.On("SendTTSAudio", mock.Anything).Return(nil) // 创建 Pipeline - pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, "gpt-4o", "alloy", 1.0) + pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, &config.Config{ + AI: config.AIConfig{ + LLM: config.LLMConfig{Model: "gpt-4o"}, + TTS: config.TTSConfig{Voice: "alloy", Speed: 1.0, OutputFormat: "mp3", SampleRate: 24000}, + }, + }) // 执行 ctx := context.Background() @@ -305,7 +311,12 @@ func TestProcessQuery_STTError(t *testing.T) { mockSender.On("SendError", mock.Anything).Return(nil) - pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, "gpt-4o", "alloy", 1.0) + pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, &config.Config{ + AI: config.AIConfig{ + LLM: config.LLMConfig{Model: "gpt-4o"}, + TTS: config.TTSConfig{Voice: "alloy", Speed: 1.0, OutputFormat: "mp3", SampleRate: 24000}, + }, + }) ctx := context.Background() err := pipeline.ProcessQuery(ctx, "session-123", req, nil, mockSender) @@ -357,7 +368,12 @@ func TestProcessQuery_LLMError(t *testing.T) { mockSender.On("SendError", mock.Anything).Return(nil) - pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, "gpt-4o", "alloy", 1.0) + pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, &config.Config{ + AI: config.AIConfig{ + LLM: config.LLMConfig{Model: "gpt-4o"}, + TTS: config.TTSConfig{Voice: "alloy", Speed: 1.0, OutputFormat: "mp3", SampleRate: 24000}, + }, + }) ctx := context.Background() err := pipeline.ProcessQuery(ctx, "session-123", req, nil, mockSender) @@ -415,7 +431,12 @@ func TestProcessQuery_TTSError(t *testing.T) { mockTTS.On("SynthesizeStream", mock.Anything, mock.Anything, mock.Anything). Return(nil, errors.New("TTS service unavailable")) - pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, "gpt-4o", "alloy", 1.0) + pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, &config.Config{ + AI: config.AIConfig{ + LLM: config.LLMConfig{Model: "gpt-4o"}, + TTS: config.TTSConfig{Voice: "alloy", Speed: 1.0, OutputFormat: "mp3", SampleRate: 24000}, + }, + }) ctx := context.Background() err := pipeline.ProcessQuery(ctx, "session-123", req, nil, mockSender) @@ -485,7 +506,12 @@ func TestProcessQuery_ContextCancelled(t *testing.T) { }() mockTTS.On("SynthesizeStream", mock.Anything, mock.Anything, mock.Anything).Return((<-chan tts.Chunk)(ttsCh), nil) - pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, "gpt-4o", "alloy", 1.0) + pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, &config.Config{ + AI: config.AIConfig{ + LLM: config.LLMConfig{Model: "gpt-4o"}, + TTS: config.TTSConfig{Voice: "alloy", Speed: 1.0, OutputFormat: "mp3", SampleRate: 24000}, + }, + }) // 创建可取消的上下文 ctx, cancel := context.WithCancel(context.Background()) @@ -545,7 +571,12 @@ func TestProcessQuery_DisabledTTS(t *testing.T) { mockSender.On("SendLLMChunk", mock.Anything).Return(nil) mockSender.On("SendLLMDone", mock.Anything).Return(nil) - pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, "gpt-4o", "alloy", 1.0) + pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, &config.Config{ + AI: config.AIConfig{ + LLM: config.LLMConfig{Model: "gpt-4o"}, + TTS: config.TTSConfig{Voice: "alloy", Speed: 1.0, OutputFormat: "mp3", SampleRate: 24000}, + }, + }) ctx := context.Background() err := pipeline.ProcessQuery(ctx, "session-123", req, nil, mockSender) @@ -617,7 +648,12 @@ func TestProcessQuery_InvalidAudio(t *testing.T) { mockSender.On("SendError", mock.Anything).Return(nil) - pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, "gpt-4o", "alloy", 1.0) + pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, &config.Config{ + AI: config.AIConfig{ + LLM: config.LLMConfig{Model: "gpt-4o"}, + TTS: config.TTSConfig{Voice: "alloy", Speed: 1.0, OutputFormat: "mp3", SampleRate: 24000}, + }, + }) ctx := context.Background() err := pipeline.ProcessQuery(ctx, "session-123", req, nil, mockSender) @@ -650,7 +686,12 @@ func TestProcessQuery_SessionNotFound(t *testing.T) { mockSender.On("SendError", mock.Anything).Return(nil) - pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, "gpt-4o", "alloy", 1.0) + pipeline := New(mockSTT, mockLLM, mockTTS, mockSession, &config.Config{ + AI: config.AIConfig{ + LLM: config.LLMConfig{Model: "gpt-4o"}, + TTS: config.TTSConfig{Voice: "alloy", Speed: 1.0, OutputFormat: "mp3", SampleRate: 24000}, + }, + }) ctx := context.Background() err := pipeline.ProcessQuery(ctx, "session-123", req, nil, mockSender) diff --git a/backend/internal/ws/handler_test.go b/backend/internal/ws/handler_test.go index 97cf697..2872d5c 100644 --- a/backend/internal/ws/handler_test.go +++ b/backend/internal/ws/handler_test.go @@ -13,6 +13,7 @@ import ( "github.com/stretchr/testify/require" "context" + "github.com/hhs/camtalk/internal/config" "github.com/hhs/camtalk/internal/logger" "github.com/hhs/camtalk/internal/models" "github.com/hhs/camtalk/internal/orchestrator" @@ -138,7 +139,12 @@ func setupTestServer(t *testing.T, orch orchestrator.Orchestrator) (*httptest.Se t.Cleanup(func() { sessionMgr.Stop() }) r := gin.New() - r.GET("/ws", ServeWS(sessionMgr, orch)) + cfg := &config.Config{ + App: config.AppConfig{Version: "test"}, + Server: config.ServerConfig{HeartbeatInterval: 30, HeartbeatTimeout: 60}, + Session: config.SessionConfig{MaxHistory: 20}, + } + r.GET("/ws", ServeWS(sessionMgr, orch, cfg)) srv := httptest.NewServer(r) @@ -181,7 +187,7 @@ func TestWS_Connected(t *testing.T) { msg := readJSON(t, conn) assert.Equal(t, "connected", msg["type"]) assert.NotEmpty(t, msg["session_id"]) - assert.Equal(t, "0.1.0", msg["server_version"]) + assert.Equal(t, "test", msg["server_version"]) } // TestWS_PingPong 验证 ping/pong 心跳。