Files
digital-psychology/apps/api/internal/llm/deepseek/client_test.go
T
jackyu66gitandCursor 7ab9add5dd
ci / h5 (push) Canceled after 0s
ci / api (push) Canceled after 0s
ci / ess-docs (push) Canceled after 0s
feat(ops): ECR-007 行为分析与 ECR-008 内容运营后台
落地埋点 ingest/数据看板、首页宫格 CMS 与测评上下架;含账号引导、问答流式与免责声明去重,以及 review P1 审计同事务修复。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-07 02:26:16 +08:00

76 lines
2.1 KiB
Go

package deepseek
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/yuxingu/digital-psychology/apps/api/internal/config"
)
func TestChat_success(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer test-key" {
t.Fatalf("auth header: %s", r.Header.Get("Authorization"))
}
var req chatRequest
_ = json.NewDecoder(r.Body).Decode(&req)
if req.Model != "deepseek-chat" {
t.Fatalf("model=%s", req.Model)
}
_ = json.NewEncoder(w).Encode(chatResponse{
Choices: []struct {
Message Message `json:"message"`
}{{Message: Message{Role: "assistant", Content: "你好,我是成长助手"}}},
})
}))
defer srv.Close()
c := New(config.DeepSeekConfig{
APIKey: "test-key", BaseURL: srv.URL, Model: "deepseek-chat", TimeoutSec: 5,
})
out, err := c.Chat(context.Background(), []Message{{Role: "user", Content: "hi"}})
if err != nil {
t.Fatal(err)
}
if out != "你好,我是成长助手" {
t.Fatalf("got %q", out)
}
}
func TestChatStream_success(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var req chatRequest
_ = json.NewDecoder(r.Body).Decode(&req)
if !req.Stream {
t.Fatal("expected stream=true")
}
w.Header().Set("Content-Type", "text/event-stream")
flusher := w.(http.Flusher)
_, _ = w.Write([]byte("data: {\"choices\":[{\"delta\":{\"content\":\"你好\"}}]}\n\n"))
flusher.Flush()
_, _ = w.Write([]byte("data: {\"choices\":[{\"delta\":{\"content\":\"世界\"}}]}\n\n"))
flusher.Flush()
_, _ = w.Write([]byte("data: [DONE]\n\n"))
flusher.Flush()
}))
defer srv.Close()
c := New(config.DeepSeekConfig{
APIKey: "test-key", BaseURL: srv.URL, Model: "deepseek-chat", TimeoutSec: 5,
})
var got string
out, err := c.ChatStream(context.Background(), []Message{{Role: "user", Content: "hi"}}, func(d string) error {
got += d
return nil
})
if err != nil {
t.Fatal(err)
}
if out != "你好世界" || got != "你好世界" {
t.Fatalf("out=%q got=%q", out, got)
}
}