Skip to content

Commit b57b133

Browse files
committed
Refactor tests for SwitchNode to enhance clarity and organization
1 parent 514c984 commit b57b133

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

crates/core/src/runtime/nodes/function_nodes/switch/mod.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl SwitchRuleOperator {
199199
(Variant::Object(a), Some(b)) => Ok(a.contains_key(b)),
200200
_ => Ok(false),
201201
},
202-
Self::Else => Ok(*a == Variant::Bool(true)),
202+
Self::Else => Ok(false),
203203
_ => Err(EdgelinkError::NotSupported("Unsupported operator".to_owned()).into()),
204204
}
205205
}
@@ -406,32 +406,41 @@ impl SwitchNode {
406406
let msg = orig_msg.read().await;
407407
let from_value = self.eval_property_value(&msg).await?;
408408
let last_property_value = from_value.clone();
409+
let mut matched = false;
409410
for (port, rule) in self.config.rules.iter().enumerate() {
410-
if rule.value_type == SwitchPropertyType::Prev {
411-
let prev_guard = self.prev_value.read().await;
412-
if prev_guard.is_null() {
411+
if rule.operator == SwitchRuleOperator::Else {
412+
// only match if no previous rule matched
413+
if !matched {
413414
envelopes.push(Envelope { port, msg: orig_msg.clone() });
415+
}
416+
// else never match
417+
} else {
418+
if rule.value_type == SwitchPropertyType::Prev {
419+
let prev_guard = self.prev_value.read().await;
420+
if prev_guard.is_null() {
421+
envelopes.push(Envelope { port, msg: orig_msg.clone() });
422+
if !self.config.check_all {
423+
break;
424+
}
425+
continue;
426+
}
427+
}
428+
if rule.value2_type == Some(SwitchPropertyType::Prev) {
429+
let prev_guard = self.prev_value.read().await;
430+
if prev_guard.is_null() {
431+
continue;
432+
}
433+
}
434+
let v1 = self.get_v1(rule, &msg).await?;
435+
let v2 = self.get_v2(rule, &msg).await?;
436+
if rule.operator.apply(&from_value, &v1, &v2, rule.case, &[])? {
437+
envelopes.push(Envelope { port, msg: orig_msg.clone() });
438+
matched = true;
414439
if !self.config.check_all {
415440
break;
416441
}
417-
continue;
418-
}
419-
}
420-
if rule.value2_type == Some(SwitchPropertyType::Prev) {
421-
let prev_guard = self.prev_value.read().await;
422-
if prev_guard.is_null() {
423-
continue;
424442
}
425443
}
426-
let v1 = self.get_v1(rule, &msg).await?;
427-
let v2 = self.get_v2(rule, &msg).await?;
428-
if rule.operator.apply(&from_value, &v1, &v2, rule.case, &[])? {
429-
envelopes.push(Envelope { port, msg: orig_msg.clone() });
430-
}
431-
432-
if !self.config.check_all {
433-
break;
434-
}
435444
}
436445
// Update prev_value
437446
if !last_property_value.is_null() {

tests/nodes/function/test_switch_node.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -791,11 +791,9 @@ async def test_it_should_handle_persistable_flow_and_global_contexts_with_jsonat
791791
"checkall": True, "outputs": 1, "wires": [["3"]]},
792792
{"id": "3", "z": "100", "type": "test-once"}
793793
]
794-
795794
injections = [
796795
{"nid": "1", "msg": {"payload": "pass"}},
797796
]
798-
799797
msgs = await run_flow_with_msgs_ntimes(flows_obj=flows, msgs=injections, nexpected=1, timeout=0.5)
800798
assert len(msgs) == 1
801799
assert msgs[0]["payload"] == "pass"
@@ -812,9 +810,8 @@ async def test_it_should_handle_env_var_expression(self):
812810
"checkall": True, "outputs": 1, "wires": [["2"]]},
813811
{"id": "2", "z": "100", "type": "test-once"}
814812
]
815-
816-
# Set environment variable (this depends on your implementation)
817-
# os.environ["VAR"] = "VAL"
813+
# Set environment variable
814+
os.environ["VAR"] = "VAL"
818815
await _custom_flow_switch_test(flow, True, "OK")
819816

820817
# Sequence handling tests (these require sequence/parts support)

0 commit comments

Comments
 (0)