#include "flatpak-manager.h" #include #include static void testParseSearchOutput_parsesAlignedRows() { std::string output = "Name Description Application ID Version Branch Remotes\n" "Firefox Fast and privacy-aware browser org.mozilla.firefox 128.0.2 stable flathub\n" "Calculator Simple calculator org.gnome.Calculator 46.0 stable flathub\n"; auto pkgs = FlatpakManager::parseSearchOutput(output); assert(pkgs.size() == 2); assert(pkgs[0].name == "org.mozilla.firefox"); assert(pkgs[0].version == "128.0.2"); assert(pkgs[0].repository == "flathub"); // The display name is prefixed to the description. assert(pkgs[0].description == "Firefox — Fast and privacy-aware browser"); assert(pkgs[1].name == "org.gnome.Calculator"); } static void testParseSearchOutput_keepsMultiWordDescriptions() { std::string output = "Name Description Application ID Version Branch Remotes\n" "Spotify Desktop music player with sync org.spotify.Client 1.2.3 stable flathub\n"; auto pkgs = FlatpakManager::parseSearchOutput(output); assert(pkgs.size() == 1); assert(pkgs[0].name == "org.spotify.Client"); assert(pkgs[0].description == "Spotify — Desktop music player with sync"); } static void testParseSearchOutput_ignoresVersionLikeTokens() { // Versions such as 128.0.2 must not be mistaken for an application ID. std::string output = "Name Description Application ID Version Branch Remotes\n" "GIMP Image editor org.gimp.GIMP 2.10.36 stable flathub\n"; auto pkgs = FlatpakManager::parseSearchOutput(output); assert(pkgs.size() == 1); assert(pkgs[0].name == "org.gimp.GIMP"); assert(pkgs[0].version == "2.10.36"); } static void testParseSearchOutput_emptyInput() { assert(FlatpakManager::parseSearchOutput("").empty()); } static void testParseInstalledOutput_parsesTabSeparated() { std::string output = "org.mozilla.firefox\t128.0.2\tflathub\n" "org.gnome.Calculator\t46.0\tflathub\n"; auto pkgs = FlatpakManager::parseInstalledOutput(output); assert(pkgs.size() == 2); assert(pkgs[0].name == "org.mozilla.firefox"); assert(pkgs[0].version == "128.0.2"); assert(pkgs[0].repository == "flathub"); assert(pkgs[0].installed); assert(!pkgs[0].updateAvailable); } static void testParseInstalledOutput_emptyInput() { assert(FlatpakManager::parseInstalledOutput("").empty()); } // The same application installed more than once: one copy per installation, // per remote or per branch. Each row must say which one it is, or the view // cannot list the copies and the provider cannot act on any of them. static void testParseInstalledOutput_namesEveryInstallation() { std::string output = "com.hoptodesk.HopToDesk\t\thoptodesk-origin\tcom.hoptodesk.HopToDesk/x86_64/master\tmaster\tuser\n" "com.hoptodesk.HopToDesk\t1.46.38\tflathub\tcom.hoptodesk.HopToDesk/x86_64/stable\tstable\tuser\n" "com.usebottles.bottles\t67.3\tflathub\tcom.usebottles.bottles/x86_64/stable\tstable\tsystem\n" "com.usebottles.bottles\t67.3\tflathub\tcom.usebottles.bottles/x86_64/stable\tstable\tuser\n"; auto pkgs = FlatpakManager::parseInstalledOutput(output); assert(pkgs.size() == 4); // An empty version is a column, not a gap: dropping it shifted every field // after it, and the row said nothing about which copy it was. assert(pkgs[0].version.empty()); assert(pkgs[0].repository == "hoptodesk-origin"); // The target is ":": the two halves are what flatpak // needs to refuse the application id and name this copy instead. assert(pkgs[0].instances[0].target == "user:com.hoptodesk.HopToDesk/x86_64/master"); assert(pkgs[1].instances[0].target == "user:com.hoptodesk.HopToDesk/x86_64/stable"); assert(pkgs[2].instances[0].target == "system:com.usebottles.bottles/x86_64/stable"); assert(pkgs[3].instances[0].target == "user:com.usebottles.bottles/x86_64/stable"); // A label says the installation, and the remote or branch when it is what // tells two copies of the same id apart. assert(pkgs[0].instances[0].label == "utilisateur · hoptodesk-origin · master"); assert(pkgs[1].instances[0].label == "utilisateur"); assert(pkgs[2].instances[0].label == "système"); } // A listing without the ref/branch/installation columns stays readable: the // row is the application, with no copy to choose between. static void testParseInstalledOutput_withoutInstallationColumns() { auto pkgs = FlatpakManager::parseInstalledOutput("org.gnome.Calculator\t47.0\tflathub\n"); assert(pkgs.size() == 1); assert(pkgs[0].name == "org.gnome.Calculator"); assert(pkgs[0].instances.empty()); } // Once merged, a row must keep the copies of every row it absorbed: that is // what the chooser lists. static void testMergedRowKeepsEveryCopy() { std::vector rows = FlatpakManager::parseInstalledOutput( "com.usebottles.bottles\t67.3\tflathub\tcom.usebottles.bottles/x86_64/stable\tstable\tsystem\n" "com.usebottles.bottles\t67.3\tflathub\tcom.usebottles.bottles/x86_64/stable\tstable\tuser\n"); mergeProviderDuplicates(rows); assert(rows.size() == 1); assert(rows[0].instances.size() == 2); assert(rows[0].instances[0].target == "system:com.usebottles.bottles/x86_64/stable"); assert(rows[0].instances[1].target == "user:com.usebottles.bottles/x86_64/stable"); } static void testParseUpdatesOutput_parsesRows() { std::string output = "org.mozilla.firefox\t130.0\n" "org.gnome.Calculator\t47.0\n"; auto pkgs = FlatpakManager::parseUpdatesOutput(output); assert(pkgs.size() == 2); assert(pkgs[0].name == "org.mozilla.firefox"); assert(pkgs[0].version == "130.0"); assert(pkgs[0].installed); assert(pkgs[0].updateAvailable); } static void testParseUpdatesOutput_emptyInput() { assert(FlatpakManager::parseUpdatesOutput("").empty()); } // ---- Installing from a file (drag & drop, "Ouvrir avec PacManager") ---- static void testClassifyFile_recognisesTheThreeFlatpakFormats() { using FileKind = FlatpakManager::FileKind; assert(FlatpakManager::classifyFile("/home/jane/Téléchargements/org.gnome.Dictionary.flatpakref") == FileKind::Ref); assert(FlatpakManager::classifyFile("flathub.flatpakrepo") == FileKind::Repo); assert(FlatpakManager::classifyFile("/tmp/app.flatpak") == FileKind::Bundle); // A bundle's extension starts like a ref one's, so it must not be read as // a .flatpakref by accident. assert(FlatpakManager::classifyFile("/tmp/Org.App.flatpak") != FileKind::Ref); // Case and an URL query string do not change the format. assert(FlatpakManager::classifyFile("https://flathub.org/repo/appstream/x.FLATPAKREF") == FileKind::Ref); assert(FlatpakManager::classifyFile("https://example.org/app.flatpakref?v=2") == FileKind::Ref); // Anything else is not ours: a pacman package, a directory named after the // format, or a plain document. assert(FlatpakManager::classifyFile("/tmp/firefox.pkg.tar.zst") == FileKind::None); assert(FlatpakManager::classifyFile("/tmp/flatpak") == FileKind::None); assert(FlatpakManager::classifyFile("") == FileKind::None); } static void testRepoName_comesFromTheFileName() { assert(FlatpakManager::repoNamespace("flathub.flatpakrepo") == "flathub"); assert(FlatpakManager::repoNamespace("/tmp/My-Repo.FlatpakRepo") == "my-repo"); // A nameless path still has to register a remote under some name. assert(!FlatpakManager::repoNamespace("").empty()); } static void testInstallFileCommand_buildsTheFlatpakCalls() { using FileKind = FlatpakManager::FileKind; // A ref file or bundle is handed to `flatpak install`, non-interactively so // a dropped file cannot open a prompt nobody is looking at. const std::string ref = FlatpakManager::installFileCommand( FileKind::Ref, "/tmp/org.gnome.Dictionary.flatpakref"); assert(ref.find("flatpak install") == 0); assert(ref.find("--noninteractive") != std::string::npos); assert(ref.find("--assumeyes") != std::string::npos); assert(ref.find("/tmp/org.gnome.Dictionary.flatpakref") != std::string::npos); assert(ref.find("--user") == std::string::npos); // The per-user retry (polkit refused, no admin rights) adds --user. const std::string userRef = FlatpakManager::installFileCommand( FileKind::Ref, "/tmp/org.gnome.Dictionary.flatpakref", /*userScope=*/true); assert(userRef.find("--user") != std::string::npos); // A .flatpakrepo registers the remote under the file's name instead. const std::string repo = FlatpakManager::installFileCommand( FileKind::Repo, "/tmp/flathub.flatpakrepo", /*userScope=*/true); assert(repo.find("flatpak remote-add") == 0); assert(repo.find("--if-not-exists") != std::string::npos); assert(repo.find("flathub ") != std::string::npos); assert(repo.find("/tmp/flathub.flatpakrepo") != std::string::npos); // A path with spaces stays a single argument. const std::string quoted = FlatpakManager::installFileCommand( FileKind::Bundle, "/tmp/mon paquet.flatpak"); assert(quoted.find("'/tmp/mon paquet.flatpak'") != std::string::npos); } // Naming one copy is what makes the call unambiguous: the ref alone picks the // remote and the branch, the scope picks the installation. static void testUninstallAndUpdateCommands_targetOneCopy() { const std::string userCopy = FlatpakManager::uninstallCommand("user:com.example.App/x86_64/stable"); assert(userCopy.find("flatpak uninstall") == 0); assert(userCopy.find("--user") != std::string::npos); assert(userCopy.find("com.example.App/x86_64/stable") != std::string::npos); // The application's own id must not be in there any more: that is exactly // what flatpak refuses when several copies exist. assert(userCopy.find("--assumeyes com.example.App ") == std::string::npos); const std::string systemCopy = FlatpakManager::uninstallCommand("system:com.example.App/x86_64/stable"); assert(systemCopy.find("flatpak uninstall") == 0); assert(systemCopy.find("--user") == std::string::npos); assert(systemCopy.find("com.example.App/x86_64/stable") != std::string::npos); const std::string update = FlatpakManager::updateCommand("user:com.example.App/x86_64/stable"); assert(update.find("flatpak update") == 0); assert(update.find("--user") != std::string::npos); assert(update.find("com.example.App/x86_64/stable") != std::string::npos); } // A plain application id keeps working as it always did — the provider only // needs the target when the id is ambiguous, and finds out by trying. static void testUninstallCommand_stillAcceptsAnApplicationId() { const std::string plain = FlatpakManager::uninstallCommand("com.example.App"); assert(plain.find("flatpak uninstall") == 0); assert(plain.find("com.example.App") != std::string::npos); assert(plain.find("--user") == std::string::npos); } int main() { testParseSearchOutput_parsesAlignedRows(); testParseSearchOutput_keepsMultiWordDescriptions(); testParseSearchOutput_ignoresVersionLikeTokens(); testParseSearchOutput_emptyInput(); testParseInstalledOutput_parsesTabSeparated(); testParseInstalledOutput_emptyInput(); testParseInstalledOutput_namesEveryInstallation(); testParseInstalledOutput_withoutInstallationColumns(); testMergedRowKeepsEveryCopy(); testParseUpdatesOutput_parsesRows(); testParseUpdatesOutput_emptyInput(); testClassifyFile_recognisesTheThreeFlatpakFormats(); testRepoName_comesFromTheFileName(); testInstallFileCommand_buildsTheFlatpakCalls(); testUninstallAndUpdateCommands_targetOneCopy(); testUninstallCommand_stillAcceptsAnApplicationId(); std::cout << "All flatpak-manager tests passed." << std::endl; return 0; }