//! Single cookie-jar entry, JSON-serializable. Intentionally a thin shape —
//! per-adapter credential bundles wrap a `Vec<CredCookie>` plus their own
//! site-specific fields (auth tokens, user ids, etc).

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CredCookie {
    pub name: String,
    pub value: String,
    pub domain: String,
    pub path: String,
    pub secure: bool,
    pub http_only: bool,
}

impl CredCookie {
    /// Test helper for constructing a synthetic cookie with sensible defaults.
    /// Pub-ished (not just `pub(crate)`) so downstream crates can reuse it
    /// in their own integration tests.
    #[cfg(any(test, feature = "test-helpers"))]
    pub fn test_new(name: &str, value: &str) -> Self {
        Self {
            name: name.into(),
            value: value.into(),
            domain: ".example.com".into(),
            path: "/".into(),
            secure: true,
            http_only: false,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn serde_roundtrip() {
        let original = CredCookie {
            name: "cf_clearance".into(),
            value: "abc123".into(),
            domain: ".nansen.ai".into(),
            path: "/".into(),
            secure: true,
            http_only: true,
        };
        let json = serde_json::to_string(&original).unwrap();
        let back: CredCookie = serde_json::from_str(&json).unwrap();
        assert_eq!(back, original);
    }

    #[test]
    fn camel_to_snake_field_mapping_is_explicit() {
        // The on-disk JSON uses snake_case (http_only) — verify by inspection
        // that we DON'T emit camelCase by accident.
        let c = CredCookie {
            name: "x".into(),
            value: "y".into(),
            domain: "z".into(),
            path: "/".into(),
            secure: true,
            http_only: true,
        };
        let json = serde_json::to_string(&c).unwrap();
        assert!(json.contains("\"http_only\":true"), "got: {json}");
        assert!(
            !json.contains("httpOnly"),
            "should not emit camelCase: {json}"
        );
    }
}
