#![cfg(feature = "bundled-tzdb")]
use opportune::Engine;
use opportune::model::{
Block, BlockStatus, Explain, Goal, Participant, Proposal, ProposalEvent, RangesQuery, Request,
Requirement, Rule, RuleKind, Span, StatusEffect, StatusPolicy, StatusPolicyEntry, Term,
};
use opportune::presets;
use opportune::reasons::ReasonCode;
use opportune::time::{
BlackoutSpan, BlackoutSpec, Date, Duration, Instant, LocalDateTime, LocalResolution, RuleZone,
TimeOfDay, Zone, ZoneName,
};
const MONDAY: (i16, i8, i8) = (2027, 6, 14);
const TUESDAY: (i16, i8, i8) = (2027, 6, 15);
fn engine() -> Engine {
Engine::builder()
.build()
.expect("the bundled database builds")
}
fn date(day: (i16, i8, i8)) -> Date {
Date::new(day.0, day.1, day.2).expect("a fixture date is a real date")
}
fn time(hour: u8, minute: u8) -> TimeOfDay {
TimeOfDay::new(hour, minute).expect("a fixture time is a time of day")
}
fn utc(day: (i16, i8, i8), hour: u8, minute: u8) -> Instant {
let LocalResolution::Unique(instant) = Zone::utc()
.resolve_local(date(day), time(hour, minute))
.expect("the fixture instant is inside the supported range")
else {
panic!("UTC has no transitions");
};
instant
}
fn span(start: Instant, end: Instant) -> Span {
Span::new(start, end).expect("the fixture span is non-empty")
}
fn block(start: Instant, end: Instant) -> Block {
Block::new(span(start, end))
}
fn evening_rest() -> Rule {
presets::rest(time(18, 0), time(10, 0))
}
fn host(id: &str, blocks: Vec<Block>) -> Participant {
let mut participant = Participant::new(id, ZoneName::new("UTC").expect("a zone name"));
participant.calendar = Some(blocks);
participant
}
fn request(
now: Instant,
search: Span,
candidates: Vec<Instant>,
participants: Vec<Participant>,
rules: Vec<Rule>,
) -> Request {
let mut goal = Goal::single(
ZoneName::new("UTC").expect("a zone name"),
search,
Duration::from_minutes(30),
Requirement::new(vec![Term::person("rich")]),
);
goal.candidates = Some(candidates);
let mut request = Request::new(now, goal);
request.participants = participants;
request.rules = rules;
request.explain = Some(Explain::new(10_000));
request
}
fn proposal(start: Instant) -> Proposal {
Proposal {
events: vec![ProposalEvent {
part: "main".to_owned(),
start,
assignment: vec![vec!["rich".to_owned()]],
}],
}
}
fn offered(response: &opportune::model::Response) -> Vec<Instant> {
response
.offers
.iter()
.map(|offer| offer.start().expect("an offer has an event"))
.collect()
}
#[test]
fn the_rest_preset_builds_the_rule_it_names() {
let rule = evening_rest();
assert_eq!(rule.id, "rest");
let rest = rule.rest().expect("the preset writes a rest rule");
assert_eq!(rest.late_after.to_string(), "18:00");
assert_eq!(rest.not_before.to_string(), "10:00");
let morning = utc(TUESDAY, 9, 0);
let request = request(
utc(MONDAY, 0, 0),
span(utc(TUESDAY, 8, 0), utc(TUESDAY, 10, 0)),
vec![morning],
vec![host(
"rich",
vec![block(utc(MONDAY, 19, 0), utc(MONDAY, 20, 0))],
)],
vec![rule],
);
assert!(engine().validate(&request).is_valid());
let response = engine().find_availability(&request).unwrap();
assert!(offered(&response).is_empty());
let reason = &response.excluded[0].reasons[0];
assert_eq!(reason.code, ReasonCode::Rest);
assert_eq!(reason.rule.as_deref(), Some("rest"));
assert_eq!(
reason.to_string(),
"rich: too early after a late finish (rule `rest`)"
);
}
#[test]
fn a_blackout_never_counts_as_rest_activity() {
let blackout = Rule::new(
"freeze",
RuleKind::Blackout(BlackoutSpec::new(
RuleZone::Named(ZoneName::new("UTC").expect("a zone name")),
vec![
BlackoutSpan::new(
LocalDateTime::new(date(MONDAY), time(18, 0)),
LocalDateTime::new(date(MONDAY), time(23, 0)),
)
.expect("the fixture blackout is ascending"),
BlackoutSpan::new(
LocalDateTime::new(date(TUESDAY), time(0, 0)),
LocalDateTime::new(date(TUESDAY), time(8, 0)),
)
.expect("the fixture blackout is ascending"),
],
)),
);
let morning = utc(TUESDAY, 9, 0);
let request = request(
utc(MONDAY, 0, 0),
span(utc(TUESDAY, 8, 0), utc(TUESDAY, 10, 0)),
vec![morning],
vec![host("rich", Vec::new())],
vec![blackout, evening_rest()],
);
let response = engine().find_availability(&request).unwrap();
assert_eq!(offered(&response), [morning]);
}
#[test]
fn an_ignored_block_and_its_travel_never_count_as_activity() {
let mut tentative = block(utc(MONDAY, 19, 0), utc(MONDAY, 19, 30));
tentative.status = BlockStatus::Tentative;
tentative.travel_after = Duration::from_minutes(120);
let morning = utc(TUESDAY, 9, 0);
let search = span(utc(TUESDAY, 8, 0), utc(TUESDAY, 10, 0));
let ignored = Rule::new(
"ignore-tentative",
RuleKind::StatusPolicy(StatusPolicy {
entries: vec![StatusPolicyEntry::status(
BlockStatus::Tentative,
StatusEffect::Ignore,
)],
}),
);
let ignored_request = request(
utc(MONDAY, 0, 0),
search,
vec![morning],
vec![host("rich", vec![tentative.clone()])],
vec![ignored, evening_rest()],
);
let response = engine().find_availability(&ignored_request).unwrap();
assert_eq!(
offered(&response),
[morning],
"an ignored block and its travel are not activity"
);
let request = request(
utc(MONDAY, 0, 0),
search,
vec![morning],
vec![host("rich", vec![tentative])],
vec![evening_rest()],
);
let response = engine().find_availability(&request).unwrap();
assert!(offered(&response).is_empty());
assert_eq!(
response.excluded[0].reasons[0].code,
ReasonCode::Rest,
"a blocking block's travel reaches the late hours"
);
}
#[test]
fn a_booking_that_crosses_midnight_excludes_itself() {
let crossing = utc(MONDAY, 23, 30);
let evening = utc(MONDAY, 22, 0);
let request = request(
utc(MONDAY, 0, 0),
span(utc(MONDAY, 21, 0), utc(TUESDAY, 2, 0)),
vec![evening, crossing],
vec![host("rich", Vec::new())],
vec![evening_rest()],
);
let mut request = request;
request.goal.parts[0].duration = Duration::from_minutes(90);
let response = engine().find_availability(&request).unwrap();
assert_eq!(
offered(&response),
[evening],
"a booking ending at 23:30 never reaches the protected morning"
);
assert_eq!(
response
.excluded
.iter()
.map(|entry| (entry.start, entry.reasons[0].code))
.collect::<Vec<_>>(),
[(crossing, ReasonCode::Rest)]
);
}
#[test]
fn clearance_precedes_rest_and_check_slot_reports_both() {
let morning = utc(TUESDAY, 9, 0);
let buffers = presets::buffers(Duration::from_minutes(30), Duration::from_minutes(0));
let request = request(
utc(MONDAY, 0, 0),
span(utc(TUESDAY, 8, 0), utc(TUESDAY, 10, 0)),
vec![morning],
vec![host(
"rich",
vec![
block(utc(MONDAY, 19, 0), utc(MONDAY, 20, 0)),
block(utc(TUESDAY, 8, 0), utc(TUESDAY, 8, 40)),
],
)],
vec![buffers, evening_rest()],
);
let response = engine().find_availability(&request).unwrap();
let reason = &response.excluded[0].reasons[0];
assert_eq!(reason.code, ReasonCode::Clearance);
assert_eq!(reason.rule.as_deref(), Some("buffers"));
let check = engine().check_slot(&request, &proposal(morning)).unwrap();
assert!(!check.valid);
let codes: Vec<ReasonCode> = check.reasons.iter().map(|reason| reason.code).collect();
assert_eq!(
codes,
[ReasonCode::Clearance, ReasonCode::Rest],
"confirmation reports every failing reason in precedence order"
);
assert_eq!(
check.reasons[1].rule.as_deref(),
Some("rest"),
"the rest reason cites its rule"
);
let later = utc(TUESDAY, 10, 0);
let mut offered_request = request.clone();
offered_request.goal.candidates = Some(vec![later]);
offered_request.goal.search_window = span(utc(TUESDAY, 9, 0), utc(TUESDAY, 11, 0));
let response = engine().find_availability(&offered_request).unwrap();
assert_eq!(offered(&response), [later]);
let check = engine()
.check_slot(&offered_request, &proposal(later))
.unwrap();
assert!(check.valid);
assert!(check.reasons.is_empty());
}
#[test]
fn required_ranges_covers_the_neighbouring_days_rest_reads() {
let search = span(utc(TUESDAY, 9, 0), utc(TUESDAY, 17, 0));
let evening = block(utc(MONDAY, 19, 0), utc(MONDAY, 20, 0));
let request = request(
utc(MONDAY, 0, 0),
search,
vec![utc(TUESDAY, 9, 0)],
vec![host("rich", vec![evening.clone()])],
vec![evening_rest()],
);
let mut query = RangesQuery::new(request.now, request.goal.clone());
query.rules.clone_from(&request.rules);
query.participants.clone_from(&request.participants);
let ranges = engine().required_ranges(&query).unwrap();
assert!(
ranges.calendar.start() <= evening.span.start(),
"the calendar range {}..{} does not reach the evening before the search",
ranges.calendar.start(),
ranges.calendar.end()
);
assert!(ranges.calendar.contains(utc(MONDAY, 23, 59)));
let response = engine().find_availability(&request).unwrap();
assert!(offered(&response).is_empty());
assert_eq!(response.excluded[0].reasons[0].code, ReasonCode::Rest);
}
#[test]
fn a_rest_rule_naming_no_participant_selects_every_host() {
let ana = host("ana", vec![block(utc(MONDAY, 19, 0), utc(MONDAY, 20, 0))]);
let bo = host("bo", vec![block(utc(MONDAY, 19, 0), utc(MONDAY, 20, 0))]);
let mut goal = Goal::single(
ZoneName::new("UTC").expect("a zone name"),
span(utc(TUESDAY, 8, 0), utc(TUESDAY, 10, 0)),
Duration::from_minutes(30),
Requirement::new(vec![Term::at_least(
2,
opportune::tags::Selector::Role(opportune::tags::Role::Host),
)]),
);
goal.candidates = Some(vec![utc(TUESDAY, 9, 0)]);
let mut request = Request::new(utc(MONDAY, 0, 0), goal);
request.participants = vec![ana, bo];
request.rules = vec![evening_rest()];
request.explain = Some(Explain::new(10_000));
let response = engine().find_availability(&request).unwrap();
assert!(offered(&response).is_empty());
let reasons: Vec<(Option<&str>, ReasonCode)> = response.excluded[0]
.reasons
.iter()
.map(|reason| (reason.participant.as_deref(), reason.code))
.collect();
assert_eq!(
reasons,
[
(Some("ana"), ReasonCode::Rest),
(Some("bo"), ReasonCode::Rest),
]
);
request.rules =
vec![evening_rest().with_who(opportune::tags::Selector::Ids(vec!["ana".to_owned()]))];
let response = engine().find_availability(&request).unwrap();
assert!(offered(&response).is_empty());
let reasons: Vec<(Option<&str>, ReasonCode)> = response.excluded[0]
.reasons
.iter()
.map(|reason| (reason.participant.as_deref(), reason.code))
.collect();
assert_eq!(reasons, [(Some("ana"), ReasonCode::Rest)]);
}
#[test]
fn a_rest_rule_leaves_an_event_with_no_activity_alone() {
let starts = vec![utc(TUESDAY, 9, 0), utc(TUESDAY, 11, 0)];
let request = request(
utc(MONDAY, 0, 0),
span(utc(TUESDAY, 8, 0), utc(TUESDAY, 12, 0)),
starts.clone(),
vec![host("rich", Vec::new())],
vec![evening_rest()],
);
let response = engine().find_availability(&request).unwrap();
assert_eq!(offered(&response), starts);
assert!(response.excluded.is_empty());
}
#[test]
fn required_ranges_reaches_a_zone_the_participant_left_before_the_window() {
let monday = (2027, 6, 14);
let tuesday = (2027, 6, 15);
let mut participant = host(
"rich",
vec![
block(utc(monday, 8, 0), utc(monday, 8, 30)),
block(utc(monday, 10, 0), utc(monday, 10, 30)),
],
);
participant.zone = ZoneName::new("Etc/GMT+11").expect("a zone name");
participant.zone_periods.push(
opportune::model::ZonePeriod::new(
ZoneName::new("Pacific/Kiritimati").expect("a zone name"),
utc((2027, 6, 13), 0, 0),
utc(monday, 12, 0),
)
.expect("the fixture leg is non-empty"),
);
let booking = utc(tuesday, 15, 0); let request = request(
utc(monday, 0, 0),
span(utc(tuesday, 12, 0), utc(tuesday, 23, 0)),
vec![booking],
vec![participant],
vec![evening_rest()],
);
let mut query = RangesQuery::new(request.now, request.goal.clone());
query.rules.clone_from(&request.rules);
query.participants.clone_from(&request.participants);
let ranges = engine().required_ranges(&query).unwrap();
assert!(
ranges.calendar.start() <= utc(monday, 8, 0),
"the calendar range {}..{} misses the Kiritimati evening",
ranges.calendar.start(),
ranges.calendar.end()
);
assert!(ranges.calendar.start() <= utc(monday, 10, 0));
let response = engine().find_availability(&request).unwrap();
assert!(offered(&response).is_empty());
assert_eq!(response.excluded[0].reasons[0].code, ReasonCode::Rest);
}
#[test]
fn rest_scoped_to_an_attendee_gates_the_offer() {
let monday = (2027, 6, 14);
let tuesday = (2027, 6, 15);
let mut attendee = host("tom", vec![block(utc(monday, 19, 0), utc(monday, 20, 0))]);
attendee.role = opportune::tags::Role::Attendee;
let scoped = evening_rest().with_who(opportune::tags::Selector::Role(
opportune::tags::Role::Attendee,
));
let booking = utc(tuesday, 9, 0);
let request = request(
utc(monday, 0, 0),
span(utc(tuesday, 8, 0), utc(tuesday, 10, 0)),
vec![booking],
vec![host("rich", Vec::new()), attendee],
vec![scoped],
);
let response = engine().find_availability(&request).unwrap();
assert!(offered(&response).is_empty());
let reasons: Vec<(Option<&str>, ReasonCode)> = response.excluded[0]
.reasons
.iter()
.map(|reason| (reason.participant.as_deref(), reason.code))
.collect();
assert_eq!(
reasons,
[
(Some("tom"), ReasonCode::Rest),
(None, ReasonCode::AttendeeUnavailable),
]
);
assert_eq!(
response.excluded[0].reasons[0].rule.as_deref(),
Some("rest")
);
}
#[test]
fn rest_reads_the_local_date_a_backward_transition_repeats() {
let saturday = (2010, 11, 6);
let sunday = (2010, 11, 7);
let rule = presets::rest(time(22, 0), time(23, 30));
let mut participant = Participant::new("rich", ZoneName::new("America/St_Johns").unwrap());
participant.calendar = Some(vec![block(utc(sunday, 2, 30), utc(sunday, 3, 0))]);
let evening_booking = utc(saturday, 0, 30);
let req = request(
utc((2010, 11, 1), 0, 0),
span(utc(saturday, 0, 0), utc(saturday, 12, 0)),
vec![evening_booking],
vec![participant],
vec![rule.clone()],
);
let response = engine().find_availability(&req).unwrap();
assert!(
offered(&response).is_empty(),
"the 6 November early minutes the transition repeats are rest activity, \
so the 5 November evening booking is excluded"
);
assert_eq!(response.excluded[0].reasons[0].code, ReasonCode::Rest);
let mut participant = Participant::new("rich", ZoneName::new("America/St_Johns").unwrap());
participant.calendar = Some(vec![block(utc(saturday, 0, 30), utc(saturday, 1, 0))]);
let crossing_booking = utc(sunday, 2, 30);
let req = request(
utc((2010, 11, 1), 0, 0),
span(utc(sunday, 2, 0), utc(sunday, 4, 0)),
vec![crossing_booking],
vec![participant],
vec![rule],
);
let response = engine().find_availability(&req).unwrap();
assert!(
offered(&response).is_empty(),
"a booking crossing the backward transition reaches the repeated \
6 November early minutes and is excluded"
);
assert_eq!(response.excluded[0].reasons[0].code, ReasonCode::Rest);
}
#[test]
fn required_ranges_cover_the_date_line_case_rest_adds() {
let saturday = (2027, 3, 6);
let sunday = (2027, 3, 7);
let mut participant = host(
"rich",
vec![block(utc(saturday, 5, 0), utc(saturday, 5, 30))],
);
participant.zone = ZoneName::new("Pacific/Kiritimati").expect("a zone name");
participant.zone_periods.push(
opportune::model::ZonePeriod::new(
ZoneName::new("Etc/GMT+12").expect("a zone name"),
utc(sunday, 18, 0),
utc((2027, 3, 8), 18, 0),
)
.expect("the fixture leg is non-empty"),
);
let booking = utc(sunday, 20, 0); let request = request(
utc(sunday, 0, 0),
span(utc(sunday, 20, 0), utc((2027, 3, 8), 20, 0)),
vec![booking],
vec![participant],
vec![evening_rest()],
);
let mut query = RangesQuery::new(request.now, request.goal.clone());
query.rules.clone_from(&request.rules);
query.participants.clone_from(&request.participants);
let ranges = engine().required_ranges(&query).unwrap();
assert!(
ranges.calendar.start() <= utc(saturday, 5, 0),
"the calendar range {}..{} misses the Kiritimati evening",
ranges.calendar.start(),
ranges.calendar.end()
);
let response = engine().find_availability(&request).unwrap();
assert!(offered(&response).is_empty());
assert_eq!(response.excluded[0].reasons[0].code, ReasonCode::Rest);
}
#[test]
fn rest_protects_the_last_supported_minute() {
let evening = block(utc((2099, 12, 31), 17, 0), utc((2099, 12, 31), 18, 0));
let mut participant = host("rich", vec![evening.clone()]);
participant.zone = ZoneName::new("Europe/Paris").expect("a zone name");
let last = Instant::from_minutes(Instant::MAX.minutes() - 1).expect("a supported instant");
let mut request = request(
utc((2099, 12, 30), 0, 0),
span(utc((2099, 12, 30), 0, 0), Instant::MAX),
vec![last],
vec![participant],
vec![evening_rest()],
);
request.goal.parts[0].duration = Duration::from_minutes(1);
let check = engine()
.check_slot(&request, &proposal(last))
.expect("the proposal fits the request");
assert!(!check.valid);
assert_eq!(
check
.reasons
.iter()
.map(|reason| reason.code)
.collect::<Vec<_>>(),
vec![ReasonCode::Rest],
"the previous evening's late finish protects the last supported minute"
);
}