#include #include #include #include #include #include #include using Echoion::Core::TrackCache; namespace { struct TempDir { std::filesystem::path path; TempDir() { path = std::filesystem::temp_directory_path() / ("echoion-cache-test-" + std::to_string( std::chrono::steady_clock::now() .time_since_epoch() .count())); std::filesystem::create_directories(path); } ~TempDir() { std::error_code ec; std::filesystem::remove_all(path, ec); } }; std::string makeFile(const std::filesystem::path& dir, const std::string& name, size_t bytes) { const auto p = dir / name; std::ofstream out(p, std::ios::binary); out << std::string(bytes, 'x'); return p.string(); } } // namespace TEST_CASE("TrackCache::parseSize handles common suffixes") { CHECK(TrackCache::parseSize("1024") == 1024); CHECK(TrackCache::parseSize("1KB") == 1024); CHECK(TrackCache::parseSize("500MB") == 500ull << 20); CHECK(TrackCache::parseSize("1GB") == 1ull << 30); CHECK(TrackCache::parseSize("2gb") == 2ull << 30); CHECK(TrackCache::parseSize("garbage") == 0); CHECK(TrackCache::parseSize("") == 0); } TEST_CASE("TrackCache stores, looks up and owns files") { TempDir staging; TempDir cacheDir; TrackCache cache( TrackCache::Config{.enabled = true, .maxBytes = 1 << 20, .ttl = std::chrono::seconds(3600)}, cacheDir.path); CHECK_FALSE(cache.lookup("videoA").has_value()); const auto source = makeFile(staging.path, "download.m4a", 100); const auto cachedPath = cache.store("videoA", source); CHECK(cachedPath != source); CHECK(cache.owns(cachedPath)); CHECK_FALSE(cache.owns(source)); CHECK_FALSE(std::filesystem::exists(source)); // moved, not copied auto hit = cache.lookup("videoA"); REQUIRE(hit.has_value()); CHECK(*hit == cachedPath); CHECK(std::filesystem::exists(cachedPath)); } TEST_CASE("TrackCache evicts LRU past the size budget") { TempDir staging; TempDir cacheDir; TrackCache cache( TrackCache::Config{.enabled = true, .maxBytes = 250, // fits two 100-byte files .ttl = std::chrono::seconds(3600)}, cacheDir.path); (void)cache.store("first", makeFile(staging.path, "a.m4a", 100)); std::this_thread::sleep_for(std::chrono::milliseconds(20)); (void)cache.store("second", makeFile(staging.path, "b.m4a", 100)); std::this_thread::sleep_for(std::chrono::milliseconds(20)); // Third store pushes the total to 300 > 250: "first" (oldest) goes. (void)cache.store("third", makeFile(staging.path, "c.m4a", 100)); CHECK_FALSE(cache.lookup("first").has_value()); CHECK(cache.lookup("second").has_value()); CHECK(cache.lookup("third").has_value()); } TEST_CASE("TrackCache expires entries past the TTL") { TempDir staging; TempDir cacheDir; TrackCache cache(TrackCache::Config{.enabled = true, .maxBytes = 1 << 20, .ttl = std::chrono::seconds(0)}, cacheDir.path); (void)cache.store("old", makeFile(staging.path, "a.m4a", 10)); std::this_thread::sleep_for(std::chrono::milliseconds(20)); CHECK_FALSE(cache.lookup("old").has_value()); } TEST_CASE("TrackCache disabled is a transparent pass-through") { TempDir staging; TempDir cacheDir; TrackCache cache(TrackCache::Config{.enabled = false}, cacheDir.path); const auto source = makeFile(staging.path, "a.m4a", 10); CHECK(cache.store("x", source) == source); CHECK(std::filesystem::exists(source)); CHECK_FALSE(cache.lookup("x").has_value()); }