kiran 1.0.0

Kiran — AI-native game engine for AGNOS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
//! Scripting integration via kavach
//!
//! Provides sandboxed WASM script execution for game logic:
//! - [`ScriptEngine`] resource managing kavach sandbox lifecycle
//! - [`Script`] component attaching a script to an entity
//! - [`ScriptMessage`] for passing data between scripts and the engine

use serde::{Deserialize, Serialize};

use crate::world::{Entity, World};

/// Maximum number of pending script messages per buffer (inbox/outbox).
/// Prevents unbounded memory growth from runaway scripts.
const MAX_SCRIPT_MESSAGES: usize = 1024;

// ---------------------------------------------------------------------------
// Script component
// ---------------------------------------------------------------------------

/// A script attached to an entity.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Script {
    /// Path to the WASM script file.
    pub source: String,
    /// Whether this script is enabled.
    pub enabled: bool,
    /// Script-local state as JSON string (round-tripped to WASM).
    #[serde(default)]
    pub state: String,
}

impl Script {
    /// Create a new enabled script from a source path.
    pub fn new(source: impl Into<String>) -> Self {
        Self {
            source: source.into(),
            enabled: true,
            state: String::new(),
        }
    }

    /// Mark this script as disabled.
    pub fn disabled(mut self) -> Self {
        self.enabled = false;
        self
    }

    /// Set initial script-local state.
    pub fn with_state(mut self, state: impl Into<String>) -> Self {
        self.state = state.into();
        self
    }
}

// ---------------------------------------------------------------------------
// Script messages
// ---------------------------------------------------------------------------

/// A message passed between the engine and a script.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ScriptMessage {
    /// Source entity that sent the message.
    pub sender: Option<u64>,
    /// Target entity (None = broadcast).
    pub target: Option<u64>,
    /// Message type identifier.
    pub kind: String,
    /// Payload as JSON string.
    pub payload: String,
}

impl ScriptMessage {
    /// Create a broadcast message with the given kind and payload.
    pub fn new(kind: impl Into<String>, payload: impl Into<String>) -> Self {
        Self {
            sender: None,
            target: None,
            kind: kind.into(),
            payload: payload.into(),
        }
    }

    /// Set the sender entity.
    pub fn from_entity(mut self, entity: Entity) -> Self {
        self.sender = Some(entity.id());
        self
    }

    /// Set the target entity.
    pub fn to_entity(mut self, entity: Entity) -> Self {
        self.target = Some(entity.id());
        self
    }
}

// ---------------------------------------------------------------------------
// Script engine resource
// ---------------------------------------------------------------------------

/// Script execution configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScriptConfig {
    /// Maximum execution time per script call (ms).
    pub timeout_ms: u64,
    /// Maximum memory per script (bytes).
    pub max_memory: usize,
}

impl Default for ScriptConfig {
    fn default() -> Self {
        Self {
            timeout_ms: 16,               // ~1 frame at 60fps
            max_memory: 16 * 1024 * 1024, // 16 MB
        }
    }
}

/// The script engine resource — manages kavach sandboxes for WASM execution.
pub struct ScriptEngine {
    config: ScriptConfig,
    /// Pending outbound messages from scripts.
    outbox: Vec<ScriptMessage>,
    /// Pending inbound messages to scripts.
    inbox: Vec<ScriptMessage>,
    /// Number of scripts executed this frame.
    exec_count: u64,
    /// Kavach WASM backend (if scripting feature enabled).
    #[cfg(feature = "scripting")]
    wasm: Option<kavach::backend::wasm::WasmBackend>,
    /// Reusable tokio runtime for async kavach calls (created once).
    #[cfg(feature = "scripting")]
    rt: Option<tokio::runtime::Runtime>,
}

impl ScriptEngine {
    /// Create a script engine with the given configuration.
    pub fn new(config: ScriptConfig) -> Self {
        #[cfg(feature = "scripting")]
        let wasm = {
            // NOTE: config.max_memory is not enforced here — kavach's
            // SandboxConfigBuilder does not yet expose a memory-limit setter.
            // Track upstream: https://github.com/AgnOS-AI/kavach/issues
            let sandbox_config = kavach::SandboxConfig::builder()
                .backend(kavach::Backend::Wasm)
                .timeout_ms(config.timeout_ms)
                .build();
            kavach::backend::wasm::WasmBackend::new(&sandbox_config).ok()
        };

        #[cfg(feature = "scripting")]
        let rt = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .ok();

        Self {
            config,
            outbox: Vec::new(),
            inbox: Vec::new(),
            exec_count: 0,
            #[cfg(feature = "scripting")]
            wasm,
            #[cfg(feature = "scripting")]
            rt,
        }
    }

    /// Check if the WASM backend is available.
    pub fn wasm_available(&self) -> bool {
        #[cfg(feature = "scripting")]
        {
            self.wasm.is_some()
        }
        #[cfg(not(feature = "scripting"))]
        {
            false
        }
    }

    /// Execute a WASM file and return the result.
    ///
    /// Returns `None` if scripting feature is disabled, WASM backend unavailable,
    /// or the path escapes the current working directory.
    #[cfg(feature = "scripting")]
    pub fn exec_wasm(&self, wasm_path: &str) -> Option<kavach::ExecResult> {
        use kavach::backend::SandboxBackend;
        use std::path::Path;

        // Validate the path doesn't escape the working directory
        let root = std::env::current_dir().ok()?;
        let canonical = Path::new(wasm_path).canonicalize().ok()?;
        if !canonical.starts_with(&root) {
            tracing::warn!(
                path = %wasm_path,
                root = %root.display(),
                "wasm path escapes working directory — rejecting",
            );
            return None;
        }

        let backend = self.wasm.as_ref()?;
        let rt = self.rt.as_ref()?;
        let policy = kavach::SandboxPolicy::minimal();

        rt.block_on(backend.exec(wasm_path, &policy)).ok()
    }

    /// Send a message to a script.
    ///
    /// Drops the message if the inbox has reached `MAX_SCRIPT_MESSAGES`.
    pub fn send(&mut self, msg: ScriptMessage) {
        if self.inbox.len() >= MAX_SCRIPT_MESSAGES {
            tracing::warn!(
                limit = MAX_SCRIPT_MESSAGES,
                "script inbox full — dropping message kind={}",
                msg.kind,
            );
            return;
        }
        self.inbox.push(msg);
    }

    /// Drain outbound messages from scripts.
    pub fn drain_outbox(&mut self) -> Vec<ScriptMessage> {
        std::mem::take(&mut self.outbox)
    }

    /// Drain inbound messages (consumed by script execution).
    pub fn drain_inbox(&mut self) -> Vec<ScriptMessage> {
        std::mem::take(&mut self.inbox)
    }

    /// Number of scripts executed this frame.
    pub fn exec_count(&self) -> u64 {
        self.exec_count
    }

    /// Reset per-frame counters.
    pub fn begin_frame(&mut self) {
        self.exec_count = 0;
    }

    /// Get the script configuration.
    pub fn config(&self) -> &ScriptConfig {
        &self.config
    }

    /// Record a script execution (called by the script runner system).
    pub fn record_exec(&mut self) {
        self.exec_count += 1;
    }

    /// Push a message to the outbox (from script execution).
    ///
    /// Drops the message if the outbox has reached `MAX_SCRIPT_MESSAGES`.
    pub fn push_outbox(&mut self, msg: ScriptMessage) {
        if self.outbox.len() >= MAX_SCRIPT_MESSAGES {
            tracing::warn!(
                limit = MAX_SCRIPT_MESSAGES,
                "script outbox full — dropping message kind={}",
                msg.kind,
            );
            return;
        }
        self.outbox.push(msg);
    }
}

impl Default for ScriptEngine {
    fn default() -> Self {
        Self::new(ScriptConfig::default())
    }
}

/// Collect all entities with enabled scripts and run them.
///
/// With `scripting` feature: executes WASM modules via kavach for entities
/// that have a `.wasm` source file. Falls back to message-based state update
/// for entities without WASM files.
///
/// Without `scripting` feature: delivers messages to script state directly.
pub fn run_scripts(world: &mut World) {
    let Some(engine) = world.get_resource_mut::<ScriptEngine>() else {
        return;
    };
    engine.begin_frame();
    let messages = engine.drain_inbox();

    // Process inbound messages — update script state
    for msg in &messages {
        let Some(target_id) = msg.target else {
            continue;
        };
        let target = Entity::from_id(target_id);

        // Read script info without holding a mutable borrow
        let script_info = world
            .get_component::<Script>(target)
            .map(|s| (s.enabled, s.source.ends_with(".wasm"), s.source.clone()));
        #[allow(unused_variables)]
        let Some((enabled, is_wasm, source)) = script_info else {
            continue;
        };
        if !enabled {
            continue;
        }

        // Try WASM execution for .wasm sources
        #[cfg(feature = "scripting")]
        if is_wasm {
            let wasm_result = world
                .get_resource::<ScriptEngine>()
                .and_then(|e| e.exec_wasm(&source));
            if let Some(result) = wasm_result {
                if let Some(script) = world.get_component_mut::<Script>(target)
                    && !result.stdout.is_empty()
                {
                    script.state = result.stdout;
                }
                if let Some(engine) = world.get_resource_mut::<ScriptEngine>() {
                    engine.record_exec();
                }
                continue;
            }
        }

        // Fallback: store message payload as script state
        if let Some(script) = world.get_component_mut::<Script>(target) {
            script.state = msg.payload.clone();
        }
        if let Some(engine) = world.get_resource_mut::<ScriptEngine>() {
            engine.record_exec();
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn script_builder() {
        let s = Script::new("scripts/player.wasm").with_state(r#"{"hp": 100}"#);
        assert_eq!(s.source, "scripts/player.wasm");
        assert!(s.enabled);
        assert_eq!(s.state, r#"{"hp": 100}"#);
    }

    #[test]
    fn script_disabled() {
        let s = Script::new("test.wasm").disabled();
        assert!(!s.enabled);
    }

    #[test]
    fn script_message() {
        let msg = ScriptMessage::new("damage", r#"{"amount": 50}"#);
        assert_eq!(msg.kind, "damage");
        assert!(msg.sender.is_none());
        assert!(msg.target.is_none());
    }

    #[test]
    fn script_message_routing() {
        let sender = Entity::new(1, 0);
        let target = Entity::new(2, 0);
        let msg = ScriptMessage::new("heal", r#"{"amount": 25}"#)
            .from_entity(sender)
            .to_entity(target);
        assert_eq!(msg.sender, Some(sender.id()));
        assert_eq!(msg.target, Some(target.id()));
    }

    #[test]
    fn script_message_serde() {
        let msg = ScriptMessage::new("test", "{}");
        let json = serde_json::to_string(&msg).unwrap();
        let decoded: ScriptMessage = serde_json::from_str(&json).unwrap();
        assert_eq!(msg, decoded);
    }

    #[test]
    fn script_config_default() {
        let cfg = ScriptConfig::default();
        assert_eq!(cfg.timeout_ms, 16);
        assert_eq!(cfg.max_memory, 16 * 1024 * 1024);
    }

    #[test]
    fn script_engine_messaging() {
        let mut engine = ScriptEngine::default();

        engine.send(ScriptMessage::new("init", "{}"));
        engine.send(ScriptMessage::new("update", r#"{"dt": 0.016}"#));

        let inbox = engine.drain_inbox();
        assert_eq!(inbox.len(), 2);
        assert_eq!(inbox[0].kind, "init");

        // Inbox is now empty
        assert!(engine.drain_inbox().is_empty());
    }

    #[test]
    fn script_engine_outbox() {
        let mut engine = ScriptEngine::default();
        engine.push_outbox(ScriptMessage::new("spawn", r#"{"entity": "bullet"}"#));

        let outbox = engine.drain_outbox();
        assert_eq!(outbox.len(), 1);
        assert!(engine.drain_outbox().is_empty());
    }

    #[test]
    fn script_engine_frame_counter() {
        let mut engine = ScriptEngine::default();
        engine.begin_frame();
        assert_eq!(engine.exec_count(), 0);

        engine.record_exec();
        engine.record_exec();
        assert_eq!(engine.exec_count(), 2);

        engine.begin_frame();
        assert_eq!(engine.exec_count(), 0);
    }

    #[test]
    fn script_as_component() {
        let mut world = World::new();
        let e = world.spawn();
        world
            .insert_component(e, Script::new("ai/npc.wasm"))
            .unwrap();

        assert!(world.has_component::<Script>(e));
        let s = world.get_component::<Script>(e).unwrap();
        assert_eq!(s.source, "ai/npc.wasm");
    }

    #[test]
    fn run_scripts_delivers_messages() {
        let mut world = World::new();
        let mut engine = ScriptEngine::default();

        let entity = world.spawn();
        world
            .insert_component(entity, Script::new("test.wasm").with_state("initial"))
            .unwrap();

        // Send message to entity
        engine.send(ScriptMessage::new("update", "new_state").to_entity(entity));

        world.insert_resource(engine);
        run_scripts(&mut world);

        let script = world.get_component::<Script>(entity).unwrap();
        assert_eq!(script.state, "new_state");
    }

    #[test]
    fn run_scripts_increments_exec_count() {
        let mut world = World::new();
        let mut engine = ScriptEngine::default();

        let entity = world.spawn();
        world
            .insert_component(entity, Script::new("test.wasm"))
            .unwrap();

        engine.send(ScriptMessage::new("update", "data").to_entity(entity));

        world.insert_resource(engine);
        run_scripts(&mut world);

        let engine = world.get_resource::<ScriptEngine>().unwrap();
        assert_eq!(engine.exec_count(), 1);
    }

    #[test]
    fn script_serde_roundtrip() {
        let script = Script::new("game.wasm").with_state(r#"{"level": 5}"#);
        let json = serde_json::to_string(&script).unwrap();
        let decoded: Script = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.source, "game.wasm");
        assert_eq!(decoded.state, r#"{"level": 5}"#);
        assert!(decoded.enabled);
    }

    #[test]
    fn broadcast_message_not_delivered() {
        // Broadcast (target=None) is not delivered to specific entities
        let mut world = World::new();
        let mut engine = ScriptEngine::default();

        let entity = world.spawn();
        world
            .insert_component(entity, Script::new("test.wasm").with_state("original"))
            .unwrap();

        // No target — broadcast
        engine.send(ScriptMessage::new("broadcast", "data"));

        world.insert_resource(engine);
        run_scripts(&mut world);

        let script = world.get_component::<Script>(entity).unwrap();
        assert_eq!(script.state, "original"); // unchanged
    }

    #[test]
    fn recycled_entity_message_delivery() {
        let mut world = World::new();
        let mut engine = ScriptEngine::default();

        // Spawn, despawn, respawn to get generation > 0
        let e1 = world.spawn();
        world.despawn(e1).unwrap();
        let e2 = world.spawn(); // same index, generation 1
        assert_eq!(e2.generation(), 1);

        world
            .insert_component(e2, Script::new("test.wasm").with_state("initial"))
            .unwrap();

        // Message targets the recycled entity using its full id
        engine.send(ScriptMessage::new("update", "recycled_data").to_entity(e2));

        world.insert_resource(engine);
        run_scripts(&mut world);

        let script = world.get_component::<Script>(e2).unwrap();
        assert_eq!(script.state, "recycled_data");
    }

    #[test]
    fn run_scripts_skips_disabled() {
        let mut world = World::new();
        let mut engine = ScriptEngine::default();

        let entity = world.spawn();
        world
            .insert_component(
                entity,
                Script::new("test.wasm").disabled().with_state("original"),
            )
            .unwrap();

        engine.send(ScriptMessage::new("update", "should_not_apply").to_entity(entity));

        world.insert_resource(engine);
        run_scripts(&mut world);

        let script = world.get_component::<Script>(entity).unwrap();
        assert_eq!(script.state, "original");
    }

    #[test]
    fn multiple_messages_same_frame() {
        let mut world = World::new();
        let mut engine = ScriptEngine::default();

        let entity = world.spawn();
        world
            .insert_component(entity, Script::new("test.wasm").with_state("initial"))
            .unwrap();

        // Send multiple messages — last one wins
        engine.send(ScriptMessage::new("update", "first").to_entity(entity));
        engine.send(ScriptMessage::new("update", "second").to_entity(entity));
        engine.send(ScriptMessage::new("update", "third").to_entity(entity));

        world.insert_resource(engine);
        run_scripts(&mut world);

        let script = world.get_component::<Script>(entity).unwrap();
        assert_eq!(script.state, "third");

        let engine = world.get_resource::<ScriptEngine>().unwrap();
        assert_eq!(engine.exec_count(), 3);
    }

    #[test]
    fn message_to_nonexistent_entity() {
        let mut world = World::new();
        let mut engine = ScriptEngine::default();

        // Target an entity that doesn't exist
        let fake = Entity::new(999, 0);
        engine.send(ScriptMessage::new("update", "data").to_entity(fake));

        world.insert_resource(engine);
        run_scripts(&mut world); // should not panic
    }

    #[test]
    fn multiple_entities_receive_messages() {
        let mut world = World::new();
        let mut engine = ScriptEngine::default();

        let e1 = world.spawn();
        let e2 = world.spawn();
        world
            .insert_component(e1, Script::new("a.wasm").with_state("a_init"))
            .unwrap();
        world
            .insert_component(e2, Script::new("b.wasm").with_state("b_init"))
            .unwrap();

        engine.send(ScriptMessage::new("update", "a_new").to_entity(e1));
        engine.send(ScriptMessage::new("update", "b_new").to_entity(e2));

        world.insert_resource(engine);
        run_scripts(&mut world);

        assert_eq!(world.get_component::<Script>(e1).unwrap().state, "a_new");
        assert_eq!(world.get_component::<Script>(e2).unwrap().state, "b_new");
    }

    #[test]
    fn script_config_serde() {
        let cfg = ScriptConfig::default();
        let json = serde_json::to_string(&cfg).unwrap();
        let decoded: ScriptConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(decoded.timeout_ms, 16);
    }

    #[cfg(feature = "scripting")]
    #[test]
    fn wasm_backend_available() {
        let engine = ScriptEngine::default();
        assert!(engine.wasm_available());
    }

    #[cfg(not(feature = "scripting"))]
    #[test]
    fn wasm_backend_unavailable_without_feature() {
        let engine = ScriptEngine::default();
        assert!(!engine.wasm_available());
    }

    #[cfg(feature = "scripting")]
    #[test]
    fn exec_wasm_nonexistent_file() {
        let engine = ScriptEngine::default();
        let result = engine.exec_wasm("/nonexistent/script.wasm");
        // Should return None (execution fails gracefully)
        assert!(result.is_none());
    }

    #[test]
    fn run_scripts_non_wasm_source_still_works() {
        let mut world = World::new();
        let mut engine = ScriptEngine::default();

        let entity = world.spawn();
        // Source is not a .wasm file — fallback to message state
        world
            .insert_component(entity, Script::new("logic.lua").with_state("initial"))
            .unwrap();

        engine.send(ScriptMessage::new("update", "new_state").to_entity(entity));

        world.insert_resource(engine);
        run_scripts(&mut world);

        let script = world.get_component::<Script>(entity).unwrap();
        assert_eq!(script.state, "new_state");
    }

    #[test]
    fn custom_script_config() {
        let config = ScriptConfig {
            timeout_ms: 100,
            max_memory: 32 * 1024 * 1024,
        };
        let engine = ScriptEngine::new(config);
        assert_eq!(engine.config().timeout_ms, 100);
        assert_eq!(engine.config().max_memory, 32 * 1024 * 1024);
    }

    #[test]
    fn mixed_wasm_and_non_wasm_entities() {
        let mut world = World::new();
        let mut engine = ScriptEngine::default();

        let e_wasm = world.spawn();
        world
            .insert_component(e_wasm, Script::new("game.wasm").with_state("wasm_init"))
            .unwrap();

        let e_lua = world.spawn();
        world
            .insert_component(e_lua, Script::new("game.lua").with_state("lua_init"))
            .unwrap();

        // Send messages to both
        engine.send(ScriptMessage::new("tick", "wasm_data").to_entity(e_wasm));
        engine.send(ScriptMessage::new("tick", "lua_data").to_entity(e_lua));

        world.insert_resource(engine);
        run_scripts(&mut world);

        // Non-wasm entity gets message payload
        let lua_script = world.get_component::<Script>(e_lua).unwrap();
        assert_eq!(lua_script.state, "lua_data");

        // WASM entity: exec_wasm fails (no real file) → falls back to message state
        let wasm_script = world.get_component::<Script>(e_wasm).unwrap();
        assert_eq!(wasm_script.state, "wasm_data");
    }

    #[test]
    fn run_scripts_10_entities() {
        let mut world = World::new();
        let mut engine = ScriptEngine::default();

        let mut entities = Vec::new();
        for i in 0..10 {
            let e = world.spawn();
            world
                .insert_component(e, Script::new(format!("s{i}.lua")))
                .unwrap();
            engine.send(ScriptMessage::new("tick", format!("data_{i}")).to_entity(e));
            entities.push(e);
        }

        world.insert_resource(engine);
        run_scripts(&mut world);

        for (i, &e) in entities.iter().enumerate() {
            let script = world.get_component::<Script>(e).unwrap();
            assert_eq!(script.state, format!("data_{i}"));
        }

        let engine = world.get_resource::<ScriptEngine>().unwrap();
        assert_eq!(engine.exec_count(), 10);
    }

    #[test]
    fn run_scripts_disabled_wasm_entity_skipped() {
        let mut world = World::new();
        let mut engine = ScriptEngine::default();

        let entity = world.spawn();
        world
            .insert_component(
                entity,
                Script::new("game.wasm").disabled().with_state("unchanged"),
            )
            .unwrap();

        engine.send(ScriptMessage::new("tick", "should_not_apply").to_entity(entity));

        world.insert_resource(engine);
        run_scripts(&mut world);

        let script = world.get_component::<Script>(entity).unwrap();
        assert_eq!(script.state, "unchanged");
    }

    #[test]
    fn script_engine_outbox_flow() {
        let mut engine = ScriptEngine::default();

        // Simulate script producing output messages
        engine.push_outbox(ScriptMessage::new("spawn_bullet", r#"{"x": 10}"#));
        engine.push_outbox(ScriptMessage::new("play_sound", "explosion.wav"));

        let outbox = engine.drain_outbox();
        assert_eq!(outbox.len(), 2);
        assert_eq!(outbox[0].kind, "spawn_bullet");
        assert_eq!(outbox[1].kind, "play_sound");

        // Outbox drained
        assert!(engine.drain_outbox().is_empty());
    }
}