Skip to content

Commit c9667bc

Browse files
authored
test: add reporter jsonrpc coverage (#421)
Summary: - Extract the JSONRPC reporter environment-kind filter into a private helper for direct testing. - Add tests for filter behavior, telemetry payload serialization, and log payload serialization. - Covers the `pet-reporter` slice from the #389 coverage plan. Validation: - cargo test -p pet-reporter - cargo fmt --all - cargo clippy --all -- -D warnings Refs #389
1 parent 452fd27 commit c9667bc

File tree

1 file changed

+98
-12
lines changed

1 file changed

+98
-12
lines changed

crates/pet-reporter/src/jsonrpc.rs

Lines changed: 98 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,32 @@ impl Reporter for JsonRpcReporter {
3939
}
4040

4141
fn report_environment(&self, env: &PythonEnvironment) {
42-
if let Some(report_only) = &self.report_only {
43-
if env.kind != Some(*report_only) {
44-
trace!(
45-
"Skip Reporting Environment ({:?}) {:?} due to refresh request to report only {:?}",
46-
env.kind,
47-
env.executable
48-
.clone()
49-
.unwrap_or(env.prefix.clone().unwrap_or_default()),
50-
report_only
51-
);
52-
return;
53-
}
42+
if !should_report_environment(self.report_only, env) {
43+
trace!(
44+
"Skip Reporting Environment ({:?}) {:?} due to refresh request to report only {:?}",
45+
env.kind,
46+
env.executable
47+
.clone()
48+
.unwrap_or(env.prefix.clone().unwrap_or_default()),
49+
self.report_only
50+
);
51+
return;
5452
}
5553
trace!("Reporting Environment {:?}", env);
5654
send_message("environment", env.into())
5755
}
5856
}
5957

58+
fn should_report_environment(
59+
report_only: Option<PythonEnvironmentKind>,
60+
env: &PythonEnvironment,
61+
) -> bool {
62+
match report_only {
63+
Some(kind) => env.kind == Some(kind),
64+
None => true,
65+
}
66+
}
67+
6068
pub fn create_reporter(report_only: Option<PythonEnvironmentKind>) -> impl Reporter {
6169
JsonRpcReporter { report_only }
6270
}
@@ -100,3 +108,81 @@ pub fn initialize_logger(log_level: LevelFilter) {
100108
.filter(None, log_level)
101109
.init();
102110
}
111+
112+
#[cfg(test)]
113+
mod tests {
114+
use super::*;
115+
use pet_core::telemetry::TelemetryEvent;
116+
use serde_json::json;
117+
use std::{collections::BTreeMap, path::PathBuf};
118+
119+
fn create_environment(kind: PythonEnvironmentKind) -> PythonEnvironment {
120+
PythonEnvironment::new(
121+
Some(PathBuf::from("/tmp/.venv/bin/python")),
122+
Some(kind),
123+
Some(PathBuf::from("/tmp/.venv")),
124+
None,
125+
Some("3.12.0".to_string()),
126+
)
127+
}
128+
129+
#[test]
130+
fn environment_filter_allows_all_without_requested_kind() {
131+
let environment = create_environment(PythonEnvironmentKind::Venv);
132+
133+
assert!(should_report_environment(None, &environment));
134+
}
135+
136+
#[test]
137+
fn environment_filter_allows_matching_requested_kind() {
138+
let environment = create_environment(PythonEnvironmentKind::Poetry);
139+
140+
assert!(should_report_environment(
141+
Some(PythonEnvironmentKind::Poetry),
142+
&environment
143+
));
144+
}
145+
146+
#[test]
147+
fn environment_filter_rejects_non_matching_requested_kind() {
148+
let environment = create_environment(PythonEnvironmentKind::Venv);
149+
150+
assert!(!should_report_environment(
151+
Some(PythonEnvironmentKind::Poetry),
152+
&environment
153+
));
154+
}
155+
156+
#[test]
157+
fn telemetry_data_serializes_event_name_and_payload() {
158+
let event = TelemetryEvent::RefreshPerformance(
159+
pet_core::telemetry::refresh_performance::RefreshPerformance {
160+
total: 10,
161+
locators: BTreeMap::new(),
162+
breakdown: BTreeMap::new(),
163+
},
164+
);
165+
let payload = TelemetryData {
166+
event: get_telemetry_event_name(&event).to_string(),
167+
data: event,
168+
};
169+
170+
let value = serde_json::to_value(payload).unwrap();
171+
172+
assert_eq!(value["event"], json!("RefreshPerformance"));
173+
assert_eq!(value["data"]["refreshPerformance"]["total"], json!(10));
174+
}
175+
176+
#[test]
177+
fn log_payload_uses_camel_case_fields_and_level_renames() {
178+
let payload = Log {
179+
message: "hello".to_string(),
180+
level: LogLevel::Warning,
181+
};
182+
183+
assert_eq!(
184+
serde_json::to_value(payload).unwrap(),
185+
json!({ "message": "hello", "level": "warning" })
186+
);
187+
}
188+
}

0 commit comments

Comments
 (0)