Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions pkgs/id/tests/probe_compile_reply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Probe: WorldClient::compile() reply-shape handling.
// Scripted host replies with a canned frame; the client result is printed
// per case. Expectation (siblings install_wasm/info_details/invite):
// a "compiled" reply missing or mistyping module_hash/sequence must Err,
// not silently default to ("", 0).
use id::frames::{read_frame, write_frame};
use id::world_net::{WorldClient, WORLD_ALPN};
use iroh::{
endpoint::{presets, RelayMode},
protocol::{AcceptError, ProtocolHandler, Router},
Endpoint, EndpointAddr,
};

#[derive(Debug, Clone)]
struct ScriptedHost {
reply: serde_json::Value,
}

impl ProtocolHandler for ScriptedHost {
async fn accept(
&self,
conn: iroh::endpoint::Connection,
) -> Result<(), AcceptError> {
let reply = self.reply.clone();
tokio::spawn(async move {
while let Ok((mut send, mut recv)) = conn.accept_bi().await {
let reply = reply.clone();
tokio::spawn(async move {
let _ = read_frame(&mut recv).await;
let body = serde_json::to_vec(&reply).unwrap();
let _ = write_frame(&mut send, &body).await;
let _ = send.finish();
});
}
});
Ok(())
}
}

async fn endpoint() -> Endpoint {
Endpoint::builder(presets::Minimal)
.relay_mode(RelayMode::Disabled)
.bind()
.await
.unwrap()
}

async fn run_case(reply: serde_json::Value) -> anyhow::Result<(String, u64, String)> {
let server_ep = endpoint().await;
let router = Router::builder(server_ep.clone())
.accept(WORLD_ALPN, ScriptedHost { reply })
.spawn();
let addr = EndpointAddr::from_parts(
server_ep.id(),
server_ep.bound_sockets().iter().map(|a| {
let ip = if a.ip().is_unspecified() {
std::net::Ipv4Addr::LOCALHOST.into()
} else {
a.ip()
};
iroh::TransportAddr::Ip(std::net::SocketAddr::new(ip, a.port()))
}),
);
let client_ep = endpoint().await;
let mut client = WorldClient::connect(&client_ep, addr).await.unwrap();
let result = client
.compile(
"admin",
vec![("main.roc".to_owned(), "x = 1".to_owned())],
7,
false,
)
.await;
client.close();
client_ep.close().await;
router.shutdown().await.unwrap();
result
}

#[tokio::test]
async fn probe_compile_reply_shapes() {
let control = run_case(serde_json::json!({
"type": "compiled", "module_hash": "abc123", "sequence": 7, "diagnostics": "warn"
}))
.await;
println!("CONTROL full reply -> {control:?}");
assert_eq!(
control.unwrap(),
("abc123".to_owned(), 7, "warn".to_owned()),
"control: a well-formed compiled reply returns its fields"
);

let missing_hash = run_case(serde_json::json!({
"type": "compiled", "sequence": 7, "diagnostics": ""
}))
.await;
println!("CASE missing module_hash -> {missing_hash:?}");

let mistyped = run_case(serde_json::json!({
"type": "compiled", "module_hash": 123, "sequence": "7", "diagnostics": 42
}))
.await;
println!("CASE mistyped fields -> {mistyped:?}");

let bare = run_case(serde_json::json!({"type": "compiled"})).await;
println!("CASE bare compiled -> {bare:?}");
}
66 changes: 66 additions & 0 deletions pkgs/id/tests/probe_info_reply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Probe: WorldClient::info_details() active_module_hash shape handling.
use id::frames::{read_frame, write_frame};
use id::world_net::{WorldClient, WorldModuleInfo, WORLD_ALPN};
use iroh::{
endpoint::{presets, RelayMode},
protocol::{AcceptError, ProtocolHandler, Router},
Endpoint, EndpointAddr,
};

#[derive(Debug, Clone)]
struct ScriptedHost { reply: serde_json::Value }

impl ProtocolHandler for ScriptedHost {
async fn accept(&self, conn: iroh::endpoint::Connection) -> Result<(), AcceptError> {
let reply = self.reply.clone();
tokio::spawn(async move {
while let Ok((mut send, mut recv)) = conn.accept_bi().await {
let reply = reply.clone();
tokio::spawn(async move {
let _ = read_frame(&mut recv).await;
let body = serde_json::to_vec(&reply).unwrap();
let _ = write_frame(&mut send, &body).await;
let _ = send.finish();
});
}
});
Ok(())
}
}

async fn endpoint() -> Endpoint {
Endpoint::builder(presets::Minimal).relay_mode(RelayMode::Disabled).bind().await.unwrap()
}

async fn run_case(reply: serde_json::Value) -> anyhow::Result<WorldModuleInfo> {
let server_ep = endpoint().await;
let router = Router::builder(server_ep.clone()).accept(WORLD_ALPN, ScriptedHost { reply }).spawn();
let addr = EndpointAddr::from_parts(server_ep.id(), server_ep.bound_sockets().iter().map(|a| {
let ip = if a.ip().is_unspecified() { std::net::Ipv4Addr::LOCALHOST.into() } else { a.ip() };
iroh::TransportAddr::Ip(std::net::SocketAddr::new(ip, a.port()))
}));
let client_ep = endpoint().await;
let mut client = WorldClient::connect(&client_ep, addr).await.unwrap();
let result = client.info_details("cap").await;
client.close(); client_ep.close().await; router.shutdown().await.unwrap();
result
}

#[tokio::test]
async fn probe_info_reply_shapes() {
let control = run_case(serde_json::json!({"type":"module_info","world_id":"w1","active_module_hash":"abc123","current_sequence":7})).await;
println!("CONTROL string hash -> {control:?}");
assert_eq!(control.unwrap().active_module_hash.as_deref(), Some("abc123"));

let legit_none = run_case(serde_json::json!({"type":"module_info","world_id":"w1","active_module_hash":null,"current_sequence":7})).await;
println!("CONTROL null hash -> {legit_none:?}");
assert_eq!(legit_none.unwrap().active_module_hash, None);

for (name, v) in [("number", serde_json::json!(123)), ("bool", serde_json::json!(true)), ("array", serde_json::json!(["abc"])), ("object", serde_json::json!({"h":"abc"}))] {
let r = run_case(serde_json::json!({"type":"module_info","world_id":"w1","active_module_hash":v,"current_sequence":7})).await;
println!("CASE mistyped {name} -> {r:?}");
}
// oracle contrast: mistyped provenance in same reply IS refused
let prov = run_case(serde_json::json!({"type":"module_info","world_id":"w1","active_module_hash":"abc","current_sequence":7,"active_module_provenance":123})).await;
println!("ORACLE mistyped provenance -> {prov:?}");
}
99 changes: 99 additions & 0 deletions pkgs/id/tests/probe_worlds_reply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Probe: WorldClient::list_worlds reply-entry handling.
// A "worlds" reply whose entries are malformed: are they refused, or
// silently dropped / defaulted while the reply as a whole succeeds?
use id::frames::{read_frame, write_frame};
use id::world_net::{WorldClient, WORLD_ALPN};
use iroh::{
endpoint::{presets, RelayMode},
protocol::{AcceptError, ProtocolHandler, Router},
Endpoint, EndpointAddr,
};

#[derive(Debug, Clone)]
struct ScriptedHost {
reply: serde_json::Value,
}

impl ProtocolHandler for ScriptedHost {
async fn accept(&self, conn: iroh::endpoint::Connection) -> Result<(), AcceptError> {
let reply = self.reply.clone();
tokio::spawn(async move {
while let Ok((mut send, mut recv)) = conn.accept_bi().await {
let reply = reply.clone();
tokio::spawn(async move {
let _ = read_frame(&mut recv).await;
let body = serde_json::to_vec(&reply).unwrap();
let _ = write_frame(&mut send, &body).await;
let _ = send.finish();
});
}
});
Ok(())
}
}

async fn endpoint() -> Endpoint {
Endpoint::builder(presets::Minimal)
.relay_mode(RelayMode::Disabled)
.bind()
.await
.unwrap()
}

async fn run_case(reply: serde_json::Value) -> anyhow::Result<(String, Vec<(String, bool, usize)>)> {
let server_ep = endpoint().await;
let router = Router::builder(server_ep.clone())
.accept(WORLD_ALPN, ScriptedHost { reply })
.spawn();
let addr = EndpointAddr::from_parts(
server_ep.id(),
server_ep.bound_sockets().iter().map(|a| {
let ip = if a.ip().is_unspecified() {
std::net::Ipv4Addr::LOCALHOST.into()
} else {
a.ip()
};
iroh::TransportAddr::Ip(std::net::SocketAddr::new(ip, a.port()))
}),
);
let client_ep = endpoint().await;
let mut client = WorldClient::connect(&client_ep, addr).await.unwrap();
let result = client.list_worlds("admin").await;
client.close();
client_ep.close().await;
router.shutdown().await.unwrap();
result
}

#[tokio::test]
async fn probe_worlds_reply_shapes() {
let control = run_case(serde_json::json!({
"type": "worlds", "default": "lobby",
"worlds": [
{"name": "lobby", "open": true, "presence": 3},
{"name": "arena", "open": false, "presence": 0}
]
}))
.await;
println!("CONTROL two valid worlds -> {control:?}");
assert_eq!(control.unwrap().1.len(), 2, "control: both valid worlds listed");

let mixed = run_case(serde_json::json!({
"type": "worlds", "default": "lobby",
"worlds": [
{"name": "lobby", "open": true, "presence": 3},
{"name": 123, "open": true, "presence": 1},
{"name": "ghost", "open": "yes", "presence": 2},
{"name": "arena", "open": false, "presence": "many"},
"not-an-object"
]
}))
.await;
println!("CASE mixed malformed entries -> {mixed:?}");

let no_list = run_case(serde_json::json!({
"type": "worlds", "default": "lobby", "worlds": {"name": "lobby"}
}))
.await;
println!("CASE worlds-not-a-list -> {no_list:?}");
}
Loading