|
24 | 24 | #include <yaml-cpp/yaml.h> |
25 | 25 | #include <nlohmann/json.hpp> |
26 | 26 | #include <algorithm> |
| 27 | +#include <regex> |
| 28 | + |
27 | 29 | #include <cctype> |
28 | 30 |
|
29 | 31 | namespace fs = std::filesystem; |
30 | 32 |
|
31 | 33 | namespace { |
32 | 34 | using namespace a2ui::tests; |
33 | 35 |
|
| 36 | +inline std::string strip(const std::string& s) { |
| 37 | + auto start = std::find_if_not(s.begin(), s.end(), [](unsigned char ch) { return std::isspace(ch); }); |
| 38 | + auto end = std::find_if_not(s.rbegin(), s.rend(), [](unsigned char ch) { return std::isspace(ch); }).base(); |
| 39 | + return (start < end) ? std::string(start, end) : ""; |
| 40 | +} |
| 41 | + |
34 | 42 | // --- Validator Conformance --- |
| 43 | + |
35 | 44 | TEST(ValidatorConformanceTest, RunAll) { |
36 | 45 | fs::path repo_root = find_repo_root(); |
37 | 46 | ASSERT_FALSE(repo_root.empty()) << "Could not find repo root"; |
@@ -62,6 +71,101 @@ TEST(ValidatorConformanceTest, RunAll) { |
62 | 71 | } |
63 | 72 | } |
64 | 73 |
|
| 74 | +// --- Catalog Conformance --- |
| 75 | +TEST(CatalogConformanceTest, RunAll) { |
| 76 | + fs::path repo_root = find_repo_root(); |
| 77 | + ASSERT_FALSE(repo_root.empty()) << "Could not find repo root"; |
| 78 | + |
| 79 | + fs::path conformance_dir = repo_root / "agent_sdks" / "conformance"; |
| 80 | + fs::path catalog_tests_path = conformance_dir / "catalog.yaml"; |
| 81 | + |
| 82 | + YAML::Node yaml_tests = YAML::LoadFile(catalog_tests_path.string()); |
| 83 | + nlohmann::json tests = yaml_to_json(yaml_tests); |
| 84 | + |
| 85 | + for (const auto& test_case : tests) { |
| 86 | + std::string name = test_case["name"]; |
| 87 | + SCOPED_TRACE("Test case: " + name); |
| 88 | + |
| 89 | + if (name == "test_load_examples_validation_fails_on_schema_error") { |
| 90 | + std::cout << "[SKIPPED] Validation failure test in C++: " << name << std::endl; |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + nlohmann::json catalog_config = test_case["catalog"]; |
| 95 | + |
| 96 | + a2ui::A2uiCatalog catalog = setup_catalog(catalog_config, conformance_dir); |
| 97 | + std::string action = test_case["action"]; |
| 98 | + nlohmann::json args = test_case.contains("args") ? test_case["args"] : nlohmann::json::object(); |
| 99 | + |
| 100 | + if (action == "prune") { |
| 101 | + std::vector<std::string> allowed_components; |
| 102 | + if (args.contains("allowed_components")) { |
| 103 | + allowed_components = args["allowed_components"].get<std::vector<std::string>>(); |
| 104 | + } |
| 105 | + std::vector<std::string> allowed_messages; |
| 106 | + if (args.contains("allowed_messages")) { |
| 107 | + allowed_messages = args["allowed_messages"].get<std::vector<std::string>>(); |
| 108 | + } |
| 109 | + |
| 110 | + auto pruned = std::move(catalog).with_pruning(allowed_components, allowed_messages); |
| 111 | + |
| 112 | + nlohmann::json expected = test_case["expect"]; |
| 113 | + |
| 114 | + if (expected.contains("catalog_schema")) { |
| 115 | + EXPECT_EQ(pruned.catalog_schema(), expected["catalog_schema"]); |
| 116 | + } |
| 117 | + if (expected.contains("s2c_schema")) { |
| 118 | + EXPECT_EQ(pruned.s2c_schema(), expected["s2c_schema"]); |
| 119 | + } |
| 120 | + if (expected.contains("common_types_schema")) { |
| 121 | + EXPECT_EQ(pruned.common_types_schema(), expected["common_types_schema"]); |
| 122 | + } |
| 123 | + } else if (action == "render") { |
| 124 | + std::string output = catalog.render_as_llm_instructions(); |
| 125 | + std::string expected_output = test_case["expect_output"]; |
| 126 | + |
| 127 | + // Normalize whitespace for comparison |
| 128 | + std::string output_norm = std::regex_replace(strip(output), std::regex("\\s+"), " "); |
| 129 | + std::string expected_norm = std::regex_replace(strip(expected_output), std::regex("\\s+"), " "); |
| 130 | + |
| 131 | + EXPECT_EQ(output_norm, expected_norm); |
| 132 | + } else if (action == "load") { |
| 133 | + std::string path = ""; |
| 134 | + if (args.contains("path") && !args["path"].is_null()) { |
| 135 | + path = args["path"].get<std::string>(); |
| 136 | + } |
| 137 | + |
| 138 | + // Skip glob tests in C++ as it doesn't support them |
| 139 | + if (path.find('*') != std::string::npos || path.find('[') != std::string::npos) { |
| 140 | + std::cout << "[SKIPPED] Glob test in C++: " << name << std::endl; |
| 141 | + continue; |
| 142 | + } |
| 143 | + |
| 144 | + std::string full_path = ""; |
| 145 | + if (!path.empty()) { |
| 146 | + full_path = (conformance_dir / path).string(); |
| 147 | + } |
| 148 | + bool validate = args.value("validate", false); |
| 149 | + |
| 150 | + if (test_case.contains("expect_error")) { |
| 151 | + EXPECT_THROW(catalog.load_examples(full_path, validate), std::runtime_error); |
| 152 | + } else { |
| 153 | + std::string output = catalog.load_examples(full_path, validate); |
| 154 | + std::string expected_output = test_case["expect_output"]; |
| 155 | + |
| 156 | + // Normalize whitespace for comparison |
| 157 | + std::string output_norm = std::regex_replace(strip(output), std::regex("\\s+"), " "); |
| 158 | + std::string expected_norm = std::regex_replace(strip(expected_output), std::regex("\\s+"), " "); |
| 159 | + |
| 160 | + |
| 161 | + EXPECT_EQ(output_norm, expected_norm); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | + |
65 | 169 | // --- Streaming Parser Conformance (v0.8) --- |
66 | 170 | TEST(StreamingParserConformanceTest, RunV08) { |
67 | 171 | fs::path repo_root = find_repo_root(); |
|
0 commit comments