use super::settings::{ClockTime, DayLock};
use super::*;
const MONDAY: Day = 20_787;
const DAY_MS: i64 = 24 * 60 * MINUTE;
struct Utc;
impl Zone for Utc {
fn instant(&self, day: Day, minutes: u16) -> i64 {
day * DAY_MS + i64::from(minutes) * MINUTE
}
fn day_of(&self, wall: i64) -> Day {
wall.div_euclid(DAY_MS)
}
}
struct Pc {
engine: Engine,
now: Moment,
session: Session,
}
fn at(day: Day, h: i64, m: i64) -> i64 {
day * DAY_MS + (h * 60 + m) * MINUTE
}
fn settings(strict: bool) -> LockSettings {
LockSettings {
monday: DayLock {
enabled: true,
time: ClockTime(17 * 60 + 45),
strict,
},
..Default::default()
}
}
impl Pc {
fn new(settings: &LockSettings) -> Pc {
let now = Moment {
wall: at(MONDAY, 9, 0),
tick: 1_000_000,
awake: 1_000_000,
sign_in: 1,
};
let (engine, _) = Engine::start(Saved::default(), settings, now, &Utc);
let session = Session {
lock: LockState::Unlocked,
connected: true,
};
Pc {
engine,
now,
session,
}
}
fn tick(&mut self) -> Vec<Effect> {
self.engine.advance(self.now, self.session, &Utc)
}
fn run_to(&mut self, wall: i64) -> Vec<Effect> {
let mut out = Vec::new();
while self.now.wall < wall {
self.pass(1000);
out.extend(self.tick());
}
out
}
fn pass(&mut self, ms: i64) {
self.now.wall += ms;
self.now.tick += ms;
self.now.awake += ms;
}
fn sleep_to(&mut self, wall: i64) -> Vec<Effect> {
self.now.tick += wall - self.now.wall;
self.now.wall = wall;
self.tick()
}
fn reopen(&mut self, wall: i64, settings: &LockSettings) -> Vec<Effect> {
let text = toml::to_string_pretty(self.engine.saved()).unwrap();
let saved = toml::from_str(&text).unwrap();
self.now = Moment {
wall,
tick: 5_000,
awake: 5_000,
sign_in: self.now.sign_in + 1,
};
let (engine, mut out) = Engine::start(saved, settings, self.now, &Utc);
self.engine = engine;
out.extend(self.tick());
out
}
fn set_lock(&mut self, lock: LockState) {
self.session.lock = lock;
self.tick();
}
fn card(&mut self) -> Option<CardKind> {
let card = self.engine.card(self.now, self.session, &Utc);
card.map(|c| c.kind)
}
}
#[test]
fn locks_on_time_then_relocks_until_the_period_ends() {
let mut pc = Pc::new(&settings(false));
assert!(pc.run_to(at(MONDAY, 17, 44)).is_empty());
assert!(pc.run_to(at(MONDAY, 17, 45)).contains(&Effect::Lock));
pc.set_lock(LockState::Locked);
pc.run_to(at(MONDAY, 17, 50));
pc.set_lock(LockState::Unlocked);
assert_eq!(pc.card(), Some(CardKind::Relock));
assert!(pc.run_to(at(MONDAY, 17, 51)).contains(&Effect::Lock));
pc.set_lock(LockState::Locked);
pc.run_to(at(MONDAY, 18, 1));
pc.set_lock(LockState::Unlocked);
assert!(pc.run_to(at(MONDAY, 23, 0)).is_empty());
}
#[test]
fn postponing_moves_the_cutoff_and_may_cross_midnight() {
let mut s = settings(false);
s.monday.time = ClockTime(23 * 60 + 30);
s.postpone_minutes = 60;
s.max_postponements = 1;
let mut pc = Pc::new(&s);
pc.run_to(at(MONDAY, 22, 55));
assert!(pc.engine.postpone(None, pc.now, &Utc));
assert!(!pc.engine.postpone(None, pc.now, &Utc), "only one allowed");
assert!(pc.run_to(at(MONDAY, 23, 45)).is_empty());
assert!(
pc.engine.skip(None, pc.now, &Utc),
"the cutoff moved to midnight"
);
pc.engine.saved.occurrences[0].skipped = false;
pc.run_to(at(MONDAY + 1, 0, 1));
assert!(!pc.engine.skip(None, pc.now, &Utc));
assert!(pc.run_to(at(MONDAY + 1, 0, 30)).contains(&Effect::Lock));
}
#[test]
fn editing_the_file_while_closed_cannot_avoid_a_strict_lock() {
let mut s = settings(true);
s.tuesday = s.monday;
let mut pc = Pc::new(&s);
pc.run_to(at(MONDAY, 10, 0));
pc.reopen(at(MONDAY + 1, 17, 50), &LockSettings::default());
assert!(pc.engine.exit_refused(pc.now, &Utc));
assert_eq!(pc.card(), Some(CardKind::Late));
let until = pc.now.wall + 61_000;
assert!(pc.run_to(until).contains(&Effect::Lock));
}
#[test]
fn strict_lock_is_fixed_after_its_cutoff_and_a_normal_one_is_not() {
for strict in [true, false] {
let mut pc = Pc::new(&settings(strict));
pc.run_to(at(MONDAY, 17, 20));
assert_eq!(pc.engine.exit_refused(pc.now, &Utc), strict);
let off = LockSettings::default();
assert_eq!(pc.engine.apply_settings(&off, pc.now, &Utc), strict);
let locked = pc.run_to(at(MONDAY, 17, 46)).contains(&Effect::Lock);
assert_eq!(locked, strict);
}
}
#[test]
fn turning_a_day_on_after_its_cutoff_waits_for_the_next() {
let mut pc = Pc::new(&LockSettings::default());
pc.run_to(at(MONDAY, 17, 20));
assert!(pc.engine.apply_settings(&settings(true), pc.now, &Utc));
assert!(pc.run_to(at(MONDAY, 18, 30)).is_empty());
assert!(!pc.engine.exit_refused(pc.now, &Utc));
}
#[test]
fn a_return_inside_the_window_is_warned_and_a_later_one_is_missed() {
let mut pc = Pc::new(&settings(true));
pc.run_to(at(MONDAY, 17, 40));
assert!(pc.sleep_to(at(MONDAY, 18, 0)).is_empty());
assert_eq!(pc.card(), Some(CardKind::Late));
assert!(pc.run_to(at(MONDAY, 18, 1)).contains(&Effect::Lock));
let mut pc = Pc::new(&settings(true));
pc.run_to(at(MONDAY, 17, 40));
pc.sleep_to(at(MONDAY, 18, 16));
assert!(pc.run_to(at(MONDAY, 19, 0)).is_empty());
assert!(!pc.engine.exit_refused(pc.now, &Utc));
}
#[test]
fn a_late_return_warning_is_not_renewed_by_sleeping() {
let mut pc = Pc::new(&settings(true));
pc.run_to(at(MONDAY, 17, 40));
pc.sleep_to(at(MONDAY, 17, 50));
pc.sleep_to(at(MONDAY, 17, 55));
let soon = pc.now.wall + 11_000;
assert!(pc.run_to(soon).contains(&Effect::Lock));
let mut pc = Pc::new(&settings(true));
pc.run_to(at(MONDAY, 17, 40));
pc.sleep_to(at(MONDAY, 17, 50));
pc.sleep_to(at(MONDAY, 20, 0));
assert!(pc.run_to(at(MONDAY, 20, 5)).is_empty());
}
#[test]
fn the_short_countdown_of_a_late_return_survives_sleep_and_restarts() {
let s = settings(true);
let mut pc = Pc::new(&s);
pc.run_to(at(MONDAY, 17, 40));
pc.sleep_to(at(MONDAY, 17, 50));
pc.sleep_to(at(MONDAY, 17, 55));
pc.run_to(pc.now.wall + 3_000);
assert!(!pc.sleep_to(pc.now.wall + 60_000).contains(&Effect::Lock));
let closed = pc.now.wall + 2_000;
assert!(!pc.run_to(closed).contains(&Effect::Lock));
let restarted = closed + 2_000;
let mut out = pc.reopen(restarted, &s);
out.extend(pc.run_to(restarted + 6_000));
assert!(out.contains(&Effect::Lock));
}
#[test]
fn restarting_in_a_locked_period_gives_one_warning() {
let s = settings(true);
let mut pc = Pc::new(&s);
pc.run_to(at(MONDAY, 17, 45));
pc.set_lock(LockState::Locked);
pc.session.lock = LockState::Unlocked;
pc.reopen(at(MONDAY, 17, 50), &s);
assert_eq!(pc.card(), Some(CardKind::Startup));
pc.reopen(at(MONDAY, 17, 52), &s);
assert!(pc.run_to(at(MONDAY, 17, 52) + 1000).is_empty());
assert_eq!(pc.card(), Some(CardKind::Relock));
let soon = pc.now.wall + 11_000;
assert!(pc.run_to(soon).contains(&Effect::Lock));
pc.set_lock(LockState::Locked);
pc.session.lock = LockState::Unlocked;
pc.reopen(at(MONDAY, 17, 59) + 30_000, &s);
assert!(pc.run_to(at(MONDAY, 18, 30)).contains(&Effect::Lock));
}
#[test]
fn too_little_left_at_the_first_restart_means_no_lock() {
let s = settings(true);
let mut pc = Pc::new(&s);
pc.run_to(at(MONDAY, 17, 45));
pc.set_lock(LockState::Locked);
pc.session.lock = LockState::Unlocked;
pc.reopen(at(MONDAY, 17, 59) + 30_000, &s);
assert_eq!(pc.card(), None);
assert!(pc.run_to(at(MONDAY, 18, 30)).is_empty());
}
#[test]
fn clock_changes_do_not_move_a_fixed_lock_or_extend_a_period() {
let mut pc = Pc::new(&settings(true));
pc.run_to(at(MONDAY, 17, 20));
pc.now.wall -= 3 * 60 * MINUTE;
let due = pc.now.wall + 25 * MINUTE;
assert!(pc.run_to(due).contains(&Effect::Lock));
pc.set_lock(LockState::Locked);
pc.now.wall -= DAY_MS;
let end = pc.now.wall + 16 * MINUTE;
pc.run_to(end);
assert!(!pc.engine.in_period());
}
#[test]
fn moving_the_clock_forward_and_back_does_not_discard_a_lock() {
let mut pc = Pc::new(&settings(true));
pc.now.wall += 12 * 60 * MINUTE;
pc.tick();
pc.now.wall -= 12 * 60 * MINUTE;
assert!(pc.run_to(at(MONDAY, 17, 45)).contains(&Effect::Lock));
}
#[test]
fn a_failed_lock_is_retried_and_an_unconfirmed_zero_period_is_not_repeated() {
let mut pc = Pc::new(&settings(false));
pc.run_to(at(MONDAY, 17, 45));
let retried = pc.run_to(at(MONDAY, 17, 46));
assert!(retried.contains(&Effect::Lock));
assert_eq!(pc.card(), Some(CardKind::Failed));
let mut s = settings(false);
s.locked_minutes = 0;
let mut pc = Pc::new(&s);
assert!(pc.run_to(at(MONDAY, 17, 45)).contains(&Effect::Lock));
let out = pc.reopen(at(MONDAY, 17, 45) + 2000, &s);
assert!(matches!(out[..], [Effect::Log(_)]), "{out:?}");
assert!(pc.run_to(at(MONDAY, 18, 0)).is_empty());
}
#[test]
fn locked_by_hand_at_the_lock_time_starts_the_period_then() {
let mut pc = Pc::new(&settings(false));
pc.run_to(at(MONDAY, 17, 40));
pc.set_lock(LockState::Locked);
let out = pc.run_to(at(MONDAY, 17, 50));
assert_eq!(out, vec![Effect::AlreadyLocked]);
pc.set_lock(LockState::Unlocked);
assert_eq!(pc.card(), Some(CardKind::Relock));
pc.engine.hold(pc.now);
assert!(pc.run_to(at(MONDAY, 17, 52)).is_empty());
assert!(pc.engine.end_period());
assert!(pc.run_to(at(MONDAY, 18, 30)).is_empty());
}
#[test]
fn retiming_after_a_late_enable_applies_today_when_its_cutoff_allows() {
let mut pc = Pc::new(&LockSettings::default());
pc.run_to(at(MONDAY, 17, 20));
let mut s = settings(false);
assert!(pc.engine.apply_settings(&s, pc.now, &Utc));
s.monday.time = ClockTime(19 * 60);
assert!(!pc.engine.apply_settings(&s, pc.now, &Utc));
assert!(pc.run_to(at(MONDAY, 19, 0)).contains(&Effect::Lock));
pc.set_lock(LockState::Locked);
pc.run_to(at(MONDAY, 19, 30));
s.monday.time = ClockTime(21 * 60);
assert!(
pc.engine.apply_settings(&s, pc.now, &Utc),
"already done today, so the user is told"
);
}
#[test]
fn a_missed_lock_that_was_turned_off_stays_off() {
let mut pc = Pc::new(&settings(false));
pc.run_to(at(MONDAY, 12, 0));
pc.sleep_to(at(MONDAY, 18, 30));
let off = LockSettings::default();
pc.engine.apply_settings(&off, pc.now, &Utc);
pc.now.wall -= 3 * 60 * MINUTE;
assert!(pc.run_to(at(MONDAY, 18, 0)).is_empty());
}
#[test]
fn a_corrected_clock_does_not_hold_a_strict_lock_open() {
let s = settings(true);
let mut pc = Pc::new(&s);
pc.run_to(at(MONDAY, 17, 20));
pc.reopen(at(MONDAY - 300, 9, 0), &s);
let out = pc.run_to(at(MONDAY - 300, 9, 31));
assert!(out.contains(&Effect::Lock));
}
#[test]
fn a_far_clock_jump_keeps_skips_and_strict_locks() {
let mut pc = Pc::new(&settings(true));
pc.run_to(at(MONDAY, 17, 20));
pc.now.wall += 20 * DAY_MS;
pc.tick();
assert!(pc.engine.exit_refused(pc.now, &Utc));
let mut pc = Pc::new(&settings(false));
pc.run_to(at(MONDAY, 12, 0));
assert!(pc.engine.skip(Some(MONDAY), pc.now, &Utc));
pc.now.wall += 20 * DAY_MS;
pc.tick();
pc.now.wall -= 20 * DAY_MS;
assert!(pc.run_to(at(MONDAY, 18, 30)).is_empty());
}
#[test]
fn skipping_acts_only_on_the_lock_that_was_shown() {
let mut s = settings(true);
s.tuesday = DayLock {
enabled: true,
time: ClockTime(10),
strict: true,
};
let mut pc = Pc::new(&s);
pc.run_to(at(MONDAY, 17, 16));
assert!(!pc.engine.skip(Some(MONDAY), pc.now, &Utc));
assert!(pc.engine.skip(Some(MONDAY + 1), pc.now, &Utc));
}