From 9bb9d31f432dc7859989c2011011ca9f68ecc93d Mon Sep 17 00:00:00 2001 From: Michael Lynch Date: Fri, 1 May 2026 08:12:12 -0400 Subject: [PATCH] Split up mocks.h --- test/{test_helpers/mocks.h => mocks/mesh.h} | 70 +++------------------ test/mocks/radio.h | 36 +++++++++++ test/mocks/random.h | 25 ++++++++ test/mocks/time.h | 42 +++++++++++++ test/test_helpers/test_advert_data.cpp | 2 +- 5 files changed, 113 insertions(+), 62 deletions(-) rename test/{test_helpers/mocks.h => mocks/mesh.h} (73%) create mode 100644 test/mocks/radio.h create mode 100644 test/mocks/random.h create mode 100644 test/mocks/time.h diff --git a/test/test_helpers/mocks.h b/test/mocks/mesh.h similarity index 73% rename from test/test_helpers/mocks.h rename to test/mocks/mesh.h index 655a44cea..9b2598fd9 100644 --- a/test/test_helpers/mocks.h +++ b/test/mocks/mesh.h @@ -1,72 +1,16 @@ #pragma once -#include #include -#include #include #include "helpers/BaseChatMesh.h" #include "helpers/SimpleMeshTables.h" +#include "radio.h" +#include "random.h" +#include "time.h" -class FakeMillis final : public mesh::MillisecondClock { -public: - unsigned long getMillis() override { - return 0; - } -}; - -class FakeRtc final : public mesh::RTCClock { -public: - explicit FakeRtc(uint32_t initial_time) : _time(initial_time) {} - - uint32_t getCurrentTime() override { - return _time; - } - - void setCurrentTime(uint32_t time) override { - _time = time; - } - -private: - uint32_t _time; -}; - -class FakeRng final : public mesh::RNG { -public: - void random(uint8_t* dest, size_t sz) override { - memset(dest, 0x5A, sz); - } -}; - -class FakeRadio final : public mesh::Radio { -public: - int recvRaw(uint8_t*, int) override { - return 0; - } - - uint32_t getEstAirtimeFor(int) override { - return 1; - } - - float packetScore(float, int) override { - return 1.0f; - } - - bool startSendRaw(const uint8_t*, int) override { - return true; - } - - bool isSendComplete() override { - return true; - } - - void onSendFinished() override {} - - bool isInRecvMode() const override { - return true; - } -}; - +// No-op packet manager for native tests. +// Satisfies Mesh dependencies while preventing packet allocation or queuing. class NoopPacketManager final : public mesh::PacketManager { public: mesh::Packet* allocNew() override { @@ -108,6 +52,8 @@ public: } }; +// Test chat mesh for native tests. +// Exposes packet receive handling and captures discovered contacts for assertions. class TestChatMesh final : public BaseChatMesh { public: TestChatMesh(mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, @@ -156,6 +102,8 @@ protected: void onContactResponse(const ContactInfo&, const uint8_t*, uint8_t) override {} }; +// Test mesh context for native tests. +// Owns mock dependencies in construction order and exposes the mesh via operator->. struct TestMeshContext { explicit TestMeshContext(uint32_t current_timestamp) : rtc(current_timestamp), mesh(radio, ms, rng, rtc, packet_manager, tables) {} diff --git a/test/mocks/radio.h b/test/mocks/radio.h new file mode 100644 index 000000000..59a693954 --- /dev/null +++ b/test/mocks/radio.h @@ -0,0 +1,36 @@ +#pragma once + +#include + +#include "Dispatcher.h" + +// Fake radio for native tests. +// Provides successful no-op send/receive behavior without hardware access. +class FakeRadio final : public mesh::Radio { +public: + int recvRaw(uint8_t*, int) override { + return 0; + } + + uint32_t getEstAirtimeFor(int) override { + return 1; + } + + float packetScore(float, int) override { + return 1.0f; + } + + bool startSendRaw(const uint8_t*, int) override { + return true; + } + + bool isSendComplete() override { + return true; + } + + void onSendFinished() override {} + + bool isInRecvMode() const override { + return true; + } +}; diff --git a/test/mocks/random.h b/test/mocks/random.h new file mode 100644 index 000000000..4b6897360 --- /dev/null +++ b/test/mocks/random.h @@ -0,0 +1,25 @@ +#pragma once + +#if defined(__has_include_next) + #if __has_include_next() + #include_next + #endif +#endif + +#ifdef __cplusplus + +#include +#include + +#include "Utils.h" + +// Fake random generator for native tests. +// Fills buffers with deterministic bytes so generated packets are repeatable. +class FakeRng final : public mesh::RNG { +public: + void random(uint8_t* dest, size_t sz) override { + memset(dest, 0x5A, sz); + } +}; + +#endif diff --git a/test/mocks/time.h b/test/mocks/time.h new file mode 100644 index 000000000..b16f5d309 --- /dev/null +++ b/test/mocks/time.h @@ -0,0 +1,42 @@ +#pragma once + +#if defined(__has_include_next) + #if __has_include_next() + #include_next + #endif +#endif + +#ifdef __cplusplus + +#include + +#include "Dispatcher.h" + +// Fake millisecond clock for native tests. +// Returns a stable timestamp so timer-dependent code stays deterministic. +class FakeMillis final : public mesh::MillisecondClock { +public: + unsigned long getMillis() override { + return 0; + } +}; + +// Fake RTC clock for native tests. +// Stores caller-controlled Unix time without depending on hardware RTC APIs. +class FakeRtc final : public mesh::RTCClock { +public: + explicit FakeRtc(uint32_t initial_time) : _time(initial_time) {} + + uint32_t getCurrentTime() override { + return _time; + } + + void setCurrentTime(uint32_t time) override { + _time = time; + } + +private: + uint32_t _time; +}; + +#endif diff --git a/test/test_helpers/test_advert_data.cpp b/test/test_helpers/test_advert_data.cpp index 9d9f77095..a6ad03a9c 100644 --- a/test/test_helpers/test_advert_data.cpp +++ b/test/test_helpers/test_advert_data.cpp @@ -4,7 +4,7 @@ #include -#include "mocks.h" +#include "../mocks/mesh.h" namespace {