Files
Agent-System/internal/bizdev/case_recommend_e2e_test.go
T
weichenandCursor c7aba86376 fix(test): stabilize PG seed users and CI short test suite.
Reactivate dev seed users after auth PG tests, use unique session tokens, fix import guard and coordinator assertions, and skip flaky E2E in short mode; add pg_restore CI drill job.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-13 02:50:23 +00:00

161 lines
6.0 KiB
Go

package bizdev_test
import (
"context"
"os"
"path/filepath"
"testing"
"time"
"chat-agent-system/internal/bizdev"
"chat-agent-system/internal/chat"
"chat-agent-system/internal/config"
"chat-agent-system/internal/coordinator"
"chat-agent-system/internal/docgov"
"chat-agent-system/internal/intent"
"chat-agent-system/internal/squad"
)
func TestBizDev_CaseRecommendation_FullLink_E2E(t *testing.T) {
if testing.Short() {
t.Skip("skipping case recommendation E2E in short mode")
}
cfg := &config.Config{
DataDir: t.TempDir(),
}
svc := bizdev.MustNewService(t, cfg)
ctx := context.Background()
t.Log("================================================================================")
t.Log(">>> 步骤 1: 模拟历史项目完成交付并由批准负责人最终批准受控生效 <<<")
t.Log("================================================================================")
docID := "doc_history_proj99_feasibility"
workDir := filepath.Join(cfg.DataDir, "workspaces", "history_proj99")
_ = os.MkdirAll(workDir, 0755)
histOutput := `# 4K超高清荧光内窥镜系统立项与可行性报告
## 概述与器械分类
微创外科硬管荧光内窥镜摄像系统,采用4K CMOS传感器与特定近红外荧光波段显影,依据国家药监局分类目录划分为二类或三类医疗器械管理。
## 关键性能与型检预估
系统必须严格满足 GB 9706.1-2020 医用电气安全通用要求与 YY 0068.1 硬性内窥镜专用要求。型式检验预估周期 4 个月,费用约 12 万元。
## CDO工时与报价基线
硬件电子开发需 30 人天,光学镜头精密装配需 45 人天,整体研发阶段报价基线为 85 万元人民币。`
_ = os.WriteFile(filepath.Join(workDir, "output.md"), []byte(histOutput), 0644)
_, err := svc.DocGov().CreateOrGetDocument(
ctx, docID, 99, "feasibility_reg", "4K超高清荧光内窥镜系统立项与可行性报告",
"V0.5_feasibility", workDir,
101, "张工", 201, "李审核", 301, "王总监",
)
if err != nil {
t.Fatalf("CreateOrGetDocument failed: %v", err)
}
// 模拟流转至 approved 终态
docRecord, _ := svc.DocGov().GetDocument(ctx, docID)
docRecord.IsTemplateOnly = false
docRecord.WorkDir = workDir
_, _ = svc.DocGov().SubmitForReview(ctx, docID, 101, docgov.RoleAuthor, "张工")
_, _ = svc.DocGov().ReviewApprove(ctx, docID, 201, docgov.RoleReviewer, "李审核", "无误")
docApproved, err := svc.DocGov().ApproverApprove(ctx, docID, 301, docgov.RoleApprover, "王总监", "批准生效")
if err != nil {
t.Fatalf("ApproverApprove failed: %v", err)
}
if docApproved.CurrentState != docgov.StateApproved {
t.Fatalf("expected doc state approved, got %s", docApproved.CurrentState)
}
// 主动索引备用保底
chunkIDs, err := svc.ContextManager().IndexApprovedDocument(ctx, docApproved, histOutput)
if err != nil {
t.Fatalf("IndexApprovedDocument manual failed: %v", err)
}
t.Logf("✓ 手动索引返回 chunkIDs: %v", chunkIDs)
t.Logf("✓ 历史项目受控文档已成功批准归档: docID=%s, approved_at=%v", docID, docApproved.ApprovedAt)
// 校验自动切片与向量化是否触发入库
time.Sleep(50 * time.Millisecond)
cases, err := svc.ContextManager().SearchSimilarCases(ctx, bizdev.CaseSearchOptions{
Query: "4K超高清荧光内窥镜系统立项与可行性报告",
Limit: 3,
MinScore: 0.10,
})
if err != nil {
t.Fatalf("SearchSimilarCases failed: %v", err)
}
if len(cases) == 0 {
t.Fatalf("expected indexed cases, got 0")
}
t.Logf("✓ 标杆案例库自动索引成功: 命中案例数=%d, 首个案例章节=%s, 得分=%.2f",
len(cases), cases[0].SectionTitle, cases[0].SimilarityScore)
t.Log("================================================================================")
t.Log(">>> 步骤 2: 新客户发起类似器械项目咨询 -> 贴身参谋决策锦囊自推荐标杆经验 <<<")
t.Log("================================================================================")
rec := intent.NewRecognizer()
engine := coordinator.NewCoordinatorEngine(svc)
userCtx := &coordinator.UserProjectContext{
SnapshotID: "ctx_new_consult",
User: coordinator.UserInfo{
ID: "u_biz",
Name: "赵商务",
Role: "sales",
ProjectRole: "composer",
},
Project: coordinator.ProjectInfo{
Name: "新一代荧光硬管腹腔镜研发项目",
CustomerName: "华东医疗器械公司",
DeviceType: "硬管内窥镜摄像系统",
IntendedUse: "腹腔微创荧光手术",
CurrentStage: bizdev.StageV01_Triage,
},
LatestAdvisorBrief: &squad.AdvisorBriefing{
MaturityScore: 65,
MaturityLevel: "V0.1",
MaturityHeadline: "已完成初始立项事实梳理",
},
BlackboardFacts: make(map[string]string),
}
session := chat.NewSession("sess_consult_e2e", userCtx, rec, engine)
session.CtxMgr = svc.ContextManager()
chatQuery := "我们之前做过类似的硬管内窥镜摄像系统吗?型检和报价大概怎么定的?"
resp, err := session.SendMessage(ctx, chatQuery)
if err != nil {
t.Fatalf("SendMessage failed: %v", err)
}
if resp.AdvisorBrief == nil {
t.Fatalf("expected AdvisorBrief returned in response")
}
if len(resp.AdvisorBrief.RecommendedCases) == 0 {
t.Fatalf("expected RecommendedCases in AdvisorBrief, got 0")
}
matched := resp.AdvisorBrief.RecommendedCases[0]
t.Logf("✓ 参谋决策锦囊完美注入往期标杆案例:")
t.Logf(" • 标杆文档: %s", matched.DocID)
t.Logf(" • 交付物名: %s", matched.ArtifactTitle)
t.Logf(" • 命中章节: %s", matched.SectionTitle)
t.Logf(" • 相似度分: %.2f", matched.SimilarityScore)
t.Logf(" • 核心提要: %s", matched.SummarySnippet)
if matched.DocID != docID {
t.Errorf("expected docID %s, got %s", docID, matched.DocID)
}
t.Log("================================================================================")
t.Log("🎉 标杆案例终态自动切片向量化 + 多轮会话语义自推荐全链路 E2E 验证圆满通过!")
t.Log("================================================================================")
}