-
Notifications
You must be signed in to change notification settings - Fork 0
test: add unit tests for zero-coverage crates/core modules #424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,3 +116,130 @@ pub mod packet_helpers { | |
| batch | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: Comment-heavy tests may conflict with the repository’s preference but are not a correctness issue The repository guidance discourages comments that restate obvious code, and several touched files already contain substantial API documentation and the new tests add descriptive names rather than new production comments. I did not report this as a bug because comments/documentation style issues are not severe and the added test code mostly relies on test names for intent; any cleanup would be editorial rather than behavioral. Was this helpful? React with 👍 or 👎 to provide feedback. Debug |
||
| mod tests { | ||
| use super::*; | ||
| use serde::Deserialize; | ||
|
|
||
| #[derive(Debug, Deserialize, Default, PartialEq)] | ||
| struct TestConfig { | ||
| #[serde(default)] | ||
| gain: f32, | ||
| #[serde(default)] | ||
| channels: u16, | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_config_optional_with_valid_json() { | ||
| let params = serde_json::json!({"gain": 0.5, "channels": 2}); | ||
| let cfg: TestConfig = config_helpers::parse_config_optional(Some(¶ms)).unwrap(); | ||
| assert_eq!(cfg.gain, 0.5); | ||
| assert_eq!(cfg.channels, 2); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_config_optional_with_none_returns_default() { | ||
| let cfg: TestConfig = config_helpers::parse_config_optional(None).unwrap(); | ||
| assert_eq!(cfg, TestConfig::default()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_config_optional_with_partial_json_fills_defaults() { | ||
| let params = serde_json::json!({"gain": 1.5}); | ||
| let cfg: TestConfig = config_helpers::parse_config_optional(Some(¶ms)).unwrap(); | ||
| assert_eq!(cfg.gain, 1.5); | ||
| assert_eq!(cfg.channels, 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_config_required_with_valid_json() { | ||
| let params = serde_json::json!({"gain": 2.0, "channels": 1}); | ||
| let cfg: TestConfig = config_helpers::parse_config_required(Some(¶ms)).unwrap(); | ||
| assert_eq!(cfg.gain, 2.0); | ||
| assert_eq!(cfg.channels, 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_config_required_with_none_returns_error() { | ||
| let result = config_helpers::parse_config_required::<TestConfig>(None); | ||
| assert!(result.is_err()); | ||
| let err_str = result.unwrap_err().to_string(); | ||
| assert!(err_str.contains("Configuration"), "expected Configuration error, got: {err_str}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_config_required_with_invalid_type_returns_error() { | ||
| let params = serde_json::json!({"gain": "not_a_number"}); | ||
| let result = config_helpers::parse_config_required::<TestConfig>(Some(¶ms)); | ||
| assert!(result.is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_config_with_context_missing_params() { | ||
| let result = config_helpers::parse_config_with_context::<TestConfig>(None, "AudioGain"); | ||
| assert!(result.is_err()); | ||
| let err_str = result.unwrap_err().to_string(); | ||
| assert!(err_str.contains("AudioGain")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_config_with_context_invalid_json() { | ||
| let params = serde_json::json!("just a string"); | ||
| let result = | ||
| config_helpers::parse_config_with_context::<TestConfig>(Some(¶ms), "AudioGain"); | ||
| assert!(result.is_err()); | ||
| let err_str = result.unwrap_err().to_string(); | ||
| assert!(err_str.contains("AudioGain")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_config_optional_with_invalid_type_falls_back_to_default() { | ||
| let params = serde_json::json!({"gain": "not_a_number"}); | ||
| let cfg: TestConfig = config_helpers::parse_config_optional(Some(¶ms)).unwrap(); | ||
| assert_eq!(cfg, TestConfig::default()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_config_with_context_valid_json() { | ||
| let params = serde_json::json!({"gain": 3.0, "channels": 4}); | ||
| let cfg: TestConfig = | ||
| config_helpers::parse_config_with_context(Some(¶ms), "AudioGain").unwrap(); | ||
| assert_eq!(cfg.gain, 3.0); | ||
| assert_eq!(cfg.channels, 4); | ||
| } | ||
|
|
||
| #[test] | ||
| fn batch_packets_greedy_drains_one_extra_packet() { | ||
| let (tx, mut rx) = tokio::sync::mpsc::channel(16); | ||
| let first = Packet::Text(std::sync::Arc::from("hello")); | ||
| tx.try_send(Packet::Text(std::sync::Arc::from("world"))).unwrap(); | ||
| let batch = packet_helpers::batch_packets_greedy(first, &mut rx, 4); | ||
| assert_eq!(batch.len(), 2); | ||
| } | ||
|
|
||
| #[test] | ||
| fn batch_packets_greedy_empty_channel() { | ||
| let (_tx, mut rx) = tokio::sync::mpsc::channel::<Packet>(16); | ||
| let first = Packet::Text(std::sync::Arc::from("only")); | ||
| let batch = packet_helpers::batch_packets_greedy(first, &mut rx, 8); | ||
| assert_eq!(batch.len(), 1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn batch_packets_greedy_respects_batch_size() { | ||
| let (tx, mut rx) = tokio::sync::mpsc::channel(16); | ||
| for i in 0..10 { | ||
| tx.try_send(Packet::Text(std::sync::Arc::from(format!("{i}")))).unwrap(); | ||
| } | ||
| let first = Packet::Text(std::sync::Arc::from("first")); | ||
| let batch = packet_helpers::batch_packets_greedy(first, &mut rx, 3); | ||
| assert_eq!(batch.len(), 3); | ||
| } | ||
|
|
||
| #[test] | ||
| fn default_batch_capacity_is_reasonable() { | ||
| const { assert!(packet_helpers::DEFAULT_BATCH_CAPACITY >= 8) }; | ||
| const { assert!(packet_helpers::DEFAULT_BATCH_CAPACITY <= 128) }; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Info: PR is test-only and leaves production paths unchanged
All hunks in this PR add
#[cfg(test)]modules below existing production definitions; no non-test function, struct, enum, or constant bodies were changed. That means the existing production behaviors for control serialization, config parsing, batching, output sending, pin matching, registry lookup/resource loading, metric boundaries, and view-data emission are unaffected by this PR.Was this helpful? React with 👍 or 👎 to provide feedback.
Debug
Playground