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        if cfg!(windows) {
30            format!(r"\\.\pipe\punkgo-test-{}", Uuid::new_v4())
31        } else {
32            format!("punkgo-test-{}", Uuid::new_v4())
33        }
34    }
35}
36
37impl Drop for TestStateDir {
38    fn drop(&mut self) {
39        let _ = std::fs::remove_dir_all(&self.path);
40    }
41}
42
43pub fn make_request(request_type: RequestType, payload: Value) -> RequestEnvelope {
44    RequestEnvelope {
45        request_id: Uuid::new_v4().to_string(),
46        request_type,
47        payload,
48    }
49}