//! Contract tests for the public `curate_etl` `shorts` builder API and CLI binary (T-76 / M1.2f). //! //! Uses only the crate's public surface and compiled binary (`CARGO_BIN_EXE_curate-etl`), guarding: //! - Canadian TSX holiday calendar & weekend observance rules (2008–2030), //! - `+4` Canadian business-day lag calculation & out-of-range error handling, //! - `America/Toronto` ET→UTC DST conversion (including spring-gap and fall-overlap rejections), //! - `build_shorts` bad-row resilience, optional-field sanitization, `-0.0` normalization, //! deduplication, accounting invariants, no-look-ahead, and permutation invariance, //! - `bench-shorts` reproducibility and CLI argument validation. use std::process::Command; use curate_etl::bench::run_shorts_benchmark; use curate_etl::{ add_canadian_business_days, build_shorts, epoch_days_to_ymd, et_to_utc_micros, is_canadian_business_day, ymd_to_epoch_days, CeoShortReport, ShortRow, ShortsBuild, ShortsError, ShortsStats, MAX_SUPPORTED_DATE, MAX_SUPPORTED_YEAR, MIN_SUPPORTED_DATE, MIN_SUPPORTED_YEAR, SHORTS_DISSEMINATION_HOUR_ET, SHORTS_DISSEMINATION_MINUTE_ET, SHORTS_LAG_BUSINESS_DAYS, SHORTS_SOURCE_CEO, }; fn day(y: i32, m: u32, d: u32) -> i32 { ymd_to_epoch_days(y, m, d).expect("valid fixture date") } fn utc_micros(y: i32, m: u32, d: u32, hour: u8, minute: u8, second: u8) -> i64 { let epoch_day = day(y, m, d); let secs = i64::from(epoch_day) * 86_400 + i64::from(hour) * 3_600 + i64::from(minute) * 60 + i64::from(second); secs * 1_000_000 } struct Rng(u64); impl Rng { fn next(&mut self) -> u64 { self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15); let mut z = self.0; z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9); z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB); z ^ (z >> 31) } } fn shuffle(items: &mut [T], rng: &mut Rng) { for i in (1..items.len()).rev() { let j = (rng.next() % (i as u64 + 1)) as usize; items.swap(i, j); } } fn assert_accounting_and_order(build: &ShortsBuild) { let s = &build.stats; assert_eq!( s.rows_in, s.rows_out + s.dropped_invalid_shares_short + s.duplicate_collapsed + s.duplicate_conflict_dropped, "accounting invariant failed: {s:?}" ); assert_eq!(s.rows_out, build.rows.len()); let mut distinct_keys = 0usize; let mut prev_key: Option<&K> = None; for row in &build.rows { assert!( row.known_at_date > row.report_date, "look-ahead violation: known_at_date {} <= report_date {}", row.known_at_date, row.report_date ); assert_eq!(row.source, SHORTS_SOURCE_CEO); assert_eq!(row.source, "ceo_shorts"); if prev_key != Some(&row.key) { distinct_keys += 1; prev_key = Some(&row.key); } } assert_eq!(s.keys_out, distinct_keys); for pair in build.rows.windows(2) { assert!( (&pair[0].key, pair[0].report_date) < (&pair[1].key, pair[1].report_date), "rows must be strictly sorted by (key, report_date)" ); } } fn assert_rows_bit_eq(a: &[ShortRow], b: &[ShortRow]) { assert_eq!(a.len(), b.len()); for (x, y) in a.iter().zip(b) { assert_eq!(x.key, y.key); assert_eq!(x.report_date, y.report_date); assert_eq!(x.known_at_date, y.known_at_date); assert_eq!(x.known_at_utc_micros, y.known_at_utc_micros); assert_eq!(x.shares_short.to_bits(), y.shares_short.to_bits()); assert_eq!( x.net_change.map(f64::to_bits), y.net_change.map(f64::to_bits) ); assert_eq!( x.pct_float.map(f64::to_bits), y.pct_float.map(f64::to_bits) ); assert_eq!(x.source, y.source); } } #[test] fn public_constants_and_calendar_holidays_match_contract() { assert_eq!(SHORTS_SOURCE_CEO, "ceo_shorts"); assert_eq!(SHORTS_LAG_BUSINESS_DAYS, 4); assert_eq!(SHORTS_DISSEMINATION_HOUR_ET, 17); assert_eq!(SHORTS_DISSEMINATION_MINUTE_ET, 0); assert_eq!(MIN_SUPPORTED_YEAR, 2008); assert_eq!(MAX_SUPPORTED_YEAR, 2030); assert_eq!(epoch_days_to_ymd(MIN_SUPPORTED_DATE), (2008, 1, 1)); assert_eq!(epoch_days_to_ymd(MAX_SUPPORTED_DATE), (2030, 12, 31)); // Pinned TSX holidays and weekend observances let non_business = [ (2024, 3, 29), // Good Friday (2023, 7, 3), // Canada Day observed (2022, 12, 26), // Christmas observed (2022, 12, 27), // Boxing Day observed (2021, 2, 15), // Family Day (2024, 5, 20), // Victoria Day (2024, 8, 5), // Civic Holiday (2024, 10, 14), // Thanksgiving (2022, 1, 3), // New Year's Day observed (Sat Jan 1 -> Mon Jan 3) (2023, 1, 2), // New Year's Day observed (Sun Jan 1 -> Mon Jan 2) (2020, 12, 28), // Boxing Day observed (Fri Dec 25 / Sat Dec 26 -> Mon Dec 28) (2021, 12, 27), // Christmas observed (Sat Dec 25 -> Mon Dec 27) (2021, 12, 28), // Boxing Day observed (Sun Dec 26 -> Tue Dec 28) ]; for (y, m, d) in non_business { assert_eq!( is_canadian_business_day(day(y, m, d)), Ok(false), "expected {y:04}-{m:02}-{d:02} to be a non-business day" ); } // Ordinary weekday + federal holidays where TSX trades normally for (y, m, d) in [(2024, 6, 12), (2023, 10, 2), (2024, 11, 11)] { assert_eq!( is_canadian_business_day(day(y, m, d)), Ok(true), "expected {y:04}-{m:02}-{d:02} to be a business day" ); } } #[test] fn plus_four_business_days_and_out_of_range_errors() { assert_eq!( add_canadian_business_days(day(2024, 3, 15), 4), Ok(day(2024, 3, 21)) ); assert_eq!( add_canadian_business_days(day(2024, 3, 28), 4), Ok(day(2024, 4, 4)) ); assert_eq!( add_canadian_business_days(day(2023, 12, 29), 4), Ok(day(2024, 1, 5)) ); assert_eq!( add_canadian_business_days(day(2023, 9, 30), 4), Ok(day(2023, 10, 5)) ); assert_eq!( add_canadian_business_days(day(2024, 6, 12), 0), Ok(day(2024, 6, 12)) ); // Out-of-range dates return typed ShortsError::DateOutOfRange without panicking for bad in [ MIN_SUPPORTED_DATE - 1, MAX_SUPPORTED_DATE + 1, i32::MIN, i32::MAX, ] { assert!(matches!( is_canadian_business_day(bad), Err(ShortsError::DateOutOfRange { .. }) )); assert!(matches!( add_canadian_business_days(bad, 4), Err(ShortsError::DateOutOfRange { .. }) )); assert!(matches!( et_to_utc_micros(bad, 17, 0, 0), Err(ShortsError::DateOutOfRange { .. }) )); } assert!(matches!( add_canadian_business_days(day(2030, 12, 30), 4), Err(ShortsError::DateOutOfRange { .. }) )); } #[test] fn et_to_utc_micros_dst_transitions_and_local_time_validation() { // Summer EDT (21:00Z) vs Winter EST (22:00Z) assert_eq!( et_to_utc_micros(day(2024, 7, 15), 17, 0, 0), Ok(utc_micros(2024, 7, 15, 21, 0, 0)) ); assert_eq!( et_to_utc_micros(day(2024, 1, 15), 17, 0, 0), Ok(utc_micros(2024, 1, 15, 22, 0, 0)) ); // Both sides of March spring-forward (2024-03-10) assert_eq!( et_to_utc_micros(day(2024, 3, 9), 17, 0, 0), Ok(utc_micros(2024, 3, 9, 22, 0, 0)) ); assert_eq!( et_to_utc_micros(day(2024, 3, 10), 17, 0, 0), Ok(utc_micros(2024, 3, 10, 21, 0, 0)) ); // Both sides of November fall-back (2024-11-03) assert_eq!( et_to_utc_micros(day(2024, 11, 2), 17, 0, 0), Ok(utc_micros(2024, 11, 2, 21, 0, 0)) ); assert_eq!( et_to_utc_micros(day(2024, 11, 3), 17, 0, 0), Ok(utc_micros(2024, 11, 3, 22, 0, 0)) ); // Events 16:05 ET reuse assert_eq!( et_to_utc_micros(day(2026, 3, 6), 16, 5, 0), Ok(utc_micros(2026, 3, 6, 21, 5, 0)) ); assert_eq!( et_to_utc_micros(day(2026, 3, 9), 16, 5, 0), Ok(utc_micros(2026, 3, 9, 20, 5, 0)) ); // Nonexistent, ambiguous, and invalid clock times assert!(matches!( et_to_utc_micros(day(2026, 3, 8), 2, 30, 0), Err(ShortsError::NonexistentLocalTime { .. }) )); assert!(matches!( et_to_utc_micros(day(2026, 11, 1), 1, 30, 0), Err(ShortsError::AmbiguousLocalTime { .. }) )); assert!(matches!( et_to_utc_micros(day(2024, 6, 12), 25, 0, 0), Err(ShortsError::InvalidLocalTime { .. }) )); } #[test] fn build_shorts_happy_path_bad_rows_dedup_and_empty_input() { let empty = build_shorts::(Vec::new()).unwrap(); assert!(empty.rows.is_empty()); assert_eq!(empty.stats, ShortsStats::default()); assert_accounting_and_order(&empty); let d_winter = day(2023, 12, 29); // +4bd -> 2024-01-05 (EST -> 22:00Z) let d_summer = day(2024, 6, 15); // Sat -> +4bd -> 2024-06-20 (EDT -> 21:00Z) let d_easter = day(2024, 3, 28); // Thu before Good Friday -> +4bd -> 2024-04-04 (EDT -> 21:00Z) let rows = vec![ CeoShortReport { key: 20, report_date: d_summer, shares_short: 250_000.0, net_change: Some(-12_500.0), pct_float: Some(3.25), }, // Identical duplicate with -0.0 normalization CeoShortReport { key: 10, report_date: d_winter, shares_short: -0.0, net_change: Some(-0.0), pct_float: Some(-0.0), }, CeoShortReport { key: 10, report_date: d_winter, shares_short: 0.0, net_change: Some(0.0), pct_float: Some(0.0), }, // Non-finite optional fields nulled and counted CeoShortReport { key: 10, report_date: d_easter, shares_short: 42_000.0, net_change: Some(f64::NEG_INFINITY), pct_float: Some(f64::NAN), }, // Invalid shares_short dropped without incrementing optional-field null counters CeoShortReport { key: 30, report_date: d_winter, shares_short: -1.0, net_change: Some(f64::NAN), pct_float: Some(f64::INFINITY), }, CeoShortReport { key: 30, report_date: d_summer, shares_short: f64::NAN, net_change: None, pct_float: None, }, // Conflicting duplicate group dropped entirely CeoShortReport { key: 40, report_date: d_summer, shares_short: 10_000.0, net_change: Some(500.0), pct_float: None, }, CeoShortReport { key: 40, report_date: d_summer, shares_short: 10_000.0, net_change: Some(600.0), pct_float: None, }, ]; let built = build_shorts(rows).unwrap(); assert_accounting_and_order(&built); assert_eq!(built.stats.rows_in, 8); assert_eq!(built.stats.rows_out, 3); assert_eq!(built.stats.keys_out, 2); assert_eq!(built.stats.dropped_invalid_shares_short, 2); assert_eq!(built.stats.nulled_invalid_net_change, 1); assert_eq!(built.stats.nulled_invalid_pct_float, 1); assert_eq!(built.stats.duplicate_collapsed, 1); assert_eq!(built.stats.duplicate_conflict_dropped, 2); assert_eq!(built.rows[0].key, 10); assert_eq!(built.rows[0].known_at_date, day(2024, 1, 5)); assert_eq!( built.rows[0].known_at_utc_micros, utc_micros(2024, 1, 5, 22, 0, 0) ); assert_eq!(built.rows[0].shares_short.to_bits(), 0.0_f64.to_bits()); assert_eq!(built.rows[1].key, 10); assert_eq!(built.rows[1].known_at_date, day(2024, 4, 4)); assert_eq!( built.rows[1].known_at_utc_micros, utc_micros(2024, 4, 4, 21, 0, 0) ); assert_eq!(built.rows[1].net_change, None); assert_eq!(built.rows[1].pct_float, None); assert_eq!(built.rows[2].key, 20); assert_eq!(built.rows[2].known_at_date, day(2024, 6, 20)); assert_eq!( built.rows[2].known_at_utc_micros, utc_micros(2024, 6, 20, 21, 0, 0) ); } #[test] fn permutation_invariance_holds_across_fixed_seed_shuffles() { let dates = [ day(2017, 4, 15), day(2020, 12, 31), day(2022, 12, 15), day(2023, 9, 30), day(2024, 3, 15), day(2024, 3, 28), day(2025, 11, 15), day(2026, 4, 30), ]; let mut input = Vec::new(); for key in 1..=25_i32 { for (idx, &d) in dates.iter().enumerate() { let base = CeoShortReport { key, report_date: d, shares_short: ((key as usize * 1_000) + idx * 50) as f64, net_change: if idx % 3 == 0 { Some(f64::NAN) } else { Some((idx as f64 - 3.0) * 100.0) }, pct_float: if idx % 4 == 0 { Some(f64::INFINITY) } else { Some(0.5 + idx as f64 * 0.25) }, }; input.push(base.clone()); if (key + idx as i32) % 7 == 0 { input.push(base.clone()); // identical duplicate } if (key + idx as i32) % 11 == 0 { let mut conflict = base; conflict.shares_short += 1.0; input.push(conflict); // conflicting duplicate } } if key % 5 == 0 { input.push(CeoShortReport { key, report_date: dates[0], shares_short: -10.0, net_change: Some(f64::NAN), pct_float: None, }); } } let baseline = build_shorts(input.clone()).unwrap(); assert_accounting_and_order(&baseline); assert!(baseline.stats.duplicate_collapsed > 0); assert!(baseline.stats.duplicate_conflict_dropped > 0); assert!(baseline.stats.dropped_invalid_shares_short > 0); assert!(baseline.stats.nulled_invalid_net_change > 0); assert!(baseline.stats.nulled_invalid_pct_float > 0); let mut rng = Rng(0x570C_4B07_5404_9999); for _ in 0..16 { let mut perm = input.clone(); shuffle(&mut perm, &mut rng); let trial = build_shorts(perm).unwrap(); assert_accounting_and_order(&trial); assert_rows_bit_eq(&baseline.rows, &trial.rows); assert_eq!(baseline.stats, trial.stats); } } #[test] fn run_shorts_benchmark_and_cli_binary_are_reproducible() { let a = run_shorts_benchmark(2_000).unwrap(); let b = run_shorts_benchmark(2_000).unwrap(); assert_eq!(a.rows, 2_000); assert_eq!(a.rows_out, b.rows_out); assert_eq!(a.keys_out, b.keys_out); assert_eq!(a.stats, b.stats); assert_eq!(a.checksum, b.checksum); assert_ne!(a.checksum, 0); let bin = env!("CARGO_BIN_EXE_curate-etl"); let run1 = Command::new(bin) .args(["bench-shorts", "5000"]) .output() .expect("run bench-shorts #1"); let run2 = Command::new(bin) .args(["bench-shorts", "5000"]) .output() .expect("run bench-shorts #2"); assert!(run1.status.success(), "bench-shorts #1 failed"); assert!(run2.status.success(), "bench-shorts #2 failed"); let out1 = String::from_utf8_lossy(&run1.stdout); let out2 = String::from_utf8_lossy(&run2.stdout); assert!(out1.contains("curate-etl bench-shorts")); assert!(out1.contains("rows/s")); assert!(out1.contains("checksum=0x")); let extract_checksum = |s: &str| -> String { s.split("checksum=") .nth(1) .and_then(|rest| rest.split_whitespace().next()) .expect("checksum token present") .to_string() }; assert_eq!(extract_checksum(&out1), extract_checksum(&out2)); // Invalid CLI args exit non-zero and mention bench-shorts in usage for bad in [ &["bench-shorts", "0"][..], &["bench-shorts", "abc"][..], &["bench-shorts", "10", "20"][..], ] { let out = Command::new(bin) .args(bad) .output() .expect("run invalid bench-shorts"); assert!(!out.status.success(), "expected failure for {bad:?}"); let stderr = String::from_utf8_lossy(&out.stderr); assert!(stderr.contains("bench-shorts")); } }