diff --git a/crates/wasm-actor/tests/wasm_actor.rs b/crates/wasm-actor/tests/wasm_actor.rs index c4556e0..c4f37b6 100644 --- a/crates/wasm-actor/tests/wasm_actor.rs +++ b/crates/wasm-actor/tests/wasm_actor.rs @@ -11738,4 +11738,120 @@ proptest! { } } } +} + +// ── Cycle 82 ───────────────────────────────────────────────────────────────── + +// Guest computes max of all bytes in payload +#[test] +fn guest_computes_max_byte() { + let wat = r#"(module + (memory (export "memory") 1) + (func (export "alloc") (param $len i32) (result i32) i32.const 1024) + (func (export "handle") (param $ptr i32) (param $len i32) + (local $i i32) + (local $max i32) + (local $val i32) + (local.set $max (i32.const 0)) + (local.set $i (i32.const 0)) + (block $exit + (loop $loop + (br_if $exit (i32.ge_u (local.get $i) (local.get $len))) + (local.set $val (i32.load8_u (i32.add (local.get $ptr) (local.get $i)))) + (if (i32.gt_u (local.get $val) (local.get $max)) + (then (local.set $max (local.get $val))) + ) + (local.set $i (i32.add (local.get $i) (i32.const 1))) + (br $loop) + ) + ) + (i32.store (i32.const 0) (local.get $max)) + ) + )"#; + let engine = SharedEngine::new().unwrap(); + let actor = WasmActorBuilder::new(engine, wat::parse_str(wat).unwrap()) + .build().unwrap(); + let rt = Runtime::new(RuntimeConfig::default()); + let addr = rt.spawn(actor).unwrap(); + rt.send_to(addr, ByteMessage(vec![10, 250, 30, 100])).unwrap(); + rt.tick(); +} + +// Echo actor handles message containing its own address bytes +#[test] +fn echo_message_containing_own_address() { + let engine = SharedEngine::new().unwrap(); + let actor = WasmActorBuilder::new(engine, guest_wasm("echo")).build().unwrap(); + let rt = Runtime::new(RuntimeConfig::default()); + let inbox = rt.new_inbox::().unwrap(); + let addr = rt.spawn(actor).unwrap(); + + // Payload contains the actor's own address bytes + rt.send_to(addr, framed_msg(inbox.addr(), &addr.0)).unwrap(); + rt.tick(); + + let resp = inbox.try_recv().expect("echo response"); + assert_eq!(resp.0, addr.0.to_vec(), "actor's address echoed as payload"); +} + +// Rapidly create and destroy engines +#[test] +fn rapid_engine_creation_destruction() { + for _ in 0..50 { + let engine = SharedEngine::new().unwrap(); + let _ = WasmActorBuilder::new(engine, guest_wasm("echo")).build().unwrap(); + // engine and actor dropped here + } +} + +// Guest uses select instruction to pick between two values +#[test] +fn guest_select_conditional_value() { + let wat = r#"(module + (memory (export "memory") 1) + (func (export "alloc") (param $len i32) (result i32) i32.const 1024) + (func (export "handle") (param $ptr i32) (param $len i32) + ;; select: if len > 10, store 999, else store 111 + (i32.store (local.get $ptr) + (select + (i32.const 999) + (i32.const 111) + (i32.gt_u (local.get $len) (i32.const 10)) + ) + ) + ) + )"#; + let engine = SharedEngine::new().unwrap(); + let actor = WasmActorBuilder::new(engine, wat::parse_str(wat).unwrap()) + .build().unwrap(); + let rt = Runtime::new(RuntimeConfig::default()); + let addr = rt.spawn(actor).unwrap(); + rt.send_to(addr, ByteMessage(vec![0u8; 5])).unwrap(); + rt.send_to(addr, ByteMessage(vec![0u8; 20])).unwrap(); + rt.tick(); +} + +// Echo handles exactly 1 byte — smallest meaningful payload +#[test] +fn echo_single_byte_payload_integrity() { + let engine = SharedEngine::new().unwrap(); + let actor = WasmActorBuilder::new(engine, guest_wasm("echo")).build().unwrap(); + let rt = Runtime::new(RuntimeConfig::default()); + let inbox = rt.new_inbox::().unwrap(); + let addr = rt.spawn(actor).unwrap(); + + for byte in 0u8..=255 { + rt.send_to(addr, framed_msg(inbox.addr(), &[byte])).unwrap(); + } + // Process all 256 in batches + for _ in 0..10 { + rt.tick(); + } + + let mut responses: Vec = std::iter::from_fn(|| inbox.try_recv().map(|m| m.0[0])).collect(); + responses.sort(); + assert_eq!(responses.len(), 256); + for (i, &b) in responses.iter().enumerate() { + assert_eq!(b, i as u8); + } } \ No newline at end of file