forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_features.rs
More file actions
458 lines (342 loc) · 12.5 KB
/
Copy pathtest_features.rs
File metadata and controls
458 lines (342 loc) · 12.5 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
#![cfg(test)]
extern crate std;
use super::*;
use soroban_sdk::{
Address, Env, testutils::{Address as _, Ledger}, token::{Client as TokenClient, StellarAssetClient}, vec
};
// ─── Test helpers ─────────────────────────────────────────────────────────────
struct TestEnv {
env: Env,
contract_id: Address,
token_id: Address,
sender: Address,
recipient: Address,
}
impl TestEnv {
fn setup() -> Self {
let env = Env::default();
env.mock_all_auths();
let contract_id = env.register(StreamingContract, ());
let sender = Address::generate(&env);
let recipient = Address::generate(&env);
let token_admin = Address::generate(&env);
let token_id = env.register_stellar_asset_contract_v2(token_admin.clone()).address();
let asset_client = StellarAssetClient::new(&env, &token_id);
asset_client.mint(&sender, &1_000_000_0000000);
TestEnv { env, contract_id, token_id, sender, recipient }
}
fn client(&self) -> StreamingContractClient {
StreamingContractClient::new(&self.env, &self.contract_id)
}
fn token(&self) -> TokenClient {
TokenClient::new(&self.env, &self.token_id)
}
fn set_time(&self, timestamp: u64) {
self.env.ledger().with_mut(|l| l.timestamp = timestamp);
}
fn default_params(&self, now: u64) -> CreateStreamParams {
CreateStreamParams {
recipient: self.recipient.clone(),
token: self.token_id.clone(),
total_amount: 1_000_0000000,
start_time: now,
end_time: now + 1000,
cliff_time: now,
cliff_amount: 0,
}
}
}
// ─── #70: Structured events with enriched payloads ─────────────────────────────
#[test]
fn test_stream_created_event_includes_all_fields() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
let params = t.default_params(now);
let total = params.total_amount;
t.token().approve(&t.sender, &t.contract_id, &total, &(t.env.ledger().sequence() + 500));
// Create stream and verify event is emitted with enriched fields
let stream_id = client.create_stream(&t.sender, ¶ms);
let stream = client.get_stream(&stream_id);
assert_eq!(stream.sender, t.sender);
assert_eq!(stream.recipient, params.recipient);
assert_eq!(stream.token, params.token);
assert_eq!(stream.start_time, now);
assert_eq!(stream.end_time, now + 1000);
assert_eq!(stream.cliff_time, now);
}
#[test]
fn test_withdraw_event_includes_remaining_withdrawable() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
let params = t.default_params(now);
let total = params.total_amount;
t.token().approve(&t.sender, &t.contract_id, &total, &(t.env.ledger().sequence() + 500));
let stream_id = client.create_stream(&t.sender, ¶ms);
t.set_time(now + 500);
let withdrawable = client.get_withdrawable(&stream_id);
client.withdraw(&stream_id, &withdrawable);
let stream = client.get_stream(&stream_id);
assert_eq!(stream.withdrawn_amount, withdrawable);
let remaining = client.get_withdrawable(&stream_id);
assert!(remaining > 0);
}
#[test]
fn test_cancel_event_includes_sender_recipient_timestamp() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
let params = t.default_params(now);
let total = params.total_amount;
t.token().approve(&t.sender, &t.contract_id, &total, &(t.env.ledger().sequence() + 500));
let stream_id = client.create_stream(&t.sender, ¶ms);
t.set_time(now + 500);
client.cancel(&stream_id);
let stream = client.get_stream(&stream_id);
assert!(stream.cancelled);
}
#[test]
fn test_bump_stream_event_emitted() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
let params = t.default_params(now);
let total = params.total_amount;
t.token().approve(&t.sender, &t.contract_id, &total, &(t.env.ledger().sequence() + 500));
let stream_id = client.create_stream(&t.sender, ¶ms);
// Bump TTL should succeed without panic
client.bump_stream(&stream_id);
let stream = client.get_stream(&stream_id);
assert_eq!(stream.id, stream_id);
}
// ─── #71: Pause/unpause mechanism ─────────────────────────────────────────────
#[test]
fn test_pause_blocks_create_stream() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
let params = t.default_params(now);
let total = params.total_amount;
// Initialize with admin
client.initialize(&t.sender);
// Pause contract
client.pause();
t.token().approve(&t.sender, &t.contract_id, &total, &(t.env.ledger().sequence() + 500));
// Attempt to create stream should panic
client.create_stream(&t.sender, ¶ms);
}
#[test]
fn test_pause_blocks_withdraw() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
let params = t.default_params(now);
let total = params.total_amount;
client.initialize(&t.sender);
t.token().approve(&t.sender, &t.contract_id, &total, &(t.env.ledger().sequence() + 500));
let stream_id = client.create_stream(&t.sender, ¶ms);
t.set_time(now + 500);
client.pause();
let withdrawable = client.get_withdrawable(&stream_id);
client.withdraw(&stream_id, &withdrawable);
}
#[test]
fn test_pause_blocks_cancel() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
let params = t.default_params(now);
let total = params.total_amount;
client.initialize(&t.sender);
t.token().approve(&t.sender, &t.contract_id, &total, &(t.env.ledger().sequence() + 500));
let stream_id = client.create_stream(&t.sender, ¶ms);
client.pause();
client.cancel(&stream_id);
}
#[test]
fn test_read_operations_work_while_paused() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
let params = t.default_params(now);
let total = params.total_amount;
client.initialize(&t.sender);
t.token().approve(&t.sender, &t.contract_id, &total, &(t.env.ledger().sequence() + 500));
let stream_id = client.create_stream(&t.sender, ¶ms);
client.pause();
// Read operations should still work
let stream = client.get_stream(&stream_id);
assert_eq!(stream.id, stream_id);
let withdrawable = client.get_withdrawable(&stream_id);
assert!(withdrawable >= 0);
}
#[test]
fn test_unpause_allows_operations() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
let params = t.default_params(now);
let total = params.total_amount;
client.initialize(&t.sender);
t.token().approve(&t.sender, &t.contract_id, &total, &(t.env.ledger().sequence() + 500));
let stream_id = client.create_stream(&t.sender, ¶ms);
client.pause();
client.unpause();
// Operations should work again
t.set_time(now + 500);
let withdrawable = client.get_withdrawable(&stream_id);
assert!(withdrawable > 0);
}
#[test]
fn test_only_admin_can_pause() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
client.initialize(&t.sender);
let other = Address::generate(&t.env);
// Non-admin should not be able to pause
// This test assumes mock_all_auths is set; otherwise auth will fail
client.pause();
}
#[test]
fn test_only_admin_can_unpause() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
client.initialize(&t.sender);
client.pause();
// Only admin can unpause
client.unpause();
let stream = client.get_stream(&1u64);
}
#[test]
fn test_pause_events_emitted() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
client.initialize(&t.sender);
// Emit pause event
client.pause();
client.unpause();
// Emit unpause event - verified by not panicking
}
// ─── #72: Optimize index storage from Vec to Map ────────────────────────────────
#[test]
fn test_index_operations_remain_functional_after_optimization() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
// Create 3 streams
for i in 0..3 {
let mut params = t.default_params(now);
let total = params.total_amount;
t.token().approve(&t.sender, &t.contract_id, &total, &(t.env.ledger().sequence() + 500 + i as u32));
client.create_stream(&t.sender, ¶ms);
}
// Verify sent streams are indexed correctly
let sent = client.get_sent_streams(&t.sender, &0, &10);
assert_eq!(sent.len(), 3);
}
#[test]
fn test_remove_from_index_o1_operation() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
// Create 5 streams
for i in 0..5 {
let mut params = t.default_params(now);
let total = params.total_amount;
t.token().approve(&t.sender, &t.contract_id, &total, &(t.env.ledger().sequence() + 500 + i as u32));
client.create_stream(&t.sender, ¶ms);
}
// Transfer stream 3 (remove from recipient index)
let new_recipient = Address::generate(&t.env);
client.transfer_stream(&3u64, &new_recipient);
// Verify recipient index is correct
let received = client.get_received_streams(&t.recipient, &0, &10);
assert_eq!(received.len(), 4);
}
#[test]
fn test_pagination_works_with_optimized_index() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
// Create 20 streams
for i in 0..20 {
let mut params = t.default_params(now);
let total = params.total_amount;
t.token().approve(&t.sender, &t.contract_id, &total, &(t.env.ledger().sequence() + 500 + i as u32));
client.create_stream(&t.sender, ¶ms);
}
// Paginate through sent streams
let page1 = client.get_sent_streams(&t.sender, &0, &10);
let page2 = client.get_sent_streams(&t.sender, &10, &10);
assert_eq!(page1.len(), 10);
assert_eq!(page2.len(), 10);
}
#[test]
fn test_pagination_clamps_when_offset_plus_limit_overflows() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
for i in 0..3 {
let mut params = t.default_params(now);
let total = params.total_amount;
t.token().approve(
&t.sender,
&t.contract_id,
&total,
&(t.env.ledger().sequence() + 500 + i as u32),
);
client.create_stream(&t.sender, ¶ms);
}
let page = client.get_sent_streams(&t.sender, &u32::MAX, &u32::MAX);
assert!(page.is_empty());
}
#[test]
fn test_get_sent_stream_count_accurate() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
for i in 0..7 {
let mut params = t.default_params(now);
let total = params.total_amount;
t.token().approve(&t.sender, &t.contract_id, &total, &(t.env.ledger().sequence() + 500 + i as u32));
client.create_stream(&t.sender, ¶ms);
}
let count = client.get_sent_stream_count(&t.sender);
assert_eq!(count, 7);
}
#[test]
fn test_get_received_stream_count_accurate() {
let t = TestEnv::setup();
let now = 1_000_000u64;
t.set_time(now);
let client = t.client();
for i in 0..5 {
let mut params = t.default_params(now);
let total = params.total_amount;
t.token().approve(&t.sender, &t.contract_id, &total, &(t.env.ledger().sequence() + 500 + i as u32));
client.create_stream(&t.sender, ¶ms);
}
let count = client.get_received_stream_count(&t.recipient);
assert_eq!(count, 5);
}