Files
lilygo-t-deck/emulator/tests/e2e_tests.cpp
T
Dejvino 1dcc87d8d4 Fix trackball click: I2S MCLK was claiming GPIO0; emulator pin-ownership parity
Root cause (on device): sound_engine zero-initialized i2s_pin_config_t,
leaving mck_io_num == 0. i2s_set_pin() then claimed GPIO0 — an
MCLK-capable pin on the S3 and the trackball centre / BOOT line — as
its MCLK output. Verified on device with a diagnostic build: pin0 read
LOW at idle, exactly one latched FALLING edge, clicks never registered.

- sound_engine: set pins.mck_io_num = I2S_PIN_NO_CHANGE explicitly.
- NetDeck: register the trackball click on release with contact-bounce
  collapse (the proven CrtTerminal pattern); pollTouch re-arms when
  isPressed() stays true but getPoint() reports no points (stuck GT911
  IRQ on some units).
- Emulator parity (so this class of bug dies before flashing):
  GPIO-matrix pin-ownership model in the shim — i2s_set_pin() claims
  its pins, claimed pins are dead inputs (digitalRead LOW, no edges,
  loud WARNING on claim/ISR collisions); host pin writes go through
  emu::setPin/togglePin with an edge-accurate ISR latch (a full
  press+release pulse between dispatches no longer gets swallowed);
  E2E inits SDL_INIT_AUDIO so the I2S path actually runs there.
  Regression coverage: e2e/trackball_click (fails on the old engine),
  unit/shim_claimed_pin_is_dead_input,
  unit/shim_edge_survives_same_dispatch_pulse.
- Makefile: firmware step follows the sketch move (root platformio.ini,
  src_dir = src/netdeck).
- HW.md: new notes file for emulator-vs-hardware quirks and the
  on-device debugging recipe.

On-device verified: clicks register in both views (beacon drops, node
routing). Fast layer 23 unit + 7 integration green. Note: e2e
boot_and_controls + network_journey failures are pre-existing at HEAD
(reproduced on a clean stash before this change).
2026-11-15 10:24:30 +01:00

228 lines
8.8 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;
bool pin = false; // trackball centre press (GPIO0 pulse) instead
};
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 schedulePin(double at)
{
Evt e;
e.at = at;
e.pin = true;
g_evts.push_back(e);
}
static void pumpEvents(double el)
{
for (auto &e : g_evts) {
if (!e.fired && el >= e.at) {
e.fired = true;
if (e.pin) {
emu::setPin(BOARD_BOOT_PIN, LOW); // trackball centre press
e.holdIters = 2; // released 2 iterations later
} else 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) {
if (e.pin) emu::setPin(BOARD_BOOT_PIN, HIGH); // click release
else 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]"));
}
// The trackball centre click must act exactly like a screen tap. This drives
// the FULL device pipeline (attachTrackball ISR -> tbClicks latch ->
// applyTrackball release gate -> registerClick) — and it doubles as the
// regression for the I2S sound engine silently claiming GPIO0 as its MCLK
// output (a zero-initialized i2s_pin_config_t), which killed the click line
// on hardware while every emulator build looked fine. With the pin-ownership
// model in the shim, the claimed GPIO0 is a dead input here too, so this
// journey fails until the sketch stops claiming the pin.
TEST(e2e/trackball_click)
{
reboot();
EXPECT(pumpUntil(8.0, bootDone));
test::serialClear();
schedulePin(1.0); // trackball centre click
EXPECT(pumpFor(3.0));
EXPECT(test::serialHas("[click]")); // registered (beacon or blocked)
}
int main(int argc, char **argv)
{
// headless: no window needed — rendering goes into the shim framebuffer.
// AUDIO matters for parity too: without it the sketch's i2s_driver_install
// fails early and i2s_set_pin() never runs, so the shim's pin-ownership
// model (and any bug it exists to catch) would stay dormant in E2E.
::setenv("SDL_VIDEODRIVER", "dummy", 1);
::setenv("SDL_AUDIODRIVER", "dummy", 1);
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
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;
}