234 lines
14 KiB
C++
234 lines
14 KiB
C++
#include "settlement/core/rules_manifest.hpp"
|
|
|
|
#include <Void/Testing/Suite.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
using namespace settlement;
|
|
using Void::Testing::Test;
|
|
|
|
void require(bool condition, std::string_view message) {
|
|
if (!condition) throw std::runtime_error(std::string(message));
|
|
}
|
|
|
|
template <class T>
|
|
T checked(std::expected<T, RulesDiagnostic> result) {
|
|
if (!result) {
|
|
const auto& d = result.error();
|
|
throw std::runtime_error(d.source + " " + d.package + " " + d.field + " " + d.message);
|
|
}
|
|
return std::move(*result);
|
|
}
|
|
|
|
std::string package(std::string_view id, std::string_view definitions = "[]",
|
|
std::string_view dependencies = "[]", std::string_view capabilities = "[]",
|
|
std::string_view version = "1.0.0") {
|
|
return "{\"schema\":1,\"id\":\"" + std::string(id) + "\",\"version\":\"" + std::string(version) +
|
|
"\",\"dependencies\":" + std::string(dependencies) + ",\"exclusive_capabilities\":" +
|
|
std::string(capabilities) + ",\"definitions\":" + std::string(definitions) + "}";
|
|
}
|
|
|
|
constexpr std::string_view wood = R"({"id":"game.core:wood","kind":"item","data":{"stack_limit":64},"references":[]})";
|
|
constexpr std::string_view stone = R"({"id":"game.core:stone","kind":"item","data":{"stack_limit":32},"references":[]})";
|
|
constexpr std::string_view board = R"([{"id":"game.colony:board","kind":"recipe","data":{"work_ticks":8},"references":["game.core:wood","game.core:stone"]}])";
|
|
constexpr std::string_view core_dependency = R"([{"id":"game.core","version":">=1.0.0 <2.0.0"}])";
|
|
|
|
RulePackage load(std::string_view json) { return checked(load_rule_package("fixture.json", json)); }
|
|
|
|
RulesManifest resolve(std::vector<RulePackage> packages, std::vector<std::string> selected = {"game.colony"},
|
|
const RulesLimits& limits = {}) {
|
|
return checked(resolve_rules(packages, selected, limits));
|
|
}
|
|
|
|
void parse_error(std::string_view json, std::string_view field, const RulesLimits& limits = {}) {
|
|
const auto result = load_rule_package("bad-package.json", json, limits);
|
|
require(!result, "invalid package was accepted");
|
|
require(result.error().source == "bad-package.json" && result.error().field.find(field) != std::string::npos &&
|
|
!result.error().message.empty(), "parser diagnostic lost source or field");
|
|
}
|
|
|
|
void resolution_error(const std::vector<RulePackage>& packages, const std::vector<std::string>& selected,
|
|
std::string_view field, const RulesLimits& limits = {}) {
|
|
const auto result = resolve_rules(packages, selected, limits);
|
|
require(!result, "invalid package set was accepted");
|
|
require(result.error().field.find(field) != std::string::npos && !result.error().source.empty() &&
|
|
!result.error().message.empty(), "resolver diagnostic lost source or field");
|
|
}
|
|
|
|
struct [[= Void::Testing::name("RulesTests")]] RulesTests : Void::Testing::Suite<RulesTests> {
|
|
[[= Void::Testing::tag("rules")]] Test test_manifest_identity_is_independent_of_discovery_order();
|
|
[[= Void::Testing::tag("rules")]] Test test_frozen_identity_covers_content_and_exact_versions();
|
|
[[= Void::Testing::tag("rules")]] Test test_transitive_selection_and_unselected_packages();
|
|
[[= Void::Testing::tag("rules")]] Test test_invalid_dependencies_and_provider_conflicts();
|
|
[[= Void::Testing::tag("rules")]] Test test_definition_references_and_namespaces();
|
|
[[= Void::Testing::tag("rules")]] Test test_strict_json_schema_and_input_budgets();
|
|
[[= Void::Testing::tag("rules")]] Test test_catalog_and_model_limits();
|
|
[[= Void::Testing::tag("rules")]] Test test_prerelease_policy();
|
|
[[= Void::Testing::tag("rules")]] Test test_builtin_default_and_known_digest();
|
|
};
|
|
|
|
Test RulesTests::test_manifest_identity_is_independent_of_discovery_order() {
|
|
const auto core_a = load(package("game.core", "[" + std::string(wood) + "," + std::string(stone) + "]"));
|
|
const auto core_b = load(package("game.core", "[" + std::string(stone) + "," + std::string(wood) + "]"));
|
|
const auto tools = load(package("game.tools"));
|
|
const auto colony_a = load(package("game.colony", board,
|
|
R"([{"id":"game.core","version":">=1.0.0 <2.0.0"},{"id":"game.tools","version":"1.0.0"}])",
|
|
R"(["game.colony:weather","game.colony:building"])"));
|
|
std::string reversed_board(board);
|
|
const auto refs = reversed_board.find("\"game.core:wood\",\"game.core:stone\"");
|
|
reversed_board.replace(refs, std::string("\"game.core:wood\",\"game.core:stone\"").size(),
|
|
"\"game.core:stone\",\"game.core:wood\"");
|
|
const auto colony_b = load(package("game.colony", reversed_board,
|
|
R"([{"id":"game.tools","version":"1.0.0"},{"id":"game.core","version":">=1.0.0 <2.0.0"}])",
|
|
R"(["game.colony:building","game.colony:weather"])"));
|
|
const auto first = resolve({core_a, colony_a, tools});
|
|
const auto second = resolve({tools, colony_b, core_b});
|
|
require(first == second, "permuting package, definition, capability or reference order changed identity");
|
|
require(first.packages().size() == 3 && first.packages()[0].id == "game.colony" &&
|
|
first.packages()[1].id == "game.core", "frozen packages are not sorted by ID");
|
|
require(core_a.canonical_json() == core_b.canonical_json(), "canonical package bytes changed with input order");
|
|
const auto reloaded = load(core_a.canonical_json());
|
|
require(reloaded.identity() == core_a.identity(), "canonical document did not round-trip");
|
|
}
|
|
|
|
Test RulesTests::test_frozen_identity_covers_content_and_exact_versions() {
|
|
const auto source = package("game.core", "[" + std::string(wood) + "]");
|
|
const auto original = resolve({load(source)}, {"game.core"});
|
|
const auto compare = [&](std::string changed) {
|
|
require(resolve({load(changed)}, {"game.core"}) != original, "simulation change kept the frozen fingerprint");
|
|
};
|
|
auto modified = source;
|
|
modified.replace(modified.find("64"), 2, "65");
|
|
compare(modified);
|
|
compare(package("game.core", "[" + std::string(wood) + "]", "[]", "[]", "1.0.1"));
|
|
compare(package("game.core", "[" + std::string(wood) + "]", "[]", "[]", "1.0.0+build2"));
|
|
compare(package("game.core"));
|
|
modified = source;
|
|
modified.replace(modified.find("game.core:wood"), 14, "game.core:logs");
|
|
compare(modified);
|
|
const auto a = resolve({load(package("game.core", R"([{"id":"game.core:path","kind":"event","data":[1,2],"references":[]}])"))}, {"game.core"});
|
|
const auto b = resolve({load(package("game.core", R"([{"id":"game.core:path","kind":"event","data":[2,1],"references":[]}])"))}, {"game.core"});
|
|
require(a != b, "ordered definition data was sorted as a set");
|
|
}
|
|
|
|
Test RulesTests::test_transitive_selection_and_unselected_packages() {
|
|
const auto core = load(package("game.core"));
|
|
const auto colony = load(package("game.colony", "[]", core_dependency));
|
|
const auto ui = load(package("game.ui", "[]", R"([{"id":"game.colony","version":"1.0.0"}])"));
|
|
const auto broken_unselected = load(package("game.unselected", "[]", R"([{"id":"game.missing","version":"1.0.0"}])"));
|
|
const auto first = resolve({ui, colony, core, broken_unselected}, {"game.ui"});
|
|
const auto second = resolve({core, ui, colony}, {"game.core", "game.ui"});
|
|
require(first == second && first.packages().size() == 3, "transitive dependency closure changed with selection order");
|
|
resolution_error({core}, {"game.absent"}, "selected");
|
|
resolution_error({core}, {}, "selected");
|
|
resolution_error({core}, {"game.core", "game.core"}, "selected");
|
|
}
|
|
|
|
Test RulesTests::test_invalid_dependencies_and_provider_conflicts() {
|
|
const auto core = load(package("game.core"));
|
|
resolution_error({core, core}, {"game.core"}, "id");
|
|
resolution_error({load(package("game.colony", "[]", core_dependency))}, {"game.colony"}, "dependencies");
|
|
const auto incompatible = load(package("game.colony", "[]", R"([{"id":"game.core","version":"^2.0.0"}])"));
|
|
resolution_error({core, incompatible}, {"game.colony"}, "dependencies.game.core");
|
|
const auto back_edge = load(package("game.core", "[]", R"([{"id":"game.colony","version":"*"}])"));
|
|
resolution_error({back_edge, load(package("game.colony", "[]", core_dependency))}, {"game.colony"}, "dependencies");
|
|
const auto provider_a = load(package("game.first", "[]", "[]", R"(["game.core:terrain"])"));
|
|
const auto provider_b = load(package("game.second", "[]", "[]", R"(["game.core:terrain"])"));
|
|
resolution_error({provider_a, provider_b}, {"game.first", "game.second"}, "exclusive_capabilities");
|
|
parse_error(package("game.colony", "[]", R"([{"id":"game.core","version":"=>broken"}])"), "version");
|
|
parse_error(package("game.colony", "[]", R"([{"id":"game.core","version":"*"},{"id":"game.core","version":"*"}])"), "dependencies");
|
|
}
|
|
|
|
Test RulesTests::test_definition_references_and_namespaces() {
|
|
const auto core = load(package("game.core", "[" + std::string(wood) + "," + std::string(stone) + "]"));
|
|
const auto colony = load(package("game.colony", board, core_dependency));
|
|
static_cast<void>(resolve({core, colony}));
|
|
resolution_error({load(package("game.core")), colony}, {"game.colony"}, "references");
|
|
resolution_error({core, load(package("game.colony", board))}, {"game.core", "game.colony"}, "references");
|
|
parse_error(package("game.colony", "[" + std::string(wood) + "]"), ".id");
|
|
parse_error(package("game.core", "[" + std::string(wood) + "," + std::string(wood) + "]"), ".id");
|
|
parse_error(package("game.core", R"([{"id":"game.core:bad","kind":"native","data":{},"references":[]}])"), ".kind");
|
|
parse_error(package("game.core", R"([{"id":"game.core:bad","kind":"item","data":{},"references":["not-qualified"]}])"), ".references");
|
|
}
|
|
|
|
Test RulesTests::test_strict_json_schema_and_input_budgets() {
|
|
const auto valid = package("game.core");
|
|
parse_error("{\"schema\":1,\"schema\":1}", "$");
|
|
parse_error(valid + " false", "$");
|
|
parse_error("{\"id\":\"\xff\"}", "$");
|
|
parse_error("{}", "$");
|
|
auto modified = valid;
|
|
modified.insert(1, "\"sha256\":\"forged\",");
|
|
parse_error(modified, "sha256");
|
|
modified = valid;
|
|
modified.replace(modified.find("\"schema\":1"), 10, "\"schema\":2");
|
|
parse_error(modified, "schema");
|
|
parse_error(package("Game"), "id");
|
|
parse_error(package("game.core", "[]", "[]", "[]", "v1.0.0"), "version");
|
|
parse_error(package("game.core", R"([{"id":"game.core:bad","kind":"item","data":0.5,"references":[]}])"), "$");
|
|
RulesLimits limits;
|
|
limits.package_bytes = valid.size() - 1;
|
|
parse_error(valid, "$", limits);
|
|
limits = {};
|
|
limits.json_nodes = 4;
|
|
parse_error(valid, "$", limits);
|
|
limits = {};
|
|
limits.json_depth = 2;
|
|
parse_error(package("game.core", "[" + std::string(wood) + "]"), "$", limits);
|
|
}
|
|
|
|
Test RulesTests::test_catalog_and_model_limits() {
|
|
const auto core = load(package("game.core", "[" + std::string(wood) + "," + std::string(stone) + "]"));
|
|
const auto colony = load(package("game.colony", board, core_dependency));
|
|
RulesLimits limits;
|
|
limits.packages = 1;
|
|
resolution_error({core, colony}, {"game.colony"}, "packages", limits);
|
|
limits = {};
|
|
limits.catalog_bytes = 2;
|
|
resolution_error({core, colony}, {"game.colony"}, "packages", limits);
|
|
limits = {};
|
|
limits.definitions = 1;
|
|
resolution_error({core}, {"game.core"}, "definitions", limits);
|
|
parse_error(package("game.core", "[" + std::string(wood) + "," + std::string(stone) + "]"), "definitions", limits);
|
|
limits = {};
|
|
limits.dependencies = 0;
|
|
parse_error(package("game.colony", board, core_dependency), "dependencies", limits);
|
|
limits = {};
|
|
limits.references = 1;
|
|
parse_error(package("game.colony", board, core_dependency), "references", limits);
|
|
}
|
|
|
|
Test RulesTests::test_prerelease_policy() {
|
|
const auto prerelease = load(package("game.core", "[]", "[]", "[]", "1.0.0-preview+test"));
|
|
const auto broad = load(package("game.colony", "[]", R"([{"id":"game.core","version":"*"}])"));
|
|
resolution_error({prerelease, broad}, {"game.colony"}, "dependencies");
|
|
const auto exact = load(package("game.colony", "[]", R"([{"id":"game.core","version":"=1.0.0-preview+test"}])"));
|
|
const auto result = resolve({prerelease, exact});
|
|
require(result.packages()[1].exact_version == "1.0.0-preview+test", "prerelease exact identity was lost");
|
|
}
|
|
|
|
Test RulesTests::test_builtin_default_and_known_digest() {
|
|
const auto first = checked(default_rules_manifest());
|
|
const auto second = checked(default_rules_manifest());
|
|
require(first == second && first.packages().size() == 2, "default rules were not reproducible");
|
|
// Recorded by profile-baseline.exe against the compiled pre-profile core.
|
|
require(rules_digest_hex(first.fingerprint()) ==
|
|
"06d39e42fa79f06859b9bee1dcb08088142cb83e8cdcf72dc0984cfc62cd83d1",
|
|
"default profile fingerprint changed");
|
|
require(first.canonical_json() ==
|
|
R"({"packages":[{"id":"settlement.colony","sha256":"08e18a58989430c5435a2de6fb8f6a27a912b4d2970c1b965282408880d2c089","version":"1.0.0"},{"id":"settlement.core","sha256":"61b36b8a230eebbe862e696603e021ace7151a9faf0ab3c069ab7124188d337f","version":"1.0.0"}],"schema":1})",
|
|
"default canonical manifest changed");
|
|
const auto empty = load(package("game.core"));
|
|
require(rules_digest_hex(empty.identity().content_sha256) == "f6d1f704ccbb3ec707d2e1ba7b0db27dab8494b7787eeee10f1449dabee1477a", "package SHA-256 differs from the canonical-byte known vector");
|
|
require(empty.definitions().empty(), "empty package gained a definition");
|
|
}
|
|
|
|
} // namespace
|