Skip to content

Commit

Permalink
auto test message passing
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo committed Nov 16, 2023
1 parent f05faaa commit 6500983
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/std_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,28 @@ impl<C: CustomTypes<StdSystem<C>>> StdSystem<C> {

ws_sender_sender.send(Message::Text(json!({ "type": "set-uuid", "clientId": client_id }).to_string())).await.unwrap();

let src_id = format!("{project_name}@{client_id}#vm");
fn resolve_targets<'a>(targets: &'a mut [String], src_id: &String) -> &'a mut [String] {
for target in targets.iter_mut() {
if target == "everyone in room" {
target.clone_from(src_id);
}
}
targets
}
while let Ok(request) = out_receiver.recv() {
let msg = match request {
OutgoingMessage::Normal { msg_type, values, targets } => json!({
OutgoingMessage::Normal { msg_type, values, mut targets } => json!({
"type": "message",
"dstId": targets,
"srcId": format!("{project_name}@{client_id}"),
"dstId": resolve_targets(&mut targets, &src_id),
"srcId": src_id,
"msgType": msg_type,
"content": values.into_iter().collect::<JsonMap<_,_>>(),
}),
OutgoingMessage::Blocking { msg_type, values, targets, reply_key } => json!({
OutgoingMessage::Blocking { msg_type, values, mut targets, reply_key } => json!({
"type": "message",
"dstId": targets,
"srcId": format!("{project_name}@{client_id}"),
"dstId": resolve_targets(&mut targets, &src_id),
"srcId": src_id,
"msgType": msg_type,
"requestId": reply_key.request_id,
"content": values.into_iter().collect::<JsonMap<_,_>>(),
Expand Down
25 changes: 25 additions & 0 deletions src/test/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,31 @@ fn test_proj_broadcast_to() {
});
}

#[test]
fn test_proj_messaging() {
let system = Rc::new(StdSystem::new_sync(BASE_URL.to_owned(), None, Config::default(), Arc::new(Clock::new(UtcOffset::UTC, None))));
let proj = get_running_project(include_str!("projects/messaging.xml"), system);
proj.mutate(|mc, proj| {
run_till_term(mc, &mut *proj.proj.borrow_mut(mc)).unwrap();
let global_context = proj.proj.borrow().get_global_context();
let global_context = global_context.borrow();

let expected = Value::from_simple(mc, SimpleValue::from_json(json!([
25,
[49, 121, 169],
[49, [256, 16], 169],
[9, 4],
["2", 5, 9],
["2", 6, 24],
["2", [3, 4], [7, 8]],
["2", [2, 4], [8, 16]],
[["6", "3"], [7, 5], [11, 9]],
[["6", "3"], [6, 6], [24, 24]],
])).unwrap());
assert_values_eq(&global_context.globals.lookup("res").unwrap().get().clone(), &expected, 1e-20, "res");
});
}

#[test]
fn test_proj_any_msg() {
let system = Rc::new(StdSystem::new_sync(BASE_URL.to_owned(), None, Config::default(), Arc::new(Clock::new(UtcOffset::UTC, None))));
Expand Down
1 change: 1 addition & 0 deletions src/test/projects/messaging.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<room name="untitled (5)" app="NetsBlox 2.1.6, http://netsblox.org"><role name="myRole"><project collabStartIndex="197" name="myRole" app="NetsBlox 2.1.6, http://netsblox.org" version="2.1.6"><notes></notes><stage name="Stage" width="480" height="360" collabId="item_-1_1" costume="0" color="255,255,255,1" tempo="60" threadsafe="false" penlog="false" volume="100" pan="0" lines="round" ternary="false" hyperops="true" codify="false" inheritance="false" sublistIDs="false" scheduled="false" id="1"><costumes><list struct="atomic" id="2"></list></costumes><sounds><list struct="atomic" id="3"></list></sounds><variables></variables><blocks></blocks><messageTypes><messageType><name>message</name><fields><field>msg</field></fields></messageType><messageType><name>squarify</name><fields><field>x</field></fields></messageType><messageType><name>my pusher</name><fields><field>x</field><field>y</field><field>z</field></fields></messageType></messageTypes><scripts></scripts><sprites><sprite name="Sprite" collabId="item_-1" idx="1" x="0" y="0" heading="90" scale="1" volume="100" pan="0" rotation="1" draggable="true" costume="0" color="80,80,80,1" pen="tip" id="12"><costumes><list struct="atomic" id="13"></list></costumes><sounds><list struct="atomic" id="14"></list></sounds><blocks></blocks><variables></variables><scripts><script x="952.7783203125001" y="310.1428571428572"><block collabId="item_103" var="res"/></script><script x="25.714285714285715" y="7.142857142857143"><block collabId="item_9" s="receiveGo"></block><block collabId="item_20" s="doSetVar"><l>res</l><block collabId="item_24" s="reportNewList"><list></list></block></block><block collabId="item_25" s="doAddToList"><block collabId="item_12" s="doSocketRequest"><l>squarify</l><l>5</l><l><option>everyone in room</option></l></block><block collabId="item_27" var="res"/></block><block collabId="item_28" s="doAddToList"><block collabId="item_28_1" s="doSocketRequest"><l>squarify</l><block collabId="item_29" s="reportNewList"><list><l>7</l><l>11</l><l>13</l></list></block><l><option>everyone in room</option></l></block><block collabId="item_28_2" var="res"/></block><block collabId="item_154" s="doAddToList"><block collabId="item_154_1" s="doSocketRequest"><l>squarify</l><block collabId="item_154_3" s="reportNewList"><list><l>7</l><block collabId="item_156" s="reportNewList"><list><l>16</l><l>4</l></list></block><l>13</l></list></block><block collabId="item_160" s="getJSFromRPCStruct" inputNames=""><l>PublicRoles</l><l>getPublicRoleId</l></block></block><block collabId="item_154_2" var="res"/></block><block collabId="item_164" s="doAddToList"><block collabId="item_164_1" s="doSocketRequest"><l>squarify</l><block collabId="item_164_3" s="reportNewList"><list><l>3</l><l>2</l></list></block><block collabId="item_168" s="reportNewList"><list><block collabId="item_164_4" s="getJSFromRPCStruct" inputNames=""><l>PublicRoles</l><l>getPublicRoleId</l></block></list></block></block><block collabId="item_164_2" var="res"/></block><custom-block collabId="item_102" s="wait for %n values in %l after %cs"><l>2</l><block collabId="item_146" var="res"/><script><block collabId="item_147" s="doSocketMessage"><l>my pusher</l><l>2</l><l>3</l><l>4</l><l><option>everyone in room</option></l></block></script></custom-block><custom-block collabId="item_176" s="wait for %n values in %l after %cs"><l>2</l><block collabId="item_176_1" var="res"/><script><block collabId="item_176_2" s="doSocketMessage"><l>my pusher</l><l>2</l><block collabId="item_177" s="reportNewList"><list><l>1</l><l>2</l></list></block><l>4</l><block collabId="item_181" s="getJSFromRPCStruct" inputNames=""><l>PublicRoles</l><l>getPublicRoleId</l></block></block></script></custom-block><custom-block collabId="item_182" s="wait for %n values in %l after %cs"><l>2</l><block collabId="item_182_1" var="res"/><script><block collabId="item_182_2" s="doSocketMessage"><l>my pusher</l><block collabId="item_183" s="reportNewList"><list><l>6</l><l>3</l></list></block><block collabId="item_182_3" s="reportNewList"><list><l>1</l><l>2</l></list></block><l>4</l><block collabId="item_188" s="reportNewList"><list><block collabId="item_188_1" s="getJSFromRPCStruct" inputNames=""><l>PublicRoles</l><l>getPublicRoleId</l></block></list></block></block></script></custom-block></script><script x="25.714285714285715" y="459.14285714285717"><block collabId="item_-1_2" s="receiveSocketMessage"><l>squarify</l><l>x</l></block><block collabId="item_2" s="doSocketResponse"><block collabId="item_3" s="reportPower"><block collabId="item_5" var="x"/><l>2</l></block></block></script><script x="226.42857142857144" y="458.5714285714286"><block collabId="item_80" s="receiveSocketMessage"><l>my pusher</l><l>x</l><l>y</l><l>z</l></block><block collabId="item_84" s="doAddToList"><block collabId="item_87" s="reportNewList"><list><block collabId="item_88" var="x"/><block collabId="item_89" s="reportVariadicSum"><list><block collabId="item_90" var="x"/><block collabId="item_91" var="y"/></list></block><block collabId="item_92" s="reportVariadicSum"><list><block collabId="item_93" var="x"/><block collabId="item_94" var="y"/><block collabId="item_95" var="z"/></list></block></list></block><block collabId="item_86" var="res"/></block></script><script x="226" y="542.8809523809524"><block collabId="item_99" s="receiveSocketMessage"><l>my pusher</l><l>x</l><l>y</l><l>z</l></block><block collabId="item_99_1" s="doAddToList"><block collabId="item_99_2" s="reportNewList"><list><block collabId="item_99_4" var="x"/><block collabId="item_99_5" s="reportVariadicProduct"><list><block collabId="item_99_10" var="x"/><block collabId="item_99_11" var="y"/></list></block><block collabId="item_99_6" s="reportVariadicProduct"><list><block collabId="item_99_7" var="x"/><block collabId="item_99_8" var="y"/><block collabId="item_99_9" var="z"/></list></block></list></block><block collabId="item_99_3" var="res"/></block></script><script x="190.71428571428572" y="23.0714285714286"><block collabId="item_193" s="doSetVar"><l>res</l><block collabId="item_195" s="reportNewList"><list></list></block></block></script></scripts></sprite><watcher var="res" style="normal" x="10" y="10" color="243,118,29" extX="112" extY="98.00000000000001"/></sprites></stage><hidden></hidden><headers></headers><code></code><blocks><block-definition collabId="item_38" s="wait for %&apos;n&apos; values in %&apos;arr&apos; after %&apos;f&apos;" type="command" category="custom"><header></header><code></code><translations></translations><inputs><input type="%n"></input><input type="%l"></input><input type="%cs"></input></inputs><script><block collabId="item_139" s="doChangeVar"><l>n</l><block collabId="item_66" s="reportListAttribute"><l><option>length</option></l><block collabId="item_66_1" var="arr"/></block></block><block collabId="item_68" s="doRun"><block collabId="item_69" var="f"/><list></list></block><block collabId="item_44" s="doResetTimer"></block><block collabId="item_58" s="doUntil"><block collabId="item_48" s="reportGreaterThanOrEquals"><block collabId="item_61" s="reportListAttribute"><l><option>length</option></l><block collabId="item_62" var="arr"/></block><block collabId="item_63" var="n"/></block><script><block collabId="item_71" s="doIf"><block collabId="item_47" s="reportGreaterThan"><block collabId="item_50" s="getTimer"></block><l>3</l></block><script><block collabId="item_56" s="doThrow"><l>took too long!</l></block></script></block></script></block></script></block-definition></blocks><variables><variable name="res"><list struct="atomic" id="243"></list></variable></variables><history></history></project><media name="myRole" app="NetsBlox 2.1.6, http://netsblox.org" version="2.1.6"></media></role></room>

0 comments on commit 6500983

Please sign in to comment.