use crate::config::profile::{home_for_profile, with_profile_home_async};
use crate::utils::plan_files::{
PRE_INIT_STALE_THRESHOLD, PlanModeState, plan_mode_state, pre_init_marker_path,
set_pre_init_editing,
};
use std::time::SystemTime;
use uuid::Uuid;
async fn in_temp_home<F, T>(f: F) -> T
where
F: std::future::Future<Output = T>,
{
let profile = format!("plan-stale-test-{}", Uuid::new_v4());
let out = with_profile_home_async(Some(&profile), f).await;
let home = home_for_profile(Some(&profile));
let _ = std::fs::remove_dir_all(&home);
out
}
#[tokio::test]
async fn fresh_marker_blocks_plan_creation() {
in_temp_home(async {
let session_id = Uuid::new_v4();
set_pre_init_editing(session_id).await.unwrap();
let state = plan_mode_state(session_id).await;
assert_eq!(
state,
PlanModeState::PreInitEditing,
"Fresh marker should block plan creation (PreInitEditing state)"
);
})
.await;
}
#[tokio::test]
async fn stale_marker_allows_plan_creation() {
in_temp_home(async {
let session_id = Uuid::new_v4();
set_pre_init_editing(session_id).await.unwrap();
let marker_path = pre_init_marker_path(session_id).await;
assert!(marker_path.exists(), "Marker should exist after creation");
let six_minutes_ago =
SystemTime::now() - (PRE_INIT_STALE_THRESHOLD + std::time::Duration::from_secs(60));
let file = std::fs::File::open(&marker_path).unwrap();
file.set_modified(six_minutes_ago).unwrap();
let state = plan_mode_state(session_id).await;
assert_eq!(
state,
PlanModeState::NoPlan,
"Stale marker (>5 min old) should be cleared, returning to NoPlan"
);
assert!(!marker_path.exists(), "Stale marker file should be deleted");
})
.await;
}
#[tokio::test]
async fn marker_at_threshold_is_fresh() {
in_temp_home(async {
let session_id = Uuid::new_v4();
set_pre_init_editing(session_id).await.unwrap();
let marker_path = pre_init_marker_path(session_id).await;
let just_under_threshold =
SystemTime::now() - (PRE_INIT_STALE_THRESHOLD - std::time::Duration::from_secs(1));
let file = std::fs::File::open(&marker_path).unwrap();
file.set_modified(just_under_threshold).unwrap();
let state = plan_mode_state(session_id).await;
assert_eq!(
state,
PlanModeState::PreInitEditing,
"Marker just under threshold (299s) should still be fresh"
);
})
.await;
}