// Unit tests for the pure parts of UpdateManager: version comparison and the // update-strategy choice (no network, toolchain or filesystem needed). #include #include #include #include "update-manager.h" static int failures = 0; static void check(bool cond, const char* name) { if (cond) { std::cout << "PASS " << name << std::endl; } else { std::cout << "FAIL " << name << std::endl; ++failures; } } using Caps = UpdateManager::UpdateCapabilities; using Method = UpdateManager::UpdateMethod; static void testChooseMethod_checkoutWinsOverEverything() { Caps caps; caps.checkoutUsable = true; caps.hasGit = caps.hasCmake = caps.hasCompiler = true; caps.hasPrebuilt = caps.hasTarball = true; check(UpdateManager::chooseUpdateMethod(caps) == Method::Checkout, "method_checkout_wins"); } static void testChooseMethod_cloneWhenToolchainPresent() { Caps caps; caps.hasGit = caps.hasCmake = caps.hasCompiler = true; caps.hasPrebuilt = caps.hasTarball = true; // The clone path is deliberately preferred over the prebuilt asset. check(UpdateManager::chooseUpdateMethod(caps) == Method::Clone, "method_clone_first"); } static void testChooseMethod_cloneRequiresCompiler() { Caps caps; caps.hasGit = caps.hasCmake = true; // no compiler -> cannot rebuild check(UpdateManager::chooseUpdateMethod(caps) == Method::None, "method_clone_needs_compiler"); } static void testChooseMethod_prebuiltWithoutToolchain() { Caps caps; caps.hasPrebuilt = caps.hasTarball = true; check(UpdateManager::chooseUpdateMethod(caps) == Method::Prebuilt, "method_prebuilt_without_tools"); } static void testChooseMethod_prebuiltWithGitOnly() { Caps caps; caps.hasGit = true; // git alone cannot build caps.hasPrebuilt = true; check(UpdateManager::chooseUpdateMethod(caps) == Method::Prebuilt, "method_prebuilt_git_only"); } static void testChooseMethod_prebuiltPreferredOverTarball() { Caps caps; caps.hasCmake = caps.hasCompiler = true; // no git caps.hasPrebuilt = caps.hasTarball = true; check(UpdateManager::chooseUpdateMethod(caps) == Method::Prebuilt, "method_prebuilt_over_tarball"); } static void testChooseMethod_tarballWithoutGit() { Caps caps; caps.hasCmake = caps.hasCompiler = true; // no git caps.hasTarball = true; check(UpdateManager::chooseUpdateMethod(caps) == Method::Tarball, "method_tarball_without_git"); } static void testChooseMethod_tarballRequiresCompiler() { Caps caps; caps.hasCmake = true; // no compiler caps.hasTarball = true; check(UpdateManager::chooseUpdateMethod(caps) == Method::None, "method_tarball_needs_compiler"); } static void testChooseMethod_noneWhenNothingAvailable() { Caps caps; check(UpdateManager::chooseUpdateMethod(caps) == Method::None, "method_none_without_anything"); } int main() { // Basic ordering check(UpdateManager::isNewerVersion("1.3.2", "1.3.1") == true, "newer_patch"); check(UpdateManager::isNewerVersion("1.3.1", "1.3.1") == false, "same_version"); check(UpdateManager::isNewerVersion("1.3.0", "1.3.1") == false, "older_patch"); check(UpdateManager::isNewerVersion("1.4.0", "1.3.9") == true, "newer_minor"); check(UpdateManager::isNewerVersion("2.0.0", "1.9.9") == true, "newer_major"); // Leading v/V (git tags) is normalized check(UpdateManager::isNewerVersion("v1.3.2", "1.3.1") == true, "tag_v_prefix"); check(UpdateManager::isNewerVersion("V1.3.2", "1.3.1") == true, "tag_V_prefix"); check(UpdateManager::isNewerVersion("v1.3.1", "1.3.1") == false, "tag_same"); // Different component counts: 1.3 == 1.3.0 check(UpdateManager::isNewerVersion("1.3.1", "1.3") == true, "longer_newer"); check(UpdateManager::isNewerVersion("1.3", "1.3.0") == false, "equal_implicit_zero"); // Empty remote never triggers an update prompt check(UpdateManager::isNewerVersion("", "1.3.1") == false, "empty_remote"); // Update-strategy choice testChooseMethod_checkoutWinsOverEverything(); testChooseMethod_cloneWhenToolchainPresent(); testChooseMethod_cloneRequiresCompiler(); testChooseMethod_prebuiltWithoutToolchain(); testChooseMethod_prebuiltWithGitOnly(); testChooseMethod_prebuiltPreferredOverTarball(); testChooseMethod_tarballWithoutGit(); testChooseMethod_tarballRequiresCompiler(); testChooseMethod_noneWhenNothingAvailable(); if (failures == 0) std::cout << "All update-manager tests passed" << std::endl; return failures == 0 ? 0 : 1; }