Files
lilygo-t-deck/emulator/tests/e2e_tests.cpp
T
Dejvino 83ec0217f4 NetDeck: freeze tint stamps a checkered 8x8 sprite pattern
- replace the nonexistent draw16bppSprite_P(NULL,...) call with
  pushImage(bx, by, 8, 8, checkered8x8): device = TFT_eSprite::pushImage
  (blits into the fb sprite), emulator = new shim TFT_eSPI::pushImage
  blit into the host framebuffer — same signature on both backends
- shim: define PROGMEM (no-op on host/ESP32, flash is memory-mapped)
- pattern data fixed into a real 2px checkerboard (black / 0x1F1F teal)
- E2E pixel assertions updated to the checker geometry

Verified: make netdeck (both targets) + full test suite, 0 failures
2026-11-14 15:24:42 +01:00

185 lines
6.6 KiB
C++

/**
* @file e2e_tests.cpp
* @brief End-to-end tests — SEPARATE BUILD (NetDeck-e2e).
*
* Unlike the fast suites (test_main.cpp), E2E drives the sketch through its
* REAL entry points: setup() -> loop() iterations, with inputs injected
* through the actual device pipeline (0x55 keyboard queue, GT911 touch
* state, trackball pin ISRs) and wall-clock boot grace. Assertions read the
* serial transcript and the rendered framebuffer, like a player would.
*
* Slower by nature (~1-2 min: the scenario runs at 20 Hz real time), which
* is why it gets its own build and its own make target.
*/
#include "harness.h"
#include <SDL.h>
#include <SDL_ttf.h>
// pull in the whole sketch as a single translation unit (-DAPP_INO)
#include APP_INO
#include "sim_helpers.h" // stepTicks() etc. (needs sketch statics)
// ---------------------------------------------------------------------------
// mini host: scheduled inputs by wall-clock time, loop() driven for real
// ---------------------------------------------------------------------------
struct Evt {
double at; // seconds after journey start
char key; // 0 = screen tap instead
int x, y; // tap coords
bool fired = false;
int holdIters = 0;
};
static std::vector<Evt> g_evts;
static unsigned long g_iters = 0;
static void schedule(double at, char key) { g_evts.push_back({at, key, 0, 0}); }
static void scheduleTap(double at, int x, int y) { g_evts.push_back({at, 0, x, y}); }
static void pumpEvents(double el)
{
for (auto &e : g_evts) {
if (!e.fired && el >= e.at) {
e.fired = true;
if (e.key) {
test::typeKey(e.key);
} else {
emu::touchDown = true;
emu::touchX = e.x;
emu::touchY = e.y;
e.holdIters = 60; // ~80 ms press, like main.cpp
}
} else if (e.fired && e.holdIters > 0 && --e.holdIters == 0) {
emu::touchDown = false; // release -> pollTouch edge
}
}
}
// run loop() iterations until done() fires or the deadline expires.
// With a completion predicate: true = done() fired. Without one (plain
// timed run): true once the deadline elapses.
// Does NOT touch the schedule — journeys clear/schedule explicitly.
static bool pumpUntil(double deadlineSec, bool (*done)())
{
double t0 = millis() / 1000.0;
g_iters = 0;
for (;;) {
double el = millis() / 1000.0 - t0;
pumpEvents(el);
emu::dispatchIsrs();
loop();
g_iters++;
if (done && done()) return true;
if (millis() / 1000.0 - t0 > deadlineSec) return done == nullptr;
}
}
static bool pumpFor(double seconds)
{
return pumpUntil(seconds, nullptr);
}
// fresh boot for a journey: setup() does not rewind the app's own flow state,
// so the harness rewinds what a power-cycle would (boot phase + view baseline)
static void reboot()
{
gameState = ST_BOOT;
viewMode = VM_CAMERAS;
test::resetInput();
test::serialClear();
g_evts.clear();
setup();
}
// ---------------------------------------------------------------------------
// journeys
// ---------------------------------------------------------------------------
static bool bootDone() { return test::serialHas("[boot] encounter loaded"); }
static bool spikeSeen() { return test::serialHas("[net] FEEDBACK SPIKE"); }
static bool droidDown() { return test::serialHas("SECURITY DROID destroyed"); }
TEST(e2e/boot_and_controls)
{
reboot();
EXPECT(pumpUntil(8.0, bootDone));
schedule(2.2, 'p'); // -> Network view
schedule(2.6, ' '); // freeze OFF
schedule(3.0, 'r'); // reset (re-pauses)
test::serialClear();
EXPECT(pumpFor(4.5)); // run the inputs through loop()
EXPECT(test::serialHas("[key] view -> NETWORK"));
EXPECT(test::serialHas("[key] tactical freeze OFF"));
EXPECT(test::serialHas("[key] RESET"));
// HUD separator rendered across the top of the HUD bar
EXPECT(test::pixelIs565(160, nd::HUD_Y, nd::C_BORDER));
// paused -> checkered freeze tint: 2px checker of black + dark teal
// P(x,y): black when ((x>>1)+(y>>1)) is even
EXPECT(test::pixelIs565(50, 0, 0x1F1F)); // (25+0) odd -> teal
EXPECT(test::pixelIs565(50, 2, 0x0000)); // (25+1) even -> black
EXPECT(test::pixelIs565(52, 0, 0x0000)); // (26+0) even -> black
EXPECT(!test::pixelIs565(52, 0, 0x1F1F));
}
TEST(e2e/network_journey)
{
reboot();
schedule(2.0, 'p'); // Network view
scheduleTap(2.4, 36, 164); // AIRLK
scheduleTap(2.8, 148, 52); // JUNC (route, 1.5 s)
schedule(3.1, ' '); // unpause -> build completes
scheduleTap(6.0, 284, 36); // HIVE (3 s ICE)
EXPECT(pumpUntil(30.0, spikeSeen)); // ... trace ... feedback spike
EXPECT(test::serialHas("[spawn] reinforcement droid"));
stepTicks(1);
EXPECT(units[6].alive);
EXPECT(heatT > 0); // OVERHEAT lockout engaged
}
TEST(e2e/corridor_assault)
{
reboot();
schedule(2.0, 'r'); // clean re-arm (clears overheat)
scheduleTap(2.4, 164, 100); // beacon into the corridor
schedule(2.7, ' '); // unpause
EXPECT(pumpUntil(60.0, droidDown)); // squad engages the garrison
stepTicks(2);
bool garrisonHit = !units[2].alive || !units[3].alive;
EXPECT(garrisonHit || test::serialHas("[combat]"));
}
int main(int argc, char **argv)
{
// headless: no window needed — rendering goes into the shim framebuffer
SDL_setenv("SDL_VIDEODRIVER", "dummy", 1);
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::printf("SDL_Init failed: %s\n", SDL_GetError());
return 2;
}
TTF_Init(); // drawString needs real fonts
const char *filter = argc > 1 ? argv[1] : nullptr;
test::captureSerial();
test::g_group = "e2e";
std::srand(0xC0FFEE);
int run = 0;
for (const auto &c : test::cases()) {
if (filter && std::strncmp(c.name, filter, std::strlen(filter)) != 0)
continue;
test::g_current = c.name;
std::printf("RUN %s\n", c.name);
c.fn();
run++;
}
TTF_Quit();
SDL_Quit();
std::printf("\n%s: %d %s, %d failure%s\n",
run ? "DONE" : "NO TESTS MATCHED FILTER",
run, run == 1 ? "journey" : "journeys",
test::g_failures, test::g_failures == 1 ? "" : "s");
return (run > 0 && test::g_failures == 0) ? 0 : 1;
}