#include <unity.h>
#include "TinyComponent.h"

void setUp(void) {
    // set stuff up here
}

void tearDown(void) {
    // clean stuff up here
}

void test_unknown_sensor_type(void) {
    StaticJsonDocument<200> jsonDoc;
    unsigned long value = TYPE(0);
    TEST_ASSERT_EQUAL(false, buildSensorJson(value, jsonDoc));
}

void test_max_temp(void) {
    StaticJsonDocument<200> jsonDoc;
    unsigned long value = TEMP(1023) | TYPE(SensorType::TEMPERATURE);
    TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
    
    JsonObject sensor = jsonDoc["sensor"];
    TEST_ASSERT_EQUAL(102.3, sensor["temperature"]);
}

void test_overflow_value(void) {
    StaticJsonDocument<200> jsonDoc;
    unsigned long value = VALUE(1024) | TYPE(SensorType::GENERIC);
    TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
    
    JsonObject sensor = jsonDoc["sensor"];
    TEST_ASSERT_EQUAL(0, sensor["value"]);
}

void test_max_voltage(void) {
    StaticJsonDocument<200> jsonDoc;
    unsigned long value = VCC(8191) | TYPE(SensorType::GENERIC);
    TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
    
    JsonObject diagnostic = jsonDoc["sensor"]["diagnostic"];
    TEST_ASSERT_EQUAL(8.191, diagnostic["voltage"]);
}

void test_overflow_voltage(void) {
    StaticJsonDocument<200> jsonDoc;
    unsigned long value = VCC(8192) | TYPE(SensorType::GENERIC);
    TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
    
    JsonObject diagnostic = jsonDoc["sensor"]["diagnostic"];
    TEST_ASSERT_EQUAL(0, diagnostic["voltage"]);
}

void test_temp_sensor(void) {
    StaticJsonDocument<200> jsonDoc;
    unsigned long value = ID(SensorId::TEMP_SENSOR) | TEMP(210) | TYPE(SensorType::TEMPERATURE);
    TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
    
    JsonObject sensor = jsonDoc["sensor"];
    TEST_ASSERT_EQUAL(SensorId::TEMP_SENSOR, sensor["id"]);
    TEST_ASSERT_EQUAL(21, sensor["temperature"]);
}

void test_temp_sensor_with_voltage(void) {
    StaticJsonDocument<200> jsonDoc;
    unsigned long value = ID(SensorId::TEMP_SENSOR) | TEMP(320) | TYPE(SensorType::TEMPERATURE) | VCC(2847L);
    TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
    
    JsonObject sensor = jsonDoc["sensor"];
    TEST_ASSERT_EQUAL(SensorId::TEMP_SENSOR, sensor["id"]);
    TEST_ASSERT_EQUAL(32, sensor["temperature"]);

    JsonObject diagnostic = sensor["diagnostic"];
    TEST_ASSERT_EQUAL(2.847, diagnostic["voltage"]);
}

void test_oil_sensor(void) {
    StaticJsonDocument<200> jsonDoc;
    unsigned long value = ID(SensorId::OIL_SENSOR) | VALUE(150) | TYPE(SensorType::GENERIC);
    TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
    
    JsonObject sensor = jsonDoc["sensor"];
    TEST_ASSERT_EQUAL(SensorId::OIL_SENSOR, sensor["id"]);
    TEST_ASSERT_EQUAL(150, sensor["value"]);
}

void test_oil_sensor_with_voltage(void) {
    StaticJsonDocument<200> jsonDoc;
    unsigned long value = ID(SensorId::OIL_SENSOR) | TEMP(200) | TYPE(SensorType::GENERIC) | VCC(2847L);
    TEST_ASSERT_EQUAL(true, buildSensorJson(value, jsonDoc));
    
    JsonObject sensor = jsonDoc["sensor"];
    TEST_ASSERT_EQUAL(SensorId::OIL_SENSOR, sensor["id"]);
    TEST_ASSERT_EQUAL(200, sensor["value"]);

    JsonObject diagnostic = sensor["diagnostic"];
    TEST_ASSERT_EQUAL(2.847, diagnostic["voltage"]);
}

int main(int argc, char **argv) {
    UNITY_BEGIN();
    RUN_TEST(test_unknown_sensor_type);
    RUN_TEST(test_max_temp);
    RUN_TEST(test_overflow_value);
    RUN_TEST(test_max_voltage);
    RUN_TEST(test_overflow_voltage);
    RUN_TEST(test_temp_sensor);
    RUN_TEST(test_temp_sensor_with_voltage);
    RUN_TEST(test_oil_sensor);
    RUN_TEST(test_oil_sensor_with_voltage);
    UNITY_END();

    return 0;
}
