use crate::reducer::event::PipelinePhase;
use crate::reducer::state::PipelineState;
pub(super) const fn should_exit_before_effect(state: &PipelineState) -> bool {
if !state.is_complete() {
return false;
}
let should_allow_checkpoint_save = matches!(state.phase, PipelinePhase::Interrupted)
&& matches!(state.previous_phase, Some(PipelinePhase::AwaitingDevFix))
&& state.checkpoint_saved_count == 0;
let is_awaiting_dev_fix_not_triggered =
matches!(state.phase, PipelinePhase::AwaitingDevFix) && !state.dev_fix_triggered;
let should_allow_restoration =
state.prompt_permissions.restore_needed && !state.prompt_permissions.restored;
!should_allow_checkpoint_save && !is_awaiting_dev_fix_not_triggered && !should_allow_restoration
}
pub(super) const fn should_exit_after_effect(state: &PipelineState) -> bool {
should_exit_before_effect(state)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_should_exit_before_effect_allows_restoration() {
use crate::reducer::state::PromptPermissionsState;
let state_before = PipelineState {
phase: PipelinePhase::Interrupted,
checkpoint_saved_count: 1,
prompt_permissions: PromptPermissionsState {
locked: true,
restore_needed: true,
restored: false,
last_warning: None,
},
..PipelineState::initial(1, 0)
};
let should_exit = should_exit_before_effect(&state_before);
assert!(
!should_exit,
"should_exit_before_effect must return false when restoration pending"
);
let state_after = PipelineState {
prompt_permissions: PromptPermissionsState {
locked: true,
restore_needed: true,
restored: true,
last_warning: None,
},
..state_before
};
let should_exit_after = should_exit_before_effect(&state_after);
assert!(
should_exit_after,
"should_exit_before_effect should return true after restoration"
);
}
#[test]
fn test_should_exit_before_effect_complete_phase_with_restoration_pending() {
use crate::reducer::state::PromptPermissionsState;
let state = PipelineState {
phase: PipelinePhase::Complete,
prompt_permissions: PromptPermissionsState {
locked: true,
restore_needed: true,
restored: false,
last_warning: None,
},
..PipelineState::initial(0, 0)
};
let should_exit = should_exit_before_effect(&state);
assert!(
!should_exit,
"Even in Complete phase, must allow restoration if pending"
);
}
#[test]
fn test_should_exit_after_effect_delegates_to_before() {
let state = PipelineState::initial(1, 0);
assert_eq!(
should_exit_before_effect(&state),
should_exit_after_effect(&state),
"should_exit_after_effect should delegate to should_exit_before_effect"
);
}
}