Skip to main content

portalis_test/
lib.rs

1//! Test Agent - WASM Validation
2//!
3//! Executes and validates WASM modules.
4
5use async_trait::async_trait;
6use portalis_core::{Agent, AgentCapability, AgentId, ArtifactMetadata, Result};
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct TestInput {
11    pub wasm_bytes: Vec<u8>,
12}
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct TestOutput {
16    pub passed: usize,
17    pub failed: usize,
18    pub metadata: ArtifactMetadata,
19}
20
21pub struct TestAgent {
22    id: AgentId,
23}
24
25impl TestAgent {
26    pub fn new() -> Self {
27        Self { id: AgentId::new() }
28    }
29}
30
31impl Default for TestAgent {
32    fn default() -> Self {
33        Self::new()
34    }
35}
36
37#[async_trait]
38impl Agent for TestAgent {
39    type Input = TestInput;
40    type Output = TestOutput;
41
42    async fn execute(&self, input: Self::Input) -> Result<Self::Output> {
43        tracing::info!("Testing WASM module");
44
45        // Simplified validation for POC
46        let passed = if input.wasm_bytes.starts_with(&[0x00, 0x61, 0x73, 0x6D]) {
47            1
48        } else {
49            0
50        };
51
52        let failed = 1 - passed;
53
54        let metadata = ArtifactMetadata::new(self.name())
55            .with_tag("passed", passed.to_string())
56            .with_tag("failed", failed.to_string());
57
58        Ok(TestOutput {
59            passed,
60            failed,
61            metadata,
62        })
63    }
64
65    fn id(&self) -> AgentId {
66        self.id
67    }
68
69    fn name(&self) -> &str {
70        "TestAgent"
71    }
72
73    fn capabilities(&self) -> Vec<AgentCapability> {
74        vec![AgentCapability::Testing]
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[tokio::test]
83    async fn test_valid_wasm_passes() {
84        let agent = TestAgent::new();
85
86        // Valid WASM magic number
87        let wasm_bytes = vec![0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00];
88
89        let input = TestInput { wasm_bytes };
90        let output = agent.execute(input).await.unwrap();
91
92        assert_eq!(output.passed, 1);
93        assert_eq!(output.failed, 0);
94    }
95
96    #[tokio::test]
97    async fn test_invalid_wasm_fails() {
98        let agent = TestAgent::new();
99
100        // Invalid WASM (wrong magic number)
101        let wasm_bytes = vec![0xFF, 0xFF, 0xFF, 0xFF];
102
103        let input = TestInput { wasm_bytes };
104        let output = agent.execute(input).await.unwrap();
105
106        assert_eq!(output.passed, 0);
107        assert_eq!(output.failed, 1);
108    }
109
110    #[tokio::test]
111    async fn test_empty_wasm_fails() {
112        let agent = TestAgent::new();
113
114        let input = TestInput { wasm_bytes: vec![] };
115        let output = agent.execute(input).await.unwrap();
116
117        assert_eq!(output.passed, 0);
118        assert_eq!(output.failed, 1);
119    }
120
121    #[test]
122    fn test_agent_metadata() {
123        let agent = TestAgent::new();
124
125        assert_eq!(agent.name(), "TestAgent");
126        assert_eq!(agent.capabilities(), vec![AgentCapability::Testing]);
127    }
128}