From 6792aafe753a41947f69346a07ef26a57401744f Mon Sep 17 00:00:00 2001 From: link2xt Date: Sun, 4 Jun 2023 19:11:29 +0000 Subject: [PATCH 1/2] fix: do not store incomplete config from increase_get_serial() `increase_get_serial()` is called when `import` CLI command is used. If the bot is not configured, previously it stored an incomplete config with only the `serial` field. Such bot config cannot be deserialized into `BotConfig` structure afterwards. The function is changed not to store the serial and always return 1 until the bot is started at least once and users get a chance to receive an update. --- src/db.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/db.rs b/src/db.rs index 254107c6..08c2f045 100644 --- a/src/db.rs +++ b/src/db.rs @@ -206,14 +206,22 @@ impl DB { Ok(res) } + /// Tries to increase update serial number. + /// + /// Returns the next serial number that should be used + /// for the next update. + /// + /// If the bot is not configured yet, always returns 1. pub async fn increase_get_serial(&self) -> anyhow::Result { - let serial = self.get_last_serial().await?; - let _serial: Option = self - .db - .update(("config", "config")) - .merge(json!({ "serial": serial + 1 })) - .await?; - Ok(serial + 1) + if let Some(config) = self.get_config().await? { + let serial = config.serial + 1; + let config = BotConfig { serial, ..config }; + self.set_config(&config).await?; + Ok(serial) + } else { + // Bot is not configured yet. + Ok(1) + } } // TODO: take string as resource id From 0d3442bc2def8701091364069999840951c94c64 Mon Sep 17 00:00:00 2001 From: link2xt Date: Sun, 4 Jun 2023 15:37:04 +0000 Subject: [PATCH 2/2] test: test that imported applications are listed in webxdc update --- tests/test_something.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/test_something.py b/tests/test_something.py index 2b4e3a9a..3aa66b34 100644 --- a/tests/test_something.py +++ b/tests/test_something.py @@ -102,7 +102,19 @@ def test_update(acfactory, storebot): def test_import(acfactory, storebot_example): """Test that import works.""" - # Do nothing except for initializing `storebot_example` fixture. - # If the test started, it means the bot - # with imported apps has been created successfuly. - pass + (ac1,) = acfactory.get_online_accounts(1) + + bot_contact = ac1.create_contact(storebot_example.addr) + bot_chat = bot_contact.create_chat() + bot_chat.send_text("hi!") + + msg_in = ac1.wait_next_incoming_message() + ac1._evtracker.get_matching("DC_EVENT_WEBXDC_STATUS_UPDATE") + assert msg_in.text == "Welcome to the appstore bot!" + + assert msg_in.is_webxdc() + status_updates = msg_in.get_status_updates() + assert len(status_updates) == 1 + payload = status_updates[0]["payload"] + app_infos = payload["app_infos"] + assert len(app_infos) == 4