This repository was archived by the owner on May 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes_test.go
More file actions
560 lines (476 loc) · 14.2 KB
/
types_test.go
File metadata and controls
560 lines (476 loc) · 14.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
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
package main
import (
"testing"
"time"
)
func TestUsername_ToLower(t *testing.T) {
u := Username("HelloWorld")
if u.ToLower() != "helloworld" {
t.Errorf("Username.ToLower() = %q, want %q", u.ToLower(), "helloworld")
}
}
func TestUsername_ToLowerAlreadyLower(t *testing.T) {
u := Username("hello")
if u.ToLower() != "hello" {
t.Errorf("Username.ToLower() = %q, want %q", u.ToLower(), "hello")
}
}
func TestUser_GetUsername(t *testing.T) {
u := User{"username": "testuser"}
if u.GetUsername() != "testuser" {
t.Errorf("GetUsername() = %q, want %q", u.GetUsername(), "testuser")
}
}
func TestUser_GetUsername_Empty(t *testing.T) {
u := User{}
if u.GetUsername() != "" {
t.Errorf("GetUsername() on empty user should return empty string, got %q", u.GetUsername())
}
}
func TestUser_GetUsername_NonString(t *testing.T) {
u := User{"username": 123}
if u.GetUsername() != "" {
t.Errorf("GetUsername() with non-string should return empty string, got %q", u.GetUsername())
}
}
func TestUser_GetKey(t *testing.T) {
u := User{"key": "abc123"}
if u.GetKey() != "abc123" {
t.Errorf("GetKey() = %q, want %q", u.GetKey(), "abc123")
}
}
func TestUser_GetKey_Empty(t *testing.T) {
u := User{}
if u.GetKey() != "" {
t.Errorf("GetKey() on empty user should return empty string, got %q", u.GetKey())
}
}
func TestUser_GetPassword(t *testing.T) {
u := User{"password": "hash123"}
if u.GetPassword() != "hash123" {
t.Errorf("GetPassword() = %q, want %q", u.GetPassword(), "hash123")
}
}
func TestUser_GetId(t *testing.T) {
u := User{"sys.id": "user-123"}
if u.GetId() != "user-123" {
t.Errorf("GetId() = %q, want %q", u.GetId(), "user-123")
}
}
func TestUser_GetId_Empty(t *testing.T) {
u := User{}
if u.GetId() != "" {
t.Errorf("GetId() on empty user should return empty string, got %q", u.GetId())
}
}
func TestUser_IsBanned_True(t *testing.T) {
u := User{"sys.banned": true}
if !u.IsBanned() {
t.Error("IsBanned() should return true when sys.banned is true")
}
}
func TestUser_IsBanned_StringTrue(t *testing.T) {
u := User{"sys.banned": "true"}
if !u.IsBanned() {
t.Error("IsBanned() should return true when sys.banned is 'true'")
}
}
func TestUser_IsBanned_False(t *testing.T) {
u := User{"sys.banned": false}
if u.IsBanned() {
t.Error("IsBanned() should return false when sys.banned is false")
}
}
func TestUser_IsBanned_Nil(t *testing.T) {
u := User{}
if u.IsBanned() {
t.Error("IsBanned() should return false when sys.banned is nil")
}
}
func TestUser_IsPrivate(t *testing.T) {
u := User{"private": true}
if !u.IsPrivate() {
t.Error("IsPrivate() should return true")
}
}
func TestUser_IsPrivate_False(t *testing.T) {
u := User{"private": false}
if u.IsPrivate() {
t.Error("IsPrivate() should return false")
}
}
func TestUser_GetSystem(t *testing.T) {
u := User{"system": "rotur"}
if u.GetSystem() != "rotur" {
t.Errorf("GetSystem() = %q, want %q", u.GetSystem(), "rotur")
}
}
func TestUser_GetSystem_Default(t *testing.T) {
u := User{}
if u.GetSystem() != "rotur" {
t.Errorf("GetSystem() default should be 'rotur', got %q", u.GetSystem())
}
}
func TestUser_GetEmail(t *testing.T) {
u := User{"email": "test@example.com"}
if u.GetEmail() != "test@example.com" {
t.Errorf("GetEmail() = %q, want %q", u.GetEmail(), "test@example.com")
}
}
func TestUser_GetEmail_Empty(t *testing.T) {
u := User{}
if u.GetEmail() != "" {
t.Errorf("GetEmail() on empty user should return empty string, got %q", u.GetEmail())
}
}
func TestUser_GetCredits(t *testing.T) {
u := User{"sys.currency": float64(100.5)}
if u.GetCredits() != 100.5 {
t.Errorf("GetCredits() = %v, want 100.5", u.GetCredits())
}
}
func TestUser_GetCredits_Int(t *testing.T) {
u := User{"sys.currency": 50}
if u.GetCredits() != 50.0 {
t.Errorf("GetCredits() = %v, want 50.0", u.GetCredits())
}
}
func TestUser_GetCredits_Nil(t *testing.T) {
u := User{}
if u.GetCredits() != 0 {
t.Errorf("GetCredits() on empty user should return 0, got %v", u.GetCredits())
}
}
func TestUser_SetBalance_Float64(t *testing.T) {
u := User{}
u.SetBalance(99.5)
if u.GetCredits() != 99.5 {
t.Errorf("SetBalance(99.5): GetCredits() = %v, want 99.5", u.GetCredits())
}
}
func TestUser_SetBalance_Int(t *testing.T) {
u := User{}
u.SetBalance(50)
if u.GetCredits() != 50.0 {
t.Errorf("SetBalance(50): GetCredits() = %v, want 50.0", u.GetCredits())
}
}
func TestUser_SetBalance_RoundsToTwoDecimals(t *testing.T) {
u := User{}
u.SetBalance(1.2345)
credits := u.GetCredits()
if credits != roundVal(1.2345) {
t.Errorf("SetBalance should round to 2 decimals, got %v", credits)
}
}
func TestUser_Blocking(t *testing.T) {
u := User{}
u.AddBlocked("user1")
if !u.HasBlocked("user1") {
t.Error("Should have blocked user1")
}
if u.HasBlocked("user2") {
t.Error("Should not have blocked user2")
}
u.RemoveBlocked("user1")
if u.HasBlocked("user1") {
t.Error("Should no longer have blocked user1")
}
}
func TestUser_AddBlocked_Idempotent(t *testing.T) {
u := User{}
u.AddBlocked("user1")
u.AddBlocked("user1") // should not add duplicate
blocked := u.GetBlocked()
if len(blocked) != 1 {
t.Errorf("Adding same blocked user twice should result in 1 entry, got %d", len(blocked))
}
}
func TestUser_RemoveBlocked_NotBlocked(t *testing.T) {
u := User{}
u.RemoveBlocked("nonexistent") // should not panic
}
func TestUser_Friends(t *testing.T) {
// We can't fully test AddFriend/RemoveFriend because they call username.Id()
// which requires the global idToUser map. Test what we can.
u := User{
"sys.friends": []string{"id1", "id2"},
}
friends := u.GetFriends()
if len(friends) != 2 {
t.Errorf("GetFriends() should return 2, got %d", len(friends))
}
}
func TestUser_GetFriends_Empty(t *testing.T) {
u := User{}
friends := u.GetFriends()
if len(friends) != 0 {
t.Errorf("GetFriends() on empty user should return empty, got %d", len(friends))
}
}
func TestUser_GetNotes(t *testing.T) {
u := User{}
notes := u.GetNotes()
if len(notes) != 0 {
t.Errorf("GetNotes() on empty user should return empty map, got %d entries", len(notes))
}
}
func TestUser_GetStanding_Default(t *testing.T) {
u := User{}
if u.GetStanding() != StandingGood {
t.Errorf("Default standing should be 'good', got %q", u.GetStanding())
}
}
func TestUser_GetStanding_Set(t *testing.T) {
u := User{"sys.standing": "warning"}
if u.GetStanding() != StandingWarning {
t.Errorf("Standing should be 'warning', got %q", u.GetStanding())
}
}
func TestUser_CanCreatePost(t *testing.T) {
u := User{"sys.standing": "good"}
if !u.CanCreatePost() {
t.Error("Good standing user should be able to create posts")
}
u2 := User{"sys.standing": "warning"}
if u2.CanCreatePost() {
t.Error("Warning standing user should not be able to create posts")
}
}
func TestUser_CanFollow(t *testing.T) {
tests := []struct {
standing StandingLevel
expected bool
}{
{StandingGood, true},
{StandingWarning, true},
{StandingSuspended, false},
{StandingBanned, false},
}
for _, tt := range tests {
u := User{"sys.standing": string(tt.standing)}
if u.CanFollow() != tt.expected {
t.Errorf("CanFollow() with standing %q = %v, want %v", tt.standing, u.CanFollow(), tt.expected)
}
}
}
func TestUser_CanTradeBuy(t *testing.T) {
u := User{"sys.standing": "warning"}
if !u.CanTradeBuy() {
t.Error("Warning standing user should be able to trade buy")
}
u2 := User{"sys.standing": "suspended"}
if u2.CanTradeBuy() {
t.Error("Suspended standing user should not be able to trade buy")
}
}
func TestUser_HasStandingOrHigher(t *testing.T) {
u := User{"sys.standing": "good"}
if !u.HasStandingOrHigher(StandingGood) {
t.Error("Good standing should pass good check")
}
if !u.HasStandingOrHigher(StandingBanned) {
t.Error("Good standing should pass banned check (always passes)")
}
if u.HasStandingOrHigher(StandingWarning) {
// good is higher than warning, so this should NOT pass
// Actually looking at the code: good does NOT pass warning check
// Wait, the code says for Warning: current == StandingGood || current == StandingWarning
t.Error("Good standing should not pass warning check based on code logic")
}
}
// Wait, let me re-read the HasStandingOrHigher code...
// case StandingWarning: return current == StandingGood || current == StandingWarning
// So Good DOES pass the warning check. Let me fix the test.
func TestUser_HasStandingOrHigher_Correct(t *testing.T) {
// Override the previous test
u := User{"sys.standing": "good"}
if !u.HasStandingOrHigher(StandingWarning) {
t.Error("Good standing should pass warning check (good >= warning)")
}
}
func TestUser_Created(t *testing.T) {
ts := time.Now().UnixMilli()
u := User{"created": float64(ts)}
if u.GetCreated() != ts {
t.Errorf("GetCreated() = %d, want %d", u.GetCreated(), ts)
}
}
func TestUser_Created_Int64(t *testing.T) {
ts := time.Now().UnixMilli()
u := User{"created": int64(ts)}
if u.GetCreated() != ts {
t.Errorf("GetCreated() = %d, want %d", u.GetCreated(), ts)
}
}
func TestUser_Created_Empty(t *testing.T) {
u := User{}
if u.GetCreated() != 0 {
t.Errorf("GetCreated() on empty user should return 0, got %d", u.GetCreated())
}
}
func TestUser_GetTheme(t *testing.T) {
theme := map[string]any{"primary": "#222"}
u := User{"theme": theme}
got := u.GetTheme()
if got["primary"] != "#222" {
t.Errorf("GetTheme() primary = %v, want #222", got["primary"])
}
}
func TestUser_GetTheme_Empty(t *testing.T) {
u := User{}
got := u.GetTheme()
if len(got) != 0 {
t.Errorf("GetTheme() on empty user should return empty map, got %d entries", len(got))
}
}
func TestUser_Has(t *testing.T) {
u := User{"key": "value"}
if !u.Has("key") {
t.Error("Has('key') should return true")
}
if u.Has("nonexistent") {
t.Error("Has('nonexistent') should return false")
}
}
func TestUser_GetInt(t *testing.T) {
u := User{"count": 42}
if u.GetInt("count") != 42 {
t.Errorf("GetInt('count') = %d, want 42", u.GetInt("count"))
}
}
func TestUser_GetInt_Float64(t *testing.T) {
u := User{"count": float64(7)}
if u.GetInt("count") != 7 {
t.Errorf("GetInt('count') with float64 = %d, want 7", u.GetInt("count"))
}
}
func TestUser_GetInt_Missing(t *testing.T) {
u := User{}
if u.GetInt("count") != 0 {
t.Errorf("GetInt('count') on missing key should return 0, got %d", u.GetInt("count"))
}
}
func TestUser_GetString(t *testing.T) {
u := User{"name": "test"}
if u.GetString("name") != "test" {
t.Errorf("GetString('name') = %q, want %q", u.GetString("name"), "test")
}
}
func TestUser_GetString_Int(t *testing.T) {
u := User{"count": 42}
if u.GetString("count") != "42" {
t.Errorf("GetString('count') with int = %q, want %q", u.GetString("count"), "42")
}
}
func TestUser_GetString_Missing(t *testing.T) {
u := User{}
if u.GetString("name") != "" {
t.Errorf("GetString('name') on missing key should return empty, got %q", u.GetString("name"))
}
}
func TestUser_GetBlockedIps(t *testing.T) {
u := User{"blocked_ips": []string{"1.2.3.4", "5.6.7.8"}}
ips := u.GetBlockedIps()
if len(ips) != 2 {
t.Errorf("GetBlockedIps() should return 2, got %d", len(ips))
}
}
func TestUser_GetBlockedIps_Empty(t *testing.T) {
u := User{}
ips := u.GetBlockedIps()
if len(ips) != 0 {
t.Errorf("GetBlockedIps() on empty user should return empty, got %d", len(ips))
}
}
func TestTimestamp_Time(t *testing.T) {
ts := Timestamp(time.Now().UnixMilli())
result := ts.Time()
if result.IsZero() {
t.Error("Timestamp.Time() should not return zero time")
}
}
func TestUser_SocialLinks(t *testing.T) {
u := User{"sys.social_links": []string{"https://twitter.com/test", "https://github.com/test"}}
links := u.GetSocialLinks()
if len(links) != 2 {
t.Errorf("GetSocialLinks() should return 2, got %d", len(links))
}
}
func TestUser_SetSocialLinks(t *testing.T) {
u := User{}
u.SetSocialLinks([]string{"https://example.com"})
links := u.GetSocialLinks()
if len(links) != 1 || links[0] != "https://example.com" {
t.Errorf("SetSocialLinks() then GetSocialLinks() = %v, want [https://example.com]", links)
}
}
func TestGift_IsActive(t *testing.T) {
g := Gift{Id: "1", Code: "abc", Amount: 100}
if !g.IsActive() {
t.Error("New gift should be active")
}
}
func TestGift_IsNotActive_Claimed(t *testing.T) {
now := time.Now().UnixMilli()
g := Gift{Id: "1", Code: "abc", Amount: 100, ClaimedAt: &now}
if g.IsActive() {
t.Error("Claimed gift should not be active")
}
}
func TestGift_IsNotActive_Cancelled(t *testing.T) {
now := time.Now().UnixMilli()
g := Gift{Id: "1", Code: "abc", Amount: 100, CancelledAt: &now}
if g.IsActive() {
t.Error("Cancelled gift should not be active")
}
}
func TestGift_IsExpired(t *testing.T) {
g := Gift{Id: "1", Code: "abc", Amount: 100, ExpiresAt: time.Now().UnixMilli() - 1000}
if !g.IsExpired() {
t.Error("Gift with past expiry should be expired")
}
}
func TestGift_IsNotExpired(t *testing.T) {
g := Gift{Id: "1", Code: "abc", Amount: 100, ExpiresAt: time.Now().UnixMilli() + 3600000}
if g.IsExpired() {
t.Error("Gift with future expiry should not be expired")
}
}
func TestGift_IsExpired_ZeroExpiry(t *testing.T) {
g := Gift{Id: "1", Code: "abc", Amount: 100, ExpiresAt: 0}
if g.IsExpired() {
t.Error("Gift with zero expiry should not be expired")
}
}
func TestGift_CanBeClaimed(t *testing.T) {
g := Gift{Id: "1", Code: "abc", Amount: 100, ExpiresAt: time.Now().UnixMilli() + 3600000}
if !g.CanBeClaimed() {
t.Error("Active, non-expired gift should be claimable")
}
}
func TestGift_CannotBeClaimed_Expired(t *testing.T) {
g := Gift{Id: "1", Code: "abc", Amount: 100, ExpiresAt: time.Now().UnixMilli() - 1000}
if g.CanBeClaimed() {
t.Error("Expired gift should not be claimable")
}
}
func TestGift_CannotBeClaimed_Claimed(t *testing.T) {
now := time.Now().UnixMilli()
g := Gift{Id: "1", Code: "abc", Amount: 100, ClaimedAt: &now, ExpiresAt: time.Now().UnixMilli() + 3600000}
if g.CanBeClaimed() {
t.Error("Already claimed gift should not be claimable")
}
}
func TestGift_CanBeCancelled(t *testing.T) {
g := Gift{Id: "1", Code: "abc", Amount: 100, ExpiresAt: time.Now().UnixMilli() + 3600000}
if !g.CanBeCancelled() {
t.Error("Active, non-expired gift should be cancellable")
}
}
func TestGift_CannotBeCancelled_Expired(t *testing.T) {
g := Gift{Id: "1", Code: "abc", Amount: 100, ExpiresAt: time.Now().UnixMilli() - 1000}
if g.CanBeCancelled() {
t.Error("Expired gift should not be cancellable")
}
}