Skip to content

Commit 6d7d6a3

Browse files
committed
test(creator-earnings): add snapshot/restore consistency tests
Add three consistency tests: - test_snapshot_restore_consistency: deposit, withdraw, re-deposit restores the internal balance to the recorded mid-state snapshot - test_sequential_deposits_accumulate_consistently: repeated deposits sum correctly and a full withdrawal returns balance to zero - test_multi_creator_balances_independent: operations on one creator do not affect another creator's balance snapshot Closes #944
1 parent 5c21688 commit 6d7d6a3

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

  • contract/contracts/creator-earnings/src

contract/contracts/creator-earnings/src/test.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,85 @@ fn withdraw_failed_emits_no_event() {
240240
assert_eq!(client.balance(&creator), 500);
241241
assert!(env.events().all().len() >= events_before);
242242
}
243+
244+
// -------- Snapshot / restore consistency tests for issue #944 --------
245+
246+
/// Verify that depositing, withdrawing, then re-depositing the same amount
247+
/// restores the internal balance to the intermediate snapshot.
248+
#[test]
249+
fn test_snapshot_restore_consistency() {
250+
let env = Env::default();
251+
let (_admin, creator, depositor, client, _, token_admin_client) = setup(&env);
252+
253+
// Initial snapshot: balance is zero
254+
assert_eq!(client.balance(&creator), 0);
255+
256+
// Deposit to a known state
257+
client.deposit(&depositor, &creator, &400);
258+
let mid_snapshot = client.balance(&creator);
259+
assert_eq!(mid_snapshot, 400);
260+
261+
// Partial withdrawal moves balance below snapshot
262+
client.withdraw(&creator, &150);
263+
assert_eq!(client.balance(&creator), 250);
264+
265+
// Re-mint so depositor can fund the restore deposit
266+
token_admin_client.mint(&depositor, &150);
267+
268+
// Re-deposit the withdrawn amount to restore to mid-snapshot
269+
client.deposit(&depositor, &creator, &150);
270+
assert_eq!(client.balance(&creator), mid_snapshot);
271+
}
272+
273+
/// Verify that sequential deposits accumulate correctly and a full withdrawal
274+
/// returns the balance to zero.
275+
#[test]
276+
fn test_sequential_deposits_accumulate_consistently() {
277+
let env = Env::default();
278+
let (_admin, creator, depositor, client, _, token_admin_client) = setup(&env);
279+
280+
client.deposit(&depositor, &creator, &100);
281+
assert_eq!(client.balance(&creator), 100);
282+
283+
token_admin_client.mint(&depositor, &200);
284+
client.deposit(&depositor, &creator, &200);
285+
assert_eq!(client.balance(&creator), 300);
286+
287+
token_admin_client.mint(&depositor, &300);
288+
client.deposit(&depositor, &creator, &300);
289+
assert_eq!(client.balance(&creator), 600);
290+
291+
// Full withdrawal restores balance to zero
292+
client.withdraw(&creator, &600);
293+
assert_eq!(client.balance(&creator), 0);
294+
}
295+
296+
/// Verify that balances across multiple creators remain independent; a deposit
297+
/// or withdrawal for one creator does not affect the snapshot of another.
298+
#[test]
299+
fn test_multi_creator_balances_independent() {
300+
let env = Env::default();
301+
let (_admin, _creator, depositor, client, _, _) = setup(&env);
302+
303+
let creator_a = Address::generate(&env);
304+
let creator_b = Address::generate(&env);
305+
306+
// Snapshot: both creators start at 0
307+
assert_eq!(client.balance(&creator_a), 0);
308+
assert_eq!(client.balance(&creator_b), 0);
309+
310+
// Deposit to creator_a only (depositor has 1_000 from setup)
311+
client.deposit(&depositor, &creator_a, &300);
312+
assert_eq!(client.balance(&creator_a), 300);
313+
assert_eq!(client.balance(&creator_b), 0);
314+
315+
// Deposit to creator_b
316+
client.deposit(&depositor, &creator_b, &200);
317+
assert_eq!(client.balance(&creator_a), 300);
318+
assert_eq!(client.balance(&creator_b), 200);
319+
320+
// Withdrawal from creator_a must not affect creator_b
321+
client.withdraw(&creator_a, &100);
322+
assert_eq!(client.balance(&creator_a), 200);
323+
assert_eq!(client.balance(&creator_b), 200);
324+
}

0 commit comments

Comments
 (0)