Files
lilygo-t-deck/emulator/tests/e2e_tests.cpp
T
Dejvino 21d37486cf Emulator: migrate SDL2 -> SDL3
Port the shared emulator harness (all apps) to SDL 3.4 + SDL3_ttf 3.2:
- window creation without position args; renderer logical size via
  SDL_SetRenderLogicalPresentation(LETTERBOX); SDL_RenderTexture;
  SDL_SaveBMP_IO frame dumps
- SDL_EVENT_* event scheme, flattened SDL_KeyboardEvent, float mouse
  coords; clicks mapped with SDL_ConvertEventToRenderCoordinates, keeping
  the Xwayland global-state cross-check from 260a2b3
- audio shim on the stream API (SDL_OpenAudioDeviceStream +
  SDL_PutAudioStreamData) since SDL_QueueAudio is gone
- TTF_RenderText_Blended (explicit length); SDL3_ttf header layout
  handled via __has_include (Ubuntu installs under SDL3_ttf/)
- e2e freeze-tint pixel assertions updated to the committed
  diagonal-stripe pattern (stale since the freeze rework; pre-existing
  failures at HEAD under SDL2), sampled away from the freeze text

Full suite green: 14 unit + 7 integration + 3 E2E journeys, 0 failures.
MissileCommand, MiataDrive, NetDeck build; CrtTerminal (missing sndBlip
declaration) and Lotus3 (sketch not in repo) fail identically at HEAD -
pre-existing, out of scope.
2026-11-14 17:03:30 +01:00

194 lines
7.2 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 <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h> // Debian/Ubuntu layout (upstream: SDL3/SDL_ttf.h)
#include "harness.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)
schedule(3.4, 'p'); // back to Cameras (full map)
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 -> diagonal-stripe freeze tint with color-key transparency:
// within each 8x8 cell the anti-diagonal stripe ((x%8)+(y%8))%8 == 2
// stamps black, every other pixel keeps the frozen view, and the key
// color 0x1F1F is never rendered (transparency held). Sampled on the
// left side of the top wall row, away from the top-right anchored
// "* TACTICAL FREEZE *" text, so font metric drift can't leak in.
EXPECT(test::pixelIs565(10, 0, 0x0000)); // stripe dot (2,0)
EXPECT(test::pixelIs565(9, 1, 0x0000)); // stripe dot (1,1)
EXPECT(test::pixelIs565(8, 2, 0x0000)); // stripe dot (0,2)
EXPECT(!test::pixelIs565(12, 0, 0x0000)); // off-stripe: frozen view
EXPECT(!test::pixelIs565(12, 0, 0x1F1F)); // key color never rendered
for (int yy = 0; yy < 8; yy++)
for (int xx = 8; xx < 16; xx++)
EXPECT(!test::pixelIs565(xx, yy, 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
::setenv("SDL_VIDEODRIVER", "dummy", 1);
if (!SDL_Init(SDL_INIT_VIDEO)) {
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;
}