Files
lilygo-t-deck/emulator/tests/unit_tests.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

207 lines
7.3 KiB
C++

/**
* @file unit_tests.h
* @brief Unit tests: pure sketch logic in isolation (map queries, LOS,
* pathfinding, cover, link state machine, combat rules, flow
* transitions). No rendering, no input pipeline, no wall time —
* determinism comes from freshScenario()'s fixed RNG seed.
*
* Included by test_main.cpp AFTER the sketch, so sketch statics are visible.
*/
#include "sim_helpers.h"
// --- map queries -----------------------------------------------------------
TEST(unit/map_queries)
{
freshScenario();
EXPECT_EQ(tileAt(-1, 0), '#'); // out of bounds = wall
EXPECT_EQ(tileAt(40, 0), '#');
EXPECT_EQ(tileAt(28, 12), '+'); // the blast door tile
EXPECT(!walkable(0, 0)); // '#'
EXPECT(walkable(20, 12)); // corridor floor
EXPECT(walkable(2, 21)); // airlock 'A'
EXPECT(walkable(34, 7)); // objective terminal 'T'
EXPECT(blocksSight(0, 0));
EXPECT(!blocksSight(20, 12));
EXPECT_EQ(DEMO_MAP[7][34], 'T'); // the win tile really is T
}
TEST(unit/door_gates_movement_and_sight)
{
freshScenario();
EXPECT(!walkable(28, 12)); // closed door blocks movement
EXPECT(blocksSight(28, 12)); // ... and sight
doorOpen = true;
EXPECT(walkable(28, 12));
EXPECT(!blocksSight(28, 12));
doorOpen = false;
EXPECT(!walkable(28, 12));
}
// --- line of sight ---------------------------------------------------------
TEST(unit/los_wall_and_door)
{
freshScenario();
EXPECT(losClear(1, 1, 9, 1)); // along open floor
EXPECT(!losClear(1, 1, 11, 1)); // wall at col 10
EXPECT(!losClear(27, 12, 29, 12)); // through CLOSED door
doorOpen = true;
EXPECT(losClear(27, 12, 29, 12)); // through OPEN door
doorOpen = false;
}
// --- pathfinding -----------------------------------------------------------
TEST(unit/bfs_door_gates_the_route)
{
freshScenario();
int path[64];
EXPECT_EQ(bfsPath(2, 21, 34, 7, path, 64), -1); // no route, door closed
doorOpen = true;
int n = bfsPath(2, 21, 34, 7, path, 64);
EXPECT(n > 0);
for (int i = 0; i < n; i++) { // every step walkable
int t = path[i];
EXPECT(walkable(t % nd::TILE_COLS, t / nd::TILE_COLS));
}
EXPECT_EQ(bfsPath(2, 21, 0, 0, path, 64), -1); // wall target unreachable
}
TEST(unit/bfs_respects_capacity)
{
freshScenario();
doorOpen = true;
int path[4]; // tiny buffer
EXPECT_EQ(bfsPath(2, 21, 34, 7, path, 4), -1); // longer than cap -> fail
}
// --- cover -----------------------------------------------------------------
TEST(unit/cover_rule)
{
freshScenario();
spawnUnit(6, U_DROID, 4, 4); // beside the crate at (5,4)
EXPECT(inCover(6));
spawnUnit(6, U_DROID, 20, 10); // open corridor floor
EXPECT(!inCover(6));
}
// --- link state machine ------------------------------------------------------
TEST(unit/link_routing_and_online)
{
freshScenario();
tryLink(N_JUNC); // AIRLK root is online
int l = linkIdx(0); // edge AIRLK-JUNC
EXPECT(l >= 0);
EXPECT(!nodeOnline[N_JUNC]); // offline while building
stepTicks(LINK_TICKS);
EXPECT(nodeOnline[N_JUNC]); // up after 1.5 s of ticks
tryLink(N_DOOR);
stepTicks(LINK_TICKS);
EXPECT(nodeOnline[N_DOOR]);
}
TEST(unit/link_rejects_non_adjacent)
{
freshScenario();
tryLink(N_HIVE); // not adjacent to AIRLK
EXPECT_EQ(strcmp(msgText, "NO UPLINK"), 0);
EXPECT(linkIdx(2) < 0); // JUNC-HIVE not routed
}
TEST(unit/sever_cascades_downstream)
{
freshScenario();
tryLink(N_JUNC); stepTicks(LINK_TICKS);
tryLink(N_DOOR); stepTicks(LINK_TICKS);
tryLink(N_HIVE); stepTicks(ICE_TICKS);
EXPECT(nodeOnline[N_HIVE]);
int door = linkIdx(1); // JUNC-DOOR
severLink(door);
EXPECT(!nodeOnline[N_DOOR]);
EXPECT(nodeOnline[N_JUNC]); // upstream survives
EXPECT(linkIdx(1) < 0);
}
// --- trace / feedback / reinforcement ---------------------------------------
TEST(unit/ice_trace_feedback_reinforcement)
{
freshScenario();
tryLink(N_JUNC); stepTicks(LINK_TICKS);
tryLink(N_HIVE); stepTicks(ICE_TICKS);
EXPECT(hiveCracked);
EXPECT(traceActive); // red trace inbound
stepTicks(TRACE_TICKS); // trace reaches AIRLK
EXPECT(!traceActive);
EXPECT(heatT > 0 && heatT <= HEAT_TICKS); // OVERHEAT lockout engaged
EXPECT(linkIdx(0) < 0 && linkIdx(1) < 0 && linkIdx(2) < 0);
EXPECT(!nodeOnline[N_JUNC]); // everything past root drops
EXPECT(nodeOnline[N_AIRLK]);
EXPECT(units[6].alive); // reinforcement droid spawned
}
TEST(unit/overheat_gates_daemons_and_sever)
{
freshScenario();
heatT = HEAT_TICKS;
handleKey('1');
EXPECT_EQ(strcmp(msgText, "OVERHEAT"), 0);
handleKey('x');
EXPECT_EQ(strcmp(msgText, "OVERHEAT"), 0);
stepTicks(HEAT_TICKS);
EXPECT_EQ(heatT, 0); // lockout expires
}
// --- combat rules ------------------------------------------------------------
TEST(unit/target_acquisition_needs_range_and_los)
{
freshScenario();
spawnUnit(6, U_MARINE, 5, 7);
spawnUnit(0, U_DROID, 20, 12); // far outside marine range
spawnUnit(1, U_DROID, 8, 7); // same room, clear LOS
EXPECT_EQ(acquireTarget(6), 1);
units[1].tx = 11; units[1].ty = 7; // still in range (6 tiles)
EXPECT_EQ(acquireTarget(6), -1); // but the col-10 wall at
// row 7 blocks sight
}
TEST(unit/win_and_loss_transitions)
{
freshScenario();
for (int i = 2; i < 7; i++) // clear the terminal guards:
if (units[i].alive) unitKill(i); // no target -> marine walks
beaconTX = 34; beaconTY = 7;
units[0].tx = 33; units[0].ty = 7; // adjacent, path in
stepTicks(1); // arrive -> BREACH SUCCESS
EXPECT_EQ(scenario, SC_WIN);
freshScenario();
unitKill(0);
unitKill(1);
stepTicks(1);
EXPECT_EQ(scenario, SC_LOSS);
}
TEST(unit/reset_rearms_everything)
{
freshScenario();
units[0].tx = 34; units[0].ty = 7;
stepTicks(1); // -> SC_WIN mess
resetScenario();
EXPECT_EQ(scenario, SC_LIVE);
EXPECT(paused);
EXPECT(units[0].alive && units[1].alive);
EXPECT_EQ(units[0].tx, 2); // back in the airlock
EXPECT_EQ(units[0].hp, STATS[U_MARINE].maxHp);
EXPECT(!units[6].alive); // reinforcement slot cleared
EXPECT(!doorOpen);
EXPECT_EQ(heatT, 0);
EXPECT(linkIdx(0) < 0 && linkIdx(1) < 0 && linkIdx(2) < 0);
EXPECT(nodeOnline[N_AIRLK]); // the old root-online bug
EXPECT(!nodeOnline[N_JUNC] && !nodeOnline[N_DOOR] && !nodeOnline[N_HIVE]);
}