Skip to content

Commit 36b4a5d

Browse files
committed
test: add agentmemory adapter unit tests
1 parent 6eb14dc commit 36b4a5d

3 files changed

Lines changed: 100 additions & 5 deletions

File tree

codex-rs/core/src/agentmemory/mod.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,15 @@ impl AgentmemoryAdapter {
6868
/// expected by the `agentmemory` REST API. This provides a central, malleable
6969
/// place to adjust mapping logic in the future without touching the hooks engine.
7070
fn format_claude_parity_payload(&self, event_name: &str, payload: serde_json::Value) -> serde_json::Value {
71-
// TODO: As agentmemory evolves, perform explicit property mapping here.
72-
// For example, mapping Codex `turn_id` to Claude `message_id` or extracting specific nested fields.
71+
let session_id = payload.get("session_id").and_then(|v| v.as_str()).unwrap_or("unknown").to_string();
72+
73+
let timestamp = chrono::Utc::now().to_rfc3339();
7374

7475
json!({
75-
"event": event_name,
76-
"payload": payload,
76+
"sessionId": session_id,
77+
"hookType": event_name,
78+
"timestamp": timestamp,
79+
"data": payload,
7780
})
7881
}
7982

@@ -118,4 +121,26 @@ impl AgentmemoryAdapter {
118121
}
119122
Ok(())
120123
}
121-
}
124+
}
125+
#[cfg(test)]
126+
mod tests {
127+
use super::*;
128+
use serde_json::json;
129+
130+
#[test]
131+
fn test_format_claude_parity_payload() {
132+
let adapter = AgentmemoryAdapter::new();
133+
let raw_payload = json!({
134+
"session_id": "1234",
135+
"turn_id": "turn-5",
136+
"command": "echo hello"
137+
});
138+
139+
let formatted = adapter.format_claude_parity_payload("PreToolUse", raw_payload.clone());
140+
141+
assert_eq!(formatted["sessionId"], "1234");
142+
assert_eq!(formatted["hookType"], "PreToolUse");
143+
assert!(formatted.get("timestamp").is_some());
144+
assert_eq!(formatted["data"], raw_payload);
145+
}
146+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--- codex-rs/core/src/agentmemory/mod.rs
2+
+++ codex-rs/core/src/agentmemory/mod.rs
3+
@@ -118,22 +118,21 @@
4+
Ok(())
5+
}
6+
}
7+
8+
#[cfg(test)]
9+
mod tests {
10+
use super::*;
11+
use serde_json::json;
12+
13+
#[test]
14+
fn test_format_claude_parity_payload() {
15+
let adapter = AgentmemoryAdapter::new();
16+
let raw_payload = json!({
17+
"session_id": "1234",
18+
"turn_id": "turn-5",
19+
"command": "echo hello"
20+
});
21+
22+
let formatted = adapter.format_claude_parity_payload("PreToolUse", raw_payload.clone());
23+
24+
assert_eq!(formatted["event"], "PreToolUse");
25+
assert_eq!(formatted["payload"], raw_payload);
26+
}
27+
}

patch_tests.diff

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--- codex-rs/core/src/agentmemory/mod.rs
2+
+++ codex-rs/core/src/agentmemory/mod.rs
3+
@@ -118,3 +118,48 @@
4+
Ok(())
5+
}
6+
}
7+
+
8+
+#[cfg(test)]
9+
+mod tests {
10+
+ use super::*;
11+
+ use serde_json::json;
12+
+
13+
+ #[test]
14+
+ fn test_api_base_default() {
15+
+ // Ensure env var is not set
16+
+ std::env::remove_var("III_REST_PORT");
17+
+ let adapter = AgentmemoryAdapter::new();
18+
+ assert_eq!(adapter.api_base(), "http://localhost:3111");
19+
+ }
20+
+
21+
+ #[test]
22+
+ fn test_api_base_custom_port() {
23+
+ std::env::set_var("III_REST_PORT", "4000");
24+
+ let adapter = AgentmemoryAdapter::new();
25+
+ assert_eq!(adapter.api_base(), "http://localhost:4000");
26+
+ std::env::remove_var("III_REST_PORT");
27+
+ }
28+
+
29+
+ #[test]
30+
+ fn test_format_claude_parity_payload() {
31+
+ let adapter = AgentmemoryAdapter::new();
32+
+ let raw_payload = json!({
33+
+ "session_id": "1234",
34+
+ "turn_id": "turn-5",
35+
+ "command": "echo hello"
36+
+ });
37+
+
38+
+ let formatted = adapter.format_claude_parity_payload("PreToolUse", raw_payload.clone());
39+
+
40+
+ assert_eq!(formatted["event"], "PreToolUse");
41+
+ assert_eq!(formatted["payload"], raw_payload);
42+
+ }
43+
+}

0 commit comments

Comments
 (0)