Answers "how do we emulate/lint this?" with runnable tooling for every part, and factors the pure firmware logic out so it is unit-testable on the host. What changed: - firmware/src/wav.h, firmware/src/httprange.h: pure, header-only WAV header builder and HTTP Range parser (no Arduino deps), so the tricky byte-layout and range logic can be tested on a PC. storage.cpp and api_http.cpp refactored to use them. - firmware/test/test_pure/: Unity tests for the WAV header fields and Range parsing (start-end, open-ended, clamped, unsatisfiable, non-bytes). Run: pio test -e native. - firmware/platformio.ini: add [env:native] (host tests) and [env:esp32s3_wokwi] (emulator build with default WiFi = Wokwi-GUEST so the API comes up in the sim). - firmware/wokwi.toml + firmware/diagram.json: Wokwi emulator harness (ESP32-S3 + microSD + button + LED). Note: Wokwi has no I2S mic part, so audio isn't emulated; the harness targets boot + WiFi + REST API. - firmware/src/main.cpp + config.cpp: bring up WiFi + API even if audio/SD init fails (device stays reachable, reports the fault via GET /device); compile-time default WiFi honoured when NVS is empty (used by the Wokwi build). - .forgejo/workflows/ci.yml: add native tests + cppcheck to the firmware job; new openapi job (openapi-spec-validator) and emulator job (Wokwi build, plus a full run when WOKWI_CLI_TOKEN is set). - docs/testing.md: the full emulate/lint guide for firmware, server, API, app, case. - state/: NOTES points at the guide; TODO reflects this branch. Why: - The firmware can't be flashed yet (no parts) and doesn't build on this dev host, so we need host-runnable checks. Pure-logic unit tests + OpenAPI validation run anywhere; Wokwi emulates boot/WiFi/API; CI compiles the real firmware. Verified locally: the OpenAPI spec validates (12 paths, 10 schemas). Notes: - Native tests and cppcheck run in CI (no compiler on the dev host). The Wokwi full run is skipped unless a WOKWI_CLI_TOKEN secret is present; the build is still verified. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
88 lines
2.8 KiB
C++
88 lines
2.8 KiB
C++
// SPDX-License-Identifier: GPL-3.0-only
|
|
// Host-side unit tests for the pure firmware logic (no hardware needed).
|
|
// Run with: pio test -e native (see docs/testing.md)
|
|
#include <unity.h>
|
|
|
|
#include "httprange.h"
|
|
#include "wav.h"
|
|
|
|
static uint32_t rd32(const uint8_t *p) {
|
|
return p[0] | (p[1] << 8) | (p[2] << 16) | ((uint32_t)p[3] << 24);
|
|
}
|
|
static uint16_t rd16(const uint8_t *p) { return p[0] | (p[1] << 8); }
|
|
|
|
// --- WAV header ---
|
|
|
|
void test_wav_header_fields() {
|
|
uint8_t h[44];
|
|
oswav::buildHeader(h, 16000, 16, 1, 32000); // 1s of 16k mono 16-bit
|
|
|
|
TEST_ASSERT_EQUAL_UINT8_ARRAY("RIFF", h, 4);
|
|
TEST_ASSERT_EQUAL_UINT8_ARRAY("WAVE", h + 8, 4);
|
|
TEST_ASSERT_EQUAL_UINT8_ARRAY("fmt ", h + 12, 4);
|
|
TEST_ASSERT_EQUAL_UINT8_ARRAY("data", h + 36, 4);
|
|
|
|
TEST_ASSERT_EQUAL_UINT32(36 + 32000, rd32(h + 4)); // RIFF size
|
|
TEST_ASSERT_EQUAL_UINT32(16, rd32(h + 16)); // fmt chunk size
|
|
TEST_ASSERT_EQUAL_UINT16(1, rd16(h + 20)); // PCM
|
|
TEST_ASSERT_EQUAL_UINT16(1, rd16(h + 22)); // channels
|
|
TEST_ASSERT_EQUAL_UINT32(16000, rd32(h + 24)); // sample rate
|
|
TEST_ASSERT_EQUAL_UINT32(32000, rd32(h + 28)); // byte rate = 16000*1*2
|
|
TEST_ASSERT_EQUAL_UINT16(2, rd16(h + 32)); // block align
|
|
TEST_ASSERT_EQUAL_UINT16(16, rd16(h + 34)); // bits
|
|
TEST_ASSERT_EQUAL_UINT32(32000, rd32(h + 40)); // data size
|
|
}
|
|
|
|
// --- HTTP Range ---
|
|
|
|
void test_range_none() {
|
|
auto r = oshttp::parseByteRange(nullptr, 1000);
|
|
TEST_ASSERT_FALSE(r.partial);
|
|
TEST_ASSERT_TRUE(r.satisfiable);
|
|
TEST_ASSERT_EQUAL_size_t(0, r.start);
|
|
TEST_ASSERT_EQUAL_size_t(999, r.end);
|
|
}
|
|
|
|
void test_range_start_end() {
|
|
auto r = oshttp::parseByteRange("bytes=0-1023", 10000);
|
|
TEST_ASSERT_TRUE(r.partial);
|
|
TEST_ASSERT_TRUE(r.satisfiable);
|
|
TEST_ASSERT_EQUAL_size_t(0, r.start);
|
|
TEST_ASSERT_EQUAL_size_t(1023, r.end);
|
|
}
|
|
|
|
void test_range_open_ended() {
|
|
auto r = oshttp::parseByteRange("bytes=500-", 1000);
|
|
TEST_ASSERT_TRUE(r.partial);
|
|
TEST_ASSERT_TRUE(r.satisfiable);
|
|
TEST_ASSERT_EQUAL_size_t(500, r.start);
|
|
TEST_ASSERT_EQUAL_size_t(999, r.end); // clamped to last byte
|
|
}
|
|
|
|
void test_range_end_past_total_clamped() {
|
|
auto r = oshttp::parseByteRange("bytes=0-99999", 1000);
|
|
TEST_ASSERT_EQUAL_size_t(999, r.end);
|
|
}
|
|
|
|
void test_range_unsatisfiable() {
|
|
auto r = oshttp::parseByteRange("bytes=5000-6000", 1000);
|
|
TEST_ASSERT_TRUE(r.partial);
|
|
TEST_ASSERT_FALSE(r.satisfiable);
|
|
}
|
|
|
|
void test_range_not_bytes() {
|
|
auto r = oshttp::parseByteRange("items=0-1", 1000);
|
|
TEST_ASSERT_FALSE(r.partial); // unknown unit -> full body
|
|
}
|
|
|
|
int main(int, char **) {
|
|
UNITY_BEGIN();
|
|
RUN_TEST(test_wav_header_fields);
|
|
RUN_TEST(test_range_none);
|
|
RUN_TEST(test_range_start_end);
|
|
RUN_TEST(test_range_open_ended);
|
|
RUN_TEST(test_range_end_past_total_clamped);
|
|
RUN_TEST(test_range_unsatisfiable);
|
|
RUN_TEST(test_range_not_bytes);
|
|
return UNITY_END();
|
|
}
|