Composition roots copy Parts once so HTTP/CLI handlers stop calling locator getters on every request. Co-authored-by: Cursor <cursoragent@cursor.com>
57 lines
1.7 KiB
Go
57 lines
1.7 KiB
Go
package server_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"chat-agent-system/internal/bizdev"
|
|
"chat-agent-system/internal/config"
|
|
"chat-agent-system/internal/server"
|
|
)
|
|
|
|
func TestServer_ChatRecategorizeAPI(t *testing.T) {
|
|
cfg := &config.Config{
|
|
Env: "dev",
|
|
HTTPHost: "127.0.0.1",
|
|
HTTPPort: 8892,
|
|
DataDir: t.TempDir(),
|
|
}
|
|
svc := bizdev.MustNewService(t, cfg)
|
|
srv := server.NewServer(cfg, svc)
|
|
handler := srv.Routes()
|
|
|
|
sessID := "sess_recategorize_api_001"
|
|
ctx := t.Context()
|
|
ctxMgr := bizdev.Snapshot(svc).Ctx
|
|
|
|
// 注入种子消息
|
|
_, _ = ctxMgr.RecordUserMessageWithID(ctx, sessID, 1, "user", "请问二类注册审批周期?", "QUERY_REGULATIONS", "", nil)
|
|
_, _ = ctxMgr.RecordUserMessageWithID(ctx, sessID, 0, "assistant", "根据现有规定,一般需 6-9 个月。", "", "", nil)
|
|
|
|
// 调用 POST /api/v1/chat/recategorize
|
|
reqBody := `{"session_id":"` + sessID + `"}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/chat/recategorize", strings.NewReader(reqBody))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rec := httptest.NewRecorder()
|
|
handler.ServeHTTP(rec, req)
|
|
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("expected status 200, got %d: %s", rec.Code, rec.Body.String())
|
|
}
|
|
|
|
var res map[string]any
|
|
if err := json.Unmarshal(rec.Body.Bytes(), &res); err != nil {
|
|
t.Fatalf("json unmarshal failed: %v", err)
|
|
}
|
|
|
|
if success, ok := res["success"].(bool); !ok || !success {
|
|
t.Fatalf("expected success: true, got %v", res)
|
|
}
|
|
if totalProcessed, ok := res["total_processed"].(float64); !ok || totalProcessed < 2 {
|
|
t.Fatalf("expected total_processed >= 2, got %v", res["total_processed"])
|
|
}
|
|
}
|