grid_tariffs/
costs.rs

1use std::slice::Iter;
2
3use chrono::{DateTime, Datelike};
4use serde::Serialize;
5
6use crate::{
7    Country, Language, LoadType, Money, Timezone, helpers,
8    hours::Hours,
9    months::{Month, Months},
10};
11
12// TODO: Make CostBuilder!
13#[derive(Debug, Clone, Copy, Serialize)]
14#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
15pub enum Cost {
16    None,
17    /// Cost has not been verified
18    Unverified,
19    Fixed(Money),
20    Fuses(&'static [(u16, Money)]),
21    /// Fuse size combined with a yearly energy consumption limit
22    FusesYearlyConsumption(&'static [(u16, Option<u32>, Money)]),
23    FuseRange(&'static [(u16, u16, Money)]),
24}
25
26impl Cost {
27    pub const fn fuses(values: &'static [(u16, Money)]) -> Self {
28        Self::Fuses(values)
29    }
30
31    pub const fn fuse_range(ranges: &'static [(u16, u16, Money)]) -> Self {
32        Self::FuseRange(ranges)
33    }
34
35    pub const fn fuses_with_yearly_consumption(
36        values: &'static [(u16, Option<u32>, Money)],
37    ) -> Self {
38        Self::FusesYearlyConsumption(values)
39    }
40
41    pub const fn fixed(int: i64, fract: u8) -> Self {
42        Self::Fixed(Money::new(int, fract))
43    }
44
45    pub const fn fixed_yearly(int: i64, fract: u8) -> Self {
46        Self::Fixed(Money::new(int, fract).divide_by(12))
47    }
48
49    pub const fn fixed_subunit(subunit: f64) -> Self {
50        Self::Fixed(Money::new_subunit(subunit))
51    }
52
53    pub const fn is_unverified(&self) -> bool {
54        matches!(self, Self::Unverified)
55    }
56
57    pub const fn divide_by(&self, by: i64) -> Self {
58        match self {
59            Self::None => Self::None,
60            Self::Unverified => Self::Unverified,
61            Self::Fixed(money) => Self::Fixed(money.divide_by(by)),
62            Self::Fuses(_) => panic!(".divide_by() is unsupported on Cost::Fuses"),
63            Self::FusesYearlyConsumption(_) => {
64                panic!(".divide_by() is unsupported on Cost::FuseRangeYearlyConsumption")
65            }
66            Self::FuseRange(_) => panic!(".divide_by() is unsupported on Cost::FuseRange"),
67        }
68    }
69
70    pub const fn cost_for(&self, fuse_size: u16, yearly_consumption: u32) -> Option<Money> {
71        match *self {
72            Cost::None => None,
73            Cost::Unverified => None,
74            Cost::Fixed(money) => Some(money),
75            Cost::Fuses(values) => {
76                let mut i = 0;
77                while i < values.len() {
78                    let (fsize, money) = values[i];
79                    if fuse_size == fsize {
80                        return Some(money);
81                    }
82                    i += 1;
83                }
84                None
85            }
86            Cost::FusesYearlyConsumption(values) => {
87                let mut i = 0;
88                while i < values.len() {
89                    let (fsize, max_consumption, money) = values[i];
90                    if fsize == fuse_size {
91                        if let Some(max_consumption) = max_consumption {
92                            if yearly_consumption <= max_consumption {
93                                return Some(money);
94                            }
95                        } else {
96                            return Some(money);
97                        }
98                    }
99                    i += 1;
100                }
101                None
102            }
103            Cost::FuseRange(ranges) => {
104                let mut i = 0;
105                while i < ranges.len() {
106                    let (min, max, money) = ranges[i];
107                    if fuse_size >= min && fuse_size <= max {
108                        return Some(money);
109                    }
110                    i += 1;
111                }
112                None
113            }
114        }
115    }
116
117    pub const fn add_vat(&self, country: Country) -> Cost {
118        match self {
119            Cost::None => Cost::None,
120            Cost::Unverified => Cost::Unverified,
121            Cost::Fixed(money) => Cost::Fixed(money.add_vat(country)),
122            Cost::Fuses(_) => todo!(),
123            Cost::FusesYearlyConsumption(_) => todo!(),
124            Cost::FuseRange(_) => todo!(),
125        }
126    }
127
128    pub const fn remove_vat(&self, country: Country) -> Cost {
129        match self {
130            Cost::None => Cost::None,
131            Cost::Unverified => Cost::Unverified,
132            Cost::Fixed(money) => Cost::Fixed(money.remove_vat(country)),
133            Cost::Fuses(_) => todo!(),
134            Cost::FusesYearlyConsumption(_) => todo!(),
135            Cost::FuseRange(_) => todo!(),
136        }
137    }
138
139    pub fn is_yearly_consumption_based(&self, fuse_size: u16) -> bool {
140        match self {
141            Cost::FusesYearlyConsumption(items) => items
142                .iter()
143                .filter(|(fsize, _, _)| *fsize == fuse_size)
144                .any(|(_, yearly_consumption, _)| yearly_consumption.is_some()),
145            _ => false,
146        }
147    }
148}
149
150/// How to match against the different CostPeriod items
151#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
152#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
153pub enum CostPeriodMatching {
154    /// Only return the first matching CostPeriod
155    First,
156    /// Return all CostPeriod items that match
157    All,
158}
159
160#[derive(Debug, Clone, Copy, Serialize)]
161#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
162pub struct CostPeriods {
163    match_method: CostPeriodMatching,
164    periods: &'static [CostPeriod],
165}
166
167impl CostPeriods {
168    // Will return first matching cost period
169    pub const fn new_first(periods: &'static [CostPeriod]) -> Self {
170        Self {
171            match_method: CostPeriodMatching::First,
172            periods,
173        }
174    }
175
176    // Will return all matching cost periods
177    pub const fn new_all(periods: &'static [CostPeriod]) -> Self {
178        Self {
179            match_method: CostPeriodMatching::All,
180            periods,
181        }
182    }
183
184    pub const fn match_method(&self) -> CostPeriodMatching {
185        self.match_method
186    }
187
188    pub fn iter(&self) -> Iter<'_, CostPeriod> {
189        self.periods.iter()
190    }
191
192    pub(crate) fn is_yearly_consumption_based(&self, fuse_size: u16) -> bool {
193        self.periods
194            .iter()
195            .any(|cp| cp.is_yearly_consumption_based(fuse_size))
196    }
197
198    pub fn matching_periods<Tz: chrono::TimeZone>(
199        &self,
200        timestamp: DateTime<Tz>,
201    ) -> Vec<&CostPeriod>
202    where
203        DateTime<Tz>: Copy,
204    {
205        let mut ret = vec![];
206        for period in self.periods {
207            if period.matches(timestamp) {
208                ret.push(period);
209                if self.match_method == CostPeriodMatching::First {
210                    break;
211                }
212            }
213        }
214        ret
215    }
216}
217
218/// Like CostPeriods, but with costs being simple Money objects
219#[derive(Debug, Clone, Serialize)]
220#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
221pub struct CostPeriodsSimple {
222    periods: Vec<CostPeriodSimple>,
223}
224
225impl CostPeriodsSimple {
226    pub(crate) fn new(
227        periods: CostPeriods,
228        fuse_size: u16,
229        yearly_consumption: u32,
230        language: Language,
231    ) -> Self {
232        Self {
233            periods: periods
234                .periods
235                .iter()
236                .flat_map(|period| {
237                    CostPeriodSimple::new(period, fuse_size, yearly_consumption, language)
238                })
239                .collect(),
240        }
241    }
242}
243
244#[derive(Debug, Clone, Serialize)]
245#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
246pub struct CostPeriod {
247    cost: Cost,
248    load: LoadType,
249    #[serde(serialize_with = "helpers::skip_nones")]
250    include: [Option<Include>; 2],
251    #[serde(serialize_with = "helpers::skip_nones")]
252    exclude: [Option<Exclude>; 2],
253}
254
255/// Like CostPeriod, but with cost being a simple Money object
256#[derive(Debug, Clone, Serialize)]
257#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
258pub(crate) struct CostPeriodSimple {
259    cost: Money,
260    load: LoadType,
261    include: Vec<Include>,
262    exclude: Vec<Exclude>,
263    info: String,
264}
265
266impl CostPeriodSimple {
267    fn new(
268        period: &CostPeriod,
269        fuse_size: u16,
270        yearly_consumption: u32,
271        language: Language,
272    ) -> Option<Self> {
273        let cost = period.cost().cost_for(fuse_size, yearly_consumption)?;
274        Some(
275            Self {
276                cost,
277                load: period.load,
278                include: period.include.into_iter().flatten().collect(),
279                exclude: period.exclude.into_iter().flatten().collect(),
280                info: Default::default(),
281            }
282            .add_info(language),
283        )
284    }
285
286    fn add_info(mut self, language: Language) -> Self {
287        let mut infos = Vec::new();
288        for include in &self.include {
289            infos.push(include.translate(language));
290        }
291        for exclude in &self.exclude {
292            infos.push(exclude.translate(language).into());
293        }
294        self.info = infos.join(", ");
295        self
296    }
297}
298
299impl CostPeriod {
300    pub const fn builder() -> CostPeriodBuilder {
301        CostPeriodBuilder::new()
302    }
303
304    pub const fn cost(&self) -> Cost {
305        self.cost
306    }
307
308    pub const fn load(&self) -> LoadType {
309        self.load
310    }
311
312    pub fn matches<Tz: chrono::TimeZone>(&self, timestamp: DateTime<Tz>) -> bool
313    where
314        DateTime<Tz>: Copy,
315    {
316        for include in self.include_period_types() {
317            if !include.matches(timestamp) {
318                return false;
319            }
320        }
321
322        for exclude in self.exclude_period_types() {
323            if exclude.matches(timestamp) {
324                return false;
325            }
326        }
327        true
328    }
329
330    fn include_period_types(&self) -> Vec<Include> {
331        self.include.iter().flatten().copied().collect()
332    }
333
334    fn exclude_period_types(&self) -> Vec<Exclude> {
335        self.exclude.iter().flatten().copied().collect()
336    }
337
338    fn is_yearly_consumption_based(&self, fuse_size: u16) -> bool {
339        self.cost.is_yearly_consumption_based(fuse_size)
340    }
341}
342
343#[derive(Clone)]
344pub struct CostPeriodBuilder {
345    timezone: Option<Timezone>,
346    cost: Cost,
347    load: Option<LoadType>,
348    include: [Option<Include>; 2],
349    exclude: [Option<Exclude>; 2],
350}
351
352impl Default for CostPeriodBuilder {
353    fn default() -> Self {
354        Self::new()
355    }
356}
357
358impl CostPeriodBuilder {
359    pub const fn new() -> Self {
360        let builder = Self {
361            timezone: None,
362            cost: Cost::None,
363            load: None,
364            include: [None; 2],
365            exclude: [None; 2],
366        };
367        // TODO: Don't hardcode this!
368        builder.timezone(Timezone::Stockholm)
369    }
370
371    pub const fn build(self) -> CostPeriod {
372        CostPeriod {
373            cost: self.cost,
374            load: self.load.expect("`load` must be specified"),
375            include: self.include,
376            exclude: self.exclude,
377        }
378    }
379
380    pub const fn cost(mut self, cost: Cost) -> Self {
381        self.cost = cost;
382        self
383    }
384
385    pub const fn load(mut self, load: LoadType) -> Self {
386        self.load = Some(load);
387        self
388    }
389
390    pub const fn fixed_cost(mut self, int: i64, fract: u8) -> Self {
391        self.cost = Cost::fixed(int, fract);
392        self
393    }
394
395    pub const fn fixed_cost_subunit(mut self, subunit: f64) -> Self {
396        self.cost = Cost::fixed_subunit(subunit);
397        self
398    }
399
400    pub const fn timezone(mut self, timezone: Timezone) -> Self {
401        self.timezone = Some(timezone);
402        self
403    }
404
405    const fn get_timezone(&self) -> Timezone {
406        self.timezone.expect("`timezone` must be specified")
407    }
408
409    pub const fn include(mut self, period_type: Include) -> Self {
410        let mut i = 0;
411        while i < self.include.len() {
412            if self.include[i].is_some() {
413                i += 1;
414            } else {
415                self.include[i] = Some(period_type);
416                return self;
417            }
418        }
419        panic!("Too many includes");
420    }
421
422    pub const fn months(self, from: Month, to: Month) -> Self {
423        let timezone = self.get_timezone();
424        self.include(Include::Months(Months::new(from, to, timezone)))
425    }
426
427    pub const fn month(self, month: Month) -> Self {
428        self.months(month, month)
429    }
430
431    pub const fn hours(self, from: u8, to_inclusive: u8) -> Self {
432        let timezone = self.get_timezone();
433        self.include(Include::Hours(Hours::new(from, to_inclusive, timezone)))
434    }
435
436    const fn exclude(mut self, period_type: Exclude) -> Self {
437        let mut i = 0;
438        while i < self.exclude.len() {
439            if self.exclude[i].is_some() {
440                i += 1;
441            } else {
442                self.exclude[i] = Some(period_type);
443                return self;
444            }
445        }
446        panic!("Too many excludes");
447    }
448
449    pub const fn exclude_public_holidays(self, country: Country) -> Self {
450        let tz = self.get_timezone();
451        self.exclude(Exclude::PublicHolidays(country, tz))
452    }
453
454    pub const fn exclude_weekends(self) -> Self {
455        let tz = self.get_timezone();
456        self.exclude(Exclude::Weekends(tz))
457    }
458}
459
460#[derive(Debug, Clone, Copy, Serialize)]
461#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
462pub enum Include {
463    Months(Months),
464    Hours(Hours),
465}
466
467impl Include {
468    fn translate(&self, language: Language) -> String {
469        match self {
470            Include::Months(months) => months.translate(language),
471            Include::Hours(hours) => hours.translate(language),
472        }
473    }
474
475    fn matches<Tz: chrono::TimeZone>(&self, timestamp: DateTime<Tz>) -> bool {
476        match self {
477            Include::Months(months) => months.matches(timestamp),
478            Include::Hours(hours) => hours.matches(timestamp),
479        }
480    }
481}
482
483#[derive(Debug, Clone, Copy, Serialize)]
484#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
485pub enum Exclude {
486    Weekends(Timezone),
487    #[serde(rename = "Holidays")]
488    PublicHolidays(Country, Timezone),
489}
490
491impl Exclude {
492    pub(crate) fn translate(&self, language: Language) -> &'static str {
493        match language {
494            Language::En => match self {
495                Exclude::Weekends(_) => "Weekends",
496                Exclude::PublicHolidays(country, _) => match country {
497                    Country::SE => "Swedish holidays",
498                },
499            },
500            Language::Sv => match self {
501                Exclude::Weekends(_) => "Helg",
502                Exclude::PublicHolidays(country, _) => match country {
503                    Country::SE => "Svenska helgdagar",
504                },
505            },
506        }
507    }
508
509    fn matches<Tz: chrono::TimeZone>(&self, timestamp: DateTime<Tz>) -> bool {
510        let tz_timestamp = timestamp.with_timezone(&self.tz());
511        match self {
512            Exclude::Weekends(_) => (6..=7).contains(&tz_timestamp.weekday().number_from_monday()),
513            Exclude::PublicHolidays(country, _) => {
514                country.is_public_holiday(tz_timestamp.date_naive())
515            }
516        }
517    }
518
519    const fn tz(&self) -> chrono_tz::Tz {
520        match self {
521            Exclude::Weekends(timezone) => timezone.to_tz(),
522            Exclude::PublicHolidays(_, timezone) => timezone.to_tz(),
523        }
524    }
525}
526
527#[cfg(test)]
528mod tests {
529
530    use super::*;
531    use crate::money::Money;
532    use crate::months::Month::*;
533    use crate::{Stockholm, Utc};
534
535    #[test]
536    fn cost_for_none() {
537        const NONE_COST: Cost = Cost::None;
538        assert_eq!(NONE_COST.cost_for(16, 0), None);
539        assert_eq!(NONE_COST.cost_for(25, 5000), None);
540    }
541
542    #[test]
543    fn cost_for_unverified() {
544        const UNVERIFIED_COST: Cost = Cost::Unverified;
545        assert_eq!(UNVERIFIED_COST.cost_for(16, 0), None);
546        assert_eq!(UNVERIFIED_COST.cost_for(25, 5000), None);
547    }
548
549    #[test]
550    fn cost_for_fixed() {
551        const FIXED_COST: Cost = Cost::Fixed(Money::new(100, 50));
552        // Fixed cost should return the same value regardless of fuse size or consumption
553        assert_eq!(FIXED_COST.cost_for(16, 0), Some(Money::new(100, 50)));
554        assert_eq!(FIXED_COST.cost_for(25, 5000), Some(Money::new(100, 50)));
555        assert_eq!(FIXED_COST.cost_for(63, 10000), Some(Money::new(100, 50)));
556    }
557
558    #[test]
559    fn cost_for_fuses_exact_match() {
560        const FUSES_COST: Cost = Cost::fuses(&[
561            (16, Money::new(50, 0)),
562            (25, Money::new(75, 0)),
563            (35, Money::new(100, 0)),
564            (50, Money::new(150, 0)),
565        ]);
566
567        // Test exact matches
568        assert_eq!(FUSES_COST.cost_for(16, 0), Some(Money::new(50, 0)));
569        assert_eq!(FUSES_COST.cost_for(25, 0), Some(Money::new(75, 0)));
570        assert_eq!(FUSES_COST.cost_for(35, 0), Some(Money::new(100, 0)));
571        assert_eq!(FUSES_COST.cost_for(50, 0), Some(Money::new(150, 0)));
572
573        // Yearly consumption should not affect the result
574        assert_eq!(FUSES_COST.cost_for(25, 500000), Some(Money::new(75, 0)));
575    }
576
577    #[test]
578    fn cost_for_fuses_no_match() {
579        const FUSES_COST: Cost = Cost::fuses(&[(16, Money::new(50, 0)), (25, Money::new(75, 0))]);
580
581        // Test non-matching fuse sizes
582        assert_eq!(FUSES_COST.cost_for(20, 0), None);
583        assert_eq!(FUSES_COST.cost_for(63, 0), None);
584    }
585
586    #[test]
587    fn cost_for_fuses_yearly_consumption_with_limit() {
588        const FUSES_WITH_CONSUMPTION: Cost = Cost::fuses_with_yearly_consumption(&[
589            (16, Some(5000), Money::new(50, 0)),
590            (16, None, Money::new(75, 0)),
591            (25, Some(10000), Money::new(100, 0)),
592            (25, None, Money::new(125, 0)),
593        ]);
594
595        // 16A fuse with consumption below limit - matches the entry with the limit
596        assert_eq!(
597            FUSES_WITH_CONSUMPTION.cost_for(16, 3000),
598            Some(Money::new(50, 0))
599        );
600
601        // 16A fuse with consumption at limit - matches the entry with the limit
602        assert_eq!(
603            FUSES_WITH_CONSUMPTION.cost_for(16, 5000),
604            Some(Money::new(50, 0))
605        );
606
607        // 16A fuse with consumption above limit - falls through to entry with no limit
608        assert_eq!(
609            FUSES_WITH_CONSUMPTION.cost_for(16, 6000),
610            Some(Money::new(75, 0))
611        );
612
613        // 16A fuse with very high consumption - falls through to entry with no limit
614        assert_eq!(
615            FUSES_WITH_CONSUMPTION.cost_for(16, 20000),
616            Some(Money::new(75, 0))
617        );
618
619        // 25A fuse with consumption at limit - matches the entry with 10000 limit
620        assert_eq!(
621            FUSES_WITH_CONSUMPTION.cost_for(25, 10000),
622            Some(Money::new(100, 0))
623        );
624
625        // 25A fuse with consumption above limit - falls through to entry with no limit
626        assert_eq!(
627            FUSES_WITH_CONSUMPTION.cost_for(25, 15000),
628            Some(Money::new(125, 0))
629        );
630
631        // 25A fuse with consumption below limit - matches the entry with the limit
632        assert_eq!(
633            FUSES_WITH_CONSUMPTION.cost_for(25, 5000),
634            Some(Money::new(100, 0))
635        );
636    }
637
638    #[test]
639    fn cost_for_fuses_yearly_consumption_no_limit() {
640        const FUSES_NO_LIMIT: Cost = Cost::fuses_with_yearly_consumption(&[
641            (16, None, Money::new(50, 0)),
642            (25, None, Money::new(75, 0)),
643        ]);
644
645        // Should match regardless of consumption when limit is None
646        assert_eq!(FUSES_NO_LIMIT.cost_for(16, 0), Some(Money::new(50, 0)));
647        assert_eq!(FUSES_NO_LIMIT.cost_for(16, 1000), Some(Money::new(50, 0)));
648        assert_eq!(FUSES_NO_LIMIT.cost_for(16, 50000), Some(Money::new(50, 0)));
649        assert_eq!(FUSES_NO_LIMIT.cost_for(25, 100000), Some(Money::new(75, 0)));
650    }
651
652    #[test]
653    fn cost_for_fuses_yearly_consumption_no_fuse_match() {
654        const FUSES_WITH_CONSUMPTION: Cost = Cost::fuses_with_yearly_consumption(&[
655            (16, Some(5000), Money::new(50, 0)),
656            (25, Some(10000), Money::new(100, 0)),
657        ]);
658
659        // Non-matching fuse size
660        assert_eq!(FUSES_WITH_CONSUMPTION.cost_for(35, 5000), None);
661        assert_eq!(FUSES_WITH_CONSUMPTION.cost_for(50, 10000), None);
662    }
663
664    #[test]
665    fn cost_for_fuses_yearly_consumption_max_limit_no_fallback() {
666        const FUSES_ONLY_LIMITS: Cost = Cost::fuses_with_yearly_consumption(&[
667            (16, Some(5000), Money::new(50, 0)),
668            (25, Some(10000), Money::new(100, 0)),
669        ]);
670
671        // Matching fuse size with consumption at or below limit - should match
672        assert_eq!(FUSES_ONLY_LIMITS.cost_for(16, 0), Some(Money::new(50, 0)));
673        assert_eq!(
674            FUSES_ONLY_LIMITS.cost_for(16, 3000),
675            Some(Money::new(50, 0))
676        );
677        assert_eq!(
678            FUSES_ONLY_LIMITS.cost_for(16, 4999),
679            Some(Money::new(50, 0))
680        );
681        assert_eq!(
682            FUSES_ONLY_LIMITS.cost_for(16, 5000),
683            Some(Money::new(50, 0))
684        );
685        assert_eq!(
686            FUSES_ONLY_LIMITS.cost_for(25, 9999),
687            Some(Money::new(100, 0))
688        );
689        assert_eq!(
690            FUSES_ONLY_LIMITS.cost_for(25, 10000),
691            Some(Money::new(100, 0))
692        );
693
694        // Above limit with no fallback - should return None
695        assert_eq!(FUSES_ONLY_LIMITS.cost_for(16, 5001), None);
696        assert_eq!(FUSES_ONLY_LIMITS.cost_for(16, 10000), None);
697        assert_eq!(FUSES_ONLY_LIMITS.cost_for(25, 10001), None);
698        assert_eq!(FUSES_ONLY_LIMITS.cost_for(25, 20000), None);
699    }
700
701    #[test]
702    fn cost_for_fuse_range_within_range() {
703        const FUSE_BASED: Cost = Cost::fuse_range(&[
704            (16, 35, Money::new(54, 0)),
705            (35, u16::MAX, Money::new(108, 50)),
706        ]);
707
708        // Test values below the first range
709        assert_eq!(FUSE_BASED.cost_for(10, 0), None);
710        assert_eq!(FUSE_BASED.cost_for(15, 0), None);
711
712        // Test values within the first range
713        assert_eq!(FUSE_BASED.cost_for(16, 0), Some(Money::new(54, 0)));
714        assert_eq!(FUSE_BASED.cost_for(25, 0), Some(Money::new(54, 0)));
715        assert_eq!(FUSE_BASED.cost_for(35, 0), Some(Money::new(54, 0)));
716
717        // Test values within the second range
718        assert_eq!(FUSE_BASED.cost_for(36, 0), Some(Money::new(108, 50)));
719        assert_eq!(FUSE_BASED.cost_for(50, 0), Some(Money::new(108, 50)));
720        assert_eq!(FUSE_BASED.cost_for(200, 0), Some(Money::new(108, 50)));
721        assert_eq!(FUSE_BASED.cost_for(u16::MAX, 0), Some(Money::new(108, 50)));
722    }
723
724    #[test]
725    fn cost_for_fuse_range_multiple_ranges() {
726        const MULTI_RANGE: Cost = Cost::fuse_range(&[
727            (1, 15, Money::new(20, 0)),
728            (16, 35, Money::new(50, 0)),
729            (36, 63, Money::new(100, 0)),
730            (64, u16::MAX, Money::new(200, 0)),
731        ]);
732
733        // Test each range
734        assert_eq!(MULTI_RANGE.cost_for(10, 0), Some(Money::new(20, 0)));
735        assert_eq!(MULTI_RANGE.cost_for(25, 0), Some(Money::new(50, 0)));
736        assert_eq!(MULTI_RANGE.cost_for(50, 0), Some(Money::new(100, 0)));
737        assert_eq!(MULTI_RANGE.cost_for(100, 0), Some(Money::new(200, 0)));
738
739        // Yearly consumption should not affect range-based costs
740        assert_eq!(MULTI_RANGE.cost_for(25, 10000), Some(Money::new(50, 0)));
741    }
742
743    #[test]
744    fn include_matches_hours() {
745        let include = Include::Hours(Hours::new(6, 22, Stockholm));
746        let timestamp_match = Stockholm.dt(2025, 1, 15, 14, 0, 0);
747        let timestamp_no_match = Stockholm.dt(2025, 1, 15, 23, 0, 0);
748
749        assert!(include.matches(timestamp_match));
750        assert!(!include.matches(timestamp_no_match));
751    }
752
753    #[test]
754    fn include_matches_months() {
755        let include = Include::Months(Months::new(November, March, Stockholm));
756        let timestamp_match = Stockholm.dt(2025, 1, 15, 12, 0, 0);
757        let timestamp_no_match = Stockholm.dt(2025, 7, 15, 12, 0, 0);
758
759        assert!(include.matches(timestamp_match));
760        assert!(!include.matches(timestamp_no_match));
761    }
762
763    #[test]
764    fn exclude_matches_weekends_saturday() {
765        let exclude = Exclude::Weekends(Stockholm);
766        // January 4, 2025 is a Saturday
767        let timestamp = Stockholm.dt(2025, 1, 4, 12, 0, 0);
768        assert!(exclude.matches(timestamp));
769    }
770
771    #[test]
772    fn exclude_matches_weekends_sunday() {
773        let exclude = Exclude::Weekends(Stockholm);
774        // January 5, 2025 is a Sunday
775        let timestamp = Stockholm.dt(2025, 1, 5, 12, 0, 0);
776        assert!(exclude.matches(timestamp));
777    }
778
779    #[test]
780    fn exclude_does_not_match_weekday() {
781        let exclude = Exclude::Weekends(Stockholm);
782        // January 6, 2025 is a Monday
783        let timestamp = Stockholm.dt(2025, 1, 6, 12, 0, 0);
784        assert!(!exclude.matches(timestamp));
785    }
786
787    #[test]
788    fn exclude_matches_swedish_new_year() {
789        let exclude = Exclude::PublicHolidays(Country::SE, Stockholm);
790        // January 1 is a Swedish holiday
791        let timestamp = Stockholm.dt(2025, 1, 1, 12, 0, 0);
792        assert!(exclude.matches(timestamp));
793    }
794
795    #[test]
796    fn exclude_does_not_match_non_holiday() {
797        let exclude = Exclude::PublicHolidays(Country::SE, Stockholm);
798        // January 2, 2025 is not a Swedish holiday
799        let timestamp = Stockholm.dt(2025, 1, 2, 12, 0, 0);
800        assert!(!exclude.matches(timestamp));
801    }
802
803    #[test]
804    fn cost_period_matches_with_single_include() {
805        let period = CostPeriod::builder()
806            .load(LoadType::High)
807            .fixed_cost(10, 0)
808            .hours(6, 22)
809            .build();
810
811        let timestamp_match = Stockholm.dt(2025, 1, 15, 14, 0, 0);
812        let timestamp_no_match = Stockholm.dt(2025, 1, 15, 23, 0, 0);
813
814        assert!(period.matches(timestamp_match));
815        assert!(!period.matches(timestamp_no_match));
816    }
817
818    #[test]
819    fn cost_period_matches_with_multiple_includes() {
820        let period = CostPeriod::builder()
821            .load(LoadType::High)
822            .fixed_cost(10, 0)
823            .hours(6, 22)
824            .months(November, March)
825            .build();
826
827        // Winter daytime - should match
828        let timestamp_match = Stockholm.dt(2025, 1, 15, 14, 0, 0);
829        // Winter nighttime - should not match (wrong hours)
830        let timestamp_wrong_hours = Stockholm.dt(2025, 1, 15, 23, 0, 0);
831        // Summer daytime - should not match (wrong months)
832        let timestamp_wrong_months = Stockholm.dt(2025, 7, 15, 14, 0, 0);
833
834        assert!(period.matches(timestamp_match));
835        assert!(!period.matches(timestamp_wrong_hours));
836        assert!(!period.matches(timestamp_wrong_months));
837    }
838
839    #[test]
840    fn cost_period_matches_with_exclude_weekends() {
841        let period = CostPeriod::builder()
842            .load(LoadType::High)
843            .fixed_cost(10, 0)
844            .hours(6, 22)
845            .exclude_weekends()
846            .build();
847
848        println!("Excludes: {:?}", period.exclude_period_types());
849        println!("Includes: {:?}", period.include_period_types());
850
851        // Monday daytime - should match
852        let timestamp_weekday = Stockholm.dt(2025, 1, 6, 14, 0, 0);
853        // Saturday daytime - should not match (excluded)
854        let timestamp_saturday = Stockholm.dt(2025, 1, 4, 14, 0, 0);
855
856        assert!(period.matches(timestamp_weekday));
857        assert!(!period.matches(timestamp_saturday));
858    }
859
860    #[test]
861    fn cost_period_matches_with_exclude_holidays() {
862        let period = CostPeriod::builder()
863            .load(LoadType::High)
864            .fixed_cost(10, 0)
865            .hours(6, 22)
866            .exclude_public_holidays(Country::SE)
867            .build();
868
869        // Regular weekday - should match
870        let timestamp_regular = Stockholm.dt(2025, 1, 2, 14, 0, 0);
871        // New Year's Day - should not match (excluded)
872        let timestamp_holiday = Stockholm.dt(2025, 1, 1, 14, 0, 0);
873
874        assert!(period.matches(timestamp_regular));
875        assert!(!period.matches(timestamp_holiday));
876    }
877
878    #[test]
879    fn cost_period_matches_complex_scenario() {
880        // Winter high load period: Nov-Mar, 6-22, excluding weekends and holidays
881        let period = CostPeriod::builder()
882            .load(LoadType::High)
883            .fixed_cost(10, 0)
884            .months(November, March)
885            .hours(6, 22)
886            .exclude_weekends()
887            .exclude_public_holidays(Country::SE)
888            .build();
889
890        // Winter weekday daytime (not holiday) - should match
891        let timestamp_match = Stockholm.dt(2025, 1, 15, 14, 0, 0);
892
893        // Winter weekday nighttime - should not match (wrong hours)
894        let timestamp_wrong_hours = Stockholm.dt(2025, 1, 15, 23, 0, 0);
895
896        // Winter Saturday daytime - should not match (weekend)
897        let timestamp_weekend = Stockholm.dt(2025, 1, 4, 14, 0, 0);
898
899        // New Year's Day (holiday) - should not match
900        let timestamp_holiday = Stockholm.dt(2025, 1, 1, 14, 0, 0);
901
902        // Summer weekday daytime - should not match (wrong months)
903        let timestamp_summer = Stockholm.dt(2025, 7, 15, 14, 0, 0);
904
905        assert!(period.matches(timestamp_match));
906        assert!(!period.matches(timestamp_wrong_hours));
907        assert!(!period.matches(timestamp_weekend));
908        assert!(!period.matches(timestamp_holiday));
909        assert!(!period.matches(timestamp_summer));
910    }
911
912    #[test]
913    fn cost_period_matches_base_load() {
914        // Base load period with no restrictions
915        let period = CostPeriod::builder()
916            .load(LoadType::Base)
917            .fixed_cost(5, 0)
918            .build();
919
920        // Should match any time
921        let timestamp1 = Stockholm.dt(2025, 1, 1, 0, 0, 0);
922        let timestamp2 = Stockholm.dt(2025, 7, 15, 23, 59, 59);
923        let timestamp3 = Stockholm.dt(2025, 1, 4, 12, 0, 0);
924
925        assert!(period.matches(timestamp1));
926        assert!(period.matches(timestamp2));
927        assert!(period.matches(timestamp3));
928    }
929
930    #[test]
931    fn include_matches_hours_wraparound() {
932        // Night hours crossing midnight: 22:00 to 05:59
933        let include = Include::Hours(Hours::new(22, 5, Stockholm));
934
935        // Should match late evening
936        let timestamp_evening = Stockholm.dt(2025, 1, 15, 22, 0, 0);
937        assert!(include.matches(timestamp_evening));
938
939        // Should match midnight
940        let timestamp_midnight = Stockholm.dt(2025, 1, 15, 0, 0, 0);
941        assert!(include.matches(timestamp_midnight));
942
943        // Should match early morning
944        let timestamp_morning = Stockholm.dt(2025, 1, 15, 5, 30, 0);
945        assert!(include.matches(timestamp_morning));
946
947        // Should not match daytime
948        let timestamp_day = Stockholm.dt(2025, 1, 15, 14, 0, 0);
949        assert!(!include.matches(timestamp_day));
950
951        // Should not match just after the range
952        let timestamp_after = Stockholm.dt(2025, 1, 15, 6, 0, 0);
953        assert!(!include.matches(timestamp_after));
954
955        // Should not match just before the range
956        let timestamp_before = Stockholm.dt(2025, 1, 15, 21, 59, 59);
957        assert!(!include.matches(timestamp_before));
958    }
959
960    #[test]
961    fn include_matches_months_wraparound() {
962        // Winter months crossing year boundary: November to March
963        let include = Include::Months(Months::new(November, March, Stockholm));
964
965        // Should match November (start)
966        let timestamp_nov = Stockholm.dt(2025, 11, 15, 12, 0, 0);
967        assert!(include.matches(timestamp_nov));
968
969        // Should match December
970        let timestamp_dec = Stockholm.dt(2025, 12, 15, 12, 0, 0);
971        assert!(include.matches(timestamp_dec));
972
973        // Should match January
974        let timestamp_jan = Stockholm.dt(2025, 1, 15, 12, 0, 0);
975        assert!(include.matches(timestamp_jan));
976
977        // Should match March (end)
978        let timestamp_mar = Stockholm.dt(2025, 3, 15, 12, 0, 0);
979        assert!(include.matches(timestamp_mar));
980
981        // Should not match summer months
982        let timestamp_jul = Stockholm.dt(2025, 7, 15, 12, 0, 0);
983        assert!(!include.matches(timestamp_jul));
984
985        // Should not match October (just before)
986        let timestamp_oct = Stockholm.dt(2025, 10, 31, 23, 59, 59);
987        assert!(!include.matches(timestamp_oct));
988
989        // Should not match April (just after)
990        let timestamp_apr = Stockholm.dt(2025, 4, 1, 0, 0, 0);
991        assert!(!include.matches(timestamp_apr));
992    }
993
994    #[test]
995    fn cost_period_matches_hours_wraparound() {
996        // Night period: 22:00 to 05:59
997        let period = CostPeriod::builder()
998            .load(LoadType::Low)
999            .fixed_cost(5, 0)
1000            .hours(22, 5)
1001            .build();
1002
1003        let timestamp_match_evening = Stockholm.dt(2025, 1, 15, 23, 0, 0);
1004        let timestamp_match_morning = Stockholm.dt(2025, 1, 15, 3, 0, 0);
1005        let timestamp_no_match = Stockholm.dt(2025, 1, 15, 14, 0, 0);
1006
1007        assert!(period.matches(timestamp_match_evening));
1008        assert!(period.matches(timestamp_match_morning));
1009        assert!(!period.matches(timestamp_no_match));
1010    }
1011
1012    #[test]
1013    fn cost_period_matches_with_both_excludes() {
1014        let period = CostPeriod::builder()
1015            .load(LoadType::High)
1016            .fixed_cost(10, 0)
1017            .hours(6, 22)
1018            .exclude_weekends()
1019            .exclude_public_holidays(Country::SE)
1020            .build();
1021
1022        // Regular weekday - should match
1023        let weekday = Stockholm.dt(2025, 1, 2, 14, 0, 0);
1024        assert!(period.matches(weekday));
1025
1026        // Weekend - should not match
1027        let saturday = Stockholm.dt(2025, 1, 4, 14, 0, 0);
1028        assert!(!period.matches(saturday));
1029
1030        // Holiday (New Year) - should not match
1031        let holiday = Stockholm.dt(2025, 1, 1, 14, 0, 0);
1032        assert!(!period.matches(holiday));
1033
1034        // Weekday but wrong hours - should not match
1035        let wrong_hours = Stockholm.dt(2025, 1, 2, 23, 0, 0);
1036        assert!(!period.matches(wrong_hours));
1037    }
1038
1039    #[test]
1040    fn exclude_matches_friday_is_not_weekend() {
1041        let exclude = Exclude::Weekends(Stockholm);
1042        // January 3, 2025 is a Friday
1043        let friday = Stockholm.dt(2025, 1, 3, 12, 0, 0);
1044        assert!(!exclude.matches(friday));
1045    }
1046
1047    #[test]
1048    fn exclude_matches_monday_is_not_weekend() {
1049        let exclude = Exclude::Weekends(Stockholm);
1050        // January 6, 2025 is a Monday
1051        let monday = Stockholm.dt(2025, 1, 6, 12, 0, 0);
1052        assert!(!exclude.matches(monday));
1053    }
1054
1055    #[test]
1056    fn exclude_matches_holiday_midsummer() {
1057        let exclude = Exclude::PublicHolidays(Country::SE, Stockholm);
1058        // Midsummer 2025 (June 21)
1059        let midsummer = Stockholm.dt(2025, 6, 21, 12, 0, 0);
1060        assert!(exclude.matches(midsummer));
1061    }
1062
1063    #[test]
1064    fn cost_period_matches_month_and_hours() {
1065        // June with specific hours
1066        let period = CostPeriod::builder()
1067            .load(LoadType::Low)
1068            .fixed_cost(5, 0)
1069            .month(June)
1070            .hours(22, 5)
1071            .build();
1072
1073        // June during night hours - should match
1074        let match_june_night = Stockholm.dt(2025, 6, 15, 23, 0, 0);
1075        assert!(period.matches(match_june_night));
1076
1077        // June during day hours - should not match
1078        let june_day = Stockholm.dt(2025, 6, 15, 14, 0, 0);
1079        assert!(!period.matches(june_day));
1080
1081        // July during night hours - should not match (wrong month)
1082        let july_night = Stockholm.dt(2025, 7, 15, 23, 0, 0);
1083        assert!(!period.matches(july_night));
1084    }
1085
1086    #[test]
1087    fn cost_period_matches_months_and_hours_with_exclude() {
1088        // Winter high load: Nov-Mar, 6-22, excluding weekends and holidays
1089        let period = CostPeriod::builder()
1090            .load(LoadType::High)
1091            .fixed_cost(15, 0)
1092            .months(November, March)
1093            .hours(6, 22)
1094            .exclude_weekends()
1095            .exclude_public_holidays(Country::SE)
1096            .build();
1097
1098        // Perfect match: winter weekday during day hours
1099        let perfect = Stockholm.dt(2025, 1, 15, 10, 0, 0);
1100        assert!(period.matches(perfect));
1101
1102        // First hour of range
1103        let first_hour = Stockholm.dt(2025, 1, 15, 6, 0, 0);
1104        assert!(period.matches(first_hour));
1105
1106        // Last hour of range
1107        let last_hour = Stockholm.dt(2025, 1, 15, 22, 59, 59);
1108        assert!(period.matches(last_hour));
1109
1110        // Wrong hours (too early)
1111        let too_early = Stockholm.dt(2025, 1, 15, 5, 59, 59);
1112        assert!(!period.matches(too_early));
1113
1114        // Wrong hours (too late)
1115        let too_late = Stockholm.dt(2025, 1, 15, 23, 0, 0);
1116        assert!(!period.matches(too_late));
1117
1118        // Wrong month (summer)
1119        let summer = Stockholm.dt(2025, 7, 15, 10, 0, 0);
1120        assert!(!period.matches(summer));
1121
1122        // Weekend
1123        let weekend = Stockholm.dt(2025, 1, 4, 10, 0, 0);
1124        assert!(!period.matches(weekend));
1125    }
1126
1127    #[test]
1128    fn cost_period_matches_base_with_restrictions() {
1129        // Base load but with hour restrictions
1130        let period = CostPeriod::builder()
1131            .load(LoadType::Base)
1132            .fixed_cost(3, 0)
1133            .hours(0, 5)
1134            .build();
1135
1136        // Should match only during specified hours
1137        let match_night = Stockholm.dt(2025, 1, 15, 3, 0, 0);
1138        assert!(period.matches(match_night));
1139
1140        // Should not match outside hours
1141        let no_match_day = Stockholm.dt(2025, 1, 15, 14, 0, 0);
1142        assert!(!period.matches(no_match_day));
1143    }
1144
1145    #[test]
1146    fn cost_period_matches_single_month() {
1147        let period = CostPeriod::builder()
1148            .load(LoadType::High)
1149            .fixed_cost(10, 0)
1150            .month(December)
1151            .build();
1152
1153        // First day of December
1154        let dec_first = Stockholm.dt(2025, 12, 1, 0, 0, 0);
1155        assert!(period.matches(dec_first));
1156
1157        // Last day of December
1158        let dec_last = Stockholm.dt(2025, 12, 31, 23, 59, 59);
1159        assert!(period.matches(dec_last));
1160
1161        // November - should not match
1162        let nov = Stockholm.dt(2025, 11, 30, 12, 0, 0);
1163        assert!(!period.matches(nov));
1164
1165        // January - should not match
1166        let jan = Stockholm.dt(2025, 1, 1, 12, 0, 0);
1167        assert!(!period.matches(jan));
1168    }
1169
1170    #[test]
1171    fn cost_period_matches_all_hours() {
1172        // Full day coverage: 0-23
1173        let period = CostPeriod::builder()
1174            .load(LoadType::Low)
1175            .fixed_cost(5, 0)
1176            .hours(0, 23)
1177            .build();
1178
1179        let midnight = Stockholm.dt(2025, 1, 15, 0, 0, 0);
1180        let noon = Stockholm.dt(2025, 1, 15, 12, 0, 0);
1181        let almost_midnight = Stockholm.dt(2025, 1, 15, 23, 59, 59);
1182
1183        assert!(period.matches(midnight));
1184        assert!(period.matches(noon));
1185        assert!(period.matches(almost_midnight));
1186    }
1187
1188    #[test]
1189    fn cost_period_matches_edge_of_month_range() {
1190        // May to September
1191        let period = CostPeriod::builder()
1192            .load(LoadType::Low)
1193            .fixed_cost(5, 0)
1194            .months(May, September)
1195            .build();
1196
1197        // First second of May
1198        let may_start = Stockholm.dt(2025, 5, 1, 0, 0, 0);
1199        assert!(period.matches(may_start));
1200
1201        // Last second of April - should not match
1202        let april_end = Stockholm.dt(2025, 4, 30, 23, 59, 59);
1203        assert!(!period.matches(april_end));
1204
1205        // Last second of September
1206        let sept_end = Stockholm.dt(2025, 9, 30, 23, 59, 59);
1207        assert!(period.matches(sept_end));
1208
1209        // First second of October - should not match
1210        let oct_start = Stockholm.dt(2025, 10, 1, 0, 0, 0);
1211        assert!(!period.matches(oct_start));
1212    }
1213
1214    #[test]
1215    fn include_matches_month_boundary() {
1216        // Test first and last day of specific month
1217        let include = Include::Months(Months::new(February, February, Stockholm));
1218
1219        // First second of February
1220        let feb_start = Stockholm.dt(2025, 2, 1, 0, 0, 0);
1221        assert!(include.matches(feb_start));
1222
1223        // Last second of February
1224        let feb_end = Stockholm.dt(2025, 2, 28, 23, 59, 59);
1225        assert!(include.matches(feb_end));
1226
1227        // Last second of January
1228        let jan_end = Stockholm.dt(2025, 1, 31, 23, 59, 59);
1229        assert!(!include.matches(jan_end));
1230
1231        // First second of March
1232        let mar_start = Stockholm.dt(2025, 3, 1, 0, 0, 0);
1233        assert!(!include.matches(mar_start));
1234    }
1235
1236    #[test]
1237    fn include_matches_hours_exact_boundaries() {
1238        let include = Include::Hours(Hours::new(6, 22, Stockholm));
1239
1240        // First second of hour 6
1241        let start = Stockholm.dt(2025, 1, 15, 6, 0, 0);
1242        assert!(include.matches(start));
1243
1244        // Last second of hour 22
1245        let end = Stockholm.dt(2025, 1, 15, 22, 59, 59);
1246        assert!(include.matches(end));
1247
1248        // Last second of hour 5 (just before)
1249        let before = Stockholm.dt(2025, 1, 15, 5, 59, 59);
1250        assert!(!include.matches(before));
1251
1252        // First second of hour 23 (just after)
1253        let after = Stockholm.dt(2025, 1, 15, 23, 0, 0);
1254        assert!(!include.matches(after));
1255    }
1256
1257    #[test]
1258    fn exclude_matches_weekends_with_utc_timestamps() {
1259        let exclude = Exclude::Weekends(Stockholm);
1260
1261        // Saturday January 4, 2025 at 12:00 Stockholm time
1262        // = Saturday at 11:00 UTC (Stockholm is UTC+1 in winter)
1263        let saturday_utc = Utc.dt(2025, 1, 4, 11, 0, 0);
1264        assert!(exclude.matches(saturday_utc));
1265
1266        // Sunday January 5, 2025 at 12:00 Stockholm time
1267        // = Sunday at 11:00 UTC
1268        let sunday_utc = Utc.dt(2025, 1, 5, 11, 0, 0);
1269        assert!(exclude.matches(sunday_utc));
1270
1271        // Monday January 6, 2025 at 12:00 Stockholm time
1272        // = Monday at 11:00 UTC
1273        let monday_utc = Utc.dt(2025, 1, 6, 11, 0, 0);
1274        assert!(!exclude.matches(monday_utc));
1275    }
1276
1277    #[test]
1278    fn exclude_matches_weekends_timezone_boundary() {
1279        let exclude = Exclude::Weekends(Stockholm);
1280
1281        // Saturday January 4, 2025 at 00:00 Stockholm time
1282        // = Friday January 3, 2025 at 23:00 UTC
1283        // This is tricky: it's Friday in UTC but Saturday in Stockholm
1284        let friday_utc_saturday_stockholm = Utc.dt(2025, 1, 3, 23, 0, 0);
1285        assert!(
1286            exclude.matches(friday_utc_saturday_stockholm),
1287            "Should match because it's Saturday in Stockholm timezone"
1288        );
1289
1290        // Monday January 6, 2025 at 00:00 Stockholm time
1291        // = Sunday January 5, 2025 at 23:00 UTC
1292        // This is Sunday in UTC but Monday in Stockholm
1293        let sunday_utc_monday_stockholm = Utc.dt(2025, 1, 5, 23, 0, 0);
1294        assert!(
1295            !exclude.matches(sunday_utc_monday_stockholm),
1296            "Should not match because it's Monday in Stockholm timezone"
1297        );
1298
1299        // Sunday January 5, 2025 at 23:59 Stockholm time
1300        // = Sunday January 5, 2025 at 22:59 UTC
1301        let sunday_late_utc = Utc.dt(2025, 1, 5, 22, 59, 0);
1302        assert!(
1303            exclude.matches(sunday_late_utc),
1304            "Should match because it's still Sunday in Stockholm timezone"
1305        );
1306    }
1307
1308    #[test]
1309    fn exclude_matches_holidays_with_utc_timestamps() {
1310        let exclude = Exclude::PublicHolidays(Country::SE, Stockholm);
1311
1312        // New Year's Day 2025 at 12:00 Stockholm time
1313        // = January 1, 2025 at 11:00 UTC
1314        let new_year_utc = Utc.dt(2025, 1, 1, 11, 0, 0);
1315        assert!(exclude.matches(new_year_utc));
1316
1317        // Regular day: January 2, 2025 at 12:00 Stockholm time
1318        // = January 2, 2025 at 11:00 UTC
1319        let regular_day_utc = Utc.dt(2025, 1, 2, 11, 0, 0);
1320        assert!(!exclude.matches(regular_day_utc));
1321    }
1322
1323    #[test]
1324    fn exclude_matches_holidays_timezone_boundary() {
1325        let exclude = Exclude::PublicHolidays(Country::SE, Stockholm);
1326
1327        // New Year's Day 2025 at 00:00 Stockholm time
1328        // = December 31, 2024 at 23:00 UTC
1329        // This is Dec 31 in UTC but Jan 1 (holiday) in Stockholm
1330        let dec31_utc_jan1_stockholm = Utc.dt(2024, 12, 31, 23, 0, 0);
1331        assert!(
1332            exclude.matches(dec31_utc_jan1_stockholm),
1333            "Should match because it's New Year's Day in Stockholm timezone"
1334        );
1335
1336        // January 2, 2025 at 00:00 Stockholm time
1337        // = January 1, 2025 at 23:00 UTC
1338        // This is Jan 1 (holiday) in UTC but Jan 2 (not holiday) in Stockholm
1339        let jan1_utc_jan2_stockholm = Utc.dt(2025, 1, 1, 23, 0, 0);
1340        assert!(
1341            !exclude.matches(jan1_utc_jan2_stockholm),
1342            "Should not match because it's January 2 in Stockholm timezone"
1343        );
1344    }
1345
1346    #[test]
1347    fn exclude_matches_weekends_summer_timezone() {
1348        let exclude = Exclude::Weekends(Stockholm);
1349
1350        // Saturday June 7, 2025 at 12:00 Stockholm time (CEST = UTC+2)
1351        // = Saturday at 10:00 UTC
1352        let saturday_summer_utc = Utc.dt(2025, 6, 7, 10, 0, 0);
1353        assert!(exclude.matches(saturday_summer_utc));
1354
1355        // Saturday June 7, 2025 at 00:00 Stockholm time
1356        // = Friday June 6, 2025 at 22:00 UTC
1357        let friday_utc_saturday_stockholm_summer = Utc.dt(2025, 6, 6, 22, 0, 0);
1358        assert!(
1359            exclude.matches(friday_utc_saturday_stockholm_summer),
1360            "Should match because it's Saturday in Stockholm timezone (CEST)"
1361        );
1362    }
1363}