diff --git a/src/webext.rs b/src/webext.rs index 6287619..e1156cd 100644 --- a/src/webext.rs +++ b/src/webext.rs @@ -8,12 +8,13 @@ use std::path::PathBuf; use std::process; #[derive(Debug, Deserialize)] -#[serde(tag = "type", content = "data")] +#[serde(tag = "type")] enum InputMessage { Issue2Work(Issue2WorkWebExtMessage), } #[derive(Debug, Serialize)] +#[serde(tag = "type")] enum OutputContent { Issue2Work(OutputIssue2Work), } @@ -86,9 +87,9 @@ fn write_json_message( /// "Logique métier" : construit une URL "created" à partir de l'entrée. async fn handle_business_logic(input: InputMessage) -> Result { let mut default_config_path = PathBuf::new(); - //default_config_path.push(simple_home_dir::home_dir().unwrap()); - //default_config_path.push(".config/cl-cli/config.toml"); - default_config_path.push("/tmp/cl-cli/config.toml"); + default_config_path.push(simple_home_dir::home_dir().unwrap()); + default_config_path.push(".config/cl-cli/config.toml"); + // during dev: default_config_path.push("/tmp/cl-cli/config.toml"); let config = match build_config(&default_config_path) { Ok(c) => c, Err(e) => { @@ -161,3 +162,62 @@ async fn main() { } } } + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::json; + + #[test] + fn deserializes_issue2work_variant() { + // Given: a JSON with the external tag "type" matching the enum variant name + let payload = json!({ + "type": "Issue2Work", + "url": "https://example.com/issues/123", + "project": "test" + }); + + // When: deserializing to InputMessage + let msg: InputMessage = serde_json::from_value(payload).expect("valid enum JSON"); + + // Then: we get the correct variant and fields + match msg { + InputMessage::Issue2Work(inner) => { + assert_eq!(inner.url, "https://example.com/issues/123"); + assert_eq!(inner.project, "test"); + } + } + } + + #[test] + fn fails_on_unknown_type_tag() { + let payload = json!({ + "type": "Unknown", + "url": "https://example.com/issues/123", + "project": "test" + }); + + let err = serde_json::from_value::(payload).unwrap_err(); + // Basic sanity check that it's a data error mentioning the unrecognized variant + let msg = err.to_string(); + assert!( + msg.contains("unknown variant") || msg.contains("unknown") || msg.contains("expected"), + "unexpected error message: {msg}" + ); + } + + #[test] + fn fails_when_missing_required_fields() { + // Missing "url" and "project" + let payload = json!({ + "type": "Issue2Work" + }); + + let err = serde_json::from_value::(payload).unwrap_err(); + let msg = err.to_string(); + assert!( + msg.contains("missing field"), + "unexpected error message: {msg}" + ); + } +}