This project uses PlatformIO's built-in advanced testing framework (Unity) to assure code quality, parser safety, and hardware memory integrity. The tests are divided into two distinct environments to speed up development.
Desktop tests compile and run entirely on your desktop PC. They use a standard gcc/g++ compiler, mock out hardware-specific dependencies, and completely isolate the core algorithm logic. This means they run in milliseconds, making them perfect for CI/CD or rapid iteration.
You can execute these at any time (even if the CYD board is unplugged!):
pio test -e desktoptest_desktop/test_main.cpp(OpenSpool & Webhooks):- Data Integrity: Verifies that standard OpenSpool JSON strings populate the correct internal memory structs.
- Edge Cases: Tests color normalization (e.g., adding
#if missing) and mixed data types in the JSON. - URL Formatting: Rigorously tests the
WebhookFormatterlogic for{spool_id}and{toolhead}placeholder replacement, including multi-occurrence and malformed placeholder handling. - Spoolman Enrichment: Validates the logic for parsing complex JSON responses from Spoolman to extract filament names and weights (including rounding and unit conversion).
test_desktop_serial/test_serialparser.cpp(Serial Terminal): An isolated unit test of the string extraction logic used by the Serial Terminal. It specifically feeds malformed commands, strings with excess spaces, and partial keys into the parser to ensure configuration saving is stable and robust.
Embedded tests are written against the actual ESP32 silicon. PlatformIO compiles the test framework alongside the project firmware, uploads it over USB, and monitors the Serial output to report on assertion passes/failures. These require the device to be physically plugged into your PC.
# General run command (Make sure no other Serial Monitors are active)
pio test -e test_embeddedtest_embedded/test_nvs.cpp(Non-Volatile Storage): These tests verify the core reliability of the ESP32's flash memory via thePreferences.hAPI used byConfigManager. The test suite automatically writes dummy Wi-Fi passwords and Webhook URLs into flash storage, reads them back into RAM, asserts they match character-for-character, and finally invokes theformat()sequence to leave the device clean.
- "Uploading stage has failed," or "Error 1": Ensure the device is plugged in using a data-capable USB cord and that you do not have another Serial terminal or Web Installer actively grabbing the COM port.
- Linker Errors or missing definitions: Ensure you are using the correct environment. Board tests MUST use
-e test_embeddedto correctly include the project source code while excluding the main application UI and logic.
pio test -e test_embeddedThe UI simulator allows you to run the entire LVGL application on your desktop. It uses the native PlatformIO environment and SDL2 to create a window that mimics the 240x320 CYD screen.
Before running the simulator, you must have the SDL2 development libraries installed.
To build and launch the simulator:
pio run -e simulator
./simulator/programNote: The simulator skips hardware-specific code (NFC, Wi-Fi, NVS) and simulates the UI exactly as it appears on the device.
For local development and testing of the webhook integration, a simple Python-based test server is provided in the scripts/ directory.
-
Start the server:
./scripts/test_webhook_server.py
The server will start listening on port 7125 (default for Moonraker/U1).
-
Configure the device:
- Find your computer's local IP address.
- Set the webhook URL on the device or via the serial terminal:
set webhook http://<your-ip>:7125/
-
Trigger a webhook: Scan a spool tag. The test server will log the incoming request details, including:
- HTTP Method (GET or POST)
- Full Headers
- JSON Payload (for POST) or URL Query Parameters (for GET)
- No cloud setup: Test your webhook logic without needing a public endpoint or Spoolman instance.
- Debugging: See exactly what the device is sending, including content-type and payload structure.
- Simulator support: The simulator also sends webhooks, allowing for a fully virtual testing loop.
The Snapmaker U1 integration uses a direct POST to /printer/filament_detect/set. You can verify this using the simulator and the provided test server.
-
Start the Test Server:
./scripts/test_webhook_server.py
(Defaults to port 7125).
-
Configure the Simulator:
- Open
simulator/config.cfg. - Set
u1_hosttolocalhost:7125.
- Open
-
Run the Simulator:
./simulator/program
-
Load Spool:
- Scan a mock spool (by updating
simulator/spool.json). - On the Info Screen, click the Load button.
- If prompted, select a toolhead/channel.
- Scan a mock spool (by updating
-
Check Results: Observe the test server terminal output. You should see a log entry for
[U1 API] Detected Direct Filament Set!containing the structured "OpenSpool U1 Extended Format" JSON payload.
- No request received: Ensure
u1_hostis correctly set and the test server is running on the same port. - Incorrect fields: Check the logic in
NetworkManager::sendWebhookPayload. All mapped fields (VENDOR,MAIN_TYPE,RGB_1, etc.) must match the specification infilament_detect.md.