cobre-io 0.14.3

Case directory loading and validation for the Cobre power systems ecosystem
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
//! Postcard serialization helpers for MPI broadcast of [`System`] and
//! [`ScalarParameter`] collections.
//!
//! Cobre uses `postcard` (not `bincode`) for MPI serialization (see CLAUDE.md hard rules).
//! These helpers serialize payloads to compact byte buffers for broadcast
//! and deserialize them on worker ranks.
//!
//! ## Why broadcast mirror types
//!
//! [`ParameterKind`] and [`ComputedParameter`] use serde internally-tagged
//! enums (`#[serde(tag = "...")]`) to drive the user-facing JSON schema. Postcard
//! does not support that representation. To keep MPI broadcast working without
//! per-rank disk reads, the public `serialize_parameters` /
//! `deserialize_parameters` helpers convert through tag-free mirror types
//! ([`BroadcastScalarParameter`], [`BroadcastParameterKind`],
//! [`BroadcastComputedParameter`]) on the wire and reconstruct the in-memory
//! shape on the receiving end. This mirrors the `BroadcastConfig` pattern in
//! `cobre-cli` used for [`crate::Config`].
//!
//! # Usage
//!
//! On rank 0, load the case and serialize:
//!
//! ```rust,ignore
//! let system = cobre_io::load_case(&path)?;
//! let bytes = cobre_io::serialize_system(&system)?;
//! // broadcast bytes via MPI ...
//! ```
//!
//! On worker ranks, deserialize after receiving:
//!
//! ```rust,ignore
//! // ... receive bytes via MPI
//! let system = cobre_io::deserialize_system(&bytes)?;
//! // system.bus(id) works immediately — indices are rebuilt
//! ```

use cobre_core::{ComputedParameter, EntityId, ParameterKind, ScalarParameter, System};
use serde::{Deserialize, Serialize};

use crate::LoadError;

// ── Broadcast mirror types (tag-free, postcard-compatible) ──────────────────

/// Postcard-safe mirror of [`ScalarParameter`], holding a [`BroadcastParameterKind`]
/// in place of [`ParameterKind`]. Convert with `From` in both directions.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BroadcastScalarParameter {
    /// Unique parameter identifier.
    pub id: EntityId,
    /// Short name used in reports and log output.
    pub name: String,
    /// Kind in the broadcast-safe representation.
    pub kind: BroadcastParameterKind,
}

/// Postcard-safe mirror of [`ParameterKind`]. Uses externally-tagged enum
/// encoding (the serde default), which postcard supports natively.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum BroadcastParameterKind {
    /// Single scalar value applied to every stage.
    Constant(f64),
    /// Dense `Vec<f64>` indexed by stage (0-based).
    PerStage(Vec<f64>),
    /// Sorted, deduplicated `(season_id, value)` pairs.
    Seasonal(Vec<(i32, f64)>),
    /// Computed-parameter specification.
    Computed(BroadcastComputedParameter),
    /// Sorted, unique-keyed `(stage_id, block_id, value)` triples.
    PerStageBlock(Vec<(i32, i32, f64)>),
}

/// Postcard-safe mirror of [`ComputedParameter`]. Externally-tagged.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BroadcastComputedParameter {
    /// Equivalent productivity coefficient (`ρ_eq`).
    EquivalentProductivity(EntityId),
    /// Accumulated productivity coefficient (`ρ_acum`).
    AccumulatedProductivity(EntityId),
    /// Reference reservoir volume (`V_ref`).
    ReferenceVolume(EntityId),
    /// Reference turbine flow (`Q_ref`).
    ReferenceTurbine(EntityId),
    /// Minimum operational storage (`V_min`).
    MinStorage(EntityId),
    /// Maximum operational storage (`V_max`).
    MaxStorage(EntityId),
    /// Specific productivity (`ρ_esp`).
    SpecificProductivity(EntityId),
}

impl From<&ScalarParameter> for BroadcastScalarParameter {
    fn from(p: &ScalarParameter) -> Self {
        Self {
            id: p.id,
            name: p.name.clone(),
            kind: BroadcastParameterKind::from(&p.kind),
        }
    }
}

impl From<BroadcastScalarParameter> for ScalarParameter {
    fn from(b: BroadcastScalarParameter) -> Self {
        Self {
            id: b.id,
            name: b.name,
            kind: ParameterKind::from(b.kind),
        }
    }
}

impl From<&ParameterKind> for BroadcastParameterKind {
    fn from(k: &ParameterKind) -> Self {
        match k {
            ParameterKind::Constant { value } => Self::Constant(*value),
            ParameterKind::PerStage { values } => Self::PerStage(values.clone()),
            ParameterKind::Seasonal { values } => Self::Seasonal(values.clone()),
            ParameterKind::Computed { computed_spec } => {
                Self::Computed(BroadcastComputedParameter::from(*computed_spec))
            }
            ParameterKind::PerStageBlock { values } => Self::PerStageBlock(values.clone()),
        }
    }
}

impl From<BroadcastParameterKind> for ParameterKind {
    fn from(b: BroadcastParameterKind) -> Self {
        match b {
            BroadcastParameterKind::Constant(value) => Self::Constant { value },
            BroadcastParameterKind::PerStage(values) => Self::PerStage { values },
            BroadcastParameterKind::Seasonal(values) => Self::Seasonal { values },
            BroadcastParameterKind::Computed(c) => Self::Computed {
                computed_spec: ComputedParameter::from(c),
            },
            BroadcastParameterKind::PerStageBlock(values) => Self::PerStageBlock { values },
        }
    }
}

impl From<ComputedParameter> for BroadcastComputedParameter {
    fn from(c: ComputedParameter) -> Self {
        match c {
            ComputedParameter::EquivalentProductivity { hydro_id } => {
                Self::EquivalentProductivity(hydro_id)
            }
            ComputedParameter::AccumulatedProductivity { hydro_id } => {
                Self::AccumulatedProductivity(hydro_id)
            }
            ComputedParameter::ReferenceVolume { hydro_id } => Self::ReferenceVolume(hydro_id),
            ComputedParameter::ReferenceTurbine { hydro_id } => Self::ReferenceTurbine(hydro_id),
            ComputedParameter::MinStorage { hydro_id } => Self::MinStorage(hydro_id),
            ComputedParameter::MaxStorage { hydro_id } => Self::MaxStorage(hydro_id),
            ComputedParameter::SpecificProductivity { hydro_id } => {
                Self::SpecificProductivity(hydro_id)
            }
        }
    }
}

impl From<BroadcastComputedParameter> for ComputedParameter {
    fn from(b: BroadcastComputedParameter) -> Self {
        match b {
            BroadcastComputedParameter::EquivalentProductivity(hydro_id) => {
                Self::EquivalentProductivity { hydro_id }
            }
            BroadcastComputedParameter::AccumulatedProductivity(hydro_id) => {
                Self::AccumulatedProductivity { hydro_id }
            }
            BroadcastComputedParameter::ReferenceVolume(hydro_id) => {
                Self::ReferenceVolume { hydro_id }
            }
            BroadcastComputedParameter::ReferenceTurbine(hydro_id) => {
                Self::ReferenceTurbine { hydro_id }
            }
            BroadcastComputedParameter::MinStorage(hydro_id) => Self::MinStorage { hydro_id },
            BroadcastComputedParameter::MaxStorage(hydro_id) => Self::MaxStorage { hydro_id },
            BroadcastComputedParameter::SpecificProductivity(hydro_id) => {
                Self::SpecificProductivity { hydro_id }
            }
        }
    }
}

/// Serialize a [`System`] to a postcard byte buffer for MPI broadcast.
///
/// The returned `Vec<u8>` is suitable for broadcasting over MPI. The recipient
/// must call [`deserialize_system`] to reconstruct the [`System`] with working
/// O(1) lookup indices.
///
/// # Errors
///
/// Returns [`LoadError::ParseError`] with path `"<broadcast>"` if postcard
/// encounters an unsupported type during serialization. This should not occur
/// in practice given the types used in [`System`].
///
/// # Examples
///
/// ```
/// use chrono::NaiveDate;
/// use cobre_core::{Bus, DeficitSegment, EntityId, SystemBuilder};
/// use cobre_io::serialize_system;
///
/// let bus = Bus {
///     id: EntityId(1),
///     name: "Main Bus".to_string(),
///     operational_start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
///     deficit_segments: vec![DeficitSegment { depth_mw: None, cost_per_mwh: 500.0 }],
///     excess_cost: 0.0,
/// };
/// let system = SystemBuilder::new().buses(vec![bus]).build().unwrap();
/// let bytes = serialize_system(&system).unwrap();
/// assert!(!bytes.is_empty());
/// ```
pub fn serialize_system(system: &System) -> Result<Vec<u8>, LoadError> {
    postcard::to_allocvec(system)
        .map_err(|e| LoadError::parse("<broadcast>", format!("postcard serialization: {e}")))
}

/// Deserialize a [`System`] from a postcard byte buffer received via MPI broadcast.
///
/// `System`'s `Deserialize` impl rebuilds lookup indices unconditionally, so
/// O(1) entity lookups (e.g., `system.bus(id)`) work immediately on the
/// returned value.
///
/// # Errors
///
/// Returns [`LoadError::ParseError`] with path `"<broadcast>"` if the byte slice
/// is corrupted, truncated, or not a valid postcard encoding of [`System`].
///
/// # Examples
///
/// ```
/// use chrono::NaiveDate;
/// use cobre_core::{Bus, DeficitSegment, EntityId, SystemBuilder};
/// use cobre_io::{deserialize_system, serialize_system};
///
/// let bus = Bus {
///     id: EntityId(1),
///     name: "Main Bus".to_string(),
///     operational_start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
///     deficit_segments: vec![DeficitSegment { depth_mw: None, cost_per_mwh: 500.0 }],
///     excess_cost: 0.0,
/// };
/// let system = SystemBuilder::new().buses(vec![bus]).build().unwrap();
/// let bytes = serialize_system(&system).unwrap();
/// let restored = deserialize_system(&bytes).unwrap();
/// assert_eq!(restored.n_buses(), 1);
/// assert!(restored.bus(EntityId(1)).is_some());
/// ```
pub fn deserialize_system(bytes: &[u8]) -> Result<System, LoadError> {
    postcard::from_bytes(bytes)
        .map_err(|e| LoadError::parse("<broadcast>", format!("postcard deserialization: {e}")))
}

/// Serialize a list of [`ScalarParameter`] to a postcard byte buffer for MPI
/// broadcast.
///
/// The returned `Vec<u8>` encodes the entire slice as a single postcard payload
/// including a varint length prefix, so the recipient can deserialize without a
/// separate length-broadcast step. The caller is responsible for the MPI
/// broadcast call itself.
///
/// # Errors
///
/// Returns [`LoadError::ParseError`] with path `"<broadcast>"` if postcard
/// serialization fails.
///
/// # Examples
///
/// ```
/// use cobre_core::{EntityId, ParameterKind, ScalarParameter};
/// use cobre_io::serialize_parameters;
///
/// let param = ScalarParameter {
///     id: EntityId(1),
///     name: "rho_eq_h1".to_string(),
///     kind: ParameterKind::Constant { value: 3.6 },
/// };
/// let bytes = serialize_parameters(&[param.clone()]).unwrap();
/// assert!(!bytes.is_empty());
///
/// let restored = cobre_io::deserialize_parameters(&bytes).unwrap();
/// assert_eq!(restored, vec![param]);
/// ```
pub fn serialize_parameters(parameters: &[ScalarParameter]) -> Result<Vec<u8>, LoadError> {
    let mirror: Vec<BroadcastScalarParameter> = parameters
        .iter()
        .map(BroadcastScalarParameter::from)
        .collect();
    postcard::to_allocvec(&mirror)
        .map_err(|e| LoadError::parse("<broadcast>", format!("postcard serialization: {e}")))
}

/// Deserialize a `Vec<ScalarParameter>` from a postcard byte buffer received
/// via MPI broadcast.
///
/// The byte buffer must have been produced by [`serialize_parameters`]. An empty
/// slice or a corrupted buffer returns an error; this function never silently
/// discards data.
///
/// # Errors
///
/// Returns [`LoadError::ParseError`] with path `"<broadcast>"` if the byte
/// slice is corrupted, truncated, or not a valid postcard encoding of
/// `Vec<ScalarParameter>`.
///
/// # Examples
///
/// ```
/// use cobre_core::{EntityId, ParameterKind, ScalarParameter};
/// use cobre_io::{deserialize_parameters, serialize_parameters};
///
/// let param = ScalarParameter {
///     id: EntityId(1),
///     name: "rho_eq_h1".to_string(),
///     kind: ParameterKind::Constant { value: 3.6 },
/// };
/// let bytes = serialize_parameters(&[param.clone()]).unwrap();
/// let restored = deserialize_parameters(&bytes).unwrap();
/// assert_eq!(restored, vec![param]);
/// ```
pub fn deserialize_parameters(bytes: &[u8]) -> Result<Vec<ScalarParameter>, LoadError> {
    let mirror: Vec<BroadcastScalarParameter> = postcard::from_bytes(bytes)
        .map_err(|e| LoadError::parse("<broadcast>", format!("postcard deserialization: {e}")))?;
    Ok(mirror.into_iter().map(ScalarParameter::from).collect())
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::panic)]
mod tests {
    use super::*;
    use chrono::NaiveDate;
    use cobre_core::{
        AnticipatedCommitmentHistory, Bus, ComputedParameter, DeficitSegment, EntityId, Hydro,
        HydroGenerationModel, HydroPenalties, InitialConditions, ParameterKind, ScalarParameter,
        SystemBuilder, Thermal, entities::AnticipatedConfig,
    };

    fn minimal_bus(id: i32) -> Bus {
        Bus {
            id: EntityId(id),
            name: format!("Bus {id}"),
            operational_start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            deficit_segments: vec![DeficitSegment {
                depth_mw: None,
                cost_per_mwh: 500.0,
            }],
            excess_cost: 0.0,
        }
    }

    fn minimal_thermal(id: i32, bus_id: i32) -> Thermal {
        Thermal {
            id: EntityId(id),
            name: format!("Thermal {id}"),
            operational_start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            bus_id: EntityId(bus_id),
            entry_stage_id: None,
            exit_stage_id: None,
            cost_per_mwh: 50.0,
            min_generation_mw: 0.0,
            max_generation_mw: 100.0,
            anticipated_config: None,
        }
    }

    fn zero_hydro_penalties() -> HydroPenalties {
        HydroPenalties {
            spillage_cost: 0.0,
            diversion_cost: 0.0,
            turbined_cost: 0.0,
            storage_violation_below_cost: 0.0,
            filling_target_violation_cost: 0.0,
            turbined_violation_below_cost: 0.0,
            outflow_violation_below_cost: 0.0,
            outflow_violation_above_cost: 0.0,
            generation_violation_below_cost: 0.0,
            evaporation_violation_cost: 0.0,
            water_withdrawal_violation_cost: 0.0,
            water_withdrawal_violation_pos_cost: 0.0,
            water_withdrawal_violation_neg_cost: 0.0,
            evaporation_violation_pos_cost: 0.0,
            evaporation_violation_neg_cost: 0.0,
            inflow_nonnegativity_cost: 1000.0,
        }
    }

    fn minimal_hydro(id: i32, bus_id: i32) -> Hydro {
        let mut hydro = Hydro {
            unit_groups: Vec::new(),
            id: EntityId(id),
            name: format!("Hydro {id}"),
            operational_start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
            downstream_id: None,
            travel_time_hours: None,
            entry_stage_id: None,
            exit_stage_id: None,
            min_storage_hm3: 0.0,
            max_storage_hm3: 1000.0,
            min_outflow_m3s: 0.0,
            max_outflow_m3s: None,
            generation_model: HydroGenerationModel::ConstantProductivity,
            min_turbined_m3s: 0.0,
            max_turbined_m3s: 200.0,
            specific_productivity_mw_per_m3s_per_m: None,
            min_generation_mw: 0.0,
            max_generation_mw: 200.0,
            tailrace: None,
            hydraulic_losses: None,
            efficiency: None,
            evaporation_coefficients_mm: None,
            evaporation_reference_volumes_hm3: None,
            diversion: None,
            filling: None,
            penalties: zero_hydro_penalties(),
        };
        hydro.declare_mirror_unit_group(EntityId(bus_id));
        hydro
    }

    #[test]
    fn test_round_trip_minimal_system() {
        let bus = minimal_bus(1);
        let system = SystemBuilder::new().buses(vec![bus]).build().unwrap();

        let bytes = serialize_system(&system).unwrap();
        assert!(!bytes.is_empty());

        let restored = deserialize_system(&bytes).unwrap();

        assert_eq!(restored.n_buses(), system.n_buses());
        assert!(restored.bus(EntityId(1)).is_some());
    }

    #[test]
    fn test_round_trip_populated_system() {
        let buses = vec![minimal_bus(1), minimal_bus(2)];
        let thermals = vec![minimal_thermal(1, 1), minimal_thermal(2, 2)];
        let hydros = vec![minimal_hydro(1, 1)];

        let system = SystemBuilder::new()
            .buses(buses)
            .thermals(thermals)
            .hydros(hydros)
            .build()
            .unwrap();

        let bytes = serialize_system(&system).unwrap();
        let restored = deserialize_system(&bytes).unwrap();

        assert_eq!(restored.n_buses(), system.n_buses());
        assert_eq!(restored.n_thermals(), system.n_thermals());
        assert_eq!(restored.n_hydros(), system.n_hydros());

        assert!(restored.bus(EntityId(1)).is_some());
        assert!(restored.bus(EntityId(2)).is_some());
        assert!(restored.thermal(EntityId(1)).is_some());
        assert!(restored.thermal(EntityId(2)).is_some());
        assert!(restored.hydro(EntityId(1)).is_some());

        assert_eq!(restored, system);
    }

    /// A generic constraint's affine bound remainder (`bound_upper_affine`) rides the
    /// constraint's derived serde through the `System` postcard wire — there is no
    /// hand-written mirror for `GenericConstraint`, so the restored System must equal
    /// the original. Uses a genuine multi-component remainder (a nonzero constant plus
    /// a scaled term), not just the `single` special case, to prove the whole shape
    /// round-trips.
    #[test]
    fn test_round_trip_generic_constraint_bound_ref() {
        use cobre_core::{
            AffineBound, ConstraintExpression, GenericConstraint, LinearTerm, SlackConfig,
            VariableRef,
        };

        let gc = GenericConstraint {
            id: EntityId(0),
            name: "demand_cap".to_string(),
            description: None,
            expression: ConstraintExpression {
                terms: vec![LinearTerm::literal(
                    1.0,
                    VariableRef::HydroGeneration {
                        hydro_id: EntityId(1),
                        block_id: None,
                        bus_id: None,
                    },
                )],
            },
            slack: SlackConfig {
                enabled: false,
                penalty: None,
            },
            bound_lower_affine: None,
            bound_upper_affine: Some(AffineBound {
                constant: 12.0,
                terms: vec![(0.5, EntityId(7))],
            }),
        };

        let system = SystemBuilder::new()
            .buses(vec![minimal_bus(1)])
            .hydros(vec![minimal_hydro(1, 1)])
            .generic_constraints(vec![gc])
            .build()
            .unwrap();

        let bytes = serialize_system(&system).unwrap();
        let restored = deserialize_system(&bytes).unwrap();

        assert_eq!(restored, system);
        assert_eq!(
            restored.generic_constraints()[0].bound_upper_affine,
            Some(AffineBound {
                constant: 12.0,
                terms: vec![(0.5, EntityId(7))],
            }),
            "affine bound remainder must survive broadcast round-trip"
        );
    }

    #[test]
    fn test_round_trip_anticipated_thermal_system() {
        // Guards against a future Thermal field reorder or AnticipatedConfig schema
        // change silently breaking broadcast deserialization of `anticipated_config`.
        let buses = vec![minimal_bus(1), minimal_bus(2)];
        let mut anticipated = minimal_thermal(10, 1);
        anticipated.anticipated_config = Some(AnticipatedConfig::LeadStages(2));
        let regular = minimal_thermal(20, 2);
        let thermals = vec![anticipated, regular];

        let system = SystemBuilder::new()
            .buses(buses)
            .thermals(thermals)
            .build()
            .unwrap();

        let bytes = serialize_system(&system).unwrap();
        let restored = deserialize_system(&bytes).unwrap();

        assert_eq!(restored.n_thermals(), system.n_thermals());
        let Some(restored_anticipated) = restored.thermal(EntityId(10)) else {
            panic!("thermal 10 must round-trip");
        };
        assert_eq!(
            restored_anticipated.anticipated_config,
            Some(AnticipatedConfig::LeadStages(2)),
            "anticipated_config must survive broadcast round-trip"
        );
        let Some(restored_regular) = restored.thermal(EntityId(20)) else {
            panic!("thermal 20 must round-trip");
        };
        assert_eq!(
            restored_regular.anticipated_config, None,
            "non-anticipated thermal must remain None after round-trip"
        );

        assert_eq!(restored, system);
    }

    #[test]
    fn test_deserialize_corrupted_bytes() {
        let result = deserialize_system(&[0u8; 4]);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("<broadcast>"));
        assert!(matches!(err, LoadError::ParseError { .. }));
    }

    #[test]
    fn test_deserialize_empty_bytes() {
        let result = deserialize_system(&[]);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, LoadError::ParseError { .. }));
        assert!(err.to_string().contains("<broadcast>"));
    }

    #[test]
    fn test_serialized_size_reasonable() {
        let bus = minimal_bus(1);
        let system = SystemBuilder::new().buses(vec![bus]).build().unwrap();
        let bytes = serialize_system(&system).unwrap();
        assert!(bytes.len() < 1024);
    }

    /// Build a `Vec<ScalarParameter>` with one instance of each of the five
    /// `ParameterKind` variants, covering all code paths through the
    /// postcard serialization layer.
    fn five_kinds_fixture() -> Vec<ScalarParameter> {
        vec![
            ScalarParameter {
                id: EntityId(1),
                name: "constant_param".to_string(),
                kind: ParameterKind::Constant { value: 1.5 },
            },
            ScalarParameter {
                id: EntityId(2),
                name: "per_stage_param".to_string(),
                kind: ParameterKind::PerStage {
                    values: vec![1.0, 2.0, 3.0],
                },
            },
            ScalarParameter {
                id: EntityId(3),
                name: "seasonal_param".to_string(),
                kind: ParameterKind::new_seasonal(vec![(2, 1.0), (1, 0.5)]),
            },
            ScalarParameter {
                id: EntityId(4),
                name: "computed_param".to_string(),
                kind: ParameterKind::Computed {
                    computed_spec: ComputedParameter::EquivalentProductivity {
                        hydro_id: EntityId(7),
                    },
                },
            },
            ScalarParameter {
                id: EntityId(5),
                name: "per_stage_block_param".to_string(),
                kind: ParameterKind::PerStageBlock {
                    values: vec![(0, 0, 1.0), (0, 1, 2.0), (1, 0, 3.0)],
                },
            },
        ]
    }

    #[test]
    fn round_trip_all_five_parameter_kinds() {
        let original = five_kinds_fixture();
        let bytes = serialize_parameters(&original).unwrap();
        assert!(!bytes.is_empty());
        let restored = deserialize_parameters(&bytes).unwrap();
        assert_eq!(restored, original);
    }

    #[test]
    fn serialize_parameters_is_deterministic() {
        let params = five_kinds_fixture();
        let bytes_a = serialize_parameters(&params).unwrap();
        let bytes_b = serialize_parameters(&params).unwrap();
        assert_eq!(bytes_a, bytes_b);
    }

    #[test]
    fn deserialize_parameters_rejects_corrupted_bytes() {
        let result = deserialize_parameters(&[0xFF, 0xFE, 0xFD, 0xFC]);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, LoadError::ParseError { .. }));
        assert!(err.to_string().contains("<broadcast>"));
    }

    #[test]
    fn deserialize_parameters_rejects_empty_buffer() {
        let result = deserialize_parameters(&[]);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, LoadError::ParseError { .. }));
        assert!(err.to_string().contains("<broadcast>"));
    }

    #[test]
    fn test_broadcast_initial_conditions_round_trips_past_anticipated_commitments() {
        let original = InitialConditions {
            storage: vec![],
            filling_storage: vec![],
            past_anticipated_commitments: vec![
                AnticipatedCommitmentHistory {
                    thermal_id: EntityId(1),
                    start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                    end_date: NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(),
                    value_mw: 120.0,
                },
                AnticipatedCommitmentHistory {
                    thermal_id: EntityId(7),
                    start_date: NaiveDate::from_ymd_opt(2024, 1, 1).unwrap(),
                    end_date: NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(),
                    value_mw: 50.0,
                },
            ],
            recent_observations: vec![],
            past_defluences: vec![],
            future_anticipated_deliveries: vec![],
        };

        let bytes = postcard::to_allocvec(&original).unwrap();
        assert!(!bytes.is_empty());

        let restored: InitialConditions = postcard::from_bytes(&bytes).unwrap();
        assert_eq!(restored, original);
        assert_eq!(restored.past_anticipated_commitments.len(), 2);
        assert_eq!(
            restored.past_anticipated_commitments[0].thermal_id,
            EntityId(1)
        );
        assert!((restored.past_anticipated_commitments[0].value_mw - 120.0).abs() < f64::EPSILON);
        assert_eq!(
            restored.past_anticipated_commitments[1].thermal_id,
            EntityId(7)
        );
        assert!((restored.past_anticipated_commitments[1].value_mw - 50.0).abs() < f64::EPSILON);
    }

    #[test]
    fn test_broadcast_initial_conditions_empty_past_anticipated_commitments() {
        let original = InitialConditions {
            storage: vec![],
            filling_storage: vec![],
            past_anticipated_commitments: vec![],
            recent_observations: vec![],
            past_defluences: vec![],
            future_anticipated_deliveries: vec![],
        };

        let bytes = postcard::to_allocvec(&original).unwrap();
        assert!(!bytes.is_empty());

        let restored: InitialConditions = postcard::from_bytes(&bytes).unwrap();
        assert_eq!(restored, original);
        assert!(restored.past_anticipated_commitments.is_empty());
    }
}