diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..569af3c --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,30 @@ +name: CI +on: + push: + branches: [main, master] + pull_request: + branches: [main, master] +jobs: + compile: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: arduino/compile-sketches@v1 + with: + sketch-paths: | + - . + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Install test dependencies + run: | + python -m pip install --upgrade pip + pip install pytest + pip install -r requirements.txt 2>/dev/null || true + - name: Run logic tests + run: | + if [ -d tests ]; then python -m pytest tests/ -v --tb=short; fi \ No newline at end of file diff --git a/.gitignore b/.gitignore index 496ee2c..4785022 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,14 @@ -.DS_Store \ No newline at end of file +.DS_Store +# Auto-added by Marisol pipeline +node_modules/ +__pycache__/ +*.pyc +.pytest_cache/ +*.o +*.so +.env +debug_*.py +.cache/ +dist/ +build/ +*.egg-info/ diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..53aba3b --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,45 @@ +"""Auto-generated conftest.py -- stubs for Arduino/embedded C testing. + +Strategy: Arduino .ino/.cpp files can't be imported in Python, so we test by +re-implementing the core algorithms (state machines, math, protocol encoding, +parsers) in Python and verifying them with pytest. The conftest provides mock +serial and common helper dependencies. +""" +import sys +import os +import pytest +from unittest.mock import MagicMock, patch + +# Add repo root to path so test files can import source modules +sys.path.insert(0, '') + +# Mock common Arduino Python helper dependencies +for mod_name in ['serial', 'serial.tools', 'serial.tools.list_ports', + 'pyserial', 'pyfirmata', 'pyfirmata2']: + sys.modules[mod_name] = MagicMock() + + +class ArduinoConstants: + """Provides Arduino-style constants for Python re-implementations.""" + HIGH = 1 + LOW = 0 + INPUT = 0 + OUTPUT = 1 + INPUT_PULLUP = 2 + A0, A1, A2, A3, A4, A5 = range(14, 20) + + @staticmethod + def map_value(x, in_min, in_max, out_min, out_max): + """Arduino map() function.""" + return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) + + @staticmethod + def constrain(x, a, b): + """Arduino constrain() function.""" + return max(a, min(x, b)) + + +@pytest.fixture +def arduino(): + """Provides Arduino-like constants and helpers for testing re-implemented logic.""" + return ArduinoConstants() diff --git a/tests/test_example.py b/tests/test_example.py new file mode 100644 index 0000000..b3d25ea --- /dev/null +++ b/tests/test_example.py @@ -0,0 +1,55 @@ +"""test_example.py — Starter template for Arduino project tests. + +Arduino .ino/.cpp files CANNOT be imported in Python. Instead: +1. READ the source files to understand the algorithms +2. RE-IMPLEMENT the logic in Python in this test file +3. Test the Python re-implementation with pytest + +The `arduino` fixture provides constants (HIGH, LOW, A0-A5) and helpers +(map_value, constrain). Also see embedded_mocks.py for I2C/SPI/UART mocks. +DO NOT modify conftest.py. +""" +import pytest +from embedded_mocks import ( + MockI2C, MockSPI, MockUART, MockGPIO, MockNeoPixel, + MockWiFi, MockBLE, MockPreferences, + arduino_map, arduino_constrain, analog_to_voltage, +) + + +def test_arduino_map(): + """Test Arduino map() function re-implementation.""" + assert arduino_map(0, 0, 1023, 0, 255) == 0 + assert arduino_map(512, 0, 1023, 0, 255) == 127 + assert arduino_map(1023, 0, 1023, 0, 255) == 255 + + +def test_arduino_constrain(): + """Test Arduino constrain() function.""" + assert arduino_constrain(150, 0, 100) == 100 + assert arduino_constrain(-10, 0, 100) == 0 + assert arduino_constrain(50, 0, 100) == 50 + + +def test_analog_voltage_conversion(): + """Test ADC raw value to voltage conversion.""" + assert abs(analog_to_voltage(512, bits=10, ref=3.3) - 1.65) < 0.01 + assert abs(analog_to_voltage(0, bits=10) - 0.0) < 0.01 + assert abs(analog_to_voltage(1023, bits=10, ref=3.3) - 3.3) < 0.01 + + +def test_serial_protocol_example(): + """Example: test a serial command protocol.""" + uart = MockUART(baudrate=115200) + # Simulate sending a command + uart.write(b"AT+CMD\r\n") + assert uart._tx_log == b"AT+CMD\r\n" + # Simulate receiving a response + uart.inject_rx(b"OK\r\n") + assert uart.readline() == b"OK\r\n" + + +# TODO: Read the .ino source files and re-implement key algorithms below: +# def test_state_machine(): +# """Re-implement the state machine from the Arduino source.""" +# pass diff --git a/tests/test_moisture.py b/tests/test_moisture.py new file mode 100644 index 0000000..88facdf --- /dev/null +++ b/tests/test_moisture.py @@ -0,0 +1,49 @@ +"""Tests for plant moisture calculation logic. + +Re-implements the moisture percentage calculation from inplants-argon.ino: + moisture_percentage = (100 - ( (moisture_analog/4095.00) * 100 ) ) +""" + +import pytest + + +def map_moisture_analog_to_percentage(moisture_analog, max_value=4095.00): + """Re-implementation of the Arduino moisture percentage calculation. + + From inplants-argon.ino: + float moisture_percentage = (100 - ( (moisture_analog/4095.00) * 100 ) ); + """ + return 100 - ((moisture_analog / max_value) * 100) + + +def test_moisture_percentage_dry_sensor(): + """When sensor reads max value (4095), moisture should be 0% (dry).""" + assert map_moisture_analog_to_percentage(4095) == 0.0 + + +def test_moisture_percentage_wet_sensor(): + """When sensor reads 0, moisture should be 100% (wet).""" + assert map_moisture_analog_to_percentage(0) == 100.0 + + +def test_moisture_percentage_halfway(): + """When sensor reads halfway (2047.5), moisture should be 50%.""" + assert map_moisture_analog_to_percentage(2047.5) == 50.0 + + +def test_moisture_percentage_example_values(): + """Test some example sensor values.""" + # Sensor reads 1024 (about 25% of max) -> 75% moisture + assert abs(map_moisture_analog_to_percentage(1024) - 75.0) < 0.1 + + # Sensor reads 3072 (about 75% of max) -> 25% moisture + assert abs(map_moisture_analog_to_percentage(3072) - 25.0) < 0.1 + + +def test_moisture_percentage_with_integer_values(): + """Test with typical integer sensor readings.""" + # Typical dry reading + assert abs(map_moisture_analog_to_percentage(3500) - 14.53) < 0.1 + + # Typical wet reading + assert abs(map_moisture_analog_to_percentage(500) - 87.81) < 0.1