diff --git a/tools/curate-etl/tests/events_reference.rs b/tools/curate-etl/tests/events_reference.rs index f87e9138..92e1e357 100644 --- a/tools/curate-etl/tests/events_reference.rs +++ b/tools/curate-etl/tests/events_reference.rs @@ -279,3 +279,317 @@ fn default_bench_events_matches_readme_recorded_output() { assert_eq!(report.stats.earnings_duplicate_conflict_dropped, 706); assert_eq!(report.checksum, 0xbf74_cd74_5a26_46c6); } + +// --------------------------------------------------------------------------------------------- +// QA additions (T-77): robustness, PIT truncation, link eligibility and boundary edge cases. +// --------------------------------------------------------------------------------------------- + +struct QaRng(u64); + +impl QaRng { + 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 filing(key: i32, timefiled: &str, form_type: &str, id: Option<&str>) -> QmFiling { + QmFiling { + key, + timefiled: timefiled.to_string(), + form_type: form_type.to_string(), + filing_id: id.map(str::to_string), + url: None, + } +} + +fn earning(key: i32, report_date: i32, announce_time: &str, eps: f64) -> QmEarning { + QmEarning { + key, + report_date, + announce_time: announce_time.to_string(), + fiscal_period_end: None, + actual_eps: Some(eps), + revenue: None, + } +} + +#[test] +fn qa_non_ascii_and_seeded_garbage_timefiled_never_panics_and_is_counted() { + // Multibyte characters at structural offsets must not cause byte-slice panics. + let fixed = [ + "2025-01-31T06:01:27 ÉST", + "2025-01-31T06:01:27 EST", + "2025é01-31T06:01:27 EST", + "2025-01-31T06:01:2é EST", + "2025-01-31T06:01:27\u{00a0}EST", + "2025-01-31T06:01:27.123 EST", + "+025-01-31T06:01:27 EST", + "2025-01-31T06:01:27 EST\0", + ]; + for raw in fixed { + assert!(parse_filing_timefiled(raw).is_err(), "must reject {raw:?}"); + } + + let alphabet: Vec = "0123456789-T: ESTDZé\u{00a0}2/".chars().collect(); + let template: Vec = "2025-01-31T06:01:27 EST".chars().collect(); + let mut rng = QaRng(0x0A11_7E57_0000_0077); + let mut inputs = Vec::new(); + for i in 0..5_000 { + let mut chars = template.clone(); + let mutations = 1 + (rng.next() % 3) as usize; + for _ in 0..mutations { + let pos = (rng.next() as usize) % chars.len(); + chars[pos] = alphabet[(rng.next() as usize) % alphabet.len()]; + } + if rng.next() % 5 == 0 { + let cut = (rng.next() as usize) % chars.len(); + chars.truncate(cut); + } + let raw: String = chars.into_iter().collect(); + let _ = parse_filing_timefiled(&raw); // must not panic + inputs.push(filing(i % 7, &raw, "8-K", Some(&format!("g{i}")))); + } + + let built = build_events(inputs, Vec::>::new()); + let s = built.stats; + assert_eq!(s.filings_in, 5_000); + assert_eq!( + s.filings_in, + s.filings_out + + s.filings_skipped_malformed_timestamp + + s.filings_skipped_unknown_zone + + s.filings_skipped_invalid_date_time + + s.filings_duplicate_collapsed + ); + assert_eq!(s.rows_out, built.rows.len()); + // Every emitted row came from a string that parses successfully on its own. + for row in &built.rows { + assert_eq!(row.known_at_source, KNOWN_AT_SOURCE_FILING_TIMESTAMP); + assert_eq!(row.ts_micros, row.known_at_micros); + } +} + +#[test] +fn qa_skipped_or_non_qualifying_same_day_filings_never_anchor_earnings() { + let report = day(2025, 1, 31); + let filings = vec![ + // Same key, same ET date, qualifying form, but each row is unparseable → must not link. + filing(1, "2025-01-31T07:00:00 PST", "8-K", Some("bad-zone")), + filing( + 1, + "2025-01-31T25:00:00 EST", + "News release", + Some("bad-time"), + ), + filing(1, "2025-01-31 07:00:00 EST", "8-K/A", Some("bad-format")), + // Valid but non-qualifying forms on the same day. + filing(1, "2025-01-31T06:00:00 EST", "10-Q", Some("q")), + filing(1, "2025-01-31T06:30:00 EST", "NT 10-K", Some("nt")), + filing(1, "2025-01-31T06:45:00 EST", "MD&A", Some("mda")), + ]; + let built = build_events(filings, vec![earning(1, report, "BTO", 1.0)]); + + let e: Vec<_> = built + .rows + .iter() + .filter(|r| r.kind == EVENT_KIND_EARNINGS) + .collect(); + assert_eq!(e.len(), 1); + assert_eq!(e[0].known_at_source, KNOWN_AT_SOURCE_STATUTORY_LAG); + // 2025-01-31 is EST → 16:05 ET == 21:05Z. + assert_eq!( + e[0].known_at_micros, + oracle_utc_micros(2025, 1, 31, 21, 5, 0, 0) + ); + assert!(e[0].payload_json.contains("\"linked_filing_id\":null")); + assert_eq!(built.stats.earnings_linked_to_filing, 0); + assert_eq!(built.stats.earnings_lagged_1605, 1); + assert_eq!(built.stats.filings_skipped_unknown_zone, 1); + assert_eq!(built.stats.filings_skipped_invalid_date_time, 1); + assert_eq!(built.stats.filings_skipped_malformed_timestamp, 1); +} + +#[test] +fn qa_nt_and_variant_edgar_forms_are_not_periodic_reports() { + use curate_etl::{classify_form_type, is_periodic_financial_report, FilingFormGroup}; + // A late-filing notice must never be treated as the periodic report itself (PIT join safety). + for raw in [ + "NT 10-K", "NT 10-Q", "10-K405", "10-KT", "10-QT", "8-K12B", "40-FR12B", + ] { + assert!( + !is_periodic_financial_report(raw), + "{raw:?} must not be periodic" + ); + assert_eq!(classify_form_type(raw).0, FilingFormGroup::Other, "{raw:?}"); + } + // `/A` with internal whitespace still keeps the base group. + assert_eq!( + classify_form_type("10-K /A"), + (FilingFormGroup::Form10K, true) + ); + assert_eq!(classify_form_type("8-k/a"), (FilingFormGroup::Form8K, true)); +} + +#[test] +fn qa_earnings_duplicates_normalize_announce_time_and_conflicting_times_drop_group() { + let d = day(2025, 7, 15); + let earnings = vec![ + // Same normalized announce time ("bto") → collapse. + earning(1, d, "BTO", 2.0), + earning(1, d, " bto ", 2.0), + // "N/A" vs "other code" both normalize to "unknown" → collapse. + earning(2, d, "N/A", 3.0), + earning(2, d, "TBD", 3.0), + // BTO vs AMC for the same (key, report_date) → conflict, whole group dropped. + earning(3, d, "BTO", 4.0), + earning(3, d, "AMC", 4.0), + earning(3, d, "AMC", 4.0), + ]; + let built = build_events(Vec::>::new(), earnings); + let s = built.stats; + assert_eq!(s.earnings_in, 7); + assert_eq!(s.earnings_out, 2); + assert_eq!(s.earnings_duplicate_collapsed, 2); + assert_eq!(s.earnings_duplicate_conflict_dropped, 3); + assert!(built.rows.iter().all(|r| r.key != 3)); + let subtypes: Vec<_> = built + .rows + .iter() + .map(|r| (r.key, r.subtype.as_str())) + .collect(); + assert_eq!(subtypes, vec![(1, "bto"), (2, "unknown")]); + // 2025-07-15 is EDT → 16:05 ET == 20:05Z. + for row in &built.rows { + assert_eq!( + row.known_at_micros, + oracle_utc_micros(2025, 7, 15, 20, 5, 0, 0) + ); + } +} + +#[test] +fn qa_supported_range_boundaries_are_inclusive_for_unlinked_earnings() { + use curate_etl::{MAX_SUPPORTED_DATE, MIN_SUPPORTED_DATE}; + let earnings = vec![ + earning(1, MIN_SUPPORTED_DATE, "AMC", 1.0), + earning(2, MAX_SUPPORTED_DATE, "AMC", 1.0), + earning(3, MIN_SUPPORTED_DATE - 1, "AMC", 1.0), + earning(4, MAX_SUPPORTED_DATE + 1, "AMC", 1.0), + earning(5, i32::MIN, "AMC", 1.0), + earning(6, i32::MAX, "AMC", 1.0), + ]; + let built = build_events(Vec::>::new(), earnings); + assert_eq!(built.stats.earnings_out, 2); + assert_eq!(built.stats.earnings_skipped_out_of_range, 4); + // 2008-01-01 (EST) → 21:05Z; 2030-12-31 (EST) → 21:05Z. + assert_eq!( + built.rows[0].known_at_micros, + oracle_utc_micros(2008, 1, 1, 21, 5, 0, 0) + ); + assert_eq!( + built.rows[1].known_at_micros, + oracle_utc_micros(2030, 12, 31, 21, 5, 0, 0) + ); +} + +/// Extracts the `"report_date":"YYYY-MM-DD"` field from an earnings payload. +fn payload_report_date(payload: &str) -> String { + let marker = "\"report_date\":\""; + let start = payload.find(marker).expect("report_date present") + marker.len(); + payload[start..start + 10].to_string() +} + +#[test] +fn qa_seeded_truncation_rebuild_matches_full_build_for_every_cutoff() { + // Lens truncation test: deleting everything dated at/after ET date D and rebuilding must + // reproduce exactly the full build's rows dated before D (no future-dependent output). + let mut rng = QaRng(0x7A0C_A710_0000_0077); + let forms = [ + "8-K", + "News release", + "4", + "10-Q", + "8-K/A", + "Annual financial statements", + ]; + let start = day(2024, 1, 1); + let span = 120u64; + + let mut filings = Vec::new(); + for i in 0..3_000 { + let key = (rng.next() % 40) as i32; + let d = start + (rng.next() % span) as i32; + let (y, m, dd) = epoch_ymd(d); + // Include late-evening ET times that cross into the next UTC day; EST only so the + // ET-date cutoff maps to a single UTC instant. + let hour = [6, 9, 12, 16, 17, 21, 23][(rng.next() % 7) as usize]; + let ts = format!( + "{y:04}-{m:02}-{dd:02}T{hour:02}:{:02}:{:02} EST", + rng.next() % 60, + rng.next() % 60 + ); + filings.push(( + d, + filing( + key, + &ts, + forms[(rng.next() % 6) as usize], + Some(&format!("t{i}")), + ), + )); + } + let mut earnings = Vec::new(); + for _ in 0..600 { + let key = (rng.next() % 40) as i32; + let d = start + (rng.next() % span) as i32; + let at = ["BTO", "AMC", "N/A"][(rng.next() % 3) as usize]; + earnings.push((d, earning(key, d, at, (rng.next() % 500) as f64 / 100.0))); + } + + let full = build_events( + filings.iter().map(|(_, f)| f.clone()), + earnings.iter().map(|(_, e)| e.clone()), + ); + + for offset in [1, 17, 45, 60, 89, 119] { + let cutoff = start + offset; + let (cy, cm, cd) = epoch_ymd(cutoff); + let cutoff_str = format!("{cy:04}-{cm:02}-{cd:02}"); + let cutoff_utc = i64::from(cutoff) * MICROS_PER_DAY + 5 * MICROS_PER_HOUR; + + let truncated = build_events( + filings + .iter() + .filter(|(d, _)| *d < cutoff) + .map(|(_, f)| f.clone()), + earnings + .iter() + .filter(|(d, _)| *d < cutoff) + .map(|(_, e)| e.clone()), + ); + let expected: Vec<_> = full + .rows + .iter() + .filter(|r| { + if r.kind == EVENT_KIND_EARNINGS { + payload_report_date(&r.payload_json) < cutoff_str + } else { + r.ts_micros < cutoff_utc + } + }) + .cloned() + .collect(); + assert_eq!( + truncated.rows, expected, + "truncation mismatch at cutoff {cutoff_str}" + ); + } +} + +fn epoch_ymd(epoch_day: i32) -> (i32, u32, u32) { + curate_etl::shorts::epoch_days_to_ymd(epoch_day) +}