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) } }