pub struct LatePush;
use super::{BonusTrack, PushContext, Reward, Tier};
static TIERS: &[Tier] = &[
Tier {
cost: 50,
reward: Reward::Multiplier(2),
},
Tier {
cost: 500,
reward: Reward::Multiplier(3),
},
Tier {
cost: 3000,
reward: Reward::Multiplier(4),
},
Tier {
cost: 20000,
reward: Reward::Multiplier(5),
},
Tier {
cost: 120000,
reward: Reward::Multiplier(6),
},
];
impl BonusTrack for LatePush {
fn id(&self) -> &'static str {
"late_push"
}
fn name(&self) -> &'static str {
"Night Owl"
}
fn description(&self) -> &'static str {
"Multiplier for pushing code when you should probably be sleeping (10pm to 6am)."
}
fn tiers(&self) -> &'static [Tier] {
TIERS
}
fn applies(&self, ctx: &PushContext) -> u32 {
const SIX_AM: i64 = 6 * 3600;
const TEN_PM: i64 = 22 * 3600;
let after_ten = ctx.clock.local_seconds_since_midnight() >= TEN_PM;
let before_six = ctx.clock.local_seconds_since_midnight() <= SIX_AM;
if (after_ten || before_six) && !ctx.push.commits().is_empty() {
1
} else {
0
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
bonus_track::Clock,
git::{Commit, Push},
storage::{DbConnection, PushHistory},
};
const UTC_MINUS_8: i32 = -8 * 3600;
const MIDNIGHT_PST_AS_UTC: u64 = 1769760000;
fn clock_at_hour(hour: u64) -> Clock {
Clock::with_offset(MIDNIGHT_PST_AS_UTC + hour * 3600, UTC_MINUS_8)
}
#[test]
fn applies_between_10pm_and_6am() {
let conn = DbConnection::create_in_memory().unwrap();
let bonus = LatePush;
let history = PushHistory::new(&conn);
let push = Push::new(vec![Commit::default()]);
let clock = clock_at_hour(22);
let ctx = PushContext {
push: &push,
history: &history,
clock: &clock,
};
assert_eq!(bonus.applies(&ctx), 1);
let clock = clock_at_hour(0);
let ctx = PushContext {
push: &push,
history: &history,
clock: &clock,
};
assert_eq!(bonus.applies(&ctx), 1);
let clock = clock_at_hour(0);
let ctx = PushContext {
push: &push,
history: &history,
clock: &clock,
};
assert_eq!(bonus.applies(&ctx), 1);
let clock = clock_at_hour(6);
let ctx = PushContext {
push: &push,
history: &history,
clock: &clock,
};
assert_eq!(bonus.applies(&ctx), 1);
}
#[test]
fn does_not_apply_outside_10pm_to_6am() {
let conn = DbConnection::create_in_memory().unwrap();
let bonus = LatePush;
let push = Push::new(vec![Commit::default()]);
let history = PushHistory::new(&conn);
let clock = clock_at_hour(21);
let ctx = PushContext {
push: &push,
history: &history,
clock: &clock,
};
assert_eq!(bonus.applies(&ctx), 0);
let clock = clock_at_hour(7);
let ctx = PushContext {
push: &push,
history: &history,
clock: &clock,
};
assert_eq!(bonus.applies(&ctx), 0);
}
#[test]
fn does_not_apply_to_empty_pushes() {
let conn = DbConnection::create_in_memory().unwrap();
let bonus = LatePush;
let push = Push::new(vec![]);
let history = PushHistory::new(&conn);
let clock = clock_at_hour(23);
let ctx = PushContext {
push: &push,
history: &history,
clock: &clock,
};
assert_eq!(bonus.applies(&ctx), 0);
}
}