package wire import ( "strings" "testing" "git.flytoex.net/yuanwei/flyto-agent/pkg/flyto" ) // makeTools 构造指定数量的占位工具(测试辅助). func makeTools(n int) []flyto.Tool { tools := make([]flyto.Tool, n) for i := range tools { tools[i] = flyto.Tool{Name: "tool", Description: "test"} } return tools } // TestCheckToolCount_Unlimited 验证 max=0 时(未知/无限制)始终返回 nil. func TestCheckToolCount_Unlimited(t *testing.T) { if err := CheckToolCount(makeTools(9999), 0); err != nil { t.Errorf("max=0 should be unlimited, got error: %v", err) } if err := CheckToolCount(makeTools(0), 0); err != nil { t.Errorf("max=0 empty tools should be nil, got: %v", err) } // 负数 max 同样视为无限制 if err := CheckToolCount(makeTools(256), -1); err != nil { t.Errorf("max=-1 should be unlimited, got error: %v", err) } } // TestCheckToolCount_WithinLimit 验证工具数量 ≤ max 时返回 nil. func TestCheckToolCount_WithinLimit(t *testing.T) { cases := []struct { count int max int }{ {0, 128}, {1, 128}, {5, 128}, {128, 128}, {0, 20}, {20, 20}, } for _, c := range cases { err := CheckToolCount(makeTools(c.count), c.max) if err != nil { t.Errorf("count=%d max=%d: expected nil, got %v", c.count, c.max, err) } } } // TestCheckToolCount_ExceedsLimit 验证工具数量 > max 时返回包含数量的错误. func TestCheckToolCount_ExceedsLimit(t *testing.T) { cases := []struct { count int max int }{ {129, 128}, // OpenAI limit {21, 20}, // Anthropic strict limit {1, 0}, // max=0 → unlimited,不应触发(由 Unlimited 测试覆盖) } // 只测试真正应报错的 cases(max>0 且 count>max) for _, c := range cases { if c.max <= 0 { continue } err := CheckToolCount(makeTools(c.count), c.max) if err == nil { t.Errorf("count=%d max=%d: expected error, got nil", c.count, c.max) continue } // 错误信息应包含实际数量和上限 if !strings.Contains(err.Error(), "129") && c.count == 129 { // pass: just check non-nil above } _ = err // 错误非 nil 即符合预期 } } // TestCheckToolCount_ErrorMessage 验证错误消息格式包含实际数量和上限. func TestCheckToolCount_ErrorMessage(t *testing.T) { err := CheckToolCount(makeTools(129), 128) if err == nil { t.Fatal("expected error") } msg := err.Error() if !strings.Contains(msg, "129") { t.Errorf("error message should contain actual count 129: %q", msg) } if !strings.Contains(msg, "128") { t.Errorf("error message should contain limit 128: %q", msg) } err2 := CheckToolCount(makeTools(21), 20) if err2 == nil { t.Fatal("expected error for count=21 max=20") } msg2 := err2.Error() if !strings.Contains(msg2, "21") { t.Errorf("error message should contain actual count 21: %q", msg2) } if !strings.Contains(msg2, "20") { t.Errorf("error message should contain limit 20: %q", msg2) } }