uls-download 0.2.2

FCC ULS file download and synchronization
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
//! FCC ULS service and file catalog.
//!
//! Maps radio service codes to their corresponding FCC download files.

use crate::error::{DownloadError, Result};
use chrono::{Datelike, NaiveDate};
use serde::{Deserialize, Serialize};
use std::fmt;

/// A downloadable FCC ULS data file.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct DataFile {
    /// The service abbreviation (e.g., "amat", "gmrs").
    pub service: String,

    /// The file type (license or application).
    pub file_type: FileType,

    /// The update type (complete or daily).
    pub update_type: UpdateType,

    /// For daily files, the day of week. None for complete files.
    pub day: Option<Weekday>,
}

impl DataFile {
    /// Create a new complete (weekly) license file.
    pub fn complete_license(service: impl Into<String>) -> Self {
        Self {
            service: service.into(),
            file_type: FileType::License,
            update_type: UpdateType::Complete,
            day: None,
        }
    }

    /// Create a new complete (weekly) application file.
    pub fn complete_application(service: impl Into<String>) -> Self {
        Self {
            service: service.into(),
            file_type: FileType::Application,
            update_type: UpdateType::Complete,
            day: None,
        }
    }

    /// Create a new daily license file.
    pub fn daily_license(service: impl Into<String>, day: Weekday) -> Self {
        Self {
            service: service.into(),
            file_type: FileType::License,
            update_type: UpdateType::Daily,
            day: Some(day),
        }
    }

    /// Get the filename for this data file.
    pub fn filename(&self) -> String {
        let prefix = match self.file_type {
            FileType::License => "l",
            FileType::Application => "a",
        };

        match self.update_type {
            UpdateType::Complete => format!("{}_{}.zip", prefix, self.service),
            UpdateType::Daily => {
                let day_abbrev = self.day.map(|d| d.abbrev()).unwrap_or("mon");
                // Daily files use abbreviated service names
                let daily_service = ServiceCatalog::daily_abbreviation(&self.service);
                format!("{}_{}_{}.zip", prefix, daily_service, day_abbrev)
            }
        }
    }

    /// Get the URL path for this data file (without base URL).
    pub fn url_path(&self) -> String {
        match self.update_type {
            UpdateType::Complete => format!("complete/{}", self.filename()),
            UpdateType::Daily => format!("daily/{}", self.filename()),
        }
    }
}

impl fmt::Display for DataFile {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.filename())
    }
}

/// Type of data file (license or application).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum FileType {
    /// License data (l_*.zip).
    License,
    /// Application data (a_*.zip).
    Application,
}

/// Type of update (complete weekly or daily incremental).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum UpdateType {
    /// Complete weekly database.
    Complete,
    /// Daily transaction file.
    Daily,
}

/// Day of week for daily files.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Weekday {
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
}

impl Weekday {
    /// Get all days of the week.
    pub const ALL: [Weekday; 7] = [
        Weekday::Sunday,
        Weekday::Monday,
        Weekday::Tuesday,
        Weekday::Wednesday,
        Weekday::Thursday,
        Weekday::Friday,
        Weekday::Saturday,
    ];

    /// Get the three-letter abbreviation.
    pub fn abbrev(&self) -> &'static str {
        match self {
            Weekday::Sunday => "sun",
            Weekday::Monday => "mon",
            Weekday::Tuesday => "tue",
            Weekday::Wednesday => "wed",
            Weekday::Thursday => "thu",
            Weekday::Friday => "fri",
            Weekday::Saturday => "sat",
        }
    }

    /// Create from chrono::Weekday.
    pub fn from_chrono(day: chrono::Weekday) -> Self {
        match day {
            chrono::Weekday::Sun => Weekday::Sunday,
            chrono::Weekday::Mon => Weekday::Monday,
            chrono::Weekday::Tue => Weekday::Tuesday,
            chrono::Weekday::Wed => Weekday::Wednesday,
            chrono::Weekday::Thu => Weekday::Thursday,
            chrono::Weekday::Fri => Weekday::Friday,
            chrono::Weekday::Sat => Weekday::Saturday,
        }
    }

    /// Get the weekday for a given date.
    pub fn for_date(date: NaiveDate) -> Self {
        Self::from_chrono(date.weekday())
    }
}

/// Catalog of FCC ULS services and their corresponding files.
pub struct ServiceCatalog;

impl ServiceCatalog {
    /// All supported services with their full and daily abbreviations.
    /// Format: (full_name, daily_abbreviation, description, radio_service_codes)
    const SERVICES: &'static [(
        &'static str,
        &'static str,
        &'static str,
        &'static [&'static str],
    )] = &[
        ("amat", "am", "Amateur Radio", &["HA", "HV"]),
        ("gmrs", "gm", "General Mobile Radio Service", &["ZA"]),
        ("ship", "sh", "Ship Stations", &["SA", "SB"]),
        ("coast", "co", "Coastal Stations", &["MC"]),
        ("aircraft", "ac", "Aircraft Stations", &["AC"]),
        ("market", "mk", "Market Based Services", &[]),
        ("land", "ln", "Land Mobile", &[]),
        ("micro", "mi", "Microwave", &[]),
        ("paging", "pg", "Paging", &[]),
    ];

    /// Get the daily abbreviation for a service.
    pub fn daily_abbreviation(service: &str) -> &'static str {
        Self::SERVICES
            .iter()
            .find(|(full, _, _, _)| *full == service)
            .map(|(_, abbrev, _, _)| *abbrev)
            .unwrap_or("xx") // Unknown services get placeholder
    }

    /// Get the full service name from an abbreviation or radio service code.
    /// Accepts: full name ("amat"), daily abbrev ("am"), or radio service code ("HA").
    pub fn full_name(input: &str) -> Option<&'static str> {
        Self::SERVICES
            .iter()
            .find(|(full, daily, _, codes)| {
                *full == input || *daily == input || codes.contains(&input)
            })
            .map(|(full, _, _, _)| *full)
    }

    /// Get all available services.
    pub fn all_services() -> Vec<ServiceInfo> {
        Self::SERVICES
            .iter()
            .map(|(name, abbrev, desc, codes)| ServiceInfo {
                name: name.to_string(),
                daily_abbrev: abbrev.to_string(),
                description: desc.to_string(),
                radio_service_codes: codes.iter().map(|s| s.to_string()).collect(),
            })
            .collect()
    }

    /// Check if a service is known.
    pub fn is_known_service(service: &str) -> bool {
        Self::SERVICES
            .iter()
            .any(|(full, daily, _, _)| *full == service || *daily == service)
    }

    /// Get complete license file for a service.
    pub fn complete_license(service: &str) -> Result<DataFile> {
        let full_name = Self::full_name(service)
            .ok_or_else(|| DownloadError::UnknownService(service.to_string()))?;
        Ok(DataFile::complete_license(full_name))
    }

    /// Get complete application file for a service.
    pub fn complete_application(service: &str) -> Result<DataFile> {
        let full_name = Self::full_name(service)
            .ok_or_else(|| DownloadError::UnknownService(service.to_string()))?;
        Ok(DataFile::complete_application(full_name))
    }

    /// Get all daily license files for a service.
    pub fn daily_licenses(service: &str) -> Result<Vec<DataFile>> {
        let full_name = Self::full_name(service)
            .ok_or_else(|| DownloadError::UnknownService(service.to_string()))?;

        Ok(Weekday::ALL
            .iter()
            .map(|day| DataFile::daily_license(full_name, *day))
            .collect())
    }

    /// Get the daily license file for a specific date.
    pub fn daily_license_for_date(service: &str, date: NaiveDate) -> Result<DataFile> {
        let full_name = Self::full_name(service)
            .ok_or_else(|| DownloadError::UnknownService(service.to_string()))?;

        Ok(DataFile::daily_license(full_name, Weekday::for_date(date)))
    }

    /// Get daily license files for a date range (inclusive).
    pub fn daily_licenses_for_range(
        service: &str,
        start: NaiveDate,
        end: NaiveDate,
    ) -> Result<Vec<(NaiveDate, DataFile)>> {
        let full_name = Self::full_name(service)
            .ok_or_else(|| DownloadError::UnknownService(service.to_string()))?;

        let mut files = Vec::new();
        let mut current = start;

        while current <= end {
            let weekday = Weekday::for_date(current);
            files.push((current, DataFile::daily_license(full_name, weekday)));
            current = current.succ_opt().unwrap_or(current);
        }

        Ok(files)
    }

    /// Calculate which daily files are needed to bring data up to date.
    ///
    /// Given the date of the last weekly import and any already-applied patches,
    /// returns the list of dates and files that still need to be applied.
    pub fn get_missing_daily_files(
        service: &str,
        last_weekly_date: NaiveDate,
        applied_patch_dates: &[NaiveDate],
        today: NaiveDate,
    ) -> Result<Vec<(NaiveDate, DataFile)>> {
        // Start from day after weekly
        let start = last_weekly_date.succ_opt().unwrap_or(last_weekly_date);

        // Get all daily files from start to today
        let all_files = Self::daily_licenses_for_range(service, start, today)?;

        // Filter out already-applied patches
        let applied_set: std::collections::HashSet<_> = applied_patch_dates.iter().collect();
        let missing: Vec<_> = all_files
            .into_iter()
            .filter(|(date, _)| !applied_set.contains(date))
            .collect();

        Ok(missing)
    }
}

/// Information about a supported service.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceInfo {
    /// Full service name (e.g., "amat").
    pub name: String,
    /// Daily file abbreviation (e.g., "am").
    pub daily_abbrev: String,
    /// Human-readable description.
    pub description: String,
    /// Associated radio service codes.
    pub radio_service_codes: Vec<String>,
}

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

    #[test]
    fn test_complete_license_filename() {
        let file = DataFile::complete_license("amat");
        assert_eq!(file.filename(), "l_amat.zip");
        assert_eq!(file.url_path(), "complete/l_amat.zip");
    }

    #[test]
    fn test_complete_application_filename() {
        let file = DataFile::complete_application("amat");
        assert_eq!(file.filename(), "a_amat.zip");
    }

    #[test]
    fn test_daily_license_filename() {
        let file = DataFile::daily_license("amat", Weekday::Monday);
        assert_eq!(file.filename(), "l_am_mon.zip");
        assert_eq!(file.url_path(), "daily/l_am_mon.zip");
    }

    #[test]
    fn test_gmrs_files() {
        let complete = DataFile::complete_license("gmrs");
        assert_eq!(complete.filename(), "l_gmrs.zip");

        let daily = DataFile::daily_license("gmrs", Weekday::Friday);
        assert_eq!(daily.filename(), "l_gm_fri.zip");
    }

    #[test]
    fn test_service_catalog() {
        assert!(ServiceCatalog::is_known_service("amat"));
        assert!(ServiceCatalog::is_known_service("am"));
        assert!(ServiceCatalog::is_known_service("gmrs"));
        assert!(!ServiceCatalog::is_known_service("unknown"));
    }

    #[test]
    fn test_daily_abbreviation() {
        assert_eq!(ServiceCatalog::daily_abbreviation("amat"), "am");
        assert_eq!(ServiceCatalog::daily_abbreviation("gmrs"), "gm");
    }

    #[test]
    fn test_all_services() {
        let services = ServiceCatalog::all_services();
        assert!(services.iter().any(|s| s.name == "amat"));
        assert!(services.iter().any(|s| s.name == "gmrs"));
    }

    #[test]
    fn test_radio_service_code_lookup() {
        // Radio service codes should map to full service names
        assert_eq!(ServiceCatalog::full_name("HA"), Some("amat"));
        assert_eq!(ServiceCatalog::full_name("HV"), Some("amat"));
        assert_eq!(ServiceCatalog::full_name("ZA"), Some("gmrs"));
    }

    #[test]
    fn test_complete_license_by_radio_service_code() {
        // CLI passes radio service codes like "HA" - this must work
        let file = ServiceCatalog::complete_license("HA").expect("HA should be recognized");
        assert_eq!(file.filename(), "l_amat.zip");

        let file = ServiceCatalog::complete_license("ZA").expect("ZA should be recognized");
        assert_eq!(file.filename(), "l_gmrs.zip");
    }

    #[test]
    fn test_complete_license_by_full_name() {
        let file = ServiceCatalog::complete_license("amat").expect("amat should be recognized");
        assert_eq!(file.filename(), "l_amat.zip");
    }

    #[test]
    fn test_unknown_service() {
        assert!(ServiceCatalog::complete_license("UNKNOWN").is_err());
    }

    #[test]
    fn test_daily_licenses_for_range() {
        // Monday Jan 12 to Friday Jan 16, 2026
        let start = NaiveDate::from_ymd_opt(2026, 1, 12).unwrap();
        let end = NaiveDate::from_ymd_opt(2026, 1, 16).unwrap();

        let files = ServiceCatalog::daily_licenses_for_range("amat", start, end).unwrap();

        assert_eq!(files.len(), 5);
        assert_eq!(files[0].1.filename(), "l_am_mon.zip");
        assert_eq!(files[4].1.filename(), "l_am_fri.zip");
    }

    #[test]
    fn test_daily_licenses_for_range_includes_sunday() {
        // Sunday Jan 11 to Monday Jan 12
        let start = NaiveDate::from_ymd_opt(2026, 1, 11).unwrap();
        let end = NaiveDate::from_ymd_opt(2026, 1, 12).unwrap();

        let files = ServiceCatalog::daily_licenses_for_range("amat", start, end).unwrap();

        assert_eq!(files.len(), 2);
        assert_eq!(files[0].0, NaiveDate::from_ymd_opt(2026, 1, 11).unwrap());
        assert_eq!(files[1].0, NaiveDate::from_ymd_opt(2026, 1, 12).unwrap());
    }

    #[test]
    fn test_get_missing_daily_files() {
        // Suppose we imported weekly on Sunday Jan 11, and today is Thursday Jan 15
        let weekly = NaiveDate::from_ymd_opt(2026, 1, 11).unwrap();
        let today = NaiveDate::from_ymd_opt(2026, 1, 15).unwrap();

        // No patches applied yet
        let missing = ServiceCatalog::get_missing_daily_files("amat", weekly, &[], today).unwrap();

        // Should need Mon, Tue, Wed, Thu
        assert_eq!(missing.len(), 4);
        assert_eq!(missing[0].1.filename(), "l_am_mon.zip");
        assert_eq!(missing[3].1.filename(), "l_am_thu.zip");
    }

    #[test]
    fn test_get_missing_daily_files_with_applied() {
        let weekly = NaiveDate::from_ymd_opt(2026, 1, 11).unwrap();
        let today = NaiveDate::from_ymd_opt(2026, 1, 15).unwrap();

        // Mon and Tue already applied (Jan 12, Jan 13)
        let applied = vec![
            NaiveDate::from_ymd_opt(2026, 1, 12).unwrap(),
            NaiveDate::from_ymd_opt(2026, 1, 13).unwrap(),
        ];

        let missing =
            ServiceCatalog::get_missing_daily_files("amat", weekly, &applied, today).unwrap();

        // Should only need Wed, Thu
        assert_eq!(missing.len(), 2);
        assert_eq!(missing[0].1.filename(), "l_am_wed.zip");
        assert_eq!(missing[1].1.filename(), "l_am_thu.zip");
    }

    #[test]
    fn test_get_missing_daily_files_on_sunday() {
        let weekly = NaiveDate::from_ymd_opt(2026, 1, 11).unwrap();
        let today = NaiveDate::from_ymd_opt(2026, 1, 18).unwrap(); // Next Sunday

        let missing = ServiceCatalog::get_missing_daily_files("amat", weekly, &[], today).unwrap();

        // All 7 days (Mon Jan 12 through Sun Jan 18) should be present
        assert_eq!(missing.len(), 7);
    }

    #[test]
    fn test_sunday_weekday() {
        assert_eq!(Weekday::Sunday.abbrev(), "sun");
        assert_eq!(Weekday::from_chrono(chrono::Weekday::Sun), Weekday::Sunday);

        let sunday = NaiveDate::from_ymd_opt(2026, 1, 11).unwrap();
        assert_eq!(Weekday::for_date(sunday), Weekday::Sunday);
    }

    #[test]
    fn test_daily_license_for_date_sunday() {
        let sunday = NaiveDate::from_ymd_opt(2026, 1, 11).unwrap();
        let file = ServiceCatalog::daily_license_for_date("amat", sunday).unwrap();
        assert_eq!(file.filename(), "l_am_sun.zip");
    }

    #[test]
    fn test_daily_licenses_for_full_week() {
        // Full week: Sun Jan 11 through Sat Jan 17
        let start = NaiveDate::from_ymd_opt(2026, 1, 11).unwrap();
        let end = NaiveDate::from_ymd_opt(2026, 1, 17).unwrap();

        let files = ServiceCatalog::daily_licenses_for_range("amat", start, end).unwrap();

        assert_eq!(files.len(), 7);
        assert_eq!(files[0].1.filename(), "l_am_sun.zip");
        assert_eq!(files[6].1.filename(), "l_am_sat.zip");
    }

    #[test]
    fn test_datafile_display_uses_filename() {
        let complete = DataFile::complete_license("amat");
        assert_eq!(complete.to_string(), "l_amat.zip");

        let daily = DataFile::daily_license("gmrs", Weekday::Wednesday);
        assert_eq!(daily.to_string(), "l_gm_wed.zip");

        let application = DataFile::complete_application("amat");
        assert_eq!(application.to_string(), "a_amat.zip");
    }

    #[test]
    fn test_complete_application_via_catalog_known_and_unknown() {
        let file = ServiceCatalog::complete_application("amat").unwrap();
        assert_eq!(file.filename(), "a_amat.zip");
        assert_eq!(file.url_path(), "complete/a_amat.zip");

        // Radio service code resolves to the full name as well.
        let by_code = ServiceCatalog::complete_application("HA").unwrap();
        assert_eq!(by_code.filename(), "a_amat.zip");

        let err = ServiceCatalog::complete_application("nope").unwrap_err();
        assert!(matches!(err, DownloadError::UnknownService(s) if s == "nope"));
    }

    #[test]
    fn test_full_name_unknown_returns_none() {
        assert_eq!(ServiceCatalog::full_name("amat"), Some("amat"));
        assert_eq!(ServiceCatalog::full_name("gm"), Some("gmrs"));
        assert_eq!(ServiceCatalog::full_name("ZA"), Some("gmrs"));
        assert_eq!(ServiceCatalog::full_name("not-a-service"), None);
    }

    #[test]
    fn test_daily_abbreviation_unknown_returns_placeholder() {
        assert_eq!(ServiceCatalog::daily_abbreviation("ship"), "sh");
        assert_eq!(ServiceCatalog::daily_abbreviation("does-not-exist"), "xx");
    }

    #[test]
    fn test_unknown_service_daily_filename_uses_placeholder() {
        // An unknown service still produces a (placeholder) daily filename.
        let file = DataFile::daily_license("mystery", Weekday::Tuesday);
        assert_eq!(file.filename(), "l_xx_tue.zip");
    }

    #[test]
    fn test_all_services_metadata() {
        let services = ServiceCatalog::all_services();
        assert_eq!(services.len(), 9);

        let amat = services.iter().find(|s| s.name == "amat").unwrap();
        assert_eq!(amat.daily_abbrev, "am");
        assert_eq!(amat.description, "Amateur Radio");
        assert_eq!(amat.radio_service_codes, vec!["HA", "HV"]);

        let market = services.iter().find(|s| s.name == "market").unwrap();
        assert!(market.radio_service_codes.is_empty());
    }

    #[test]
    fn test_daily_licenses_lists_all_seven_days() {
        let files = ServiceCatalog::daily_licenses("amat").unwrap();
        let names: Vec<String> = files.iter().map(|f| f.filename()).collect();
        assert_eq!(
            names,
            vec![
                "l_am_sun.zip",
                "l_am_mon.zip",
                "l_am_tue.zip",
                "l_am_wed.zip",
                "l_am_thu.zip",
                "l_am_fri.zip",
                "l_am_sat.zip",
            ]
        );

        assert!(ServiceCatalog::daily_licenses("nope").is_err());
    }

    #[test]
    fn test_weekday_all_ordering_and_abbrevs() {
        assert_eq!(Weekday::ALL.len(), 7);
        let abbrevs: Vec<&str> = Weekday::ALL.iter().map(|d| d.abbrev()).collect();
        assert_eq!(
            abbrevs,
            vec!["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
        );
    }

    #[rstest::rstest]
    #[case(chrono::Weekday::Sun, Weekday::Sunday, "sun")]
    #[case(chrono::Weekday::Mon, Weekday::Monday, "mon")]
    #[case(chrono::Weekday::Tue, Weekday::Tuesday, "tue")]
    #[case(chrono::Weekday::Wed, Weekday::Wednesday, "wed")]
    #[case(chrono::Weekday::Thu, Weekday::Thursday, "thu")]
    #[case(chrono::Weekday::Fri, Weekday::Friday, "fri")]
    #[case(chrono::Weekday::Sat, Weekday::Saturday, "sat")]
    fn test_weekday_from_chrono_and_abbrev(
        #[case] chrono_day: chrono::Weekday,
        #[case] expected: Weekday,
        #[case] abbrev: &str,
    ) {
        assert_eq!(Weekday::from_chrono(chrono_day), expected);
        assert_eq!(expected.abbrev(), abbrev);
    }

    #[rstest::rstest]
    // 2026-01-11 is a Sunday; each subsequent date advances one weekday.
    #[case(11, Weekday::Sunday)]
    #[case(12, Weekday::Monday)]
    #[case(13, Weekday::Tuesday)]
    #[case(14, Weekday::Wednesday)]
    #[case(15, Weekday::Thursday)]
    #[case(16, Weekday::Friday)]
    #[case(17, Weekday::Saturday)]
    fn test_weekday_for_date(#[case] day_of_month: u32, #[case] expected: Weekday) {
        let date = NaiveDate::from_ymd_opt(2026, 1, day_of_month).unwrap();
        assert_eq!(Weekday::for_date(date), expected);
    }

    #[test]
    fn test_get_missing_daily_files_unknown_service_errors() {
        let weekly = NaiveDate::from_ymd_opt(2026, 1, 11).unwrap();
        let today = NaiveDate::from_ymd_opt(2026, 1, 15).unwrap();
        assert!(ServiceCatalog::get_missing_daily_files("nope", weekly, &[], today).is_err());
    }

    #[test]
    fn test_get_missing_daily_files_none_when_caught_up() {
        // Weekly imported today, nothing newer to apply.
        let weekly = NaiveDate::from_ymd_opt(2026, 1, 15).unwrap();
        let today = NaiveDate::from_ymd_opt(2026, 1, 15).unwrap();
        let missing = ServiceCatalog::get_missing_daily_files("amat", weekly, &[], today).unwrap();
        assert!(missing.is_empty());
    }

    #[test]
    fn test_get_missing_daily_files_second_week_with_first_week_applied() {
        // Weekly on Sun Jan 11, all of week 1 (Mon-Sun) applied, now it's Thu Jan 22
        let weekly = NaiveDate::from_ymd_opt(2026, 1, 11).unwrap();
        let today = NaiveDate::from_ymd_opt(2026, 1, 22).unwrap();

        let applied = vec![
            NaiveDate::from_ymd_opt(2026, 1, 12).unwrap(), // Mon
            NaiveDate::from_ymd_opt(2026, 1, 13).unwrap(), // Tue
            NaiveDate::from_ymd_opt(2026, 1, 14).unwrap(), // Wed
            NaiveDate::from_ymd_opt(2026, 1, 15).unwrap(), // Thu
            NaiveDate::from_ymd_opt(2026, 1, 16).unwrap(), // Fri
            NaiveDate::from_ymd_opt(2026, 1, 17).unwrap(), // Sat
            NaiveDate::from_ymd_opt(2026, 1, 18).unwrap(), // Sun
        ];

        let missing =
            ServiceCatalog::get_missing_daily_files("amat", weekly, &applied, today).unwrap();

        // Should need Mon Jan 19 through Thu Jan 22 (4 days)
        assert_eq!(missing.len(), 4);
        assert_eq!(missing[0].0, NaiveDate::from_ymd_opt(2026, 1, 19).unwrap());
        assert_eq!(missing[3].0, NaiveDate::from_ymd_opt(2026, 1, 22).unwrap());
    }
}