From dfea092583c3ccd2c975c25047e8419a138ce38e Mon Sep 17 00:00:00 2001 From: latticeon <35923185+latticeon@users.noreply.github.com> Date: Mon, 16 Mar 2026 11:58:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0=E6=B5=8B=E8=AF=95=20m?= =?UTF-8?q?ock=20=E7=BB=93=E6=9E=84=E4=BD=93=E4=BB=A5=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=9A=84=E6=8E=A5=E5=8F=A3=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 会话管理功能新增接口方法后,同步更新测试 mock 结构体: - mockOpenAIConfig: 添加 AutoDeleteSessions() 方法 - streamStatusDSStub: 添加 DeleteAllSessionsForToken() 方法 - testingDSMock: 添加 DeleteAllSessionsForToken() 和 GetSessionCountForToken() 方法 同时修复 client_session_delete.go 中 fmt.Errorf 使用非常量格式字符串的编译错误,改用 errors.New() --- internal/adapter/openai/deps_injection_test.go | 1 + internal/adapter/openai/stream_status_test.go | 4 ++++ internal/admin/handler_accounts_testing_test.go | 9 +++++++++ internal/deepseek/client_session_delete.go | 2 +- 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/internal/adapter/openai/deps_injection_test.go b/internal/adapter/openai/deps_injection_test.go index 6286c0c..9025ef3 100644 --- a/internal/adapter/openai/deps_injection_test.go +++ b/internal/adapter/openai/deps_injection_test.go @@ -19,6 +19,7 @@ func (m mockOpenAIConfig) ToolcallMode() string { return m.toolMo func (m mockOpenAIConfig) ToolcallEarlyEmitConfidence() string { return m.earlyEmit } func (m mockOpenAIConfig) ResponsesStoreTTLSeconds() int { return m.responsesTTL } func (m mockOpenAIConfig) EmbeddingsProvider() string { return m.embedProv } +func (m mockOpenAIConfig) AutoDeleteSessions() bool { return false } func TestNormalizeOpenAIChatRequestWithConfigInterface(t *testing.T) { cfg := mockOpenAIConfig{ diff --git a/internal/adapter/openai/stream_status_test.go b/internal/adapter/openai/stream_status_test.go index 4d66b46..9bd6ccd 100644 --- a/internal/adapter/openai/stream_status_test.go +++ b/internal/adapter/openai/stream_status_test.go @@ -53,6 +53,10 @@ func (m streamStatusDSStub) CallCompletion(_ context.Context, _ *auth.RequestAut return m.resp, nil } +func (m streamStatusDSStub) DeleteAllSessionsForToken(_ context.Context, _ string) (int, error) { + return 0, nil +} + func makeOpenAISSEHTTPResponse(lines ...string) *http.Response { body := strings.Join(lines, "\n") if !strings.HasSuffix(body, "\n") { diff --git a/internal/admin/handler_accounts_testing_test.go b/internal/admin/handler_accounts_testing_test.go index 4c84fd6..47013c1 100644 --- a/internal/admin/handler_accounts_testing_test.go +++ b/internal/admin/handler_accounts_testing_test.go @@ -9,6 +9,7 @@ import ( "ds2api/internal/auth" "ds2api/internal/config" + "ds2api/internal/deepseek" ) type testingDSMock struct { @@ -38,6 +39,14 @@ func (m *testingDSMock) CallCompletion(_ context.Context, _ *auth.RequestAuth, _ return nil, errors.New("should not call CallCompletion in this test") } +func (m *testingDSMock) DeleteAllSessionsForToken(_ context.Context, _ string) (int, error) { + return 0, nil +} + +func (m *testingDSMock) GetSessionCountForToken(_ context.Context, _ string) (*deepseek.SessionStats, error) { + return &deepseek.SessionStats{Success: true}, nil +} + func TestTestAccount_BatchModeOnlyCreatesSession(t *testing.T) { t.Setenv("DS2API_CONFIG_JSON", `{"accounts":[{"email":"batch@example.com","password":"pwd","token":""}]}`) store := config.LoadStore() diff --git a/internal/deepseek/client_session_delete.go b/internal/deepseek/client_session_delete.go index 2f49fc5..a34a56f 100644 --- a/internal/deepseek/client_session_delete.go +++ b/internal/deepseek/client_session_delete.go @@ -107,7 +107,7 @@ func (c *Client) DeleteSessionForToken(ctx context.Context, token string, sessio if status != http.StatusOK || code != 0 { msg, _ := resp["msg"].(string) result.ErrorMessage = fmt.Sprintf("request failed: status=%d, code=%d, msg=%s", status, code, msg) - return result, fmt.Errorf(result.ErrorMessage) + return result, errors.New(result.ErrorMessage) } result.Success = true