yams/tests/unit/daemon/storage_preflight_test.cpp
2026-07-02 21:34:18 -04:00

76 lines
2.4 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include <yams/daemon/components/storage_preflight.h>
#include <chrono>
#include <filesystem>
#include <fstream>
#include <string>
#ifndef _WIN32
#include <sys/stat.h>
#endif
namespace fs = std::filesystem;
namespace {
fs::path makeScratchDir(const std::string& prefix) {
auto base = fs::temp_directory_path() /
(prefix + "_" +
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
fs::create_directories(base);
return base;
}
} // namespace
TEST_CASE("probeStorage: healthy local dir returns reachable=true",
"[unit][daemon][storage_preflight][catch2]") {
const auto dir = makeScratchDir("yams_preflight_ok");
auto res = yams::daemon::probeStorage(dir);
REQUIRE(res.has_value());
REQUIRE(res.value().reachable);
REQUIRE_FALSE(res.value().slow);
REQUIRE(res.value().probeDuration.count() < 500);
fs::remove_all(dir);
}
TEST_CASE("probeStorage: missing parent path is created (idempotent)",
"[unit][daemon][storage_preflight][catch2]") {
auto base = makeScratchDir("yams_preflight_missing");
fs::path nested = base / "nested" / "deeper";
REQUIRE_FALSE(fs::exists(nested));
auto res = yams::daemon::probeStorage(nested);
REQUIRE(res.has_value());
REQUIRE(fs::exists(nested));
fs::remove_all(base);
}
TEST_CASE("probeStorage: non-directory path returns Error",
"[unit][daemon][storage_preflight][catch2]") {
const auto dir = makeScratchDir("yams_preflight_file");
const auto filePath = dir / "not-a-directory";
std::ofstream(filePath) << "not a directory\n";
auto res = yams::daemon::probeStorage(filePath);
fs::remove_all(dir);
REQUIRE_FALSE(res.has_value());
}
TEST_CASE("probeStorage: slow threshold triggers slow=true without erroring",
"[unit][daemon][storage_preflight][catch2]") {
const auto dir = makeScratchDir("yams_preflight_slow");
auto res =
yams::daemon::probeStorage(dir, std::chrono::seconds(2), std::chrono::milliseconds(0));
REQUIRE(res.has_value());
REQUIRE(res.value().slow);
REQUIRE_FALSE(res.value().detail.empty());
fs::remove_all(dir);
}
TEST_CASE("probeStorage: empty path is rejected", "[unit][daemon][storage_preflight][catch2]") {
auto res = yams::daemon::probeStorage(fs::path{});
REQUIRE_FALSE(res.has_value());
}