package engine import ( "strings" "testing" ) func TestNewQueryChain(t *testing.T) { chain := NewQueryChain() if chain.ChainId == "" { t.Fatal("NewQueryChain 应生成非空 ChainId") } if !strings.HasPrefix(chain.ChainId, "chain_") { t.Errorf("ChainId 应以 'chain_' 开头,实际: %s", chain.ChainId) } if chain.Depth != 0 { t.Errorf("主查询 Depth 应为 0,实际: %d", chain.Depth) } if chain.ParentAgentId != "" { t.Errorf("主查询 ParentAgentId 应为空,实际: %s", chain.ParentAgentId) } } func TestNewQueryChain_Unique(t *testing.T) { chain1 := NewQueryChain() chain2 := NewQueryChain() if chain1.ChainId == chain2.ChainId { t.Errorf("两次 NewQueryChain 应生成不同的 ChainId: %s == %s", chain1.ChainId, chain2.ChainId) } } func TestQueryChainTracking_Fork(t *testing.T) { parent := NewQueryChain() child := parent.Fork("agent_001") if child.ChainId != parent.ChainId { t.Errorf("Fork 应继承 ChainId: 期望 %s,实际 %s", parent.ChainId, child.ChainId) } if child.Depth != parent.Depth+1 { t.Errorf("Fork 应 Depth+1: 期望 %d,实际 %d", parent.Depth+1, child.Depth) } if child.ParentAgentId != "agent_001" { t.Errorf("Fork 应记录 ParentAgentId: 期望 agent_001,实际 %s", child.ParentAgentId) } } func TestQueryChainTracking_MultiFork(t *testing.T) { root := NewQueryChain() // depth=0 child := root.Fork("main_agent") // depth=1 grandchild := child.Fork("sub_01") // depth=2 if grandchild.ChainId != root.ChainId { t.Errorf("多级 Fork 应共享同一个 ChainId") } if grandchild.Depth != 2 { t.Errorf("孙级 Depth 应为 2,实际: %d", grandchild.Depth) } if grandchild.ParentAgentId != "sub_01" { t.Errorf("孙级 ParentAgentId 应为 sub_01,实际: %s", grandchild.ParentAgentId) } } func TestQueryChainTracking_EventFields(t *testing.T) { // 主查询:无 parent_agent_id root := NewQueryChain() fields := root.EventFields() if fields["query_chain_id"] != root.ChainId { t.Errorf("EventFields 应包含 query_chain_id") } if fields["query_depth"] != 0 { t.Errorf("EventFields query_depth 应为 0") } if _, ok := fields["parent_agent_id"]; ok { t.Errorf("主查询 EventFields 不应包含 parent_agent_id") } // 子查询:有 parent_agent_id child := root.Fork("agent_x") childFields := child.EventFields() if childFields["query_chain_id"] != root.ChainId { t.Errorf("子查询 EventFields 应继承 query_chain_id") } if childFields["query_depth"] != 1 { t.Errorf("子查询 EventFields query_depth 应为 1") } if childFields["parent_agent_id"] != "agent_x" { t.Errorf("子查询 EventFields 应包含 parent_agent_id=agent_x") } }