Skip to content

tests: add settlement panic coverage and fix settlement event assertions#625

Open
GoSTEAN wants to merge 1 commit intoSynapse-bridgez:mainfrom
GoSTEAN:new
Open

tests: add settlement panic coverage and fix settlement event assertions#625
GoSTEAN wants to merge 1 commit intoSynapse-bridgez:mainfrom
GoSTEAN:new

Conversation

@GoSTEAN
Copy link
Copy Markdown

@GoSTEAN GoSTEAN commented Mar 30, 2026

Description:
This PR adds and fixes test coverage in contract_test.rs for settlement and lifecycle guard scenarios.

Closes #602
Closes #604
Closes #605
Closes #606

What changed:

  • Fixed mark_processing_on_non_pending_tx_panics to pass the full register_deposit argument list, including callback_type.
  • Reworked finalize_settlement_emits_settlement_finalized_event to correctly assert event ordering and payloads:
    • Settled(tx_id_1, settlement_id)
    • Settled(tx_id_2, settlement_id)
    • SettlementFinalized(settlement_id, asset_code, total_amount)
  • Added test_finalize_settlement_panics_on_total_mismatch to verify finalize_settlement panics when provided total_amount does not match transaction sum.
  • Added test_finalize_settlement_panics_on_double_settle to verify finalize_settlement panics when a previously settled transaction is included again.

Copilot AI review requested due to automatic review settings March 30, 2026 09:36
@drips-wave
Copy link
Copy Markdown

drips-wave bot commented Mar 30, 2026

@GoSTEAN Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates tests/contract_test.rs to compile against the current register_deposit signature and adds/adjusts settlement-related tests (event assertions and panic-path coverage).

Changes:

  • Added the missing callback_type argument (&None) to some register_deposit calls in tests.
  • Reworked the settlement event assertion test to index into the event stream and validate Settled / SettlementFinalized payloads.
  • Added new panic tests for finalize_settlement total mismatch and double-settle scenarios.
Comments suppressed due to low confidence (1)

tests/contract_test.rs:1256

  • This event-order assertion assumes the last 3 events are Settled, Settled, SettlementFinalized, but finalize_settlement also emits SettlementCreated before SettlementFinalized (see src/lib.rs finalize_settlement). As written, event_count - 3 will likely point at Settled(tx_id_2, ...) (or SettlementCreated), making the test fail. Update the test to account for the SettlementCreated event (e.g., assert 4 events at the end, or search the event stream for the expected sequence).
    let (event_contract_1, event_topics_1, event_data_1) = all_events.get(event_count - 3).unwrap();
    let (event_contract_2, event_topics_2, event_data_2) = all_events.get(event_count - 2).unwrap();
    let (event_contract_3, event_topics_3, event_data_3) = all_events.get(event_count - 1).unwrap();

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 961 to 970
let tx_id = client.register_deposit(
&relayer,
&SorobanString::from_str(&env, "lifecycle-guard-1"),
&Address::generate(&env),
&50_000_000,
&usd(&env),
&None,
&None,
&None,
);
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call site was updated to pass the new register_deposit arguments (memo, memo_type, callback_type). However, other tests in this file still call client.register_deposit with fewer arguments (e.g. mark_processing_panics_when_completed / mark_processing_panics_when_failed above), which will not compile against the current contract signature. Update those remaining call sites (or route them through a helper) so the test suite compiles.

Copilot uses AI. Check for mistakes.
Comment on lines +1280 to +1307
#[test]
#[should_panic(expected = "total_amount mismatch")]
fn test_finalize_settlement_panics_on_total_mismatch() {
let env = Env::default();
let (admin, _, client) = setup(&env);
let relayer = Address::generate(&env);
client.grant_relayer(&admin, &relayer);
client.add_asset(&admin, &usd(&env));

let tx_id = client.register_deposit(
&relayer,
&SorobanString::from_str(&env, "mismatch-total-1"),
&Address::generate(&env),
&100_000_000,
&usd(&env),
&None,
&None,
&None,
);
client.mark_processing(&relayer, &tx_id);
client.mark_completed(&relayer, &tx_id);

client.finalize_settlement(
&relayer,
&usd(&env),
&vec![&env, tx_id],
&999_999_999,
&0u64,
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[should_panic(expected = "total_amount mismatch")] will fail because finalize_settlement currently doesn’t validate total_amount against the sum of the referenced transactions (no such panic is emitted in src/lib.rs). Either add the total-sum validation to the contract implementation, or change/remove this test to match the current behavior.

Copilot uses AI. Check for mistakes.
Comment on lines +1280 to 1353
#[test]
#[should_panic(expected = "total_amount mismatch")]
fn test_finalize_settlement_panics_on_total_mismatch() {
let env = Env::default();
let (admin, _, client) = setup(&env);
let relayer = Address::generate(&env);
client.grant_relayer(&admin, &relayer);
client.add_asset(&admin, &usd(&env));

let tx_id = client.register_deposit(
&relayer,
&SorobanString::from_str(&env, "mismatch-total-1"),
&Address::generate(&env),
&100_000_000,
&usd(&env),
&None,
&None,
&None,
);
client.mark_processing(&relayer, &tx_id);
client.mark_completed(&relayer, &tx_id);

client.finalize_settlement(
&relayer,
&usd(&env),
&vec![&env, tx_id],
&999_999_999,
&0u64,
&1u64,
);
}

#[test]
#[should_panic(expected = "transaction already settled")]
fn test_finalize_settlement_panics_on_double_settle() {
let env = Env::default();
let (admin, _, client) = setup(&env);
let relayer = Address::generate(&env);
client.grant_relayer(&admin, &relayer);
client.add_asset(&admin, &usd(&env));

let tx_id = client.register_deposit(
&relayer,
&SorobanString::from_str(&env, "double-settle-1"),
&Address::generate(&env),
&50_000_000,
&usd(&env),
&None,
&None,
&None,
);
client.mark_processing(&relayer, &tx_id);
client.mark_completed(&relayer, &tx_id);

client.finalize_settlement(
&relayer,
&usd(&env),
&vec![&env, tx_id.clone()],
&50_000_000,
&0u64,
&1u64,
);

client.finalize_settlement(
&relayer,
&usd(&env),
&vec![&env, tx_id],
&50_000_000,
&0u64,
&1u64,
);
}

#[test]
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These newly added settlement panic tests duplicate existing coverage later in this same file (e.g. finalize_settlement_panics_when_total_amount_mismatch and settle_already_settled_tx_panics). Keeping both copies increases maintenance burden and can introduce contradictory expectations; consider removing the duplicates or consolidating into a single set of tests.

Suggested change
#[test]
#[should_panic(expected = "total_amount mismatch")]
fn test_finalize_settlement_panics_on_total_mismatch() {
let env = Env::default();
let (admin, _, client) = setup(&env);
let relayer = Address::generate(&env);
client.grant_relayer(&admin, &relayer);
client.add_asset(&admin, &usd(&env));
let tx_id = client.register_deposit(
&relayer,
&SorobanString::from_str(&env, "mismatch-total-1"),
&Address::generate(&env),
&100_000_000,
&usd(&env),
&None,
&None,
&None,
);
client.mark_processing(&relayer, &tx_id);
client.mark_completed(&relayer, &tx_id);
client.finalize_settlement(
&relayer,
&usd(&env),
&vec![&env, tx_id],
&999_999_999,
&0u64,
&1u64,
);
}
#[test]
#[should_panic(expected = "transaction already settled")]
fn test_finalize_settlement_panics_on_double_settle() {
let env = Env::default();
let (admin, _, client) = setup(&env);
let relayer = Address::generate(&env);
client.grant_relayer(&admin, &relayer);
client.add_asset(&admin, &usd(&env));
let tx_id = client.register_deposit(
&relayer,
&SorobanString::from_str(&env, "double-settle-1"),
&Address::generate(&env),
&50_000_000,
&usd(&env),
&None,
&None,
&None,
);
client.mark_processing(&relayer, &tx_id);
client.mark_completed(&relayer, &tx_id);
client.finalize_settlement(
&relayer,
&usd(&env),
&vec![&env, tx_id.clone()],
&50_000_000,
&0u64,
&1u64,
);
client.finalize_settlement(
&relayer,
&usd(&env),
&vec![&env, tx_id],
&50_000_000,
&0u64,
&1u64,
);
}
#[test]
#[test]

Copilot uses AI. Check for mistakes.
Comment on lines +1280 to +1316
#[test]
#[should_panic(expected = "total_amount mismatch")]
fn test_finalize_settlement_panics_on_total_mismatch() {
let env = Env::default();
let (admin, _, client) = setup(&env);
let relayer = Address::generate(&env);
client.grant_relayer(&admin, &relayer);
client.add_asset(&admin, &usd(&env));

let tx_id = client.register_deposit(
&relayer,
&SorobanString::from_str(&env, "mismatch-total-1"),
&Address::generate(&env),
&100_000_000,
&usd(&env),
&None,
&None,
&None,
);
client.mark_processing(&relayer, &tx_id);
client.mark_completed(&relayer, &tx_id);

client.finalize_settlement(
&relayer,
&usd(&env),
&vec![&env, tx_id],
&999_999_999,
&0u64,
&1u64,
);
}

#[test]
#[should_panic(expected = "transaction already settled")]
fn test_finalize_settlement_panics_on_double_settle() {
let env = Env::default();
let (admin, _, client) = setup(&env);
Copy link

Copilot AI Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New tests here are the only ones in this file using the test_ prefix (most others are named descriptively without it). Rename them to match the existing naming pattern for consistency (e.g., finalize_settlement_panics_on_total_mismatch / finalize_settlement_panics_on_double_settle).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants