From 5047f1752e60734473c2bc585974beb63b1d4cad Mon Sep 17 00:00:00 2001 From: proddy Date: Wed, 23 Apr 2025 21:30:02 +0200 Subject: [PATCH] add tests for custom entities --- src/test/test.cpp | 64 +++++++++++++++++++++--------- src/test/test.h | 4 +- src/web/WebCustomEntityService.cpp | 25 ++++++++++++ test/test_api/api_test.http | 14 +++++++ 4 files changed, 87 insertions(+), 20 deletions(-) diff --git a/src/test/test.cpp b/src/test/test.cpp index 22059e57c..3176f7c24 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -352,6 +352,10 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const #ifdef EMSESP_STANDALONE AsyncWebServerRequest request; + JsonDocument doc; + JsonVariant json; + + // read requests request.method(HTTP_GET); request.url("/api/custom"); EMSESP::webAPIService.webAPIService(&request); @@ -363,6 +367,20 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const EMSESP::webAPIService.webAPIService(&request); shell.invoke_command("call custom info"); + // write requests + request.method(HTTP_POST); + char data[] = "{\"value\":\"99\"}"; + deserializeJson(doc, data); + json = doc.as(); + // validate + request.url("/api/custom/test_ram"); + EMSESP::webAPIService.webAPIService(&request, json); + + request.method(HTTP_GET); + request.url("/api/custom"); + EMSESP::webAPIService.webAPIService(&request); + + #endif ok = true; } @@ -1091,6 +1109,16 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const // POST COMMANDS request.method(HTTP_POST); + char data0[] = "{\"value\":12}"; + deserializeJson(doc, data0); + request.url("/api/thermostat/seltemp"); + EMSESP::webAPIService.webAPIService(&request, doc.as()); + // request.method(HTTP_GET); + // request.url("/api/thermostat/seltemp/value"); + // EMSESP::webAPIService.webAPIService(&request); + + // request.method(HTTP_POST); + // char data1[] = "{\"device\":\"system\", \"cmd\":\"restart\",\"id\":-1}"; // deserializeJson(doc, data1); // request.url("/api"); @@ -1114,28 +1142,28 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const // test version checks // use same data as in rest_server.ts // log shows first if you can upgrade to dev, and then if you can upgrade to stable - request.url("/rest/action"); - std::string LATEST_STABLE_VERSION = "3.7.2"; - std::string LATEST_DEV_VERSION = "3.7.3-dev.3"; - std::string param = LATEST_DEV_VERSION + "," + LATEST_STABLE_VERSION; - std::string action = "{\"action\":\"checkUpgrade\", \"param\":\"" + param + "\"}"; - deserializeJson(doc, action); + // request.url("/rest/action"); + // std::string LATEST_STABLE_VERSION = "3.7.2"; + // std::string LATEST_DEV_VERSION = "3.7.3-dev.3"; + // std::string param = LATEST_DEV_VERSION + "," + LATEST_STABLE_VERSION; + // std::string action = "{\"action\":\"checkUpgrade\", \"param\":\"" + param + "\"}"; + // deserializeJson(doc, action); - // case 0: on latest stable, can upgrade to dev only. So true, false - EMSESP::webStatusService.set_current_version(LATEST_STABLE_VERSION); - EMSESP::webStatusService.action(&request, doc.as()); + // // case 0: on latest stable, can upgrade to dev only. So true, false + // EMSESP::webStatusService.set_current_version(LATEST_STABLE_VERSION); + // EMSESP::webStatusService.action(&request, doc.as()); - // case 1: on latest dev, no updates to either dev or stable. So false, false - EMSESP::webStatusService.set_current_version(LATEST_DEV_VERSION); - EMSESP::webStatusService.action(&request, doc.as()); + // // case 1: on latest dev, no updates to either dev or stable. So false, false + // EMSESP::webStatusService.set_current_version(LATEST_DEV_VERSION); + // EMSESP::webStatusService.action(&request, doc.as()); - // case 2: upgrade an older stable to latest stable or the latest dev. So true, true - EMSESP::webStatusService.set_current_version("3.6.5"); - EMSESP::webStatusService.action(&request, doc.as()); + // // case 2: upgrade an older stable to latest stable or the latest dev. So true, true + // EMSESP::webStatusService.set_current_version("3.6.5"); + // EMSESP::webStatusService.action(&request, doc.as()); - // case 3: upgrade an older dev to latest dev, no stable upgrades available. So true, false - EMSESP::webStatusService.set_current_version("3.7.3-dev.2"); - EMSESP::webStatusService.action(&request, doc.as()); + // // case 3: upgrade an older dev to latest dev, no stable upgrades available. So true, false + // EMSESP::webStatusService.set_current_version("3.7.3-dev.2"); + // EMSESP::webStatusService.action(&request, doc.as()); // char data6[] = "{\"device\":\"system\", \"cmd\":\"read\",\"value\":\"8 2 27 1\"}"; // deserializeJson(doc, data6); diff --git a/src/test/test.h b/src/test/test.h index dbed1bca7..92170f7cc 100644 --- a/src/test/test.h +++ b/src/test/test.h @@ -41,7 +41,7 @@ namespace emsesp { // #define EMSESP_DEBUG_DEFAULT "310" // #define EMSESP_DEBUG_DEFAULT "render" // #define EMSESP_DEBUG_DEFAULT "api" -#define EMSESP_DEBUG_DEFAULT "api3" +// #define EMSESP_DEBUG_DEFAULT "api3" // #define EMSESP_DEBUG_DEFAULT "api4" // #define EMSESP_DEBUG_DEFAULT "crash" // #define EMSESP_DEBUG_DEFAULT "dv" @@ -56,7 +56,7 @@ namespace emsesp { // #define EMSESP_DEBUG_DEFAULT "telegram_dump" // #define EMSESP_DEBUG_DEFAULT "memory" // #define EMSESP_DEBUG_DEFAULT "coldshot" -// #define EMSESP_DEBUG_DEFAULT "custom" +#define EMSESP_DEBUG_DEFAULT "custom" // #define EMSESP_DEBUG_DEFAULT "scheduler" // #define EMSESP_DEBUG_DEFAULT "heat_exchange" // #define EMSESP_DEBUG_DEFAULT "ls" diff --git a/src/web/WebCustomEntityService.cpp b/src/web/WebCustomEntityService.cpp index 790c4cba4..19e0f6fe0 100644 --- a/src/web/WebCustomEntityService.cpp +++ b/src/web/WebCustomEntityService.cpp @@ -666,6 +666,7 @@ bool WebCustomEntityService::get_value(std::shared_ptr telegram) } // hard coded tests +// add the entity and also add the command for writeable entities #ifdef EMSESP_TEST void WebCustomEntityService::test() { update([&](WebCustomEntity & webCustomEntity) { @@ -685,6 +686,14 @@ void WebCustomEntityService::test() { entityItem.writeable = true; entityItem.data = "70"; webCustomEntity.customEntityItems.push_back(entityItem); + Command::add( + EMSdevice::DeviceType::CUSTOM, + webCustomEntity.customEntityItems.back().name.c_str(), + [webCustomEntity](const char * value, const int8_t id) { + return EMSESP::webCustomEntityService.command_setvalue(value, id, webCustomEntity.customEntityItems.back().name.c_str()); + }, + FL_(entity_cmd), + CommandFlag::ADMIN_ONLY); // test 2 entityItem.id = 2; @@ -713,6 +722,14 @@ void WebCustomEntityService::test() { entityItem.writeable = true; entityItem.data = "14"; webCustomEntity.customEntityItems.push_back(entityItem); + Command::add( + EMSdevice::DeviceType::CUSTOM, + webCustomEntity.customEntityItems.back().name.c_str(), + [webCustomEntity](const char * value, const int8_t id) { + return EMSESP::webCustomEntityService.command_setvalue(value, id, webCustomEntity.customEntityItems.back().name.c_str()); + }, + FL_(entity_cmd), + CommandFlag::ADMIN_ONLY); // test 4 entityItem.id = 4; @@ -728,6 +745,14 @@ void WebCustomEntityService::test() { entityItem.data = "14"; entityItem.value = 12; webCustomEntity.customEntityItems.push_back(entityItem); + Command::add( + EMSdevice::DeviceType::CUSTOM, + webCustomEntity.customEntityItems.back().name.c_str(), + [webCustomEntity](const char * value, const int8_t id) { + return EMSESP::webCustomEntityService.command_setvalue(value, id, webCustomEntity.customEntityItems.back().name.c_str()); + }, + FL_(entity_cmd), + CommandFlag::ADMIN_ONLY); return StateUpdateResult::CHANGED; // persist the changes }); diff --git a/test/test_api/api_test.http b/test/test_api/api_test.http index 4c7d6d33f..ae30be6fc 100755 --- a/test/test_api/api_test.http +++ b/test/test_api/api_test.http @@ -74,6 +74,20 @@ Authorization: Bearer {{token}} GET {{host}}/api/boiler/commands +### + +POST {{host}}/api/custom/test_custom +Content-Type: application/json +Authorization: Bearer {{token}} + +{ + "value" : 22.0 +} + +### + +GET {{host}}/api/custom/test_custom + # # Test on dev #