conclavelib/tests.rs
1//! Shared test-helper factory (fixtures + in-memory transports).
2//!
3//! Exported from the library — not gated behind `#[cfg(test)]` — so both in-module unit
4//! tests and the out-of-crate integration / e2e suites can build fixtures from one place
5//! (the house convention; DESIGN.md §22). Prefer the in-memory `tokio::io::duplex` transport
6//! here over real sockets wherever a test doesn't specifically exercise the network.
7//!
8//! Key / signing fixtures arrive with the identity work in M1; today this seeds the transport
9//! and path fixtures the later suites build on.
10
11use tokio::io::DuplexStream;
12
13use crate::base::SessionPath;
14
15/// Buffer size for the in-memory [`duplex`] transport (64 KiB each direction).
16pub const DUPLEX_BUF: usize = 64 * 1024;
17
18/// A connected in-memory duplex stream pair, standing in for a socket in tests.
19#[must_use]
20pub fn duplex() -> (DuplexStream, DuplexStream) {
21 tokio::io::duplex(DUPLEX_BUF)
22}
23
24/// A representative fully-qualified session path fixture (`aaron/workstation/razel`).
25#[must_use]
26pub fn sample_session_path() -> SessionPath {
27 SessionPath::new("aaron", "workstation", "razel")
28}
29
30#[cfg(test)]
31mod self_tests {
32 // Tests relax `unwrap_used` (house convention; DESIGN.md §22).
33 #![allow(clippy::unwrap_used)]
34
35 use super::*;
36 use pretty_assertions::assert_eq;
37 use tokio::io::{AsyncReadExt, AsyncWriteExt};
38
39 #[tokio::test]
40 async fn duplex_round_trips_bytes() {
41 let (mut a, mut b) = duplex();
42 a.write_all(b"ping").await.unwrap();
43
44 let mut buf = [0_u8; 4];
45 b.read_exact(&mut buf).await.unwrap();
46 assert_eq!(&buf, b"ping");
47 }
48
49 #[test]
50 fn sample_session_path_is_the_canonical_triple() {
51 assert_eq!(sample_session_path().to_string(), "aaron/workstation/razel");
52 }
53}