package handler import ( "encoding/json" "strings" "testing" "github.com/sundynix/sundynix-gateway/internal/dsl" ) // buildVoiceGraph 的产物必须是 dsl.ParseAndAssemble 能吃下的合法图,且带上转写文本。 func TestBuildVoiceGraph_Valid(t *testing.T) { const q = "帮我查一下明天上海的天气" raw := buildVoiceGraph(q, "", "") // 1) 能通过 DSL 解析与拓扑校验(与 HTTP SubmitTask 同一条解析)。 task, err := dsl.ParseAndAssemble(raw) if err != nil { t.Fatalf("语音图未通过 DSL 校验: %v", err) } if task.ID == "" { t.Fatal("task.ID 为空") } // 2) 图里带着转写文本(input 节点)与 JARVIS 系统提示(agent 节点)。 var g struct { Nodes []struct { ID string `json:"id"` Kind string `json:"kind"` Config map[string]any `json:"config"` } `json:"nodes"` Edges []struct { Source, Target string } `json:"edges"` } if err := json.Unmarshal(raw, &g); err != nil { t.Fatalf("反解语音图失败: %v", err) } if len(g.Nodes) != 2 || len(g.Edges) != 1 { t.Fatalf("期望 2 节点 1 边,得 %d 节点 %d 边", len(g.Nodes), len(g.Edges)) } var gotInput, gotAgent bool for _, n := range g.Nodes { switch n.Kind { case "input": gotInput = true if text, _ := n.Config["text"].(string); text != q { t.Errorf("input.text=%q,期望 %q", text, q) } case "agent": gotAgent = true if sys, _ := n.Config["system"].(string); !strings.Contains(sys, "JARVIS") { t.Errorf("agent.system 未含 JARVIS 提示: %q", sys) } } } if !gotInput || !gotAgent { t.Fatalf("缺 input(%v)/agent(%v) 节点", gotInput, gotAgent) } // 边必须连 input→agent(否则 compose 编译后 agent 收不到输入)。 if g.Edges[0].Source != "voice_in" || g.Edges[0].Target != "voice_agent" { t.Errorf("边应为 voice_in→voice_agent,得 %s→%s", g.Edges[0].Source, g.Edges[0].Target) } } // 空转写不该组图触发(提交侧兜底:submitVoiceTask 空转写返错)——这里只校验组图函数对空串仍产出结构。 func TestBuildVoiceGraph_EmptyStillStructured(t *testing.T) { raw := buildVoiceGraph("", "", "") if _, err := dsl.ParseAndAssemble(raw); err != nil { t.Fatalf("空转写图仍应结构合法: %v", err) } } // 用户自定义名字 + 人设应注入到 agent 节点的 system 里(名字替 JARVIS、人设附上)。 func TestBuildVoiceGraph_NamePersonaInjected(t *testing.T) { raw := buildVoiceGraph("你好", "星期五", "简洁专业不说脏话") var g struct { Nodes []struct { Kind string `json:"kind"` Config map[string]any `json:"config"` } `json:"nodes"` } if err := json.Unmarshal(raw, &g); err != nil { t.Fatalf("反解失败: %v", err) } for _, n := range g.Nodes { if n.Kind != "agent" { continue } sys, _ := n.Config["system"].(string) if !strings.Contains(sys, "星期五") { t.Errorf("system 未含自定义名字「星期五」: %q", sys) } if strings.Contains(sys, "JARVIS") { t.Errorf("有自定义名字时不应再出现 JARVIS: %q", sys) } if !strings.Contains(sys, "简洁专业不说脏话") { t.Errorf("system 未含用户人设: %q", sys) } return } t.Fatal("没找到 agent 节点") }