fix: review fixes for the shades.cfg records

- A group in a backup from before version 23 has no rolling code, and a
  restore wrote 0 to NVS for it.  It keeps the code NVS has.
- A restore reads each record into one that starts as the shade, group
  or settings it replaces, so what an older version lacks stays as it
  was (the update check below version 20 among them), as before.
- restoreFile and loadFile read the rooms, shades and groups through one
  readModel(); the settings, network and radio are applied by helpers.
- The shade's tilt is forgotten where the shades are written, not in the
  conversion to a record.
- rollingCodeAfterLoad moves to RollingCode.h and its tests to
  test_rollingcode.
- The topics a deleted shade or group clears are one list each
  (MqttTopics.h), tested against what is published.
- e2e: a restore of the shades brings back every shade and group, and
  the README has the rollback check by hand.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
cjkas 2026-10-29 17:25:07 +01:00
parent b525a800b0
commit 92cd54f097
13 changed files with 277 additions and 148 deletions

View file

@ -1,5 +1,9 @@
#include <unity.h>
#include <string>
#include <vector>
#include "MqttDisco.h"
#include "MqttTopics.h"
#include "ShadeChanges.h"
void setUp() {}
void tearDown() {}
@ -170,6 +174,29 @@ static void test_the_config_topic_of_each_kind() {
TEST_ASSERT_FALSE(isDiscoSwitch(shade_types::garage1));
}
static bool hasTopic(const char *const *topics, size_t count, const std::string &topic) {
for(size_t i = 0; i < count; i++) if(topic == topics[i]) return true;
return false;
}
static void keepRetained(const char *topic, int32_t value, bool retain, void *ctx) {
(void)value;
if(retain) static_cast<std::vector<std::string> *>(ctx)->push_back(topic);
}
// A deleted shade leaves none of its topics at the broker.
static void test_deleting_a_shade_clears_every_state_topic_the_broker_keeps() {
shade_snapshot_t shade{};
std::vector<std::string> retained;
publishShadeTopics(SHADE_CHANGED_TOPICS, shade, keepRetained, &retained);
TEST_ASSERT_FALSE(retained.empty());
for(const std::string &topic : retained)
TEST_ASSERT_TRUE_MESSAGE(hasTopic(SHADE_TOPICS, SHADE_TOPIC_COUNT, topic), topic.c_str());
}
// Group unpublish named it SunSensor, so a deleted group's stayed.
static void test_deleting_a_group_clears_its_sun_sensor_topic() {
TEST_ASSERT_TRUE(hasTopic(GROUP_TOPICS, GROUP_TOPIC_COUNT, "sunSensor"));
TEST_ASSERT_FALSE(hasTopic(GROUP_TOPICS, GROUP_TOPIC_COUNT, "SunSensor"));
}
int main(int argc, char **argv) {
UNITY_BEGIN();
RUN_TEST(test_a_roller_is_a_cover_under_the_discovery_topic);
@ -183,5 +210,7 @@ int main(int argc, char **argv) {
RUN_TEST(test_a_tilt_only_blind_has_only_the_tilt);
RUN_TEST(test_a_dry_contact_is_a_switch);
RUN_TEST(test_the_config_topic_of_each_kind);
RUN_TEST(test_deleting_a_shade_clears_every_state_topic_the_broker_keeps);
RUN_TEST(test_deleting_a_group_clears_its_sun_sensor_topic);
return UNITY_END();
}