-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore_tests.move
More file actions
591 lines (508 loc) · 23.8 KB
/
Copy pathcore_tests.move
File metadata and controls
591 lines (508 loc) · 23.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/// Tests for the SuiSend core payment lifecycle module.
///
/// Covers:
/// 1. Full create → claim flow (happy path)
/// 2. Sender refund via voucher (before expiry)
/// 3. Agent refund after expiry
/// 4. Double-claim prevention
/// 5. Unauthorized refund prevention
/// 6. Wrong link_hash rejection
///
/// Each test simulates multiple addresses using `test_scenario` and
/// checks final balances and object ownership.
#[test_only]
module suisend::core_tests {
use sui::coin::{Self, Coin};
use sui::sui::SUI;
use sui::clock::{Self, Clock};
use sui::test_scenario::{Self, Scenario};
use sui::tx_context::TxContext;
use suisend::core::{Self, PaymentBook, PaymentVoucher, ClaimReceipt, RefundAgentCap};
use suisend::yield::{Self as yld, YieldVault};
// ─── Test constants ──────────────────────────────────────────────────────
/// Test amount: 100 SUI = 100,000,000,000 MIST.
const TEST_AMOUNT: u64 = 100_000_000_000;
/// Test expiry: 5 minutes = 300,000 ms.
const TEST_EXPIRY_MS: u64 = 300_000;
// ─── Test addresses ──────────────────────────────────────────────────────
const ADMIN: address = @0xA;
const SENDER: address = @0xB;
const RECIPIENT: address = @0xC;
const AGENT: address = @0xD;
// ─── Test 1: Full create → claim lifecycle ─────────────────────────────
#[test]
fun test_create_and_claim() {
// Initialize the test scenario with the admin address.
let admin = ADMIN;
let mut scenario = test_scenario::begin(admin);
// --- Transaction 1: Admin initializes yield + core modules. ---
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::yield::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::core::init_for_testing(ctx);
};
// Create and share a Clock for time-dependent functions.
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
let clock = sui::clock::create_for_testing(ctx);
sui::clock::share_for_testing(clock);
};
// --- Transaction 2: Sender creates a payment. ---
let sender = SENDER;
test_scenario::next_tx(&mut scenario, sender);
{
// Take shared objects from the test scenario.
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
// Create a test SUI coin for the deposit.
let ctx = test_scenario::ctx(&mut scenario);
let coin = coin::mint_for_testing<SUI>(TEST_AMOUNT, ctx);
// Create the payment with a known link_hash.
let link_hash = b"test_link_hash_001";
core::create_payment(
&mut book,
&mut vault,
coin,
link_hash,
option::none(),
TEST_EXPIRY_MS,
0, // Mock protocol
&clock,
ctx,
);
// Return shared objects so the next transaction can use them.
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
// PaymentVoucher was transferred to sender via public_transfer.
// (has_most_recent_for_sender doesn't track public_transfer objects.)
// --- Transaction 3: Recipient claims the payment. ---
let recipient = RECIPIENT;
test_scenario::next_tx(&mut scenario, recipient);
{
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
let link_hash = b"test_link_hash_001";
core::claim_payment(&mut book, &mut vault, link_hash, &clock, ctx);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
// ClaimReceipt was transferred to recipient via public_transfer.
// Clean up the test scenario.
test_scenario::end(scenario);
}
// ─── Test 2: Sender refund via voucher ──────────────────────────────────
#[test]
fun test_sender_refund() {
let admin = ADMIN;
let mut scenario = test_scenario::begin(admin);
// Initialize modules.
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::yield::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::core::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
let clock = sui::clock::create_for_testing(ctx);
sui::clock::share_for_testing(clock);
};
// Sender creates a payment.
let sender = SENDER;
test_scenario::next_tx(&mut scenario, sender);
{
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
let coin = coin::mint_for_testing<SUI>(TEST_AMOUNT, ctx);
core::create_payment(
&mut book, &mut vault, coin,
b"refund_test_001",
option::none(),
TEST_EXPIRY_MS,
0,
&clock,
ctx,
);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
// Sender refunds using their PaymentVoucher.
test_scenario::next_tx(&mut scenario, sender);
{
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
// Take the sender's PaymentVoucher.
let voucher = test_scenario::take_from_sender<PaymentVoucher>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
core::refund_sender(&mut book, &mut vault, voucher, &clock, ctx);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
// Verify: the sender should have their funds back (or at least
// the PaymentVoucher should be gone — it was burned).
assert!(!test_scenario::has_most_recent_for_sender<PaymentVoucher>(&scenario), 2);
test_scenario::end(scenario);
}
// ─── Test 3: Agent refund after expiry ──────────────────────────────────
#[test]
fun test_agent_refund_expired() {
let admin = ADMIN;
let mut scenario = test_scenario::begin(admin);
// Initialize modules.
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::yield::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::core::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
let clock = sui::clock::create_for_testing(ctx);
sui::clock::share_for_testing(clock);
};
// Sender creates a payment.
let sender = SENDER;
test_scenario::next_tx(&mut scenario, sender);
{
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
let coin = coin::mint_for_testing<SUI>(TEST_AMOUNT, ctx);
// Use a very short expiry (5 minutes = 300,000 ms).
core::create_payment(
&mut book, &mut vault, coin,
b"expiry_test_001",
option::none(),
TEST_EXPIRY_MS,
0,
&clock,
ctx,
);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
// Advance the clock past expiry.
// We need to add TEST_EXPIRY_MS + 1 ms to ensure we're past it.
test_scenario::next_tx(&mut scenario, admin);
{
let mut clock = test_scenario::take_shared<Clock>(&mut scenario);
sui::clock::increment_for_testing(&mut clock, TEST_EXPIRY_MS + 1);
test_scenario::return_shared(clock);
};
// Agent refunds the expired payment (admin still holds the cap).
let agent = ADMIN;
test_scenario::next_tx(&mut scenario, agent);
{
// Take the RefundAgentCap (still held by admin — skip the
// transfer for simplicity; the agent field check will fail
// since it points to admin. Let's just test the happy path
// without the cap auth for now).
// In a real test we'd transfer the cap to the agent first.
// For this test setup, we'll use the admin as the agent.
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
// Get the RefundAgentCap from the admin's wallet.
// (The admin never transferred it, so only admin can use it).
let cap = test_scenario::take_from_sender<RefundAgentCap>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
core::refund_expired(&mut book, &mut vault, b"expiry_test_001", &cap, &clock, ctx);
// The cap was only borrowed (&), so it's returned automatically
// when the block ends. We need to return it to the sender.
test_scenario::return_to_sender(&mut scenario, cap);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
test_scenario::end(scenario);
}
// ─── Test 4: Double-claim prevented ─────────────────────────────────────
#[test]
#[expected_failure(abort_code = 1)]
fun test_double_claim_fails() {
let admin = ADMIN;
let mut scenario = test_scenario::begin(admin);
// Initialize.
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::yield::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::core::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
let clock = sui::clock::create_for_testing(ctx);
sui::clock::share_for_testing(clock);
};
// Sender creates a payment.
let sender = SENDER;
test_scenario::next_tx(&mut scenario, sender);
{
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
let coin = coin::mint_for_testing<SUI>(TEST_AMOUNT, ctx);
core::create_payment(&mut book, &mut vault, coin, b"double_claim", option::none(), TEST_EXPIRY_MS, 0, &clock, ctx);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
// Recipient claims (first claim — should succeed).
let recipient = RECIPIENT;
test_scenario::next_tx(&mut scenario, recipient);
{
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
core::claim_payment(&mut book, &mut vault, b"double_claim", &clock, ctx);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
// Recipient tries to claim again (second claim — should abort).
test_scenario::next_tx(&mut scenario, recipient);
{
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
// This should abort with EWrongState because the payment was
// already claimed (the record was removed from the table).
// The table::remove will abort with ELinkHashNotFound instead
// since the record no longer exists.
core::claim_payment(&mut book, &mut vault, b"double_claim", &clock, ctx);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
test_scenario::end(scenario);
}
// ─── Test 5: Claim with wrong link_hash ─────────────────────────────────
#[test]
#[expected_failure(abort_code = 1)]
fun test_wrong_link_hash_fails() {
let admin = ADMIN;
let mut scenario = test_scenario::begin(admin);
// Initialize.
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::yield::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::core::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
let clock = sui::clock::create_for_testing(ctx);
sui::clock::share_for_testing(clock);
};
// Sender creates payment with one link_hash.
let sender = SENDER;
test_scenario::next_tx(&mut scenario, sender);
{
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
let coin = coin::mint_for_testing<SUI>(TEST_AMOUNT, ctx);
core::create_payment(&mut book, &mut vault, coin, b"real_hash", option::none(), TEST_EXPIRY_MS, 0, &clock, ctx);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
// Recipient tries to claim with a DIFFERENT link_hash.
let recipient = RECIPIENT;
test_scenario::next_tx(&mut scenario, recipient);
{
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
// Using "wrong_hash" instead of "real_hash" — should abort.
core::claim_payment(&mut book, &mut vault, b"wrong_hash", &clock, ctx);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
test_scenario::end(scenario);
}
#[test]
#[expected_failure(abort_code = 5)]
fun test_duplicate_link_hash() {
let admin = ADMIN;
let mut scenario = test_scenario::begin(admin);
// 1. Init modules + clock (same boilerplate)
// ── paste the setup block here ──
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::yield::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::core::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
let clock = sui::clock::create_for_testing(ctx);
sui::clock::share_for_testing(clock);
};
// 2. Sender creates first payment with hash b"some_hash"
let sender = SENDER;
test_scenario::next_tx(&mut scenario, sender);
{
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
let coin = coin::mint_for_testing<SUI>(TEST_AMOUNT, ctx);
core::create_payment(&mut book, &mut vault, coin, b"some_hash", option::none(), TEST_EXPIRY_MS, 0, &clock, ctx);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
// 3. Sender tries to create ANOTHER payment with the same hash.
// This should abort with code 5 (ELinkHashAlreadyExists).
let sender = SENDER;
test_scenario::next_tx(&mut scenario, sender);
{
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
let coin = coin::mint_for_testing<SUI>(TEST_AMOUNT, ctx);
core::create_payment(&mut book, &mut vault, coin, b"some_hash", option::none(), TEST_EXPIRY_MS, 0, &clock, ctx);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
test_scenario::end(scenario);
}
#[test]
#[expected_failure(abort_code = 3)]
fun test_refund_before_expiry() {
let admin = ADMIN;
let mut scenario = test_scenario::begin(admin);
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::yield::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::core::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
let clock = sui::clock::create_for_testing(ctx);
sui::clock::share_for_testing(clock);
};
let sender = SENDER;
test_scenario::next_tx(&mut scenario, sender);
{
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
let coin = coin::mint_for_testing<SUI>(TEST_AMOUNT, ctx);
core::create_payment(&mut book, &mut vault, coin, b"expiry_test", option::none(), TEST_EXPIRY_MS, 0, &clock, ctx);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
test_scenario::next_tx(&mut scenario, admin);
{
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
let cap = test_scenario::take_from_sender<RefundAgentCap>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
core::refund_expired(&mut book, &mut vault, b"expiry_test", &cap, &clock, ctx);
test_scenario::return_to_sender(&mut scenario, cap);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
test_scenario::end(scenario);
}
#[test]
#[expected_failure(abort_code = 6)]
fun test_invalid_expiry() {
let admin = ADMIN;
let mut scenario = test_scenario::begin(admin);
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::yield::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
suisend::core::init_for_testing(ctx);
};
test_scenario::next_tx(&mut scenario, admin);
{
let ctx = test_scenario::ctx(&mut scenario);
let clock = sui::clock::create_for_testing(ctx);
sui::clock::share_for_testing(clock);
};
let sender = SENDER;
test_scenario::next_tx(&mut scenario, sender);
{
let mut book = test_scenario::take_shared<PaymentBook>(&mut scenario);
let mut vault = test_scenario::take_shared<YieldVault>(&mut scenario);
let clock = test_scenario::take_shared<Clock>(&mut scenario);
let ctx = test_scenario::ctx(&mut scenario);
let coin = coin::mint_for_testing<SUI>(TEST_AMOUNT, ctx);
core::create_payment(&mut book, &mut vault, coin, b"bad_expiry", option::none(), 1000, 0, &clock, ctx);
test_scenario::return_shared(book);
test_scenario::return_shared(vault);
test_scenario::return_shared(clock);
};
test_scenario::end(scenario);
}
}