"""Exercise color-display night controls through real config handlers and UI.""" import json from pathlib import Path import unittest import test_webconfig_lost_reply as web_fixture ROOT = Path(__file__).resolve().parents[1] class DisplayNightWebConfigTest(unittest.TestCase): compile_run = web_fixture.LostReplyWebConfigTest.compile_run setup_values = web_fixture.LostReplyWebConfigTest.setup_values run_page = web_fixture.LostReplyWebConfigTest.run_page def test_actual_snapshot_and_authenticated_cli_batch(self): fixture = web_fixture.LostReplyWebConfigTest.fixture(self) fixture = fixture[:fixture.index('String writeBody(')] for color in (False, True): with self.subTest(color=color): source = r''' #include #define MESHCORE_HAS_REAL_DISPLAY 1 @COLOR@ namespace mesh { namespace ui { struct Profile { uint8_t mode=0; uint16_t seconds=15; }; struct Prefs { Profile battery, usb; }; const Prefs& displayPowerPrefs() { static Prefs prefs; return prefs; } const char* displayModeName(uint8_t) { return "button"; } bool displayPairingSupported() { return false; } uint8_t night_mode=2; int16_t night_delay=60; bool night_active=false; uint8_t displayNightMode() { return night_mode; } int16_t displayNightDelayMinutes() { return night_delay; } bool displayNightActive() { return night_active; } }} '''.replace('@COLOR@', '#define MESHCORE_HAS_COLOR_DISPLAY 1' if color else '') + fixture + r''' int main() { assert(mesh::esp32::managementPassword::publish("password")); for(unsigned mode=0;mode<3;++mode) { WebConfigServer server; mesh::ui::night_mode=mode;mesh::ui::night_delay=mode?0:-30;mesh::ui::night_active=mode==1; AsyncWebServerRequest get;server.handleConfigGet(&get); JsonDocument snapshot; assert(get.status==200 && deserializeJson(snapshot,get.output)==DeserializationError::Ok); #ifdef MESHCORE_HAS_COLOR_DISPLAY const char* names[]={"off","on","auto"}; assert(snapshot["display"]["night_supported"].as()); assert(strcmp(snapshot["display"]["night"].as(),names[mode])==0); assert(snapshot["display"]["night_delay"].as()==(mode?0:-30)); assert(snapshot["display"]["night_active"].as()==(mode==1)); #else assert(snapshot["display"]["night_supported"].isNull()); assert(snapshot["display"]["night"].isNull()); #endif } for(const char* key:{"display.night","display.night.delay"}) { assert(wcIsAllowedSetKey(key) && !wcSetKeyRequiresReboot(key)); WebConfigServer server; const char* value=strcmp(key,"display.night")==0?"auto":"-30"; String body=String("{\"reqid\":\"0123456789abcdef\",\"set\":{\"")+key+"\":\""+value+"\"}}"; AsyncWebServerRequest unauthenticated(body);unauthenticated.header.cookies.clear(); server.handleConfigPost(&unauthenticated); assert(unauthenticated.status==401 && !server._batch_count); AsyncWebServerRequest save(body);server.handleConfigPost(&save); assert(save.status==202 && server._batch_count==1 && !server._batch_reboot); assert(String(server._batch[0].cmd)==String("set ")+key+" "+value); } for(const char* key:{"display.night.extra","display.night.delay.extra"})assert(!wcIsAllowedSetKey(key)); } ''' self.compile_run(source) def test_color_form_preserves_zero_and_saves_only_changed_night_fields(self): status, config = self.setup_values() status.update(mode="lan") config["display"] = {"mode": "button", "timeout": 15, "usb_mode": "on", "usb_timeout": 15, "night_supported": True, "night": "auto", "night_delay": 0, "night_active": False} prelude = r'''''' % (json.dumps(status), json.dumps(config)) dom = self.run_page(prelude, virtual_time=2400) for key, value in (("visible", "true"), ("enabled", "true"), ("mode", "auto"), ("delay", "0"), ("bounds", "-180,180"), ("choices", "auto,on,off"), ("keys", "display.night,display.night.delay"), ("saved", "on,-30"), ("no-reboot", "true"), ("clean", "true")): self.assertIn('data-test-night-%s="%s"' % (key, value), dom) def test_monochrome_and_headless_forms_hide_and_disable_night_controls(self): for display in (None, {"mode": "button", "timeout": 15, "usb_mode": "on", "usb_timeout": 15}): with self.subTest(display=display): status, config = self.setup_values() status.update(mode="lan") if display is None: config.pop("display", None) else: config["display"] = display prelude = r'''''' % (json.dumps(status), json.dumps(config)) dom = self.run_page(prelude) for key, value in (("hidden", "true"), ("disabled", "true"), ("dirty", "0")): self.assertIn('data-test-night-%s="%s"' % (key, value), dom) if __name__ == "__main__": unittest.main()