Skip to main content

punkgo_kernel/
testkit.rs

1//! PunkGo test utilities — helpers for integration and unit tests.
2//!
3//! - [`TestStateDir`] — creates a temporary state directory with platform-specific
4//!   IPC endpoint (Unix socket or Windows named pipe), auto-cleaned on drop
5//! - [`make_request`] — builds a [`RequestEnvelope`] with a random request ID
6
7use std::path::{Path, PathBuf};
8
9use punkgo_core::protocol::{RequestEnvelope, RequestType};
10use serde_json::Value;
11use uuid::Uuid;
12
13pub struct TestStateDir {
14    path: PathBuf,
15}
16
17impl TestStateDir {
18    pub fn new(prefix: &str) -> std::io::Result<Self> {
19        let dir = std::env::temp_dir().join(format!("{prefix}-{}", Uuid::new_v4()));
20        std::fs::create_dir_all(&dir)?;
21        Ok(Self { path: dir })
22    }
23
24    pub fn path(&self) -> &Path {
25        &self.path
26    }
27
28    pub fn ipc_endpoint(&self) -> String {
29        format!("punkgo-test-{}", Uuid::new_v4())
30    }
31}
32
33impl Drop for TestStateDir {
34    fn drop(&mut self) {
35        let _ = std::fs::remove_dir_all(&self.path);
36    }
37}
38
39pub fn make_request(request_type: RequestType, payload: Value) -> RequestEnvelope {
40    RequestEnvelope {
41        request_id: Uuid::new_v4().to_string(),
42        request_type,
43        payload,
44    }
45}