Files
lilygo-t-deck/emulator/tests/harness.h
T
Dejvino a1d0730124 NetDeck: headless test runner — unit + integration suites, E2E as separate build
- emulator/tests/: harness.h (TEST/EXPECT framework, input + serial +
  framebuffer helpers), shim_globals.cpp (shim externs for test binaries,
  replacing main.cpp ownership)
- unit_tests.h (14): map/door/LOS/BFS/cover/link state machine/trace/
  spike/reinforcement/win/loss/reset — deterministic fixed-seed, sim
  stepped tick by tick
- integration_tests.h (7): keyboard->pollKeyboard->handleKey pipeline,
  beacon clicks -> squad movement, full network journey via clicks,
  hover-sever (incl. safe sever during trace), daemon gating
- e2e_tests.cpp (separate NetDeck-e2e build): real setup()/loop() journeys
  with device-pipeline input injection (boot grace, 0x55 keyboard, GT911
  tap, trackball ISRs), transcript + pixel assertions (HUD, freeze tint)
- shim SerialSim: opt-in transcript ring for test assertions (off by
  default, interactive emulator unchanged)
- make netdeck-test / -unit / -e2e (root) and make test / test-unit /
  test-e2e (emulator/); tests/README.md documents the structure
- STATUS.md: test-structure note

Verified: make netdeck-test -> 14 unit + 7 integration + 3 E2E, 0 failures
2026-11-14 13:02:41 +01:00

116 lines
3.8 KiB
C++

/**
* @file harness.h
* @brief Minimal headless test harness for the T-Deck emulator shims.
*
* Must be included BEFORE the sketch (it only depends on the shims, never on
* sketch internals). Sketch-level helpers (stepSim, units, ...) live in the
* test suites themselves, which are included AFTER the sketch.
*
* Framework: TEST(name) registers a case; EXPECT/EXPECT_EQ assert; the runner
* in test_main.cpp / e2e_tests.cpp reports failures and exits non-zero.
*/
#pragma once
#include "Arduino.h"
#include "TFT_eSPI.h" // rgb565to888 for pixel assertions
#include "emu_gfx.h"
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
namespace test {
struct Case {
const char *name;
void (*fn)();
};
inline std::vector<Case> &cases()
{
static std::vector<Case> c;
return c;
}
struct Registrar {
Registrar(const char *name, void (*fn)()) { cases().push_back({name, fn}); }
};
inline int g_failures = 0;
inline std::string g_current; // full case name, e.g. "unit/map_queries"
inline std::string g_group; // prefix before the first '/'
inline void fail(const char *file, int line, const std::string &what)
{
std::printf(" FAIL [%s/%s] %s:%d %s\n", g_group.c_str(), g_current.c_str(),
file, line, what.c_str());
g_failures++;
}
// --- serial transcript helpers (Serial.sink is set by captureSerial()) ---
inline std::string serialJoined()
{
std::string all;
if (Serial.sink)
for (const auto &l : *Serial.sink) all += l;
return all;
}
inline bool serialHas(const std::string &pat)
{
return serialJoined().find(pat) != std::string::npos;
}
inline void serialClear()
{
if (Serial.sink) Serial.sink->clear();
}
inline void captureSerial()
{
static std::deque<std::string> ring;
Serial.sink = &ring;
}
// --- input pipeline helpers (the same state the SDL host writes) ---
inline void resetInput()
{
for (auto &p : emu::pinLevels) p = 1; // everything pulled up
while (!emu::keyboard.empty()) emu::keyboard.pop_front();
emu::touchDown = false;
emu::touchX = emu::touchY = 0;
}
// queue a key on the fake 0x55 keyboard device (consumed by pollKeyboard())
inline void typeKey(char c) { emu::keyboard.push_back((uint8_t)c); }
// --- framebuffer helpers (E2E pixel assertions) ---
inline uint32_t pixel(int x, int y)
{
if (x < 0 || y < 0 || x >= emuGfx::W || y >= emuGfx::H) return 0;
return emuGfx::fb[y * emuGfx::W + x];
}
inline bool pixelIs565(int x, int y, uint16_t c565)
{
return pixel(x, y) == rgb565to888(c565);
}
} // namespace test
// The display name may contain '/' (e.g. "unit/map_queries") — identifiers
// are generated from __LINE__, so the name string itself is never pasted.
#define TEST_CAT2(a, b) a##b
#define TEST_CAT(a, b) TEST_CAT2(a, b)
#define TEST(name) \
static void TEST_CAT(test_case_, __LINE__)(); \
static test::Registrar TEST_CAT(reg_case_, __LINE__)( \
#name, TEST_CAT(test_case_, __LINE__)); \
static void TEST_CAT(test_case_, __LINE__)()
#define EXPECT(cond) \
do { \
if (!(cond)) test::fail(__FILE__, __LINE__, "expected " #cond); \
} while (0)
#define EXPECT_EQ(a, b) \
do { \
if (!((a) == (b))) \
test::fail(__FILE__, __LINE__, \
std::string(#a " == " #b) + " (got: " + \
std::to_string((long long)(a)) + " vs " + \
std::to_string((long long)(b)) + ")"); \
} while (0)