From d30384d98156602dad92efcfc26fe0fbf2c6151a Mon Sep 17 00:00:00 2001 From: selikhov Date: Thu, 3 Dec 2026 09:38:27 +0300 Subject: [PATCH] feat(Update): deterministic package resolver with diagnostics (#4) - resolve_packages() takes caller-supplied package descriptions (id, exact version, content digest, exclusive capabilities, dependency ranges) plus selected root ids and returns an exact dependency set. - Deterministic: canonical input ordering, descending SemVer candidate order and canonical requirement order make the result invariant under permutation of installed/selected input. - Frozen PackageIdentity compares build metadata and content digest in addition to SemVer precedence, so equal-precedence rebuilds are never conflated; ambiguous same-id/same-precedence inputs are rejected. - Actionable diagnostics carry kind, input package, field (e.g. dependencies[1].range) and related id: missing dependency, incompatible version, cycle, ambiguity, competing exclusive providers. - Bounded package/edge/search-node limits with backtracking search. - 13 standalone tests cover closure, order invariance, diagnostics, backtracking, cycles, capabilities, frozen identity and limits. --- include/Void/Update/Resolver.hpp | 634 +++++++++++++++++++++++++++ tests/standalone/Update/Resolver.cpp | 268 +++++++++++ 2 files changed, 902 insertions(+) create mode 100644 include/Void/Update/Resolver.hpp create mode 100644 tests/standalone/Update/Resolver.cpp diff --git a/include/Void/Update/Resolver.hpp b/include/Void/Update/Resolver.hpp new file mode 100644 index 0000000..e09e24d --- /dev/null +++ b/include/Void/Update/Resolver.hpp @@ -0,0 +1,634 @@ +#pragma once + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace Void::Update +{ + +inline constexpr std::size_t kDefaultMaxResolvedPackages = 64; +inline constexpr std::size_t kDefaultMaxResolvedEdges = 256; +inline constexpr std::size_t kDefaultMaxResolutionNodes = 100'000; + +/// One dependency edge: the required package id and the accepted versions. +struct PackageDependency { + std::string id; + Void::Semver::VersionRange range; +}; + +/// Caller-supplied description of one installed package version. +/// +/// Each entry in `capabilities` is *exclusive*: at most one resolved package +/// may provide a given capability. `content_digest` is opaque to the resolver +/// (for example `sha256:...`) but participates in the frozen identity. +struct PackageDescription { + std::string id; + Void::Semver::Version version; + std::string content_digest; + std::vector capabilities; + std::vector dependencies; +}; + +/// Frozen identity of a resolved package. +/// +/// Unlike `Void::Semver::Version::operator==`, equality (and ordering) here +/// includes SemVer build metadata and the content digest, so a rebuild that +/// only changes `+metadata` or the digest is never mistaken for the same +/// package even though its SemVer precedence is equal. +struct PackageIdentity { + std::string id; + Void::Semver::Version version; + std::string content_digest; + + [[nodiscard]] std::strong_ordering + operator<=>(const PackageIdentity& other) const noexcept + { + if (const auto comparison = id <=> other.id; comparison != 0) { + return comparison; + } + // Precedence first, then build metadata, then digest: a total order. + if (const auto comparison = version <=> other.version; + comparison != 0) { + return comparison; + } + if (const auto comparison = version.build <=> other.version.build; + comparison != 0) { + return comparison; + } + return content_digest <=> other.content_digest; + } + + [[nodiscard]] bool operator==(const PackageIdentity& other) const noexcept + { + return (*this <=> other) == 0; + } +}; + +struct ResolvedPackage { + PackageIdentity identity; + std::vector capabilities; +}; + +struct Resolution { + /// Sorted by frozen identity; capabilities are sorted and deduplicated. + std::vector packages; +}; + +enum class ResolutionErrorKind : std::uint8_t { + invalid_request = 0, + missing_dependency, + incompatible_version, + cyclic_dependency, + ambiguous_package, + exclusive_capability_conflict, + limit_exceeded, + search_exhausted, +}; + +/// Actionable failure. `package` names the input package (or requested id) and +/// `field` the exact field, e.g. `dependencies[1].range`. +struct ResolutionDiagnostic { + ResolutionErrorKind kind{ResolutionErrorKind::invalid_request}; + std::string package; + std::string field; + std::string related; + std::string message; +}; + +struct ResolutionLimits { + std::size_t max_packages{kDefaultMaxResolvedPackages}; + std::size_t max_edges{kDefaultMaxResolvedEdges}; + std::size_t max_search_nodes{kDefaultMaxResolutionNodes}; +}; + +struct ResolutionRequest { + std::span installed; + /// Root package ids; each is resolved as `*`. + std::span selected; + ResolutionLimits limits{}; +}; + +namespace Detail +{ + +struct Requirement { + std::string id; + Void::Semver::VersionRange range; + std::string requester; + std::string field; + + [[nodiscard]] bool operator<(const Requirement& other) const noexcept + { + if (const auto comparison = id <=> other.id; comparison != 0) { + return comparison < 0; + } + if (const auto comparison = requester <=> other.requester; + comparison != 0) { + return comparison < 0; + } + return field < other.field; + } +}; + +struct SearchContext { + const std::map>& available; + ResolutionLimits limits; + std::size_t nodes{0}; + bool node_limit_hit{false}; + std::optional best; + std::size_t best_depth{0}; + + void record(ResolutionDiagnostic diagnostic, std::size_t depth) + { + const bool better = !best.has_value() || depth > best_depth || + (depth == best_depth && diagnose_less(diagnostic, *best)); + if (better) { + best = std::move(diagnostic); + best_depth = depth; + } + } + + static bool diagnose_less(const ResolutionDiagnostic& lhs, + const ResolutionDiagnostic& rhs) noexcept + { + if (const auto comparison = + static_cast(lhs.kind) <=> + static_cast(rhs.kind); + comparison != 0) { + return comparison < 0; + } + if (const auto comparison = lhs.package <=> rhs.package; + comparison != 0) { + return comparison < 0; + } + if (const auto comparison = lhs.field <=> rhs.field; comparison != 0) { + return comparison < 0; + } + return lhs.related < rhs.related; + } +}; + +using Assignment = std::map; + +[[nodiscard]] inline std::optional + check_capabilities(const Assignment& assignment, + const SearchContext& context) +{ + std::map> providers; + for (const auto& [id, index] : assignment) { + const auto& package = context.available.at(id)[index]; + std::vector capabilities = package.capabilities; + std::sort(capabilities.begin(), capabilities.end()); + capabilities.erase( + std::unique(capabilities.begin(), capabilities.end()), + capabilities.end()); + for (const auto& capability : capabilities) { + providers[capability].push_back(id); + } + } + for (const auto& [capability, owners] : providers) { + if (owners.size() > 1) { + return ResolutionDiagnostic{ + .kind = ResolutionErrorKind::exclusive_capability_conflict, + .package = owners[0], + .field = "capabilities", + .related = owners[1], + .message = std::format( + "exclusive capability '{}' is provided by both '{}' and '{}'", + capability, + owners[0], + owners[1])}; + } + } + return std::nullopt; +} + +[[nodiscard]] inline std::optional + check_cycle(const Assignment& assignment, const SearchContext& context) +{ + std::map color; // 0 unseen, 1 on stack, 2 done + std::vector stack; + + const std::function( + const std::string&)> + visit = [&](const std::string& id) + -> std::optional { + color[id] = 1; + stack.push_back(id); + const auto& package = + context.available.at(id)[assignment.at(id)]; + std::vector dependencies; + for (const auto& dependency : package.dependencies) { + if (assignment.contains(dependency.id)) { + dependencies.push_back(dependency.id); + } + } + std::sort(dependencies.begin(), dependencies.end()); + dependencies.erase(std::unique(dependencies.begin(), + dependencies.end()), + dependencies.end()); + for (const auto& dependency : dependencies) { + if (color[dependency] == 1) { + std::string path; + const auto begin = + std::find(stack.begin(), stack.end(), dependency); + for (auto it = begin; it != stack.end(); ++it) { + path += *it; + path += " -> "; + } + path += dependency; + return ResolutionDiagnostic{ + .kind = ResolutionErrorKind::cyclic_dependency, + .package = dependency, + .field = "dependencies", + .related = path, + .message = + std::format("cyclic dependency detected: {}", path)}; + } + if (color[dependency] == 0) { + if (auto diagnostic = visit(dependency)) { + return diagnostic; + } + } + } + stack.pop_back(); + color[id] = 2; + return std::nullopt; + }; + + for (const auto& [id, index] : assignment) { + (void)index; + if (color[id] == 0) { + if (auto diagnostic = visit(id)) { + return diagnostic; + } + } + } + return std::nullopt; +} + +[[nodiscard]] inline bool + solve(const Assignment& assignment, + std::vector pending, + SearchContext& context, + Assignment& solution) +{ + if (context.node_limit_hit) { + return false; + } + if (++context.nodes > context.limits.max_search_nodes) { + context.node_limit_hit = true; + return false; + } + if (assignment.size() > context.limits.max_packages) { + context.record( + ResolutionDiagnostic{ + .kind = ResolutionErrorKind::limit_exceeded, + .field = "limits.max_packages", + .message = std::format( + "resolved package count exceeds the limit of {}", + context.limits.max_packages)}, + assignment.size()); + return false; + } + + std::size_t edges = 0; + for (const auto& [id, index] : assignment) { + edges += context.available.at(id)[index].dependencies.size(); + } + if (edges > context.limits.max_edges) { + context.record( + ResolutionDiagnostic{ + .kind = ResolutionErrorKind::limit_exceeded, + .field = "limits.max_edges", + .message = std::format( + "resolved dependency edge count exceeds the limit of {}", + context.limits.max_edges)}, + assignment.size()); + return false; + } + + if (pending.empty()) { + if (auto diagnostic = check_capabilities(assignment, context)) { + context.record(std::move(*diagnostic), assignment.size()); + return false; + } + if (auto diagnostic = check_cycle(assignment, context)) { + context.record(std::move(*diagnostic), assignment.size()); + return false; + } + solution = assignment; + return true; + } + + const auto next = std::min_element(pending.begin(), pending.end()); + const Requirement requirement = *next; + pending.erase(next); + + const auto found = context.available.find(requirement.id); + if (found == context.available.end()) { + context.record( + ResolutionDiagnostic{ + .kind = ResolutionErrorKind::missing_dependency, + .package = requirement.requester.empty() + ? requirement.id + : requirement.requester, + .field = requirement.field, + .related = requirement.id, + .message = std::format( + "required package '{}' is not installed", requirement.id)}, + assignment.size()); + return false; + } + + if (const auto existing = assignment.find(requirement.id); + existing != assignment.end()) { + const auto& package = found->second[existing->second]; + if (requirement.range.contains(package.version)) { + return solve(assignment, std::move(pending), context, solution); + } + context.record( + ResolutionDiagnostic{ + .kind = ResolutionErrorKind::incompatible_version, + .package = requirement.requester.empty() + ? requirement.id + : requirement.requester, + .field = requirement.field, + .related = requirement.id, + .message = std::format( + "selected '{}' version {} does not satisfy the requested range", + requirement.id, + package.version.to_string())}, + assignment.size()); + return false; + } + + bool any_candidate = false; + for (std::size_t index = 0; index < found->second.size(); ++index) { + const auto& package = found->second[index]; + if (!requirement.range.contains(package.version)) { + continue; + } + any_candidate = true; + Assignment next_assignment = assignment; + next_assignment.emplace(requirement.id, index); + std::vector next_pending = pending; + for (std::size_t dependency_index = 0; + dependency_index < package.dependencies.size(); + ++dependency_index) { + const auto& dependency = package.dependencies[dependency_index]; + next_pending.push_back(Requirement{ + .id = dependency.id, + .range = dependency.range, + .requester = package.id, + .field = std::format("dependencies[{}].range", + dependency_index)}); + } + if (solve(next_assignment, + std::move(next_pending), + context, + solution)) { + return true; + } + if (context.node_limit_hit) { + return false; + } + } + + context.record( + ResolutionDiagnostic{ + .kind = ResolutionErrorKind::incompatible_version, + .package = requirement.requester.empty() ? requirement.id + : requirement.requester, + .field = requirement.field, + .related = requirement.id, + .message = any_candidate + ? std::format( + "no candidate for '{}' is consistent with the rest of the dependency set", + requirement.id) + : std::format( + "no installed version of '{}' satisfies the requested range", + requirement.id)}, + assignment.size()); + return false; +} + +} // namespace Detail + +/// Resolve `request.selected` and their transitive dependencies into an exact, +/// deterministic package set. +/// +/// The result is invariant under permutation of `installed` and `selected`. +/// Candidates are tried in descending SemVer precedence (ties broken by build +/// metadata and digest) and requirements are processed in a canonical order. +/// Caller-supplied content digests and versions are retained verbatim; no +/// game-specific schema is assumed. +[[nodiscard]] inline Void::Result::Result + resolve_packages(const ResolutionRequest& request) +{ + using namespace Detail; + + if (request.limits.max_packages == 0 || + request.limits.max_edges == 0 || + request.limits.max_search_nodes == 0) { + return std::unexpected{ResolutionDiagnostic{ + .kind = ResolutionErrorKind::invalid_request, + .field = "limits", + .message = "resolution limits must be non-zero"}}; + } + + // Deterministic canonical input order: everything downstream is + // permutation-invariant. + std::vector installed{request.installed.begin(), + request.installed.end()}; + std::sort(installed.begin(), + installed.end(), + [](const PackageDescription& lhs, const PackageDescription& rhs) { + const PackageIdentity left{lhs.id, lhs.version, + lhs.content_digest}; + const PackageIdentity right{rhs.id, rhs.version, + rhs.content_digest}; + return left < right; + }); + + std::map> available; + for (const auto& package : installed) { + if (package.id.empty()) { + return std::unexpected{ResolutionDiagnostic{ + .kind = ResolutionErrorKind::invalid_request, + .field = "id", + .message = "package id must not be empty"}}; + } + if (package.content_digest.empty()) { + return std::unexpected{ResolutionDiagnostic{ + .kind = ResolutionErrorKind::invalid_request, + .package = package.id, + .field = "content_digest", + .message = "package content digest must not be empty"}}; + } + for (std::size_t index = 0; index < package.capabilities.size(); + ++index) { + if (package.capabilities[index].empty()) { + return std::unexpected{ResolutionDiagnostic{ + .kind = ResolutionErrorKind::invalid_request, + .package = package.id, + .field = std::format("capabilities[{}]", index), + .message = "capability name must not be empty"}}; + } + } + for (std::size_t index = 0; index < package.dependencies.size(); + ++index) { + if (package.dependencies[index].id.empty()) { + return std::unexpected{ResolutionDiagnostic{ + .kind = ResolutionErrorKind::invalid_request, + .package = package.id, + .field = + std::format("dependencies[{}].id", index), + .message = "dependency id must not be empty"}}; + } + } + available[package.id].push_back(package); + } + + // Reject ambiguous inputs: same id and equal SemVer precedence but a + // different frozen identity (build metadata or digest). + for (auto& [id, candidates] : available) { + std::sort(candidates.begin(), + candidates.end(), + [](const PackageDescription& lhs, + const PackageDescription& rhs) { + if (const auto comparison = rhs.version <=> lhs.version; + comparison != 0) { + return comparison < 0; + } + if (const auto comparison = + rhs.version.build <=> lhs.version.build; + comparison != 0) { + return comparison < 0; + } + return rhs.content_digest < lhs.content_digest; + }); + for (std::size_t index = 1; index < candidates.size(); ++index) { + const auto& previous = candidates[index - 1]; + const auto& current = candidates[index]; + if ((previous.version <=> current.version) == 0 && + !(PackageIdentity{previous.id, previous.version, + previous.content_digest} == + PackageIdentity{current.id, current.version, + current.content_digest})) { + return std::unexpected{ResolutionDiagnostic{ + .kind = ResolutionErrorKind::ambiguous_package, + .package = id, + .field = "version", + .related = current.content_digest, + .message = std::format( + "ambiguous installed '{}': {} and {} have equal SemVer precedence but different build metadata or digest", + id, + previous.version.to_string(), + current.version.to_string())}}; + } + } + // Identical frozen identities are the same package: keep one. + candidates.erase( + std::unique(candidates.begin(), + candidates.end(), + [](const PackageDescription& lhs, + const PackageDescription& rhs) { + return PackageIdentity{ + lhs.id, lhs.version, + lhs.content_digest} == + PackageIdentity{ + rhs.id, rhs.version, + rhs.content_digest}; + }), + candidates.end()); + } + + const auto any_range = Void::Semver::VersionRange::parse("*"); + if (!any_range.has_value()) { + return std::unexpected{ResolutionDiagnostic{ + .kind = ResolutionErrorKind::invalid_request, + .field = "range", + .message = "the always-matching range could not be constructed"}}; + } + + std::vector pending; + std::set roots; + for (std::size_t index = 0; index < request.selected.size(); ++index) { + const auto& id = request.selected[index]; + if (id.empty()) { + return std::unexpected{ResolutionDiagnostic{ + .kind = ResolutionErrorKind::invalid_request, + .field = std::format("selected[{}]", index), + .message = "selected package id must not be empty"}}; + } + if (!roots.insert(id).second) { + continue; + } + pending.push_back(Requirement{ + .id = id, + .range = *any_range, + .requester = {}, + .field = std::format("selected[{}]", index)}); + } + + SearchContext context{available, request.limits}; + Assignment solution; + if (!solve(Assignment{}, std::move(pending), context, solution)) { + if (context.best.has_value()) { + return std::unexpected{std::move(*context.best)}; + } + return std::unexpected{ResolutionDiagnostic{ + .kind = context.node_limit_hit + ? ResolutionErrorKind::search_exhausted + : ResolutionErrorKind::incompatible_version, + .field = "limits.max_search_nodes", + .message = "resolution search did not produce a package set"}}; + } + + Resolution resolution; + resolution.packages.reserve(solution.size()); + for (const auto& [id, index] : solution) { + const auto& package = available.at(id)[index]; + std::vector capabilities = package.capabilities; + std::sort(capabilities.begin(), capabilities.end()); + capabilities.erase( + std::unique(capabilities.begin(), capabilities.end()), + capabilities.end()); + resolution.packages.push_back(ResolvedPackage{ + .identity = PackageIdentity{package.id, + package.version, + package.content_digest}, + .capabilities = std::move(capabilities)}); + } + return resolution; +} + +/// Convenience: build a dependency from an id and a SemVer range expression. +[[nodiscard]] inline std::optional + make_package_dependency(std::string id, std::string_view range) +{ + auto parsed = Void::Semver::VersionRange::parse(range); + if (!parsed.has_value()) { + return std::nullopt; + } + return PackageDependency{.id = std::move(id), .range = std::move(*parsed)}; +} + +} // namespace Void::Update diff --git a/tests/standalone/Update/Resolver.cpp b/tests/standalone/Update/Resolver.cpp new file mode 100644 index 0000000..f965b1c --- /dev/null +++ b/tests/standalone/Update/Resolver.cpp @@ -0,0 +1,268 @@ +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace +{ + +using Void::Update::PackageDependency; +using Void::Update::PackageDescription; +using Void::Update::Resolution; +using Void::Update::ResolutionDiagnostic; +using Void::Update::ResolutionRequest; +using Void::Semver::Version; + +[[nodiscard]] Version version(std::string_view text) +{ + return *Void::Semver::parse_strict(text); +} + +[[nodiscard]] PackageDependency dependency(std::string_view id, + std::string_view range) +{ + auto parsed = Void::Update::make_package_dependency(std::string{id}, range); + return *parsed; +} + +[[nodiscard]] PackageDescription package( + std::string id, + std::string_view version_text, + std::string digest, + std::vector capabilities = {}, + std::vector dependencies = {}) +{ + return PackageDescription{.id = std::move(id), + .version = version(version_text), + .content_digest = std::move(digest), + .capabilities = std::move(capabilities), + .dependencies = std::move(dependencies)}; +} + +using Summary = std::tuple; + +[[nodiscard]] std::vector summarize(const Resolution& resolution) +{ + std::vector out; + for (const auto& entry : resolution.packages) { + out.emplace_back(entry.identity.id, + entry.identity.version.to_string(), + entry.identity.content_digest); + } + return out; +} + +[[nodiscard]] std::vector selected(std::initializer_list ids) +{ + std::vector out; + for (const char* id : ids) { + out.push_back(id); + } + return out; +} + +} // namespace + +Void::Testing::Test resolve_packages_transitive_closure() +{ + std::vector installed{ + package("app", "1.0.0", "d-app", {}, {dependency("lib", "^1.0")}), + package("lib", "1.0.0", "d-lib-1"), + package("lib", "1.1.0", "d-lib-2"), + package("lib", "2.0.0", "d-lib-3"), + }; + auto roots = selected({"app"}); + auto result = Void::Update::resolve_packages( + ResolutionRequest{.installed = installed, .selected = roots}); + Void::Testing::require(result.has_value()); + const auto summary = summarize(*result); + Void::Testing::require(summary.size() == 2); + Void::Testing::expect_eq(std::get<0>(summary[0]), std::string{"app"}); + Void::Testing::expect_eq(std::get<0>(summary[1]), std::string{"lib"}); + Void::Testing::expect_eq(std::get<1>(summary[1]), std::string{"1.1.0"}); + Void::Testing::expect_eq(std::get<2>(summary[1]), std::string{"d-lib-2"}); +} + +Void::Testing::Test resolve_packages_is_order_invariant() +{ + std::vector installed{ + package("app", "1.0.0", "d-app", {}, {dependency("lib", "^1.0")}), + package("lib", "1.0.0", "d-lib-1"), + package("lib", "1.1.0", "d-lib-2"), + }; + auto roots = selected({"app"}); + const auto first = Void::Update::resolve_packages( + ResolutionRequest{.installed = installed, .selected = roots}); + std::reverse(installed.begin(), installed.end()); + const auto second = Void::Update::resolve_packages( + ResolutionRequest{.installed = installed, .selected = roots}); + Void::Testing::require(first.has_value() && second.has_value()); + Void::Testing::expect(summarize(*first) == summarize(*second)); +} + +Void::Testing::Test resolve_packages_missing_dependency() +{ + std::vector installed{ + package("app", "1.0.0", "d-app", {}, {dependency("gone", "^1.0")})}; + auto roots = selected({"app"}); + auto result = Void::Update::resolve_packages( + ResolutionRequest{.installed = installed, .selected = roots}); + Void::Testing::require(!result.has_value()); + Void::Testing::expect(result.error().kind == + Void::Update::ResolutionErrorKind::missing_dependency); + Void::Testing::expect_eq(result.error().package, std::string{"app"}); + Void::Testing::expect_eq(result.error().field, + std::string{"dependencies[0].range"}); + Void::Testing::expect_eq(result.error().related, std::string{"gone"}); +} + +Void::Testing::Test resolve_packages_incompatible_version() +{ + std::vector installed{ + package("app", "1.0.0", "d-app", {}, {dependency("lib", "^2.0")}), + package("lib", "1.0.0", "d-lib")}; + auto roots = selected({"app"}); + auto result = Void::Update::resolve_packages( + ResolutionRequest{.installed = installed, .selected = roots}); + Void::Testing::require(!result.has_value()); + Void::Testing::expect(result.error().kind == + Void::Update::ResolutionErrorKind::incompatible_version); + Void::Testing::expect_eq(result.error().related, std::string{"lib"}); +} + +Void::Testing::Test resolve_packages_backtracks_for_constraints() +{ + std::vector installed{ + package("app", "1.0.0", "d-app", {}, {dependency("lib", "=1.0.0")}), + package("lib", "1.0.0", "d-lib-1"), + package("lib", "1.1.0", "d-lib-2")}; + auto roots = selected({"app", "lib"}); + auto result = Void::Update::resolve_packages( + ResolutionRequest{.installed = installed, .selected = roots}); + Void::Testing::require(result.has_value()); + const auto summary = summarize(*result); + Void::Testing::require(summary.size() == 2); + Void::Testing::expect_eq(std::get<1>(summary[1]), std::string{"1.0.0"}); +} + +Void::Testing::Test resolve_packages_detects_cycle() +{ + std::vector installed{ + package("a", "1.0.0", "d-a", {}, {dependency("b", "*")}), + package("b", "1.0.0", "d-b", {}, {dependency("a", "*")})}; + auto roots = selected({"a"}); + auto result = Void::Update::resolve_packages( + ResolutionRequest{.installed = installed, .selected = roots}); + Void::Testing::require(!result.has_value()); + Void::Testing::expect(result.error().kind == + Void::Update::ResolutionErrorKind::cyclic_dependency); + Void::Testing::expect_eq(result.error().related, std::string{"a -> b -> a"}); +} + +Void::Testing::Test resolve_packages_rejects_ambiguous_identity() +{ + std::vector installed{ + package("lib", "1.0.0+one", "d-one"), + package("lib", "1.0.0+two", "d-two")}; + auto roots = selected({"lib"}); + auto result = Void::Update::resolve_packages( + ResolutionRequest{.installed = installed, .selected = roots}); + Void::Testing::require(!result.has_value()); + Void::Testing::expect(result.error().kind == + Void::Update::ResolutionErrorKind::ambiguous_package); + Void::Testing::expect_eq(result.error().package, std::string{"lib"}); +} + +Void::Testing::Test resolve_packages_rejects_competing_exclusive_capabilities() +{ + std::vector installed{ + package("gpu-a", "1.0.0", "d-a", {"gpu"}), + package("gpu-b", "1.0.0", "d-b", {"gpu"})}; + auto roots = selected({"gpu-a", "gpu-b"}); + auto result = Void::Update::resolve_packages( + ResolutionRequest{.installed = installed, .selected = roots}); + Void::Testing::require(!result.has_value()); + Void::Testing::expect( + result.error().kind == + Void::Update::ResolutionErrorKind::exclusive_capability_conflict); + Void::Testing::expect_eq(result.error().package, std::string{"gpu-a"}); + Void::Testing::expect_eq(result.error().related, std::string{"gpu-b"}); +} + +Void::Testing::Test resolve_packages_frozen_identity_includes_build_and_digest() +{ + const Void::Update::PackageIdentity base{.id = "lib", + .version = version("1.0.0+build.1"), + .content_digest = "sha256:a"}; + auto changed_build = base; + changed_build.version = version("1.0.0+build.2"); + auto changed_digest = base; + changed_digest.content_digest = "sha256:b"; + + Void::Testing::expect(!(base == changed_build)); + Void::Testing::expect(!(base == changed_digest)); + Void::Testing::expect(base == base); + // SemVer precedence alone treats both as equal; the frozen identity must not. + Void::Testing::expect((base.version <=> changed_build.version) == 0); +} + +Void::Testing::Test resolve_packages_same_precedence_distinct_digest_is_ambiguous() +{ + std::vector installed{ + package("lib", "1.0.0+build.1", "sha256:a"), + package("lib", "1.0.0+build.1", "sha256:b")}; + auto roots = selected({"lib"}); + auto result = Void::Update::resolve_packages( + ResolutionRequest{.installed = installed, .selected = roots}); + Void::Testing::require(!result.has_value()); + Void::Testing::expect(result.error().kind == + Void::Update::ResolutionErrorKind::ambiguous_package); +} + +Void::Testing::Test resolve_packages_rejects_empty_fields() +{ + std::vector installed{ + package("lib", "1.0.0", "")}; + auto roots = selected({"lib"}); + auto result = Void::Update::resolve_packages( + ResolutionRequest{.installed = installed, .selected = roots}); + Void::Testing::require(!result.has_value()); + Void::Testing::expect(result.error().kind == + Void::Update::ResolutionErrorKind::invalid_request); + Void::Testing::expect_eq(result.error().field, + std::string{"content_digest"}); +} + +Void::Testing::Test resolve_packages_enforces_package_limit() +{ + std::vector installed{ + package("app", "1.0.0", "d-app", {}, {dependency("lib", "*")}), + package("lib", "1.0.0", "d-lib")}; + auto roots = selected({"app"}); + Void::Update::ResolutionLimits limits; + limits.max_packages = 1; + auto result = Void::Update::resolve_packages( + ResolutionRequest{.installed = installed, + .selected = roots, + .limits = limits}); + Void::Testing::require(!result.has_value()); + Void::Testing::expect(result.error().kind == + Void::Update::ResolutionErrorKind::limit_exceeded); +} + +Void::Testing::Test resolve_packages_deduplicates_identical_descriptions() +{ + std::vector installed{ + package("lib", "1.0.0", "d-lib"), + package("lib", "1.0.0", "d-lib")}; + auto roots = selected({"lib"}); + auto result = Void::Update::resolve_packages( + ResolutionRequest{.installed = installed, .selected = roots}); + Void::Testing::require(result.has_value()); + Void::Testing::expect_eq(result->packages.size(), std::size_t{1}); +}