#include "../src/device_battery.hpp" #include #include // OFFLINE ONLY: these are synthetic callback messages with a controlled clock. // No scanner service, vendor DLL, SDK connection or historical log is used. using freescan::Json; using freescan::detail::DeviceBatteryCache; namespace { size_t passed = 0; constexpr int64_t now = 1700000000000; void check(bool condition, const std::string &message) { if (!condition) throw std::runtime_error(message); ++passed; } Json message() { return {{"cmd", "sdkDeviceStatus"}, {"type", "information"}}; } Json scanner(const Json &value) { auto result = message(); result["scannerBattery"] = value; return result; } Json tracker(const Json &value, const Json &index) { auto result = message(); result["trakBattery"] = value; result["index"] = index; return result; } const Json &channel(const Json &snapshot, const Json &index) { for (const auto &item : snapshot.at("tracker").at("channels")) if (item.at("index") == index) return item; throw std::runtime_error("Missing tracker channel " + index.dump()); } void unavailable(const Json &reading, bool fresh, const std::string &label) { check(reading.at("percent").is_null(), label + ": no percentage is invented."); check(reading.at("available") == false, label + ": unavailable is explicit."); check(reading.at("fresh") == fresh, label + ": freshness is independent of a valid percentage."); } void defaults_and_filtering() { DeviceBatteryCache cache; auto initial = cache.snapshot(false, now); check(initial.at("source") == "sdkDeviceStatus", "The reading source names the actual SDK message."); check(initial.at("freshnessLimitMs") == 30000 && DeviceBatteryCache::freshnessLimitMs == 30000, "The documented freshness window is thirty seconds."); unavailable(initial.at("scanner"), false, "Never-received scanner"); check(initial.at("scanner").at("rawValue").is_null() && initial.at("scanner").at("updatedUtc").is_null(), "An empty cache has no fabricated SDK timestamp or raw value."); check(initial.at("tracker").at("channels").empty(), "No tracker channel exists before an SDK report."); for (const auto &ignored : std::vector{nullptr, Json::array(), "text", 17, Json::object(), {{"cmd", "other"}, {"type", "information"}, {"scannerBattery", 66}}, {{"cmd", "sdkDeviceStatus"}, {"type", "request"}, {"scannerBattery", 66}}, {{"cmd", "sdkDeviceStatus"}, {"scannerBattery", 66}}, {{"cmd", 1}, {"type", "information"}, {"scannerBattery", 66}}, message()}) { check(!cache.update(ignored, now), "Only a device-status information message with a battery field updates."); check(cache.snapshot(false, now) == initial, "Ignored messages preserve the cache."); } } void values_and_freshness() { DeviceBatteryCache cache; for (const auto &value : std::vector{0, "0", 100, "100", "0017", "-0", 66, "66"}) { check(cache.update(scanner(value), now), "Integer and strict integer-string reports are accepted."); const int expected = value.is_string() ? std::stoi(value.get()) : value.get(); const auto result = cache.snapshot(true, now).at("scanner"); check(result.at("percent") == expected && result.at("rawValue") == expected && result.at("available") == true && result.at("fresh") == true, "Zero through one hundred are valid battery percentages, including zero."); check(result.at("updatedUtc") == freescan::utc_from_ms(now), "Report time uses the supplied clock."); } cache.update(scanner(66), now); check(cache.snapshot(true, now + 30000).at("scanner").at("percent") == 66, "A reading exactly at the freshness boundary remains available."); const auto stale = cache.snapshot(true, now + 30001).at("scanner"); unavailable(stale, false, "Expired scanner"); check(stale.at("rawValue") == 66 && stale.at("updatedUtc") == freescan::utc_from_ms(now), "Expired readings retain diagnostic raw data and their original timestamp."); unavailable(cache.snapshot(true, now - 1).at("scanner"), false, "Clock before report"); const auto offline = cache.snapshot(false, now).at("scanner"); unavailable(offline, false, "Inactive SDK session"); check(offline.at("rawValue") == 66, "Inactive snapshots retain raw diagnostics without exposing current percent."); for (const auto &value : std::vector{-1, "-1", -10, 101, "101", (std::numeric_limits::min)(), (std::numeric_limits::max)(), "-2147483648", "2147483647"}) { cache.update(scanner(value), now); const auto result = cache.snapshot(true, now).at("scanner"); unavailable(result, true, "Unknown or out-of-range battery report"); const auto expected = value.is_string() ? std::stoll(value.get()) : value.get(); check(result.at("rawValue") == expected, "Out-of-range integer reports preserve their exact raw value."); } for (const auto &invalid : std::vector{nullptr, true, false, 17.0, 17.5, "", " ", " 17", "17 ", "+17", "17.0", "17%", "1e2", "--1", "-", "0x11", "2147483648", "-2147483649", "9223372036854775808", "-9223372036854775809", (std::numeric_limits::max)(), (std::numeric_limits::min)(), (std::numeric_limits::max)(), Json::object(), Json::array()}) { cache.update(scanner(66), now); check(cache.update(scanner(invalid), now + 1), "An invalid received field invalidates the previous reading."); const auto result = cache.snapshot(true, now + 1).at("scanner"); unavailable(result, true, "Malformed battery report"); check(result.at("rawValue").is_null(), "Malformed input cannot leave a previous valid raw value."); check(result.at("updatedUtc") == freescan::utc_from_ms(now + 1), "Malformed but received reports still update the diagnostic report time."); } } void independent_devices_and_channels() { DeviceBatteryCache cache; check(cache.update(scanner("66"), now), "The scanner report is accepted."); check(cache.update(tracker("25", "000"), now + 1), "A tracker string index is accepted."); check(cache.update(tracker(75, 1), now + 2), "A tracker integer index is accepted."); auto result = cache.snapshot(true, now + 2); check(result.at("scanner").at("percent") == 66 && result.at("scanner").at("updatedUtc") == freescan::utc_from_ms(now), "Tracker messages do not overwrite the scanner reading or its timestamp."); check(result.at("tracker").at("channels").size() == 2, "Different tracker indices remain separate."); check(channel(result, "0").at("percent") == 25 && channel(result, "1").at("percent") == 75, "Tracker indices zero and one retain their own battery percentages."); cache.update(tracker(-1, 0), now + 3); result = cache.snapshot(true, now + 3); unavailable(channel(result, "0"), true, "Unknown tracker channel zero"); check(channel(result, "0").at("rawValue") == -1 && channel(result, "1").at("percent") == 75, "An unknown channel does not erase another tracker channel."); auto noIndex = message(); noIndex["trakBattery"] = "40"; cache.update(noIndex, now + 4); result = cache.snapshot(true, now + 4); check(channel(result, nullptr).at("percent") == 40 && result.at("tracker").at("channels").size() == 3, "An omitted index is retained as a distinct null-index reading."); cache.update(tracker("41", nullptr), now + 4); result = cache.snapshot(true, now + 4); check(channel(result, nullptr).at("percent") == 41 && result.at("tracker").at("channels").size() == 3, "An explicit null index updates the same unindexed tracker channel."); for (const auto &invalid : std::vector{true, false, -1, 1.0, 1.5, "", "-1", "-0", "+1", " 1", "1 ", "1.0", Json::array(), Json::object()}) { auto update = tracker(99, invalid); check(!cache.update(update, now + 5), "An invalid explicit tracker index is ignored."); check(cache.snapshot(true, now + 5).at("tracker") == result.at("tracker"), "Invalid indices cannot overwrite or invent a tracker channel."); update["scannerBattery"] = 55; check(cache.update(update, now + 5), "An invalid tracker index does not reject an independent scanner field."); check(cache.snapshot(true, now + 5).at("scanner").at("percent") == 55, "A combined report still updates the scanner with an invalid tracker index."); } cache.update(scanner(12), now + 6); check(cache.snapshot(true, now + 6).at("tracker") == result.at("tracker"), "Scanner-only reports preserve all tracker readings and timestamps."); const auto expired = cache.snapshot(true, now + 30003); check(expired.at("scanner").at("percent") == 12, "A newer scanner report has its own freshness deadline."); unavailable(channel(expired, "0"), true, "Channel zero exactly at its freshness boundary"); unavailable(channel(expired, "1"), false, "Older tracker channel expires independently"); check(channel(expired, nullptr).at("percent") == 41, "A newer unindexed tracker report remains fresh."); } void channel_limit_and_session_reset() { DeviceBatteryCache cache; for (int i = 0; i < 16; ++i) check(cache.update(tracker(i, i), now), "Each of the first sixteen tracker channels is accepted."); check(cache.snapshot(true, now).at("tracker").at("channels").size() == 16, "The tracker cache reaches its documented bounded channel count."); check(!cache.update(tracker(88, "16"), now + 1), "A seventeenth distinct channel is ignored."); check(cache.snapshot(true, now + 1).at("tracker").at("channels").size() == 16, "Untrusted indices cannot grow the channel cache without bound."); check(cache.update(tracker(88, "0001"), now + 1), "Existing normalized indices update even at capacity."); check(channel(cache.snapshot(true, now + 1), "1").at("percent") == 88, "Normalization prevents duplicate channels for integer and padded string indices."); auto combined = tracker(77, "17"); combined["scannerBattery"] = 33; cache.update(combined, now + 2); check(cache.snapshot(true, now + 2).at("scanner").at("percent") == 33, "A full tracker cache cannot block the scanner battery update."); cache.clear(); auto cleared = cache.snapshot(true, now + 2); unavailable(cleared.at("scanner"), false, "Cleared SDK session"); check(cleared.at("scanner").at("rawValue").is_null() && cleared.at("scanner").at("updatedUtc").is_null() && cleared.at("tracker").at("channels").empty(), "Session clearing drops readings, channels and report times."); check(cache.update(tracker(10, "16"), now + 3), "A new session starts with an empty channel capacity."); } void no_vendor_dll() { check(GetModuleHandleW(L"SDKCWrapper.dll") == nullptr && GetModuleHandleW(L"FreeScanNativeBridge.dll") == nullptr, "Battery cache tests must never load vendor SDK or bridge DLLs."); } } // namespace int main() { try { no_vendor_dll(); defaults_and_filtering(); values_and_freshness(); independent_devices_and_channels(); channel_limit_and_session_reset(); no_vendor_dll(); std::cout << "OFFLINE_TEST_FIXTURE: C++ battery tests passed: " << passed << "; synthetic callbacks only, no SDK, device or HTTP listener.\n"; return 0; } catch (const std::exception &error) { std::cerr << "FAIL (OFFLINE ONLY): " << error.what() << "\n"; return 1; } }