// Unit tests for ProgressTracker: the single monotonic percentage the UI bars // are fed, built out of the very different ways package managers report their // progress. No package manager, no display and no network is involved: the // tracker only reads output lines. #include "progress-tracker.h" #include #include // pacman picks up the download counter of the packages it fetches; the bar must // stay inside the download half and finish it at the last package. static void testDownloadPhaseStaysInsideItsHalf() { ProgressTracker tracker; tracker.feed(" (1/4) téléchargement de a-1.0-1-x86_64..."); const int first = tracker.percent(); assert(first >= 0 && first < 45); // The file being fetched drives the bar inside the package it belongs to. assert(tracker.feed(" a-1.0-1-x86_64 1.2 MiB 640 KiB/s [####] 50%") == 5); tracker.feed(" (4/4) téléchargement de d-1.0-1-x86_64..."); assert(tracker.feed(" d-1.0-1-x86_64 1.2 MiB 640 KiB/s [####] 100%") == 45); } // The install phase starts where the download phase stopped (pacman restarts // its counter at 1, so the raw numbers would send the bar back to zero). static void testApplyPhaseFollowsTheDownloadPhase() { ProgressTracker tracker; tracker.feed(" (1/1) téléchargement de a-1.0-1-x86_64..."); tracker.feed(" a-1.0-1-x86_64 1.2 MiB 640 KiB/s [####] 100%"); assert(tracker.percent() == 45); assert(tracker.feed(" (1/4) installation de a-1.0-1-x86_64...") == 58); assert(tracker.feed(" (2/4) installation de b-1.0-1-x86_64...") == 71); assert(tracker.feed(" (4/4) installation de d-1.0-1-x86_64...") == 97); } // A transaction with nothing to download (everything already cached) starts // near zero instead of pretending the download half happened. static void testApplyOnlyTransactionStartsLow() { ProgressTracker tracker; assert(tracker.feed(" (1/2) installation de a-1.0-1-x86_64...") == 50); assert(tracker.feed(" (2/2) installation de b-1.0-1-x86_64...") == 97); } // Steps whose share of the work is unknown — pacman's key and integrity // checks, its file conflict analysis — hold the bar instead of moving it back. static void testUnknownStepsHoldTheBar() { ProgressTracker tracker; tracker.feed(" (1/4) installation de a-1.0-1-x86_64..."); const int held = tracker.percent(); assert(tracker.feed(" (1/4) vérification des clés du trousseau") == -1); assert(tracker.feed(" (2/4) analyse des conflits entre fichiers") == -1); assert(tracker.percent() == held); } // A counter that goes backwards (a retry, a second command, a new phase) never // lowers the value: this is what used to make the bar bounce left and right. static void testValueNeverMovesBackwards() { ProgressTracker tracker; assert(tracker.feed(" (3/4) installation de c-1.0-1-x86_64...") == 84); assert(tracker.feed(" (1/4) installation de a-1.0-1-x86_64...") == -1); assert(tracker.percent() == 84); } // flatpak and the AUR helpers print "Installing 1/3…" instead of a parenthesised // counter; both must be understood. static void testFlatpakAndHelperCountersAreRead() { ProgressTracker tracker; assert(tracker.feed("Installing 1/3…") == 34); assert(tracker.feed("Installing 2/3…") == 65); assert(tracker.feed("Installing 3/3…") == 97); ProgressTracker english; assert(english.feed(" (1/2) downloading base-1.0-1-x86_64...") == 22); assert(english.feed(" (2/2) upgrading base-1.0-1-x86_64...") == 71); } // A bare "12 %" with no keyword is a download meter (that is the only place // tools print one without naming the step). static void testBarePercentIsDownloadProgress() { ProgressTracker tracker; assert(tracker.feed(" 12% ░░░░░░░░░░") == 5); assert(tracker.feed(" 50% ░░░░░░░░░░") == 22); } // Finalisation and hooks are the last step before completion, which the caller // reports: the tracker itself never claims 100 %. static void testFinalisationClosesInOnTheEnd() { ProgressTracker tracker; assert(tracker.feed(":: Finalisation de la transaction...") == 98); assert(tracker.feed(" (1/1) hook: installation de a...") == 98); assert(tracker.percent() < 100); } // Nothing that only looks like a counter may be read as one. static void testRatiosThatAreNotCountersAreIgnored() { ProgressTracker tracker; assert(tracker.feed(":: Synchronisation des bases de données du paquet…") == -1); assert(tracker.feed(" https://mirror.example.org/arch/extra/os/x86_64/") == -1); assert(tracker.feed("firefox 128.0.2/1.0 nothing to do") == -1); assert(tracker.percent() == 0); } // Repeating the same line (pacman reprints steps as it goes) costs nothing and // produces no UI update. static void testRepeatedLinesReportNoChange() { ProgressTracker tracker; assert(tracker.feed(" (1/2) installation de a-1.0-1-x86_64...") == 50); assert(tracker.feed(" (1/2) installation de a-1.0-1-x86_64...") == -1); assert(tracker.feed("") == -1); } int main() { testDownloadPhaseStaysInsideItsHalf(); testApplyPhaseFollowsTheDownloadPhase(); testApplyOnlyTransactionStartsLow(); testUnknownStepsHoldTheBar(); testValueNeverMovesBackwards(); testFlatpakAndHelperCountersAreRead(); testBarePercentIsDownloadProgress(); testFinalisationClosesInOnTheEnd(); testRatiosThatAreNotCountersAreIgnored(); testRepeatedLinesReportNoChange(); std::cout << "All progress-tracker tests passed." << std::endl; return 0; }