diff --git a/crates/particle-node-tests/tests/spells.rs b/crates/particle-node-tests/tests/spells.rs index 6c09d6937a..c88bd6cc22 100644 --- a/crates/particle-node-tests/tests/spells.rs +++ b/crates/particle-node-tests/tests/spells.rs @@ -1634,3 +1634,90 @@ async fn create_remove_worker() { panic!("expected one string result") } } + +#[tokio::test] +async fn spell_update_trigger_by_alias() { + let swarms = make_swarms(1).await; + let mut client = ConnectedClient::connect_with_keypair( + swarms[0].multiaddr.clone(), + Some(swarms[0].management_keypair.clone()), + ) + .await + .wrap_err("connect client") + .unwrap(); + + let script = format!( + r#" + (seq + (seq + (call %init_peer_id% ("getDataSrv" "spell_id") [] spell_id) + (call %init_peer_id% (spell_id "get_u32") ["counter"] counter) + ) + (call "{}" ("return" "") [spell_id]) + ) + "#, + client.peer_id + ); + + let mut config = TriggerConfig::default(); + config.connections.connect = true; + let (spell_id, worker) = create_spell(&mut client, &script, config, json!({})).await; + + let mut new_config = TriggerConfig::default(); + new_config.connections.connect = true; + new_config.connections.disconnect = true; + + let id = client.send_particle( + r#"(seq + (seq + (call relay ("op" "noop") []) + (call worker ("srv" "add_alias") ["alias" spell_id]) + ) + (seq + (call worker ("spell" "update_trigger_config") ["alias" config]) + (call %init_peer_id% ("return" "") ["ok"]) + ) + )"#, + hashmap! { + "relay" => json!(client.node.to_string()), + "worker" => json!(worker), + "spell_id" => json!(spell_id.clone()), + "config" => json!(new_config) + }, + ); + + client.wait_particle_args(id).await.unwrap(); + + // This connect should trigger the spell + let connect_num = 3; + for _ in 0..connect_num { + ConnectedClient::connect_to(swarms[0].multiaddr.clone()) + .await + .unwrap(); + } + + let mut trigger_counter = 0; + + // we must receive `connect_num` messages on connect and `connect_num` messages + // on disconnect, so 2 * `connect_num` messages in total + for _ in 0..2 * connect_num { + if let [spell_reply] = client + .receive_args() + .await + .wrap_err("receive") + .unwrap() + .as_slice() + { + let given_spell_id = spell_reply.as_str().unwrap(); + assert_eq!(spell_id, given_spell_id); + + trigger_counter += 1; + } + } + + assert_eq!( + trigger_counter, + connect_num * 2, + "spell must be triggered {connect_num} * 2 times" + ); +} diff --git a/sorcerer/src/spells.rs b/sorcerer/src/spells.rs index dcd1e0aa9d..7986a4da79 100644 --- a/sorcerer/src/spells.rs +++ b/sorcerer/src/spells.rs @@ -208,7 +208,7 @@ pub(crate) async fn spell_update_config( key_manager: KeyManager, ) -> Result<(), JError> { let mut args = args.function_args.into_iter(); - let spell_id: String = Args::next("spell_id", &mut args)?; + let spell_id_or_alias: String = Args::next("spell_id", &mut args)?; let worker_id = params.host_id; let init_peer_id = params.init_peer_id; @@ -220,10 +220,12 @@ pub(crate) async fn spell_update_config( if !is_worker_creator && !is_worker && !is_management { return Err(JError::new(format!( - "Failed to update spell config {spell_id}, spell config can be updated by worker creator {worker_creator}, worker itself {worker_id} or peer manager; init_peer_id={init_peer_id}" + "Failed to update spell config {spell_id_or_alias}, spell config can be updated by worker creator {worker_creator}, worker itself {worker_id} or peer manager; init_peer_id={init_peer_id}" ))); } + let spell_id = services.to_service_id(worker_id, spell_id_or_alias.clone())?; + let user_config: TriggerConfig = Args::next("config", &mut args)?; let config = api::from_user_config(user_config.clone())?; @@ -253,9 +255,11 @@ pub(crate) async fn spell_update_config( }; if let Err(err) = result { - log::warn!("can't update a spell {spell_id} config via spell-event-bus-api: {err}"); + log::warn!( + "can't update a spell {spell_id_or_alias} config via spell-event-bus-api: {err}" + ); return Err(JError::new(format!( - "can't update a spell {spell_id} config due to an internal error while updating the triggers: {err}" + "can't update a spell {spell_id_or_alias} config due to an internal error while updating the triggers: {err}" ))); }