#include "app-core.h" #include #include #include #include #include #include // The logic layer is exercised here without a display and without QtWidgets — // which is the point of the view/core split. Only requests that are decided // before any command runs are asserted: an operation that would actually shell // out to pacman or flatpak is covered by those providers' own tests. namespace { void testCoreDescribesTheSystem() { AppCore core; assert(!core.version().isEmpty()); assert(core.historyCount() == 0); assert(!core.busy()); assert(!core.hasPendingRequest()); // pacman is compiled in, so at least one provider is always known. const auto providers = core.availableProviders(); bool pacmanAvailable = false; for (IPackageManager* pm : providers) { if (pm->getName() == "pacman") pacmanAvailable = true; } assert(pacmanAvailable == core.hasAvailableProvider()); assert(core.providerFor("pacman") != nullptr); // An unknown id falls back to the first available provider, which is what // makes an operation on an untagged package still possible. assert(core.providerFor("not-a-provider") == core.providerFor("pacman")); assert(core.availableProviderNames().contains("pacman")); } void testSearchRefusesAnEmptyQuery() { AppCore core; const AppCore::StartResult result = core.search(" "); assert(!result.started); assert(result.error.contains("terme de recherche")); // A refused request must not leave anything pending. assert(!core.hasPendingRequest()); } void testInstallRefusesAMissingPackageName() { AppCore core; const AppCore::StartResult result = core.install(PackageInfo{}); assert(!result.started); assert(!result.error.isEmpty()); assert(!core.hasPendingRequest()); } void testFileInstallRefusesWhatNoProviderKnows() { AppCore core; // Only Flatpak files can be installed from a file; anything else is refused // rather than silently ignored, and must not leave a request pending. assert(!core.canInstallFile(QStringLiteral("/tmp/liste-de-courses.txt"))); assert(!core.canInstallFile(QString())); const AppCore::StartResult result = core.installFile(QStringLiteral("/tmp/liste-de-courses.txt")); assert(!result.started); assert(!result.error.isEmpty()); assert(!core.hasPendingRequest()); const AppCore::StartResult empty = core.installFile(QString()); assert(!empty.started); assert(empty.error.contains("fichier")); } void testBulkRemovalRefusesAnEmptySelection() { AppCore core; const AppCore::StartResult result = core.removePackages({}, QStringLiteral("rien")); assert(!result.started); assert(result.error.contains("supprimable")); } void testPrivilegedActionAsksForThePasswordFirst() { AppCore core; if (!core.hasAvailableProvider()) return; // nothing to update on this box // No password captured yet: the request is refused but kept pending, so the // view can prompt and replay it. const AppCore::StartResult result = core.updateAll(); assert(!result.started); if (result.needsPassword) { assert(core.hasPendingRequest()); // The view would now supply the password and replay: simulated here by // simply dropping the pending request, so no command is ever run. core.cancelPendingRequest(); assert(!core.hasPendingRequest()); } } void testCredentialsStayInTheCore() { AppCore core; assert(!core.hasSudoPassword()); core.setSudoPassword(QStringLiteral("hunter2")); assert(core.hasSudoPassword()); core.clearSudoPassword(); assert(!core.hasSudoPassword()); } void testCredentialModeIsPersistedAndReported() { AppCore core; int messages = 0; QObject::connect(&core, &AppCore::statusMessage, &core, [&messages](const QString&) { ++messages; }); core.setCredentialMode(AppCore::CredentialMode::AtLaunch); assert(core.credentialMode() == AppCore::CredentialMode::AtLaunch); core.setCredentialMode(AppCore::CredentialMode::EachTime); assert(core.credentialMode() == AppCore::CredentialMode::EachTime); assert(messages >= 2); // Switching back to per-operation mode must drop any captured password. core.setSudoPassword(QStringLiteral("hunter2")); core.setCredentialMode(AppCore::CredentialMode::EachTime); assert(!core.hasSudoPassword()); } void testRetryWithoutAPendingRequestIsRefused() { AppCore core; const AppCore::StartResult result = core.retryPendingRequest(); assert(!result.started); assert(result.error.contains("attente")); } void testHistoryIsOwnedByTheCore() { AppCore core; int changes = 0; QObject::connect(&core, &AppCore::historyChanged, &core, [&changes] { ++changes; }); core.clearHistory(); assert(core.historyCount() == 0); assert(changes >= 1); assert(core.history() != nullptr); } void testPluginToggleAnnouncesTheChange() { AppCore core; int changes = 0; QObject::connect(&core, &AppCore::pluginsChanged, &core, [&changes] { ++changes; }); const bool initial = core.plugins()->isEnabled("pacman"); core.setPluginEnabled("pacman", !initial); assert(core.plugins()->isEnabled("pacman") == !initial); assert(changes == 1); core.setPluginEnabled("pacman", initial); assert(core.plugins()->isEnabled("pacman") == initial); assert(changes == 2); } void testLogReadReportsItsFailure() { AppCore core; QString error; const QString text = core.readPackageLog(&error); // Either the log was read, or the reason is reported — never silence. assert(!text.isEmpty() || !error.isEmpty()); } void testUpdateStateIsReadableWithoutRunningAnything() { AppCore core; assert(!core.binDir().isEmpty()); // binDirWritable() and the update method are plain filesystem probes: they // must answer without launching a build. core.binDirWritable(); core.updateMethodDescription(); core.canSelfUpdate(); } void testDatabaseLockIsQueryable() { AppCore core; const PacmanManager::LockHolder holder = core.lockHolder(); assert(holder.pid >= 0); // A description is only produced while a lock is actually held. if (core.databaseLocked()) { assert(!core.databaseLockDescription().isEmpty()); } else { assert(core.databaseLockDescription().isEmpty()); } } } // namespace int main(int argc, char* argv[]) { // Keep the test hermetic: QSettings, the history file and the update state // all live under the user's home, which a test run must not touch. QTemporaryDir home; assert(home.isValid()); qputenv("HOME", home.path().toLocal8Bit()); qputenv("XDG_CONFIG_HOME", QDir(home.path()).filePath("config").toLocal8Bit()); qputenv("XDG_DATA_HOME", QDir(home.path()).filePath("data").toLocal8Bit()); QCoreApplication app(argc, argv); app.setApplicationName("PacManagerTest"); app.setOrganizationName("FWSTest"); testCoreDescribesTheSystem(); testSearchRefusesAnEmptyQuery(); testInstallRefusesAMissingPackageName(); testFileInstallRefusesWhatNoProviderKnows(); testBulkRemovalRefusesAnEmptySelection(); testPrivilegedActionAsksForThePasswordFirst(); testCredentialsStayInTheCore(); testCredentialModeIsPersistedAndReported(); testRetryWithoutAPendingRequestIsRefused(); testHistoryIsOwnedByTheCore(); testPluginToggleAnnouncesTheChange(); testLogReadReportsItsFailure(); testUpdateStateIsReadableWithoutRunningAnything(); testDatabaseLockIsQueryable(); std::cout << "app-core: all tests passed" << std::endl; return 0; }