metering 0.25.0

German energy metering quantities: DST-correct Europe/Berlin and 06:00-Gastag calendar arithmetic, exact decimal values, and the market's own code lists. Zero I/O, no async, no clock.
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
//! Sessions and device logs → Lastgang: a span of register readings placed
//! on the metering grid.
//!
//! A charge point's CDR, a submetered heat pump or a device log reports
//! cumulative readings over a span; [`split_session`] places the energy on
//! grid slots and [`merge_sessions`] adds several sessions on one grid. The
//! readings are the only input, so the total cannot contradict them:
//!
//! ```text
//! Σ slot energy = last reading − first reading        exactly, always
//! ```
//!
//! | Basis | Where it comes from | Quality |
//! |---|---|---|
//! | **Metered** | readings bracketing every segment inside the slot | [`QualityFlag::Measured`] |
//! | **Pro rata** | a segment between two readings straddling a boundary, divided by wall-clock time | [`QualityFlag::Estimated`] |
//!
//! Pro rata is not a profile; supply more [`MeterSample`]s (e.g. OCPP
//! clock-aligned values) for a better shape.
//!
//! Slots come from [`DayBoundary::bucket`], the function
//! [`resample`](crate::series::resample::resample()) buckets with, so DST days
//! and the Gastag are laid out the same way. Dividing one slot among several
//! claims is [`crate::allocation::community::allocate`].

use std::collections::BTreeMap;

use rust_decimal::Decimal;
use time::OffsetDateTime;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::ids::obis::ObisCode;
use crate::precision::{ALLOCATION_DP, ALLOCATION_STRATEGY};
use crate::series::interval::{MeterInterval, QualityFlag};
use crate::series::{Series, SeriesError};
use crate::time::calendar::DayBoundary;
use crate::time::resolution::Resolution;

// ── MeterSample ───────────────────────────────────────────────────────────────

/// A register reading taken during a session.
///
/// `reading` is **cumulative** (as OCPP `Energy.Active.Import.Register`), not
/// the energy since the last sample; only differences are used, so the offset
/// is irrelevant and a missing sample widens one segment. Unit: the series'
/// own — kWh for Strom (OCPP reports Wh; convert first).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MeterSample {
    /// When the register was read (UTC).
    #[cfg_attr(feature = "serde", serde(with = "crate::wire::rfc3339"))]
    pub at: OffsetDateTime,
    /// The cumulative register value at that instant.
    #[cfg_attr(feature = "serde", serde(with = "crate::wire::decimal"))]
    pub reading: Decimal,
}

impl MeterSample {
    /// A reading of `reading` at `at`.
    #[must_use]
    pub const fn new(at: OffsetDateTime, reading: Decimal) -> Self {
        Self { at, reading }
    }
}

// ── SessionSplitConfig ────────────────────────────────────────────────────────

/// Which grid to place a session on, and how to flag what lands there.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SessionSplitConfig {
    /// The slot length.
    pub resolution: Resolution,
    /// Where a day is cut; every slot is laid out from the local day start.
    pub day_boundary: DayBoundary,
    /// The OBIS channel stamped on every emitted interval, so
    /// [`MeterInterval::direction`] answers — `1-0:1.8.0` for a charge,
    /// `1-0:2.8.0` for a V2G discharge.
    pub obis_code: Option<ObisCode>,
    /// The flag for a slot whose energy came from register readings on its own
    /// boundaries.
    pub metered_quality: QualityFlag,
    /// The flag for a slot that had to be pro-rated across a boundary.
    pub prorated_quality: QualityFlag,
}

impl SessionSplitConfig {
    /// The German electricity settlement grid: quarter-hours cut at midnight,
    /// pro-rated slots flagged [`Estimated`](QualityFlag::Estimated).
    #[must_use]
    pub const fn quarter_hourly() -> Self {
        Self {
            resolution: Resolution::QUARTER_HOUR,
            day_boundary: DayBoundary::Strom,
            obis_code: None,
            metered_quality: QualityFlag::Measured,
            prorated_quality: QualityFlag::Estimated,
        }
    }

    /// A different slot length (builder style).
    #[must_use]
    pub const fn at(mut self, resolution: Resolution) -> Self {
        self.resolution = resolution;
        self
    }

    /// Cut days on this boundary (builder style) — the Gastag, for gas.
    #[must_use]
    pub const fn on(mut self, boundary: DayBoundary) -> Self {
        self.day_boundary = boundary;
        self
    }

    /// Stamp this OBIS channel on every emitted interval (builder style).
    #[must_use]
    pub const fn with_obis(mut self, code: ObisCode) -> Self {
        self.obis_code = Some(code);
        self
    }

    /// Flag pro-rated slots with something other than
    /// [`Estimated`](QualityFlag::Estimated) (builder style).
    #[must_use]
    pub const fn prorated_as(mut self, flag: QualityFlag) -> Self {
        self.prorated_quality = flag;
        self
    }

    /// Flag exactly-known slots with something other than
    /// [`Measured`](QualityFlag::Measured) (builder style) — e.g.
    /// [`Calculated`](QualityFlag::Calculated) for a device log that is not an
    /// eichrechtskonform measurement.
    #[must_use]
    pub const fn metered_as(mut self, flag: QualityFlag) -> Self {
        self.metered_quality = flag;
        self
    }
}

impl Default for SessionSplitConfig {
    fn default() -> Self {
        Self::quarter_hourly()
    }
}

// ── SessionError ──────────────────────────────────────────────────────────────

/// Why a session could not be placed on the grid.
#[derive(Debug, thiserror::Error, PartialEq, Eq, Clone)]
#[non_exhaustive]
pub enum SessionError {
    /// Fewer than two distinct instants: no span, so no energy.
    #[error("a session needs readings at two instants at least")]
    TooFewSamples,

    /// A register reading went backwards. A V2G discharge is its own session
    /// on the export register ([`SessionSplitConfig::obis_code`]).
    #[error("register reading at {at} is below the previous one")]
    SamplesNotMonotonic {
        /// The instant at which the register decreased.
        at: OffsetDateTime,
    },

    /// Two different readings at one instant.
    #[error("two different readings at {at}")]
    ConflictingSamples {
        /// The instant.
        at: OffsetDateTime,
    },

    /// A slot lies outside the calendar's supported years.
    #[error("the slot at {at} lies outside the supported calendar years")]
    OutOfRange {
        /// The instant.
        at: OffsetDateTime,
    },

    /// [`merge_sessions`] was given a series on another grid or channel.
    #[error("series {index} is not on the configured grid and channel")]
    GridMismatch {
        /// Its position in the input.
        index: usize,
    },

    /// The result series could not be formed. Unreachable for slots this
    /// module lays out; carried so no path panics.
    #[error(transparent)]
    Series(#[from] SeriesError),
}

// ── split_session ─────────────────────────────────────────────────────────────

/// The span between two adjacent readings: its energy is known exactly.
struct Segment {
    start: OffsetDateTime,
    end: OffsetDateTime,
    energy: Decimal,
    /// Energy delivered *before* this segment began.
    cum_before: Decimal,
}

/// Place a session, given as register readings, on the metering grid.
///
/// `samples` are cumulative readings in any order. One interval per slot the
/// span touches, contiguous, each spanning its **whole** slot; a touched slot
/// with no energy is a zero, not a gap. Each slot is the difference of two
/// adjacent cumulatives cut to [`ALLOCATION_DP`] toward zero, so
/// `Σ slot = last − first` exactly and no slot is negative.
///
/// A slot is [`metered_quality`](SessionSplitConfig::metered_quality) when
/// no segment reaching into it crosses its boundaries, and
/// [`prorated_quality`](SessionSplitConfig::prorated_quality) otherwise.
///
/// # Errors
///
/// [`TooFewSamples`](SessionError::TooFewSamples),
/// [`SamplesNotMonotonic`](SessionError::SamplesNotMonotonic),
/// [`ConflictingSamples`](SessionError::ConflictingSamples) and
/// [`OutOfRange`](SessionError::OutOfRange).
///
/// ```rust
/// use metering::allocation::session::{MeterSample, SessionSplitConfig, split_session};
/// use metering::QualityFlag;
/// use rust_decimal::dec;
/// use time::macros::datetime;
///
/// // Meter start 12:07, clock-aligned values at 12:15 and 12:30, stop 12:37.
/// let samples = [
///     MeterSample::new(datetime!(2026-06-01 12:07 UTC), dec!(998)),
///     MeterSample::new(datetime!(2026-06-01 12:15 UTC), dec!(1000)),
///     MeterSample::new(datetime!(2026-06-01 12:30 UTC), dec!(1006)),
///     MeterSample::new(datetime!(2026-06-01 12:37 UTC), dec!(1008)),
/// ];
/// let series = split_session(&samples, &SessionSplitConfig::quarter_hourly())?;
/// let slots = series.as_slice();
///
/// // Every segment lies inside one slot, so every slot is measured.
/// assert_eq!(slots[1].value(), dec!(6));
/// assert!(slots.iter().all(|s| s.quality() == QualityFlag::Measured));
/// assert_eq!(series.billable_total(), Some(dec!(10)));
/// # Ok::<(), metering::allocation::session::SessionError>(())
/// ```
pub fn split_session(
    samples: &[MeterSample],
    config: &SessionSplitConfig,
) -> Result<Series, SessionError> {
    let segments = build_segments(samples)?;
    let (Some(first), Some(last)) = (segments.first(), segments.last()) else {
        return Err(SessionError::TooFewSamples);
    };
    let (from, to) = (first.start, last.end);
    let energy = last.cum_before + last.energy;

    let bucket = |at| {
        config
            .day_boundary
            .bucket(at, config.resolution)
            .map(|p| p.range())
            .ok_or(SessionError::OutOfRange { at })
    };
    let mut out = Vec::new();
    let mut slot_start = bucket(from)?.0;
    // Segments and slots both run forward: one cursor, one pass.
    let mut cursor = 0usize;
    let mut opening = Decimal::ZERO;

    while slot_start < to {
        let (bucket_from, bucket_to) = bucket(slot_start)?;
        if bucket_to <= bucket_from {
            return Err(SessionError::OutOfRange { at: bucket_from });
        }
        let covered_from = bucket_from.max(from);
        let covered_to = bucket_to.min(to);

        let closing = cumulative(&segments, cursor, from, to, energy, covered_to);
        let exact = segments
            .get(cursor..)
            .unwrap_or_default()
            .iter()
            .take_while(|s| s.start < covered_to)
            .all(|s| s.start >= covered_from && s.end <= covered_to);
        while cursor + 1 < segments.len()
            && segments.get(cursor).is_some_and(|s| s.end <= covered_to)
        {
            cursor += 1;
        }

        let quality = if exact {
            config.metered_quality
        } else {
            config.prorated_quality
        };
        out.push(
            MeterInterval::build(
                bucket_from,
                bucket_to,
                closing - opening,
                quality,
                config.obis_code,
            )
            .map_err(|_| SessionError::OutOfRange { at: bucket_from })?,
        );
        opening = closing;
        slot_start = bucket_to;
    }
    Ok(Series::new(config.resolution, config.day_boundary, out)?)
}

/// Sort the readings, refuse a contradiction, and cut the span into one
/// segment per pair of adjacent readings.
fn build_segments(samples: &[MeterSample]) -> Result<Vec<Segment>, SessionError> {
    let mut sorted: Vec<MeterSample> = samples.to_vec();
    sorted.sort_by_key(|s| s.at);
    let mut distinct: Vec<MeterSample> = Vec::with_capacity(sorted.len());
    for s in sorted {
        match distinct.last() {
            Some(prev) if prev.at == s.at && prev.reading != s.reading => {
                return Err(SessionError::ConflictingSamples { at: s.at });
            }
            Some(prev) if prev.at == s.at => {}
            Some(prev) if s.reading < prev.reading => {
                return Err(SessionError::SamplesNotMonotonic { at: s.at });
            }
            _ => distinct.push(s),
        }
    }
    let base = distinct.first().map_or(Decimal::ZERO, |s| s.reading);
    Ok(distinct
        .windows(2)
        .filter_map(|pair| match pair {
            [a, b] => Some(Segment {
                start: a.at,
                end: b.at,
                energy: b.reading - a.reading,
                cum_before: a.reading - base,
            }),
            _ => None,
        })
        .collect())
}

/// `x` cut to [`ALLOCATION_DP`] toward zero.
fn cut(x: Decimal) -> Decimal {
    x.round_dp_with_strategy(ALLOCATION_DP, ALLOCATION_STRATEGY)
}

/// Energy delivered from the session start up to `t`.
///
/// Pinned to `0` at `from` and the total at `to`; in between, whole segments
/// plus the wall-clock share of the one `t` falls in, cut to
/// [`ALLOCATION_DP`] (truncation keeps it monotone).
fn cumulative(
    segments: &[Segment],
    hint: usize,
    from: OffsetDateTime,
    to: OffsetDateTime,
    energy: Decimal,
    t: OffsetDateTime,
) -> Decimal {
    if t <= from {
        return Decimal::ZERO;
    }
    if t >= to {
        return energy;
    }
    // `hint` is the caller's cursor: the segments tile the span in order, so
    // the one containing `t` is at or after it.
    let found = segments
        .get(hint..)
        .unwrap_or_default()
        .iter()
        .find(|s| s.start < t && t <= s.end)
        .or_else(|| segments.iter().find(|s| s.start < t && t <= s.end));
    let Some(seg) = found else {
        // Unreachable (the segments tile `[from, to]`); never return zero, or
        // the next slot would go negative.
        return segments
            .last()
            .map_or(Decimal::ZERO, |s| cut(s.cum_before + s.energy));
    };
    let span = (seg.end - seg.start).whole_seconds();
    if span <= 0 {
        return cut(seg.cum_before + seg.energy);
    }
    let elapsed = (t - seg.start).whole_seconds();
    cut(seg.cum_before + seg.energy * Decimal::from(elapsed) / Decimal::from(span))
}

// ── merge_sessions ────────────────────────────────────────────────────────────

/// Add several sessions on the configured grid, slot by slot.
///
/// A union of slots: an absent slot is zero energy, and only slots something
/// touched appear. Each slot carries the worst quality of its contributors.
///
/// # Errors
///
/// [`GridMismatch`](SessionError::GridMismatch) for a series on another
/// resolution, day boundary or OBIS channel than `config` names.
///
/// ```rust
/// use metering::allocation::session::{MeterSample, SessionSplitConfig, merge_sessions, split_session};
/// use rust_decimal::dec;
/// use time::macros::datetime;
///
/// let cfg = SessionSplitConfig::quarter_hourly();
/// let session = |from, to, kwh| {
///     split_session(&[MeterSample::new(from, dec!(0)), MeterSample::new(to, kwh)], &cfg)
/// };
/// // Two cars, overlapping in the 12:15 slot and nowhere else.
/// let a = session(datetime!(2026-06-01 12:00 UTC), datetime!(2026-06-01 12:30 UTC), dec!(8))?;
/// let b = session(datetime!(2026-06-01 12:15 UTC), datetime!(2026-06-01 12:45 UTC), dec!(4))?;
///
/// let merged = merge_sessions(&cfg, &[a, b])?;
/// assert_eq!(merged.len(), 3, "a union of the slots");
/// assert_eq!(merged.as_slice()[1].value(), dec!(6), "both cars charging");
/// assert_eq!(merged.billable_total(), Some(dec!(12)));
/// # Ok::<(), metering::allocation::session::SessionError>(())
/// ```
pub fn merge_sessions(
    config: &SessionSplitConfig,
    series: &[Series],
) -> Result<Series, SessionError> {
    let mut slots: BTreeMap<OffsetDateTime, (OffsetDateTime, Decimal, QualityFlag)> =
        BTreeMap::new();
    for (index, one) in series.iter().enumerate() {
        if one.resolution() != config.resolution
            || one.boundary() != config.day_boundary
            || (!one.is_empty() && one.channel() != config.obis_code)
        {
            return Err(SessionError::GridMismatch { index });
        }
        for iv in one {
            let entry =
                slots
                    .entry(iv.from())
                    .or_insert((iv.to(), Decimal::ZERO, QualityFlag::Measured));
            entry.1 += iv.value();
            entry.2 = entry.2.worse_of(iv.quality());
        }
    }
    let intervals = slots
        .into_iter()
        .map(|(from, (to, value, quality))| {
            MeterInterval::build(from, to, value, quality, config.obis_code)
                .map_err(|_| SessionError::OutOfRange { at: from })
        })
        .collect::<Result<Vec<_>, _>>()?;
    Ok(Series::new(
        config.resolution,
        config.day_boundary,
        intervals,
    )?)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::series::interval::Direction;
    use rust_decimal::dec;
    use time::Duration;
    use time::macros::{date, datetime};

    fn cfg() -> SessionSplitConfig {
        SessionSplitConfig::quarter_hourly()
    }

    fn s(at: OffsetDateTime, reading: Decimal) -> MeterSample {
        MeterSample::new(at, reading)
    }

    fn values(series: &Series) -> Vec<Decimal> {
        series.iter().map(MeterInterval::value).collect()
    }

    #[test]
    fn a_span_inside_one_slot_is_that_slot_and_measured() {
        let out = split_session(
            &[
                s(datetime!(2026-06-01 12:02 UTC), dec!(10)),
                s(datetime!(2026-06-01 12:09 UTC), dec!(13.75)),
            ],
            &cfg(),
        )
        .unwrap();
        assert_eq!(out.len(), 1);
        assert_eq!(out.as_slice()[0].from(), datetime!(2026-06-01 12:00 UTC));
        assert_eq!(out.as_slice()[0].value(), dec!(3.75));
        assert_eq!(out.as_slice()[0].quality(), QualityFlag::Measured);
    }

    #[test]
    fn a_segment_crossing_a_boundary_is_estimated_on_both_sides() {
        let out = split_session(
            &[
                s(datetime!(2026-06-01 12:10 UTC), dec!(0)),
                s(datetime!(2026-06-01 12:20 UTC), dec!(2)),
            ],
            &cfg(),
        )
        .unwrap();
        assert_eq!(values(&out), vec![dec!(1), dec!(1)]);
        assert!(out.iter().all(|iv| iv.quality() == QualityFlag::Estimated));
    }

    #[test]
    fn a_still_register_gives_zeros_not_gaps() {
        let t = datetime!(2026-06-01 12:00 UTC);
        let out = split_session(
            &[
                s(t, dec!(0)),
                s(t + Duration::minutes(15), dec!(4)),
                s(t + Duration::minutes(45), dec!(4)),
                s(t + Duration::minutes(60), dec!(6)),
            ],
            &cfg(),
        )
        .unwrap();
        assert_eq!(values(&out), vec![dec!(4), dec!(0), dec!(0), dec!(2)]);
        // The 12:15–12:45 segment crosses 12:30, so its two slots are pro rata.
        let flags: Vec<QualityFlag> = out.iter().map(MeterInterval::quality).collect();
        assert_eq!(flags[0], QualityFlag::Measured);
        assert_eq!(flags[1], QualityFlag::Estimated);
    }

    #[test]
    fn a_repeating_quotient_still_conserves_the_total() {
        let t = datetime!(2026-06-01 12:00 UTC);
        let out = split_session(
            &[s(t, dec!(0)), s(t + Duration::minutes(45), dec!(1))],
            &cfg(),
        )
        .unwrap();
        assert_eq!(out.len(), 3);
        assert_eq!(out.billable_total(), Some(dec!(1)));
    }

    #[test]
    fn the_autumn_long_day_gets_a_hundred_slots() {
        let day = DayBoundary::Strom.day(date!(2026 - 10 - 25)).unwrap();
        let out =
            split_session(&[s(day.start(), dec!(0)), s(day.end(), dec!(25))], &cfg()).unwrap();
        assert_eq!(out.len(), 100);
        assert_eq!(out.billable_total(), Some(dec!(25)));
    }

    #[test]
    fn a_gastag_grid_cuts_at_six() {
        let out = split_session(
            &[
                s(datetime!(2026-01-05 11:00 UTC), dec!(0)),
                s(datetime!(2026-01-06 11:00 UTC), dec!(24)),
            ],
            &cfg().at(Resolution::Day).on(DayBoundary::Gas),
        )
        .unwrap();
        assert_eq!(out.len(), 2);
        assert_eq!(
            out.as_slice()[1].from(),
            DayBoundary::Gas.day(date!(2026 - 01 - 06)).unwrap().start()
        );
        assert_eq!(out.billable_total(), Some(dec!(24)));
    }

    #[test]
    fn the_obis_channel_is_stamped_and_gives_the_direction() {
        let t = datetime!(2026-06-01 12:00 UTC);
        let out = split_session(
            &[s(t, dec!(0)), s(t + Duration::minutes(15), dec!(1))],
            &cfg().with_obis(ObisCode::STROM_EINSPEISUNG_LASTGANG),
        )
        .unwrap();
        assert_eq!(out.as_slice()[0].direction(), Some(Direction::Export));
    }

    /// The readings are the only input, so a total cannot contradict them;
    /// what is left to contradict is the readings themselves.
    #[test]
    fn contradictory_readings_are_refused() {
        let t = datetime!(2026-06-01 12:00 UTC);
        assert_eq!(
            split_session(&[s(t, dec!(1))], &cfg()),
            Err(SessionError::TooFewSamples)
        );
        assert_eq!(
            split_session(&[s(t, dec!(1)), s(t, dec!(1))], &cfg()),
            Err(SessionError::TooFewSamples)
        );
        let later = t + Duration::minutes(5);
        assert_eq!(
            split_session(&[s(t, dec!(5)), s(later, dec!(4))], &cfg()),
            Err(SessionError::SamplesNotMonotonic { at: later })
        );
        // Two readings at one instant: refused in either order.
        let a = [s(t, dec!(0)), s(later, dec!(1)), s(later, dec!(2))];
        let mut b = a;
        b.swap(1, 2);
        assert_eq!(
            split_session(&a, &cfg()),
            Err(SessionError::ConflictingSamples { at: later })
        );
        assert_eq!(
            split_session(&b, &cfg()),
            Err(SessionError::ConflictingSamples { at: later })
        );
    }

    #[test]
    fn merging_keeps_channels_apart_and_takes_the_worst_quality() {
        let t = datetime!(2026-06-01 12:00 UTC);
        let one = |q| {
            split_session(
                &[s(t, dec!(0)), s(t + Duration::minutes(15), dec!(2))],
                &cfg().metered_as(q),
            )
            .unwrap()
        };
        let merged = merge_sessions(
            &cfg(),
            &[one(QualityFlag::Measured), one(QualityFlag::Estimated)],
        )
        .unwrap();
        assert_eq!(values(&merged), vec![dec!(4)]);
        assert_eq!(merged.as_slice()[0].quality(), QualityFlag::Estimated);

        let export = split_session(
            &[s(t, dec!(0)), s(t + Duration::minutes(15), dec!(1))],
            &cfg().with_obis(ObisCode::STROM_EINSPEISUNG_LASTGANG),
        )
        .unwrap();
        assert_eq!(
            merge_sessions(&cfg(), &[one(QualityFlag::Measured), export]),
            Err(SessionError::GridMismatch { index: 1 })
        );
        assert!(merge_sessions(&cfg(), &[]).unwrap().is_empty());
    }

    #[test]
    fn a_minute_log_is_measured_in_every_whole_slot() {
        let day = DayBoundary::Strom.day(date!(2026 - 06 - 15)).unwrap();
        let samples: Vec<MeterSample> = (0..=1440)
            .map(|m| s(day.start() + Duration::minutes(m), Decimal::from(m)))
            .collect();
        let out = split_session(&samples, &cfg()).unwrap();
        assert_eq!(out.len(), 96);
        assert!(
            out.iter()
                .all(|iv| iv.value() == dec!(15) && iv.quality() == QualityFlag::Measured)
        );
    }
}