diff --git a/crates/wasm-actor/tests/wasm_actor.rs b/crates/wasm-actor/tests/wasm_actor.rs index 947f84c..8b49808 100644 --- a/crates/wasm-actor/tests/wasm_actor.rs +++ b/crates/wasm-actor/tests/wasm_actor.rs @@ -10749,4 +10749,130 @@ fn thousand_actors_from_same_engine() { rt.spawn(actor).unwrap(); } rt.tick(); +} + +// ── Cycle 74 ───────────────────────────────────────────────────────────────── + +// Guest accumulates state across messages — counter tracks how many messages received +#[test] +fn guest_counts_messages_via_global() { + let wat = r#"(module + (import "swactor" "send" (func $send (param i32 i32 i32))) + (memory (export "memory") 1) + (global $count (mut i32) (i32.const 0)) + (func (export "alloc") (param $len i32) (result i32) i32.const 1024) + (func (export "handle") (param $ptr i32) (param $len i32) + ;; Increment counter + (global.set $count (i32.add (global.get $count) (i32.const 1))) + ;; If we have dest (>= 32 bytes), send counter value as 4-byte payload + (if (i32.ge_u (local.get $len) (i32.const 32)) + (then + ;; Store counter value at offset 900 + (i32.store (i32.const 900) (global.get $count)) + (call $send (local.get $ptr) (i32.const 900) (i32.const 4)) + ) + ) + ) + )"#; + let engine = SharedEngine::new().unwrap(); + let actor = WasmActorBuilder::new(engine, wat::parse_str(wat).unwrap()) + .build().unwrap(); + let rt = Runtime::new(RuntimeConfig::default()); + let inbox = rt.new_inbox::().unwrap(); + let addr = rt.spawn(actor).unwrap(); + + // Send 5 messages across 5 ticks + for _ in 0..5 { + let mut msg = Vec::new(); + msg.extend_from_slice(&inbox.addr().0); + rt.send_to(addr, ByteMessage(msg)).unwrap(); + rt.tick(); + } + + // Collect all responses — each has a 4-byte LE counter + let msgs: Vec> = std::iter::from_fn(|| inbox.try_recv().map(|m| m.0)).collect(); + assert_eq!(msgs.len(), 5); + for (i, msg) in msgs.iter().enumerate() { + let count = u32::from_le_bytes([msg[0], msg[1], msg[2], msg[3]]); + assert_eq!(count, (i + 1) as u32, "counter should increment"); + } +} + +// Echo actor with DropOldest mailbox policy +#[test] +fn echo_with_drop_oldest_mailbox() { + use swactor::runtime::MailboxOverflow; + let engine = SharedEngine::new().unwrap(); + let actor = WasmActorBuilder::new(engine, guest_wasm("echo")).build().unwrap(); + let mut cfg = RuntimeConfig::default(); + cfg.default_mailbox_capacity = 3; + cfg.mailbox_overflow = MailboxOverflow::DropOldest; + let rt = Runtime::new(cfg); + let inbox = rt.new_inbox::().unwrap(); + let addr = rt.spawn(actor).unwrap(); + + // Send 10 messages — only capacity messages retained + for i in 0u8..10 { + rt.send_to(addr, framed_msg(inbox.addr(), &[i])).unwrap(); + } + rt.tick(); + + let msgs: Vec> = std::iter::from_fn(|| inbox.try_recv().map(|m| m.0)).collect(); + // With DropOldest(3), only the 3 most recent should survive + assert!(msgs.len() <= 10, "got {}", msgs.len()); + assert!(msgs.len() >= 1, "at least some messages processed"); +} + +// Guest echoes only if len is odd — conditional response +#[test] +fn guest_echoes_only_odd_length_messages() { + let wat = r#"(module + (import "swactor" "send" (func $send (param i32 i32 i32))) + (memory (export "memory") 1) + (func (export "alloc") (param $len i32) (result i32) i32.const 1024) + (func (export "handle") (param $ptr i32) (param $len i32) + ;; Only echo if payload (len-32) is odd length + (if (i32.and + (i32.ge_u (local.get $len) (i32.const 33)) + (i32.and (i32.sub (local.get $len) (i32.const 32)) (i32.const 1))) + (then + (call $send + (local.get $ptr) + (i32.add (local.get $ptr) (i32.const 32)) + (i32.sub (local.get $len) (i32.const 32)) + ) + ) + ) + ) + )"#; + let engine = SharedEngine::new().unwrap(); + let actor = WasmActorBuilder::new(engine, wat::parse_str(wat).unwrap()) + .build().unwrap(); + let rt = Runtime::new(RuntimeConfig::default()); + let inbox = rt.new_inbox::().unwrap(); + let addr = rt.spawn(actor).unwrap(); + + // Odd payload (3 bytes) — should echo + rt.send_to(addr, framed_msg(inbox.addr(), b"abc")).unwrap(); + // Even payload (2 bytes) — should not echo + rt.send_to(addr, framed_msg(inbox.addr(), b"ab")).unwrap(); + // Odd payload (1 byte) — should echo + rt.send_to(addr, framed_msg(inbox.addr(), b"x")).unwrap(); + rt.tick(); + + let msgs: Vec> = std::iter::from_fn(|| inbox.try_recv().map(|m| m.0)).collect(); + assert_eq!(msgs.len(), 2, "only odd-length payloads echoed"); + assert_eq!(msgs[0], b"abc"); + assert_eq!(msgs[1], b"x"); +} + +// Build error Display formatting includes useful info +#[test] +fn build_error_display_contains_export_name() { + let wat = r#"(module (memory (export "memory") 1))"#; + let engine = SharedEngine::new().unwrap(); + let err = WasmActorBuilder::new(engine, wat::parse_str(wat).unwrap()) + .build().err().expect("should fail"); + let msg = format!("{err}"); + assert!(msg.contains("alloc"), "error should mention missing 'alloc' export: {msg}"); } \ No newline at end of file