Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

test(pipes): make cron scheduler tests timezone-independent (#4952)

Ezra Ellettefa4b44ac7289
diff --git a/crates/screenpipe-core/src/pipes/mod.rs b/crates/screenpipe-core/src/pipes/mod.rs
index 0ab0b9de8..cf386f7d2 100644
--- a/crates/screenpipe-core/src/pipes/mod.rs
+++ b/crates/screenpipe-core/src/pipes/mod.rs
@@ -7336,21 +7336,33 @@ mod tests {
}
}
+ /// Parse a raw cron schedule string through the production entry point
+ /// (`parse_schedule`) and unwrap the cron, so tests that pin `now` via
+ /// `cron_should_fire` still cover the string→cron glue `should_run` uses.
+ fn parse_cron(s: &str) -> CronSchedule {
+ match parse_schedule(s) {
+ Some(ParsedSchedule::Cron(c)) => *c,
+ other => panic!("expected cron for {:?}, got {:?}", s, other.is_some()),
+ }
+ }
+
#[test]
fn test_should_run_human_daily() {
// Wall-clock cron only fires when we're actually inside the scheduled
- // slot (within CRON_GRACE_WINDOW). Anchor to the current hour:minute
- // so the slot is "right now" regardless of when the test runs.
- let now = Utc::now();
+ // slot (within CRON_GRACE_WINDOW). `should_run` evaluates crons against
+ // the machine's LOCAL timezone (issue #3851), so anchor the slot to
+ // Local::now() — anchoring to Utc::now() puts the slot hours away on
+ // any non-UTC machine and both assertions break.
+ let now = Local::now();
let cron_str = format!("0 {} {} * * * *", now.minute(), now.hour());
- let yesterday = now - chrono::Duration::hours(25);
+ let yesterday = (now - chrono::Duration::hours(25)).with_timezone(&Utc);
// Last run was yesterday, current minute's slot just passed → fire.
assert!(should_run(&cron_str, yesterday));
// Same cron, but last_run is "right now" (this slot already claimed) →
// don't re-fire. `cron.after(last_run)` is strictly after, so the next
// candidate is tomorrow's slot.
- assert!(!should_run(&cron_str, now));
+ assert!(!should_run(&cron_str, now.with_timezone(&Utc)));
}
#[test]
@@ -7359,13 +7371,13 @@ mod tests {
// just because today's cron slot already passed. Should wait until the
// next future slot. Regression test: a user reported creating an
// "every day at 7am" pipe at 5:39pm that fired 12 seconds later.
- let now = Utc::now();
- // Pick an hour that's unambiguously outside the grace window from now
- // (6h away in either direction).
- let safe_hour = (now.hour() + 6) % 24;
- let cron_str = format!("0 0 {} * * * *", safe_hour);
+ // Pin `now` through the `cron_should_fire` seam (the exact decision
+ // `should_run` delegates to) instead of anchoring an hour to the real
+ // clock, which drifts into the grace window on non-UTC machines.
+ let cron = parse_cron("0 0 7 * * * *"); // every day at 07:00
+ let now = Utc.with_ymd_and_hms(2026, 6, 5, 17, 39, 0).unwrap();
assert!(
- !should_run(&cron_str, DateTime::UNIX_EPOCH),
+ !cron_should_fire(&cron, DateTime::UNIX_EPOCH, now),
"newly-installed cron pipe must not fire immediately on install — \
should wait for the next scheduled slot, not catch up from 1970"
);
@@ -7374,13 +7386,15 @@ mod tests {
#[test]
fn test_should_run_cron_stale_last_run_waits() {
// App was off for days. On restart, a daily cron whose slot passed
- // hours ago must NOT fire immediately — wait for the next slot.
- let now = Utc::now();
- let safe_hour = (now.hour() + 6) % 24;
- let cron_str = format!("0 0 {} * * * *", safe_hour);
+ // hours ago (beyond CRON_CATCHUP_WINDOW) must NOT fire immediately —
+ // wait for the next slot. Pinned `now`: the slot is 16:30 daily, so
+ // its most recent occurrence was 18h before `now` and the next is 6h
+ // after — unambiguously outside the catch-up window in any timezone.
+ let cron = parse_cron("0 30 16 * * * *");
+ let now = Utc.with_ymd_and_hms(2026, 6, 5, 10, 30, 30).unwrap();
let three_days_ago = now - chrono::Duration::days(3);
assert!(
- !should_run(&cron_str, three_days_ago),
+ !cron_should_fire(&cron, three_days_ago, now),
"after extended downtime, cron must wait for the next slot \
instead of firing stale catch-up runs"
);
@@ -7390,10 +7404,12 @@ mod tests {
fn test_should_run_cron_within_grace_fires() {
// Slot was hit moments ago (typical: scheduler tick lag, brief sleep).
// Within grace → fire even though last_run is far in the past.
- let now = Utc::now();
- let cron_str = format!("0 {} {} * * * *", now.minute(), now.hour());
+ // `now` is pinned 30s past the 10:30:00 slot so the assertion holds
+ // in any timezone.
+ let cron = parse_cron("0 30 10 * * * *");
+ let now = Utc.with_ymd_and_hms(2026, 6, 5, 10, 30, 30).unwrap();
let yesterday = now - chrono::Duration::hours(25);
- assert!(should_run(&cron_str, yesterday));
+ assert!(cron_should_fire(&cron, yesterday, now));
}
#[test]
@@ -7401,15 +7417,13 @@ mod tests {
// Regression: app was offline during the scheduled slot. When it restarts
// minutes (or hours) after the slot, the pipe must still fire — not silently
// wait until tomorrow. Reproduces the "morning-brief didn't run" report where
- // the app started at 7:12am after missing the 7am slot.
- use chrono::Timelike;
- let now = Utc::now();
- // Build a cron expression whose last slot was ~12 minutes ago
- let slot_time = now - chrono::Duration::minutes(12);
- let cron_str = format!("0 {} {} * * * *", slot_time.minute(), slot_time.hour());
+ // the app started at 7:12am after missing the 7am slot — pinned literally:
+ // slot at 07:00, `now` at 07:12 (past grace, inside the catch-up window).
+ let cron = parse_cron("0 0 7 * * * *");
+ let now = Utc.with_ymd_and_hms(2026, 6, 5, 7, 12, 0).unwrap();
let last_run = now - chrono::Duration::hours(25); // ran yesterday
assert!(
- should_run(&cron_str, last_run),
+ cron_should_fire(&cron, last_run, now),
"pipe whose slot passed 12 minutes ago must fire on app restart, not wait until tomorrow"
);
}