use crate::channels::discord::DiscordState;
#[tokio::test]
async fn dropping_an_unanswered_approval_empties_the_registration() {
let state = DiscordState::new();
let (tx, rx) = tokio::sync::oneshot::channel::<(bool, bool)>();
state
.register_pending_approval("approval-timeout".to_string(), tx)
.await;
assert!(
state.drop_pending_approval("approval-timeout").await,
"the unanswered registration must still be there to drop"
);
assert!(
!state
.resolve_pending_approval("approval-timeout", true, false)
.await,
"a dropped approval must not be resolvable afterwards"
);
assert!(
rx.await.is_err(),
"dropping must close the channel: nobody is left waiting on it"
);
}
#[tokio::test]
async fn dropping_a_resolved_approval_is_a_no_op() {
let state = DiscordState::new();
let (tx, mut rx) = tokio::sync::oneshot::channel::<(bool, bool)>();
state
.register_pending_approval("approval-click".to_string(), tx)
.await;
assert!(
state
.resolve_pending_approval("approval-click", true, true)
.await
);
assert_eq!(rx.try_recv(), Ok((true, true)), "the click is delivered");
assert!(
!state.drop_pending_approval("approval-click").await,
"the entry was already consumed by the click"
);
}
#[test]
fn the_wait_drops_the_registration_before_branching() {
const SRC: &str = include_str!("../channels/discord/handler.rs");
let wait = SRC
.find("tokio::time::timeout(std::time::Duration::from_secs(300), rx).await")
.expect("the approval wait is gone from the handler");
let drop_at = SRC[wait..]
.find("state.drop_pending_approval(&approval_id).await")
.map(|i| wait + i)
.expect("the registration is never dropped, so timed-out approvals leak");
let branch = SRC[wait..]
.find("match outcome {")
.map(|i| wait + i)
.expect("the outcome match is gone from the handler");
assert!(
wait < drop_at && drop_at < branch,
"the drop must sit between the wait and the branch, so every exit is covered"
);
let arms = &SRC[branch..];
let arms = &arms[..arms.find("\n })\n })\n}").unwrap_or(arms.len())];
assert!(
!arms.contains("drop_pending_approval"),
"cleanup belongs before the branch, not inside each arm"
);
}