Skip to main content

malbox_plugin_sdk/
testkit.rs

1//! Test-only constructors for SDK types.
2//!
3//! This module is gated behind the `testkit` feature (or the built-in `cfg(test)`
4//! flag for in-crate use). It exposes construction APIs that are otherwise
5//! `pub(crate)` so that downstream test code can build `Context`
6//! values directly.
7//!
8//! **Do not depend on this module in production code.**
9
10#![cfg(any(test, feature = "testkit"))]
11
12use crate::context::Context;
13use malbox_plugin_transport::traits::TransportEmitter;
14use std::collections::HashMap;
15use std::path::PathBuf;
16use std::sync::Arc;
17
18impl Context {
19    /// Create a minimal test context with task ID 0, an empty sample path,
20    /// and no result channel. Results pushed on this context will be dropped.
21    pub fn test_new(emitter: Arc<dyn TransportEmitter + Send + Sync>) -> Context {
22        Context::new(
23            0,
24            PathBuf::new(),
25            HashMap::new(),
26            emitter,
27            None,
28            #[cfg(feature = "guest")]
29            None,
30        )
31    }
32
33    /// Create a test context wired to an mpsc sender so you can inspect
34    /// results pushed during the test.
35    pub fn test_new_with_tx(
36        emitter: Arc<dyn TransportEmitter + Send + Sync>,
37        tx: crate::context::ResultSender,
38    ) -> Context {
39        Context::new(
40            0,
41            PathBuf::new(),
42            HashMap::new(),
43            emitter,
44            Some(tx),
45            #[cfg(feature = "guest")]
46            None,
47        )
48    }
49
50    /// Create a test context with explicit values for every field.
51    pub fn test_new_full(
52        task_id: i32,
53        sample_path: PathBuf,
54        config: HashMap<String, String>,
55        emitter: Arc<dyn TransportEmitter + Send + Sync>,
56        result_tx: Option<crate::context::ResultSender>,
57    ) -> Context {
58        Context::new(
59            task_id,
60            sample_path,
61            config,
62            emitter,
63            result_tx,
64            #[cfg(feature = "guest")]
65            None,
66        )
67    }
68}