Skip to content

Commit 47ebcd1

Browse files
committed
fixup use match to catch expired session error state in replay error
1 parent 1dbde51 commit 47ebcd1

1 file changed

Lines changed: 45 additions & 15 deletions

File tree

payjoin/src/core/send/v2/session.rs

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ mod tests {
199199
events: Vec<SessionEvent>,
200200
expected_session_history: SessionHistoryExpectedOutcome,
201201
expected_sender_state: SendSession,
202+
expected_error: Option<String>,
202203
}
203204

204205
fn run_session_history_test(test: SessionHistoryTest) {
@@ -207,12 +208,36 @@ mod tests {
207208
persister.save_event(event).expect("In memory persister shouldn't fail");
208209
}
209210

210-
let (sender, session_history) =
211-
replay_event_log(&persister).expect("In memory persister shouldn't fail");
212-
assert_eq!(sender, test.expected_sender_state);
213-
assert_eq!(session_history.fallback_tx(), test.expected_session_history.fallback_tx);
214-
assert_eq!(*session_history.pj_param(), test.expected_session_history.pj_param);
215-
assert_eq!(session_history.status(), test.expected_session_history.expected_status);
211+
let session_result = replay_event_log(&persister);
212+
213+
match session_result {
214+
Ok((sender_state, session_history)) => {
215+
assert!(test.expected_error.is_none(), "Expected an error but got Ok");
216+
assert_eq!(sender_state, test.expected_sender_state);
217+
assert_eq!(
218+
session_history.fallback_tx(),
219+
test.expected_session_history.fallback_tx
220+
);
221+
assert_eq!(session_history.pj_param(), &test.expected_session_history.pj_param);
222+
assert_eq!(SessionStatus::Active, test.expected_session_history.expected_status);
223+
}
224+
Err(e) => {
225+
let err_str = e.to_string();
226+
if let Some(expected) = &test.expected_error {
227+
assert!(
228+
err_str.contains(expected),
229+
"Expected error containing '{expected}', got '{err_str}'"
230+
);
231+
} else {
232+
panic!("Unexpected error: {err_str}");
233+
}
234+
assert_eq!(
235+
SendSession::Closed(SessionOutcome::Failure),
236+
test.expected_sender_state
237+
);
238+
assert_eq!(test.expected_session_history.expected_status, SessionStatus::Failed);
239+
}
240+
};
216241
}
217242

218243
#[test]
@@ -231,6 +256,8 @@ mod tests {
231256
.unwrap();
232257
let reply_key = HpkeKeyPair::gen_keypair();
233258
let endpoint = sender.endpoint().clone();
259+
let fallback_tx = sender.psbt_ctx.original_psbt.clone().extract_tx_unchecked_fee_rate();
260+
234261
let id = crate::uri::ShortId::try_from(&b"12345670"[..]).expect("valid short id");
235262
let expiration =
236263
(std::time::SystemTime::now() - std::time::Duration::from_secs(1)).try_into().unwrap();
@@ -248,15 +275,17 @@ mod tests {
248275
psbt_ctx: sender.psbt_ctx.clone(),
249276
reply_key: reply_key.0,
250277
};
251-
let persister = InMemoryTestPersister::<SessionEvent>::default();
252-
persister
253-
.save_event(SessionEvent::Created(Box::new(with_reply_key)))
254-
.expect("save_event should succeed");
255-
256-
let err = replay_event_log(&persister).expect_err("session should be expired");
257-
let expected_err: ReplayError<SendSession, SessionEvent> =
258-
InternalReplayError::Expired(expiration).into();
259-
assert_eq!(err.to_string(), expected_err.to_string());
278+
let test = SessionHistoryTest {
279+
events: vec![SessionEvent::Created(Box::new(with_reply_key))],
280+
expected_session_history: SessionHistoryExpectedOutcome {
281+
fallback_tx,
282+
pj_param,
283+
expected_status: SessionStatus::Failed,
284+
},
285+
expected_sender_state: SendSession::Closed(SessionOutcome::Failure),
286+
expected_error: Some("Session expired at".to_string()),
287+
};
288+
run_session_history_test(test);
260289
}
261290

262291
#[test]
@@ -302,6 +331,7 @@ mod tests {
302331
expected_status: SessionStatus::Active,
303332
},
304333
expected_sender_state: SendSession::WithReplyKey(sender),
334+
expected_error: None,
305335
};
306336
run_session_history_test(test);
307337
}

0 commit comments

Comments
 (0)