forked from Cylo-Traders/Agrocylo-PIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcampaignService.test.ts
More file actions
49 lines (41 loc) · 1.45 KB
/
Copy pathcampaignService.test.ts
File metadata and controls
49 lines (41 loc) · 1.45 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
import {
validateContribution,
calculateOwnershipShare,
fundCampaign,
} from '../lib/soroban/campaignService';
function assertEqual<T>(actual: T, expected: T, message?: string) {
if (actual !== expected) {
throw new Error(message || `Expected ${expected}, got ${actual}`);
}
}
function assertTrue(condition: boolean, message?: string) {
if (!condition) {
throw new Error(message || 'Expected true, got false');
}
}
// 1. Validation Tests
const validRes = validateContribution(500, 1000);
assertEqual(validRes.valid, true);
const zeroRes = validateContribution(0, 1000);
assertEqual(zeroRes.valid, false);
assertEqual(zeroRes.error, 'Contribution amount must be greater than zero');
const negativeRes = validateContribution(-50, 1000);
assertEqual(negativeRes.valid, false);
const exceedsRes = validateContribution(1500, 1000);
assertEqual(exceedsRes.valid, false);
assertTrue(!!exceedsRes.error?.includes('exceeds remaining target'));
// 2. Ownership Share Calculation Tests
assertEqual(calculateOwnershipShare(2500, 10000), 25);
assertEqual(calculateOwnershipShare(5000, 10000), 50);
assertEqual(calculateOwnershipShare(0, 10000), 0);
// 3. Fund Campaign Execution Tests
fundCampaign(
{ campaignId: 'c1', amount: 1000, walletAddress: 'GUSER' },
2000,
10000,
).then((res) => {
assertEqual(res.success, true);
assertTrue(!!res.txHash?.startsWith('0x'));
assertEqual(res.newTotalRaised, 3000);
assertEqual(res.newRemainingTarget, 7000);
});