-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.rs
More file actions
248 lines (200 loc) · 8.2 KB
/
Copy pathtests.rs
File metadata and controls
248 lines (200 loc) · 8.2 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
//! Unit tests for Identity Protocol
//!
//! These tests run against the ACTUAL program code.
#[cfg(test)]
mod tests {
use crate::constants::*;
use crate::state::{IdentityConfig, Identity, VerificationLevel, SubscriptionTier};
use anchor_lang::prelude::Pubkey;
// ========================================================================
// Constants Tests
// ========================================================================
#[test]
fn test_pda_seeds() {
assert_eq!(IDENTITY_CONFIG_SEED, b"identity-config");
assert_eq!(IDENTITY_SEED, b"identity");
assert_eq!(SAS_ATTESTATION_SEED, b"sas-attestation");
assert_eq!(SUBSCRIPTION_SEED, b"subscription");
}
#[test]
fn test_subscription_prices() {
assert_eq!(SUBSCRIPTION_FREE, 0, "Free tier = $0");
assert_eq!(SUBSCRIPTION_VERIFIED, 4_000_000, "Verified = $4 USDC");
assert_eq!(SUBSCRIPTION_PREMIUM, 12_000_000, "Premium = $12 USDC");
assert_eq!(SUBSCRIPTION_ENTERPRISE, 59_000_000, "Enterprise = $59 USDC");
}
#[test]
fn test_subscription_duration() {
assert_eq!(SUBSCRIPTION_DURATION, 30 * 24 * 60 * 60, "30 days");
}
// ========================================================================
// State Size Tests
// ========================================================================
#[test]
fn test_identity_config_size() {
let expected = 8 + 32 + 32 + 32 + 32 + (32 * 10) + 1 + 8 + 8 + 1 + 1;
assert_eq!(IdentityConfig::LEN, expected, "Config size mismatch");
}
#[test]
fn test_identity_size() {
let expected = 8 + 32 + 32 + 1 + 32 + 32 + 1 + 8 + 8 + 1 + 1;
assert_eq!(Identity::LEN, expected, "Identity size mismatch");
}
// ========================================================================
// Verification Level Tests
// ========================================================================
#[test]
fn test_verification_level_from_u8() {
assert_eq!(VerificationLevel::from_u8(0), Some(VerificationLevel::None));
assert_eq!(VerificationLevel::from_u8(1), Some(VerificationLevel::Basic));
assert_eq!(VerificationLevel::from_u8(2), Some(VerificationLevel::KYC));
assert_eq!(VerificationLevel::from_u8(3), Some(VerificationLevel::Full));
assert_eq!(VerificationLevel::from_u8(4), Some(VerificationLevel::Enhanced));
assert_eq!(VerificationLevel::from_u8(5), None);
}
#[test]
fn test_verification_level_default() {
let level = VerificationLevel::default();
assert_eq!(level, VerificationLevel::None);
}
#[test]
fn test_verification_level_ordering() {
// Higher levels should be "more verified"
let levels = [
VerificationLevel::None,
VerificationLevel::Basic,
VerificationLevel::KYC,
VerificationLevel::Full,
VerificationLevel::Enhanced,
];
for i in 0..levels.len() {
assert_eq!(VerificationLevel::from_u8(i as u8), Some(levels[i]));
}
}
// ========================================================================
// Subscription Tier Tests
// ========================================================================
#[test]
fn test_subscription_tier_from_u8() {
assert_eq!(SubscriptionTier::from_u8(0), Some(SubscriptionTier::Free));
assert_eq!(SubscriptionTier::from_u8(1), Some(SubscriptionTier::Verified));
assert_eq!(SubscriptionTier::from_u8(2), Some(SubscriptionTier::Premium));
assert_eq!(SubscriptionTier::from_u8(3), Some(SubscriptionTier::Enterprise));
assert_eq!(SubscriptionTier::from_u8(4), None);
}
#[test]
fn test_subscription_tier_prices() {
assert_eq!(SubscriptionTier::Free.price(), SUBSCRIPTION_FREE);
assert_eq!(SubscriptionTier::Verified.price(), SUBSCRIPTION_VERIFIED);
assert_eq!(SubscriptionTier::Premium.price(), SUBSCRIPTION_PREMIUM);
assert_eq!(SubscriptionTier::Enterprise.price(), SUBSCRIPTION_ENTERPRISE);
}
#[test]
fn test_subscription_tier_default() {
let tier = SubscriptionTier::default();
assert_eq!(tier, SubscriptionTier::Free);
}
// ========================================================================
// Identity State Tests
// ========================================================================
#[test]
fn test_identity_default() {
let identity = Identity::default();
assert_eq!(identity.verification_level, 0);
assert!(!identity.is_active);
assert_eq!(identity.created_at, 0);
assert_eq!(identity.updated_at, 0);
}
#[test]
fn test_identity_username_capacity() {
let identity = Identity::default();
// Username is stored as [u8; 32]
assert_eq!(identity.username.len(), 32);
}
// ========================================================================
// Config Tests
// ========================================================================
#[test]
fn test_config_default() {
let config = IdentityConfig::default();
assert_eq!(config.attester_count, 0);
assert_eq!(config.total_identities, 0);
assert_eq!(config.verified_identities, 0);
assert!(!config.paused);
}
#[test]
fn test_trusted_attester_capacity() {
let config = IdentityConfig::default();
// Max 10 trusted attesters
assert_eq!(config.trusted_attesters.len(), 10);
}
// ========================================================================
// PDA Derivation Tests
// ========================================================================
#[test]
fn test_identity_pda_unique_per_user() {
let program_id = Pubkey::new_unique();
let user1 = Pubkey::new_unique();
let user2 = Pubkey::new_unique();
let (pda1, _) = Pubkey::find_program_address(
&[IDENTITY_SEED, user1.as_ref()],
&program_id
);
let (pda2, _) = Pubkey::find_program_address(
&[IDENTITY_SEED, user2.as_ref()],
&program_id
);
assert_ne!(pda1, pda2, "Different users should have different identity PDAs");
}
#[test]
fn test_config_pda() {
let program_id = Pubkey::new_unique();
let (pda, bump) = Pubkey::find_program_address(
&[IDENTITY_CONFIG_SEED],
&program_id
);
assert!(bump <= 255);
assert_ne!(pda, Pubkey::default());
}
// ========================================================================
// Subscription Expiry Tests
// ========================================================================
#[test]
fn test_subscription_expiry_calculation() {
let start_time = 1000i64;
let expires_at = start_time + SUBSCRIPTION_DURATION;
assert_eq!(expires_at, start_time + 30 * 24 * 60 * 60);
}
#[test]
fn test_subscription_active() {
let expires_at = 2_000_000i64;
let current_time = 1_000_000i64;
assert!(current_time < expires_at, "Subscription should be active");
}
#[test]
fn test_subscription_expired() {
let expires_at = 1_000_000i64;
let current_time = 2_000_000i64;
assert!(current_time >= expires_at, "Subscription should be expired");
}
// ========================================================================
// Invariant Tests
// ========================================================================
#[test]
fn test_invariant_verified_lte_total() {
let config = IdentityConfig {
total_identities: 100,
verified_identities: 50,
..Default::default()
};
assert!(config.verified_identities <= config.total_identities);
}
#[test]
fn test_invariant_attester_count_bounded() {
let config = IdentityConfig {
attester_count: 10,
..Default::default()
};
assert!(config.attester_count as usize <= config.trusted_attesters.len());
}
}