agent-team-mail-core 1.0.2

Daemon-free core library for local agent team mail workflows.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
//! ATM-owned observability boundary and projected log/health types.

use std::path::PathBuf;

use serde::de::Error as DeError;
use serde::ser::{Error as SerError, SerializeMap};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{Map, Value};
use tracing::warn;

use crate::error::{AtmError, AtmErrorCode};
use crate::schema::LegacyMessageId;
use crate::types::IsoTimestamp;

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct CommandEvent {
    pub command: &'static str,
    pub action: &'static str,
    pub outcome: &'static str,
    pub team: String,
    pub agent: String,
    pub sender: String,
    pub message_id: Option<LegacyMessageId>,
    pub requires_ack: bool,
    pub dry_run: bool,
    pub task_id: Option<String>,
    pub error_code: Option<AtmErrorCode>,
    pub error_message: Option<String>,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum LogMode {
    Snapshot,
    Tail,
}

#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum LogLevelFilter {
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum LogOrder {
    NewestFirst,
    OldestFirst,
}

/// ATM-owned field-key type for observability query and record projections.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LogFieldKey(String);

impl LogFieldKey {
    /// Construct a validated ATM log-field key.
    ///
    /// # Errors
    ///
    /// Returns [`AtmError`] when the provided key is empty or whitespace-only.
    pub fn new(value: impl Into<String>) -> Result<Self, AtmError> {
        let value = value.into();
        if value.trim().is_empty() {
            return Err(
                AtmError::validation("ATM log field key must not be empty").with_recovery(
                    "Provide a non-empty field key when building ATM log queries or records.",
                ),
            );
        }
        Ok(Self(value))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl Serialize for LogFieldKey {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.0)
    }
}

impl<'de> Deserialize<'de> for LogFieldKey {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Self::new(value).map_err(D::Error::custom)
    }
}

/// ATM-owned validated JSON-number representation for the observability
/// boundary.
#[derive(Debug, Clone)]
pub struct AtmJsonNumber {
    raw: String,
    number: serde_json::Number,
    normalized: String,
}

impl AtmJsonNumber {
    /// Construct a validated ATM JSON number from a raw numeric string.
    ///
    /// # Errors
    ///
    /// Returns [`AtmError`] when the input is not a valid RFC 8259 JSON
    /// number. Non-JSON values such as `NaN` and `Infinity` are rejected.
    pub fn new(value: impl Into<String>) -> Result<Self, AtmError> {
        let value = value.into();
        let parsed: Value = serde_json::from_str(&value).map_err(|source| {
            AtmError::validation(format!("invalid ATM JSON number `{value}`"))
                .with_recovery(
                    "Provide a valid RFC 8259 JSON number such as `1`, `-2.5`, or `6.02e23`.",
                )
                .with_source(source)
        })?;
        match parsed {
            Value::Number(number) => Ok(Self {
                raw: value.clone(),
                number,
                normalized: normalize_json_number(&value),
            }),
            _ => Err(
                AtmError::validation(format!("invalid ATM JSON number `{value}`")).with_recovery(
                    "Provide a valid RFC 8259 JSON number such as `1`, `-2.5`, or `6.02e23`.",
                ),
            ),
        }
    }

    pub fn as_str(&self) -> &str {
        &self.raw
    }

    fn to_json_number(&self) -> serde_json::Number {
        self.number.clone()
    }
}

impl PartialEq for AtmJsonNumber {
    fn eq(&self, other: &Self) -> bool {
        self.normalized == other.normalized
    }
}

impl Eq for AtmJsonNumber {}

impl Serialize for AtmJsonNumber {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.to_json_number().serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for AtmJsonNumber {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = Value::deserialize(deserializer)?;
        match value {
            Value::Number(number) => Self::new(number.to_string()).map_err(D::Error::custom),
            _ => Err(D::Error::custom("expected a JSON number")),
        }
    }
}

/// ATM-owned recursive JSON-value wrapper used by the observability boundary.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LogFieldValue {
    Null,
    Bool(bool),
    String(String),
    Number(AtmJsonNumber),
    Array(Vec<LogFieldValue>),
    Object(LogFieldMap),
}

impl LogFieldValue {
    pub fn null() -> Self {
        Self::Null
    }

    pub fn bool(value: bool) -> Self {
        Self::Bool(value)
    }

    pub fn string(value: impl Into<String>) -> Self {
        Self::String(value.into())
    }

    pub fn number(value: AtmJsonNumber) -> Self {
        Self::Number(value)
    }

    pub fn as_str(&self) -> Option<&str> {
        match self {
            Self::String(value) => Some(value),
            _ => None,
        }
    }

    /// Convert a serde_json value into the ATM-owned field-value wrapper.
    ///
    /// # Errors
    ///
    /// Returns [`AtmError`] when a nested field key or JSON number fails ATM
    /// validation.
    pub(crate) fn from_json_value(value: Value) -> Result<Self, AtmError> {
        match value {
            Value::Null => Ok(Self::Null),
            Value::Bool(value) => Ok(Self::Bool(value)),
            Value::String(value) => Ok(Self::String(value)),
            Value::Number(value) => Ok(Self::Number(AtmJsonNumber::new(value.to_string())?)),
            Value::Array(values) => values
                .into_iter()
                .map(Self::from_json_value)
                .collect::<Result<Vec<_>, _>>()
                .map(Self::Array),
            Value::Object(values) => LogFieldMap::from_json_map(values).map(Self::Object),
        }
    }

    /// Convert the ATM-owned field-value wrapper into a serde_json value.
    ///
    /// # Errors
    ///
    /// Returns [`AtmError`] when a nested ATM-owned JSON number cannot be
    /// materialized as a JSON value.
    pub(crate) fn to_json_value(&self) -> Result<Value, AtmError> {
        match self {
            Self::Null => Ok(Value::Null),
            Self::Bool(value) => Ok(Value::Bool(*value)),
            Self::String(value) => Ok(Value::String(value.clone())),
            Self::Number(value) => Ok(Value::Number(value.to_json_number())),
            Self::Array(values) => values
                .iter()
                .map(Self::to_json_value)
                .collect::<Result<Vec<_>, _>>()
                .map(Value::Array),
            Self::Object(values) => values.to_json_map().map(Value::Object),
        }
    }
}

impl Serialize for LogFieldValue {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.to_json_value()
            .map_err(S::Error::custom)?
            .serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for LogFieldValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = Value::deserialize(deserializer)?;
        Self::from_json_value(value).map_err(D::Error::custom)
    }
}

/// ATM-owned map wrapper used by public observability record projections.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct LogFieldMap {
    entries: Vec<(LogFieldKey, LogFieldValue)>,
}

impl LogFieldMap {
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    pub fn get(&self, key: &str) -> Option<&LogFieldValue> {
        self.entries
            .iter()
            .find_map(|(entry_key, entry_value)| (entry_key.as_str() == key).then_some(entry_value))
    }

    /// Convert a serde_json object into the ATM-owned field-map wrapper.
    ///
    /// # Errors
    ///
    /// Returns [`AtmError`] when a nested key or value fails ATM validation.
    pub(crate) fn from_json_map(values: Map<String, Value>) -> Result<Self, AtmError> {
        let entries = values
            .into_iter()
            .map(|(key, value)| {
                Ok((
                    LogFieldKey::new(key)?,
                    LogFieldValue::from_json_value(value)?,
                ))
            })
            .collect::<Result<Vec<_>, AtmError>>()?;
        Ok(Self { entries })
    }

    fn to_json_map(&self) -> Result<Map<String, Value>, AtmError> {
        // Duplicate keys collapse with a last-wins policy when projected back
        // into JSON. Serialize uses the same helper so the policy is
        // consistent across both outbound paths.
        self.entries
            .iter()
            .try_fold(Map::new(), |mut map, (key, value)| {
                map.insert(key.as_str().to_string(), value.to_json_value()?);
                Ok(map)
            })
    }
}

impl Serialize for LogFieldMap {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let json_map = self.to_json_map().map_err(S::Error::custom)?;
        let mut map = serializer.serialize_map(Some(json_map.len()))?;
        for (key, value) in json_map {
            map.serialize_entry(&key, &value)?;
        }
        map.end()
    }
}

impl<'de> Deserialize<'de> for LogFieldMap {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let values = Map::<String, Value>::deserialize(deserializer)?;
        Self::from_json_map(values).map_err(D::Error::custom)
    }
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct LogFieldMatch {
    pub key: LogFieldKey,
    pub value: LogFieldValue,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct AtmLogQuery {
    pub mode: LogMode,
    pub levels: Vec<LogLevelFilter>,
    pub field_matches: Vec<LogFieldMatch>,
    pub since: Option<IsoTimestamp>,
    pub until: Option<IsoTimestamp>,
    pub limit: Option<usize>,
    pub order: LogOrder,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct AtmLogRecord {
    pub timestamp: IsoTimestamp,
    pub severity: LogLevelFilter,
    pub service: String,
    pub target: Option<String>,
    pub action: Option<String>,
    pub message: Option<String>,
    pub fields: LogFieldMap,
}

#[derive(Debug, Clone, Default, Serialize, PartialEq)]
pub struct AtmLogSnapshot {
    pub records: Vec<AtmLogRecord>,
    pub truncated: bool,
}

#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum AtmObservabilityHealthState {
    Healthy,
    Degraded,
    Unavailable,
}

#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct AtmObservabilityHealth {
    pub active_log_path: Option<PathBuf>,
    pub logging_state: AtmObservabilityHealthState,
    pub query_state: Option<AtmObservabilityHealthState>,
    pub detail: Option<String>,
}

#[doc(hidden)]
pub mod sealed {
    pub trait Sealed {}
}

trait LogFollowPort: Send {
    fn poll(&mut self) -> Result<AtmLogSnapshot, AtmError>;
}

#[derive(Default)]
struct EmptyFollowPort;

impl LogFollowPort for EmptyFollowPort {
    fn poll(&mut self) -> Result<AtmLogSnapshot, AtmError> {
        Ok(AtmLogSnapshot::default())
    }
}

struct ClosureFollowPort<F> {
    poller: F,
}

impl<F> LogFollowPort for ClosureFollowPort<F>
where
    F: FnMut() -> Result<AtmLogSnapshot, AtmError> + Send,
{
    fn poll(&mut self) -> Result<AtmLogSnapshot, AtmError> {
        (self.poller)()
    }
}

/// One follow/tail session over retained ATM observability records.
///
/// `LogTailSession` is `Send` but intentionally not `Sync`: callers should move
/// one session onto one polling task and share the owning `ObservabilityPort`
/// behind an `Arc` if multiple async tasks need to create independent sessions.
pub struct LogTailSession {
    inner: Box<dyn LogFollowPort>,
}

impl LogTailSession {
    /// Construct an empty follow session that never yields records.
    pub fn empty() -> Self {
        Self {
            inner: Box::<EmptyFollowPort>::default(),
        }
    }

    /// Construct one follow session from a polling closure.
    pub fn from_poller<F>(poller: F) -> Self
    where
        F: FnMut() -> Result<AtmLogSnapshot, AtmError> + Send + 'static,
    {
        Self {
            inner: Box::new(ClosureFollowPort { poller }),
        }
    }

    /// Poll the next batch of followed log records.
    ///
    /// # Errors
    ///
    /// Returns an [`AtmError`] when the underlying follow session cannot
    /// produce the next batch of retained records.
    pub fn poll(&mut self) -> Result<AtmLogSnapshot, AtmError> {
        self.inner.poll()
    }
}

pub trait ObservabilityPort: sealed::Sealed {
    /// Emit one ATM command event into the configured observability sink.
    ///
    /// # Errors
    ///
    /// Returns an [`AtmError`] when the shared observability backend rejects
    /// or cannot persist the event.
    fn emit(&self, event: CommandEvent) -> Result<(), AtmError>;
    /// Query retained ATM observability records.
    ///
    /// # Errors
    ///
    /// Returns an [`AtmError`] when the shared backend cannot execute the
    /// query or when ATM-specific query projection fails.
    fn query(&self, req: AtmLogQuery) -> Result<AtmLogSnapshot, AtmError>;
    /// Start a retained follow/tail session for ATM observability records.
    ///
    /// The returned [`LogTailSession`] is designed for one polling owner at a
    /// time. Async callers that need multiple consumers should share the port
    /// behind an `Arc` and create one independent session per task.
    ///
    /// # Errors
    ///
    /// Returns an [`AtmError`] when the shared backend cannot start the follow
    /// session or ATM-specific query projection fails.
    fn follow(&self, req: AtmLogQuery) -> Result<LogTailSession, AtmError>;
    /// Report the current retained observability health state.
    ///
    /// # Errors
    ///
    /// Returns an [`AtmError`] when the shared backend health surface cannot
    /// be evaluated or projected into ATM-owned health types.
    fn health(&self) -> Result<AtmObservabilityHealth, AtmError>;
}

#[derive(Debug, Default, Clone, Copy)]
pub struct NullObservability;

impl sealed::Sealed for NullObservability {}

impl ObservabilityPort for NullObservability {
    fn emit(&self, _event: CommandEvent) -> Result<(), AtmError> {
        Ok(())
    }

    fn query(&self, _req: AtmLogQuery) -> Result<AtmLogSnapshot, AtmError> {
        Ok(AtmLogSnapshot::default())
    }

    fn follow(&self, _req: AtmLogQuery) -> Result<LogTailSession, AtmError> {
        Ok(LogTailSession::empty())
    }

    fn health(&self) -> Result<AtmObservabilityHealth, AtmError> {
        Ok(AtmObservabilityHealth {
            active_log_path: None,
            logging_state: AtmObservabilityHealthState::Unavailable,
            query_state: Some(AtmObservabilityHealthState::Unavailable),
            detail: Some("observability adapter is not configured".to_string()),
        })
    }
}

/// Normalize a JSON number string into a canonical decimal representation.
///
/// # Panics
///
/// This function does not panic on malformed exponents. If exponent parsing
/// fails unexpectedly, it logs a warning and preserves the original string.
fn normalize_json_number(raw: &str) -> String {
    const MAX_ABS_NORMALIZED_JSON_EXPONENT: i64 = 128;
    const MAX_NORMALIZED_JSON_NUMBER_LEN: usize = 64;

    let (negative, unsigned) = match raw.strip_prefix('-') {
        Some(rest) => (true, rest),
        None => (false, raw),
    };
    let (base, exponent) = match unsigned.find(['e', 'E']) {
        Some(index) => match unsigned[index + 1..].parse::<i64>() {
            Ok(exponent) => (&unsigned[..index], exponent),
            Err(error) => {
                warn!(
                    code = %AtmErrorCode::WarningMalformedAtmFieldIgnored,
                    raw,
                    %error,
                    "failed to normalize JSON number exponent; preserving original value"
                );
                return raw.to_string();
            }
        },
        None => (unsigned, 0),
    };
    if !(-MAX_ABS_NORMALIZED_JSON_EXPONENT..=MAX_ABS_NORMALIZED_JSON_EXPONENT).contains(&exponent) {
        warn!(
            code = %AtmErrorCode::WarningMalformedAtmFieldIgnored,
            raw,
            exponent,
            max_abs_exponent = MAX_ABS_NORMALIZED_JSON_EXPONENT,
            "JSON number exponent exceeds supported normalization range; preserving original value"
        );
        return raw.to_string();
    }
    let (integer, fraction) = match base.split_once('.') {
        Some((integer, fraction)) => (integer, fraction),
        None => (base, ""),
    };

    let mut digits = format!("{integer}{fraction}");
    let mut scale = match exponent.checked_sub(fraction.len() as i64) {
        Some(scale) => scale,
        None => {
            warn!(
                code = %AtmErrorCode::WarningMalformedAtmFieldIgnored,
                raw,
                exponent,
                fraction_len = fraction.len(),
                "JSON number exponent scaling overflowed; preserving original value"
            );
            return raw.to_string();
        }
    };

    let trimmed = digits.trim_start_matches('0');
    digits = if trimmed.is_empty() {
        "0".to_string()
    } else {
        trimmed.to_string()
    };
    if digits == "0" {
        return "0".to_string();
    }

    while digits.ends_with('0') {
        digits.pop();
        scale = match scale.checked_add(1) {
            Some(scale) => scale,
            None => {
                warn!(
                    code = %AtmErrorCode::WarningMalformedAtmFieldIgnored,
                    raw,
                    "JSON number exponent normalization overflowed; preserving original value"
                );
                return raw.to_string();
            }
        };
    }

    if normalized_number_len_exceeds_limit(
        negative,
        digits.len(),
        scale,
        MAX_NORMALIZED_JSON_NUMBER_LEN,
    ) {
        warn!(
            code = %AtmErrorCode::WarningMalformedAtmFieldIgnored,
            raw,
            max_normalized_len = MAX_NORMALIZED_JSON_NUMBER_LEN,
            "JSON number exponent too large to normalize; preserving original value"
        );
        return raw.to_string();
    }

    let unsigned = if scale >= 0 {
        format!("{digits}{}", "0".repeat(scale as usize))
    } else {
        let point_index = digits.len() as i64 + scale;
        if point_index > 0 {
            let point_index = point_index as usize;
            format!("{}.{}", &digits[..point_index], &digits[point_index..])
        } else {
            format!("0.{}{}", "0".repeat((-point_index) as usize), digits)
        }
    };

    if negative {
        format!("-{unsigned}")
    } else {
        unsigned
    }
}

fn normalized_number_len_exceeds_limit(
    negative: bool,
    digits_len: usize,
    scale: i64,
    max_len: usize,
) -> bool {
    let unsigned_len = if scale >= 0 {
        digits_len.saturating_add(scale as usize)
    } else {
        let point_index = digits_len as i64 + scale;
        if point_index > 0 {
            digits_len.saturating_add(1)
        } else {
            digits_len
                .saturating_add((-point_index) as usize)
                .saturating_add(2)
        }
    };
    let total_len = unsigned_len.saturating_add(usize::from(negative));
    total_len > max_len
}

#[cfg(test)]
mod tests {
    use super::{
        AtmJsonNumber, AtmLogQuery, AtmObservabilityHealthState, LogFieldKey, LogFieldMap,
        LogFieldValue, LogLevelFilter, LogMode, LogOrder, NullObservability, ObservabilityPort,
        normalize_json_number,
    };
    use serde_json::json;

    fn empty_query() -> AtmLogQuery {
        AtmLogQuery {
            mode: LogMode::Snapshot,
            levels: vec![LogLevelFilter::Info],
            field_matches: vec![],
            since: None,
            until: None,
            limit: None,
            order: LogOrder::NewestFirst,
        }
    }

    #[test]
    fn null_observability_returns_empty_snapshot_and_tail() {
        let observability = NullObservability;
        let query = empty_query();

        let snapshot = observability.query(query.clone()).expect("snapshot");
        assert!(snapshot.records.is_empty());
        assert!(!snapshot.truncated);

        let mut tail = observability.follow(query).expect("tail");
        let follow = tail.poll().expect("follow poll");
        assert!(follow.records.is_empty());
    }

    #[test]
    fn null_observability_reports_unavailable_health() {
        let observability = NullObservability;

        let health = observability.health().expect("health");
        assert_eq!(
            health.logging_state,
            AtmObservabilityHealthState::Unavailable
        );
        assert_eq!(
            health.query_state,
            Some(AtmObservabilityHealthState::Unavailable)
        );
    }

    #[test]
    fn log_mode_serde_round_trips_using_snake_case_wire_format() {
        assert_eq!(
            serde_json::to_value(LogMode::Snapshot).unwrap(),
            json!("snapshot")
        );
        assert_eq!(serde_json::to_value(LogMode::Tail).unwrap(), json!("tail"));
        assert_eq!(
            serde_json::from_value::<LogMode>(json!("snapshot")).unwrap(),
            LogMode::Snapshot
        );
        assert_eq!(
            serde_json::from_value::<LogMode>(json!("tail")).unwrap(),
            LogMode::Tail
        );
    }

    #[test]
    fn atm_json_number_rejects_non_json_numeric_values() {
        assert!(AtmJsonNumber::new("NaN").is_err());
        assert!(AtmJsonNumber::new("Infinity").is_err());
        assert!(AtmJsonNumber::new("-Infinity").is_err());
    }

    #[test]
    fn atm_json_number_accepts_valid_json_numbers() {
        for raw in ["1", "1.5", "-42", "6.02e23", "1e-6"] {
            let number = AtmJsonNumber::new(raw).expect("valid number");
            let encoded = serde_json::to_string(&number).expect("serialize");
            let decoded: AtmJsonNumber = serde_json::from_str(&encoded).expect("deserialize");
            assert_eq!(decoded, number, "number `{raw}` should round-trip");
        }
    }

    #[test]
    fn atm_json_number_equality_is_value_based() {
        assert_eq!(
            AtmJsonNumber::new("1").expect("one"),
            AtmJsonNumber::new("1.0").expect("one point zero")
        );
        assert_eq!(
            AtmJsonNumber::new("1").expect("one"),
            AtmJsonNumber::new("1e0").expect("scientific")
        );
    }

    #[test]
    fn normalize_json_number_preserves_raw_string_for_malformed_exponent() {
        assert_eq!(normalize_json_number("1e-not-a-number"), "1e-not-a-number");
    }

    #[test]
    fn normalize_json_number_preserves_raw_string_for_large_exponents() {
        // 1e63 expands to 64 chars (max allowed); 1e64 expands to 65 chars
        // (one over limit, capped).
        assert_eq!(normalize_json_number("1e1000000000"), "1e1000000000");
        assert_eq!(normalize_json_number("1e64"), "1e64");
        assert_eq!(
            normalize_json_number("1e63"),
            format!("1{}", "0".repeat(63))
        );
    }

    #[test]
    fn normalize_json_number_handles_point_index_zero_boundary() {
        assert_eq!(normalize_json_number("5e-1"), "0.5");
    }

    #[test]
    fn normalize_json_number_preserves_raw_string_for_min_i64_exponent() {
        assert_eq!(
            normalize_json_number("1.0e-9223372036854775808"),
            "1.0e-9223372036854775808"
        );
    }

    #[test]
    fn normalize_json_number_preserves_raw_string_for_max_i64_exponent() {
        assert_eq!(
            normalize_json_number("10e9223372036854775807"),
            "10e9223372036854775807"
        );
    }

    #[test]
    fn log_field_key_round_trips_through_json() {
        let key = LogFieldKey::new("task_id").expect("key");
        let encoded = serde_json::to_string(&key).expect("encode");
        let decoded: LogFieldKey = serde_json::from_str(&encoded).expect("decode");
        assert_eq!(decoded, key);
    }

    #[test]
    fn log_field_value_variants_round_trip_through_json() {
        let object: LogFieldMap = serde_json::from_value(json!({
            "nested": true,
            "answer": 42
        }))
        .expect("object");
        let cases = vec![
            LogFieldValue::Null,
            LogFieldValue::Bool(true),
            LogFieldValue::String("hello".to_string()),
            LogFieldValue::Number(AtmJsonNumber::new("1.0").expect("number")),
            LogFieldValue::Array(vec![
                LogFieldValue::String("a".to_string()),
                LogFieldValue::Bool(false),
            ]),
            LogFieldValue::Object(object),
        ];

        for case in cases {
            let encoded = serde_json::to_value(&case).expect("encode value");
            let decoded: LogFieldValue = serde_json::from_value(encoded).expect("decode value");
            assert_eq!(decoded, case);
        }
    }

    #[test]
    fn log_field_map_round_trips_with_last_key_wins() {
        let map = LogFieldMap {
            entries: vec![
                (
                    LogFieldKey::new("dup").expect("key"),
                    LogFieldValue::String("first".to_string()),
                ),
                (
                    LogFieldKey::new("stable").expect("key"),
                    LogFieldValue::Bool(true),
                ),
                (
                    LogFieldKey::new("dup").expect("key"),
                    LogFieldValue::String("second".to_string()),
                ),
            ],
        };

        let encoded = serde_json::to_value(&map).expect("encode map");
        assert_eq!(
            encoded,
            json!({
                "dup": "second",
                "stable": true
            })
        );

        let decoded: LogFieldMap = serde_json::from_value(encoded).expect("decode map");
        assert_eq!(
            decoded.get("dup").and_then(LogFieldValue::as_str),
            Some("second")
        );
        assert_eq!(decoded.get("stable"), Some(&LogFieldValue::Bool(true)));
    }
}