diff --git a/src-tauri/src/types.rs b/src-tauri/src/types.rs index 235a7666..4c7f0014 100644 --- a/src-tauri/src/types.rs +++ b/src-tauri/src/types.rs @@ -239,6 +239,7 @@ pub enum AuthMode { /// Using an OpenAI API key ApiKey, /// Using ChatGPT OAuth tokens + #[serde(alias = "chat_gpt", alias = "chatgpt", alias = "chat_g_pt")] ChatGPT, } @@ -252,6 +253,7 @@ pub enum AuthData { key: String, }, /// ChatGPT OAuth authentication + #[serde(alias = "chat_gpt", alias = "chatgpt", alias = "chat_g_pt")] ChatGPT { /// JWT ID token containing user info id_token: String, @@ -547,3 +549,42 @@ mod tests { assert!(settings.close_behavior_prompt_enabled); } } + +#[cfg(test)] +mod auth_deserialization_tests { + use super::*; + + #[test] + fn test_legacy_chatgpt_spellings_deserialize() { + let legacy_spellings = ["chat_g_p_t", "chat_g_pt", "chat_gpt", "chatgpt"]; + + for spelling in legacy_spellings { + let mode_json = format!("\"{}\"", spelling); + let mode: Result = serde_json::from_str(&mode_json); + assert_eq!( + mode.expect(&format!("Failed to deserialize AuthMode for {}", spelling)), + AuthMode::ChatGPT + ); + + let data_json = format!( + r#"{{"type": "{}", "id_token": "id", "access_token": "acc", "refresh_token": "ref", "account_id": null}}"#, + spelling + ); + let data: Result = serde_json::from_str(&data_json); + match data.expect(&format!("Failed to deserialize AuthData for {}", spelling)) { + AuthData::ChatGPT { + id_token, + access_token, + refresh_token, + account_id, + } => { + assert_eq!(id_token, "id"); + assert_eq!(access_token, "acc"); + assert_eq!(refresh_token, "ref"); + assert!(account_id.is_none()); + } + _ => panic!("Expected AuthData::ChatGPT"), + } + } + } +}