Files
FWS-PacManage/tests/app-core.test.cpp
T
BlackAngelTV 897c174925 refactor(arch): split the app into a logic core and per-screen pages
All the business logic now lives in AppCore (providers, workers,
history, plugins, sudo and pacman-lock handling), freed from any widget
so it runs headless and is testable on its own. The Qt window becomes a
thin frame around one file per screen under pages/, wired through a
common Page contract and a shared ui-kit. CMake gains the app-core-test
target, which links Qt Core only to enforce the headless guarantee.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-09-13 11:45:48 +02:00

210 lines
6.9 KiB
C++

#include "app-core.h"
#include <QCoreApplication>
#include <QDir>
#include <QStringList>
#include <QTemporaryDir>
#include <cassert>
#include <iostream>
// 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 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();
testBulkRemovalRefusesAnEmptySelection();
testPrivilegedActionAsksForThePasswordFirst();
testCredentialsStayInTheCore();
testCredentialModeIsPersistedAndReported();
testRetryWithoutAPendingRequestIsRefused();
testHistoryIsOwnedByTheCore();
testPluginToggleAnnouncesTheChange();
testLogReadReportsItsFailure();
testUpdateStateIsReadableWithoutRunningAnything();
testDatabaseLockIsQueryable();
std::cout << "app-core: all tests passed" << std::endl;
return 0;
}