File size: 8,388 Bytes
e48a2d8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | package panel
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/misakano7545/workbuddy2api-panel/internal/auth"
"github.com/misakano7545/workbuddy2api-panel/internal/upstream"
)
// expert_use_* 系列(专家页搜索并召唤 → 完成一次对话)的回归测试。
//
// 判据链与 Expert_lighthouse / WorkBuddy_Valorant 同源,但专家 id 各不相同、
// 且 market 关键词会命中多个同名专家(display_name_zh 与 profession_zh 都参与匹配)。
// 这里钉住三件事:登记完整性、按 expert_id 精确落位(不是取首条)、搜索失败时的静态兜底。
// TestExpertExperienceSpecsRegistered 登记表与 autoActions 双向一致。
// 只在一边登记就是静默故障:表里有码但 autoActions 没条目 → 任务中心不渲染按钮;
// autoActions 有条目但表里没参数 → 点击后 runExpertExperience 直接报"未登记"。
func TestExpertExperienceSpecsRegistered(t *testing.T) {
if len(expertExperienceTasks) == 0 {
t.Fatal("expertExperienceTasks 为空")
}
seen := map[string]string{}
for code, spec := range expertExperienceTasks {
act := autoActionFor(code)
if act == nil {
t.Errorf("expertExperienceTasks 有 %s、autoActions 缺(按钮点了会返回无动作)", code)
continue
}
if act.run == nil {
t.Errorf("%s 在 autoActions 里没有 run(永远跑不了)", code)
}
if strings.TrimSpace(act.Desc) == "" {
t.Errorf("%s 缺 Desc", code)
}
if !strings.HasPrefix(spec.ExpertID, "ex_") {
t.Errorf("%s 的 ExpertID=%q 不是市场真实 id 形态(自造 id 不计数)", code, spec.ExpertID)
}
if spec.ExpertType != "agent" {
t.Errorf("%s 的 ExpertType=%q,want agent", code, spec.ExpertType)
}
if spec.DisplayNameZH == "" || spec.ProfessionZH == "" || spec.Version == "" {
t.Errorf("%s 参数不全: %#v", code, spec)
}
if prev, dup := seen[spec.ExpertID]; dup {
t.Errorf("%s 与 %s 用了同一个 ExpertID %s", code, prev, spec.ExpertID)
}
seen[spec.ExpertID] = code
}
// 反向:autoActions 里的 expert_use_* 必须都有参数表条目。
for _, act := range autoActions {
if !strings.HasPrefix(act.TaskCode, "expert_use_") {
continue
}
if _, ok := expertExperienceTasks[act.TaskCode]; !ok {
t.Errorf("autoActions 有 %s、expertExperienceTasks 缺参数(执行必报未登记)", act.TaskCode)
}
}
}
// expertFake 专家体验链的假上游:market 搜索 / chat SSE / report 三端点。
type expertFake struct {
mu sync.Mutex
reported []map[string]any
searchBody map[string]any
marketFail bool
marketList []upstream.MarketExpert
searchCalls int
}
func (f *expertFake) events() []map[string]any {
f.mu.Lock()
defer f.mu.Unlock()
out := make([]map[string]any, len(f.reported))
copy(out, f.reported)
return out
}
func newExpertExperienceTestPanel(t *testing.T, f *expertFake) (*Panel, *auth.Auth) {
t.Helper()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.URL.Path == "/portal/operation-platform/market/expert/list":
raw, _ := io.ReadAll(io.LimitReader(r.Body, 1<<20))
var body map[string]any
_ = json.Unmarshal(raw, &body)
f.mu.Lock()
f.searchCalls++
f.searchBody = body
fail, list := f.marketFail, f.marketList
f.mu.Unlock()
if fail {
http.Error(w, "market unavailable", http.StatusBadGateway)
return
}
out, _ := json.Marshal(map[string]any{
"code": 0, "msg": "OK",
"data": map[string]any{"total_count": len(list), "experts": list},
})
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(out)
case r.URL.Path == "/v2/chat/completions":
w.Header().Set("Content-Type", "text/event-stream")
fmt.Fprint(w, "data: {\"id\":\"cmb-0123456789abcdef0123456789abcdef\",\"choices\":[]}\n\n")
case r.URL.Path == "/v2/report":
raw, _ := io.ReadAll(io.LimitReader(r.Body, 1<<20))
var evs []map[string]any
_ = json.Unmarshal(raw, &evs)
f.mu.Lock()
f.reported = append(f.reported, evs...)
f.mu.Unlock()
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"code":0,"msg":"OK","data":{}}`)
default:
http.NotFound(w, r)
}
}))
t.Cleanup(srv.Close)
client := upstream.New()
client.ChatBaseCN, client.WebBaseCN, client.BillingBaseCN = srv.URL, srv.URL, srv.URL
p := New(Config{Upstream: client})
return p, &auth.Auth{UID: "expert-experience-test", AccessToken: "token"}
}
// findEvent 在已上报事件里找指定 eventCode 的第一条。
func findEvent(t *testing.T, events []map[string]any, code string) map[string]any {
t.Helper()
for _, ev := range events {
if ev["eventCode"] == code {
return ev
}
}
t.Fatalf("没有上报 %s 事件(共 %d 条)", code, len(events))
return nil
}
// TestRunExpertExperiencePicksExpertByID 关键词命中多个同名专家时必须按 id 落位。
// 假市场把「同名的另一个专家」放在首条、把真实条目放在第二条且 version 与静态表不同:
// 取首条会打错专家(不计数),不读市场则 version 用的是表内旧值。
func TestRunExpertExperiencePicksExpertByID(t *testing.T) {
const code = "expert_use_douyinyin"
spec := expertExperienceTasks[code]
f := &expertFake{marketList: []upstream.MarketExpert{
{ExpertID: "ex_WmcyDlsTPaiS", ExpertType: "agent", DisplayNameZH: "抖音策略师",
ProfessionZH: "抖音全域增长策略师", Version: "1.0.0"},
{ExpertID: spec.ExpertID, ExpertType: "agent", DisplayNameZH: "斗音音",
ProfessionZH: "抖音策略师", Version: "9.9.9"},
}}
p, a := newExpertExperienceTestPanel(t, f)
if _, err := p.runExpertExperience(a, code); err != nil {
t.Fatalf("runExpertExperience: %v", err)
}
if f.searchBody["keyword"] != spec.ProfessionZH {
t.Fatalf("搜索关键词 = %v, want %q", f.searchBody["keyword"], spec.ProfessionZH)
}
events := f.events()
summon := findEvent(t, events, "expert_summon_click")
if summon["id"] != spec.ExpertID {
t.Fatalf("召唤的专家 = %v, want %s", summon["id"], spec.ExpertID)
}
if summon["version"] != "9.9.9" {
t.Fatalf("召唤事件 version = %v, want 市场值 9.9.9(说明没读市场)", summon["version"])
}
created := findEvent(t, events, "agent_task_created")
if created["has_expert"] != true || created["expert_id"] != spec.ExpertID {
t.Fatalf("agent_task_created 未带专家: has_expert=%v expert_id=%v", created["has_expert"], created["expert_id"])
}
use := findEvent(t, events, "expert_actual_use")
if use["id"] != spec.ExpertID || use["name"] != "斗音音" {
t.Fatalf("expert_actual_use 专家 = %v/%v, want %s/斗音音", use["id"], use["name"], spec.ExpertID)
}
if use["mode"] != "LOCAL" {
t.Fatalf("expert_actual_use mode = %v, want LOCAL", use["mode"])
}
// 对齐真实样本:专家页直连召唤的 actual_use type 为空、cost=0。
if use["type"] != "" || fmt.Sprint(use["cost"]) != "0" {
t.Fatalf("expert_actual_use type/cost = %v/%v, want \"\"/0", use["type"], use["cost"])
}
if use["requestId"] != "cmb-0123456789abcdef0123456789abcdef" {
t.Fatalf("expert_actual_use requestId = %v(必须是服务端真实 id)", use["requestId"])
}
}
// TestRunExpertExperienceFallsBackToStaticSpec 市场不可用时仍要能上报(召唤链不依赖搜索)。
func TestRunExpertExperienceFallsBackToStaticSpec(t *testing.T) {
const code = "expert_use_maidehao"
spec := expertExperienceTasks[code]
f := &expertFake{marketFail: true}
p, a := newExpertExperienceTestPanel(t, f)
if _, err := p.runExpertExperience(a, code); err != nil {
t.Fatalf("runExpertExperience: %v", err)
}
if f.searchCalls != 1 {
t.Fatalf("market 搜索调用 = %d, want 1", f.searchCalls)
}
summon := findEvent(t, f.events(), "expert_summon_click")
if summon["id"] != spec.ExpertID || summon["version"] != spec.Version {
t.Fatalf("兜底参数 = %v/%v, want %s/%s", summon["id"], summon["version"], spec.ExpertID, spec.Version)
}
}
// TestRunExpertExperienceUnknownCode 未登记任务码必须报错而不是静默成功。
func TestRunExpertExperienceUnknownCode(t *testing.T) {
p, a := newExpertExperienceTestPanel(t, &expertFake{})
if _, err := p.runExpertExperience(a, "expert_use_not_registered"); err == nil {
t.Fatal("未登记的任务码应当报错")
}
}
|