use super::*;
#[test]
fn new_is_all_empty() {
let goals = GoalSlots::new();
for slot in 1..=GOAL_SLOT_COUNT {
assert!(goals.get(slot).is_none(), "slot {slot} must start empty");
}
assert!(goals.render().is_none());
}
#[test]
fn set_then_get_round_trips_all_fields() {
let mut goals = GoalSlots::new();
let before = Utc::now();
goals
.set(2, "ship the release", GoalSource::Model)
.expect("valid slot");
let slot = goals.get(2).expect("slot 2 occupied");
assert_eq!(slot.text, "ship the release");
assert_eq!(slot.source, GoalSource::Model);
assert!(slot.updated_at >= before);
}
#[test]
fn set_records_model_source() {
let mut goals = GoalSlots::new();
goals.set(1, "model goal", GoalSource::Model).expect("ok");
goals
.set(2, "operator goal", GoalSource::Operator)
.expect("ok");
assert_eq!(goals.get(1).expect("slot 1").source, GoalSource::Model);
assert_eq!(goals.get(2).expect("slot 2").source, GoalSource::Operator);
}
#[test]
fn set_overwrites_existing_slot() {
let mut goals = GoalSlots::new();
goals.set(1, "first", GoalSource::Model).expect("ok");
goals.set(1, "second", GoalSource::Model).expect("ok");
assert_eq!(goals.get(1).expect("slot 1").text, "second");
}
#[test]
fn set_out_of_range_slot_errors() {
let mut goals = GoalSlots::new();
assert_eq!(
goals.set(0, "x", GoalSource::Model),
Err(GoalSlotError::OutOfRange { slot: 0 })
);
assert_eq!(
goals.set(GOAL_SLOT_COUNT + 1, "x", GoalSource::Model),
Err(GoalSlotError::OutOfRange {
slot: GOAL_SLOT_COUNT + 1
})
);
}
#[test]
fn clear_empties_slot() {
let mut goals = GoalSlots::new();
goals.set(3, "temp goal", GoalSource::Model).expect("ok");
goals.clear(3).expect("valid slot");
assert!(goals.get(3).is_none());
}
#[test]
fn clear_already_empty_slot_is_ok() {
let mut goals = GoalSlots::new();
assert!(goals.clear(4).is_ok());
assert!(goals.get(4).is_none());
}
#[test]
fn clear_out_of_range_slot_errors() {
let mut goals = GoalSlots::new();
assert_eq!(goals.clear(0), Err(GoalSlotError::OutOfRange { slot: 0 }));
assert_eq!(
goals.clear(GOAL_SLOT_COUNT + 1),
Err(GoalSlotError::OutOfRange {
slot: GOAL_SLOT_COUNT + 1
})
);
}
#[test]
fn render_none_when_all_empty() {
assert!(GoalSlots::new().render().is_none());
}
#[test]
fn render_numbers_by_slot_skipping_empty() {
let mut goals = GoalSlots::new();
goals.set(1, "goal one", GoalSource::Model).expect("ok");
goals.set(3, "goal three", GoalSource::Model).expect("ok");
let rendered = goals.render().expect("non-empty render");
assert!(rendered.contains("1. goal one"));
assert!(rendered.contains("3. goal three"));
assert!(!rendered.contains("2."));
}
#[test]
fn occupied_skips_empty_slots_and_preserves_index() {
let mut goals = GoalSlots::new();
goals.set(1, "goal one", GoalSource::Model).expect("ok");
goals
.set(3, "goal three", GoalSource::Operator)
.expect("ok");
let occupied = goals.occupied();
assert_eq!(occupied.len(), 2);
assert_eq!(occupied[0].0, 1);
assert_eq!(occupied[0].1.text, "goal one");
assert_eq!(occupied[1].0, 3);
assert_eq!(occupied[1].1.source, GoalSource::Operator);
}
#[test]
fn occupied_empty_when_all_slots_empty() {
assert!(GoalSlots::new().occupied().is_empty());
}
#[test]
fn goal_source_serialises_snake_case() {
assert_eq!(
serde_json::to_value(GoalSource::Model).unwrap(),
serde_json::json!("model")
);
assert_eq!(
serde_json::to_value(GoalSource::Operator).unwrap(),
serde_json::json!("operator")
);
}
#[test]
fn render_includes_preamble() {
let mut goals = GoalSlots::new();
goals.set(1, "goal one", GoalSource::Model).expect("ok");
let rendered = goals.render().expect("non-empty render");
assert!(rendered.starts_with("## Active Goals"));
assert!(rendered.contains("not a todo list"));
}