-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathripple_client_test.go
More file actions
626 lines (510 loc) · 15.2 KB
/
Copy pathripple_client_test.go
File metadata and controls
626 lines (510 loc) · 15.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
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
package ripple
import (
"errors"
"testing"
"time"
"github.com/Tap30/ripple-go/adapters"
)
func createTestConfig() ClientConfig {
return ClientConfig{
APIKey: "test-key",
Endpoint: "http://test.com",
HTTPAdapter: &mockHTTPAdapter{},
StorageAdapter: &mockStorageAdapter{},
}
}
func createTestClient() *Client {
client, err := NewClient(createTestConfig())
if err != nil {
panic(err)
}
return client
}
func TestClient_ConfigValidation(t *testing.T) {
t.Run("should return error if APIKey is missing", func(t *testing.T) {
_, err := NewClient(ClientConfig{
Endpoint: "http://test.com",
HTTPAdapter: &mockHTTPAdapter{},
StorageAdapter: &mockStorageAdapter{},
})
if err == nil {
t.Fatal("expected error for missing APIKey")
}
})
t.Run("should return error if Endpoint is missing", func(t *testing.T) {
_, err := NewClient(ClientConfig{
APIKey: "test-key",
HTTPAdapter: &mockHTTPAdapter{},
StorageAdapter: &mockStorageAdapter{},
})
if err == nil {
t.Fatal("expected error for missing Endpoint")
}
})
t.Run("should return error if HTTPAdapter is missing", func(t *testing.T) {
_, err := NewClient(ClientConfig{
APIKey: "test-key",
Endpoint: "http://test.com",
StorageAdapter: &mockStorageAdapter{},
})
if err == nil {
t.Fatal("expected error for missing HTTPAdapter")
}
})
t.Run("should return error if StorageAdapter is missing", func(t *testing.T) {
_, err := NewClient(ClientConfig{
APIKey: "test-key",
Endpoint: "http://test.com",
HTTPAdapter: &mockHTTPAdapter{},
})
if err == nil {
t.Fatal("expected error for missing StorageAdapter")
}
})
t.Run("should return error for negative FlushInterval", func(t *testing.T) {
_, err := NewClient(ClientConfig{
APIKey: "test-key",
Endpoint: "http://test.com",
HTTPAdapter: &mockHTTPAdapter{},
StorageAdapter: &mockStorageAdapter{},
FlushInterval: -1 * time.Second,
})
if err == nil {
t.Fatal("expected error for negative FlushInterval")
}
})
t.Run("should return error for FlushInterval less than 1ms", func(t *testing.T) {
_, err := NewClient(ClientConfig{
APIKey: "test-key",
Endpoint: "http://test.com",
HTTPAdapter: &mockHTTPAdapter{},
StorageAdapter: &mockStorageAdapter{},
FlushInterval: 500 * time.Nanosecond,
})
if err == nil {
t.Fatal("expected error for FlushInterval < 1ms")
}
})
t.Run("should return error for negative MaxBatchSize", func(t *testing.T) {
_, err := NewClient(ClientConfig{
APIKey: "test-key",
Endpoint: "http://test.com",
HTTPAdapter: &mockHTTPAdapter{},
StorageAdapter: &mockStorageAdapter{},
MaxBatchSize: -5,
})
if err == nil {
t.Fatal("expected error for negative MaxBatchSize")
}
})
t.Run("should return error for negative MaxRetries", func(t *testing.T) {
_, err := NewClient(ClientConfig{
APIKey: "test-key",
Endpoint: "http://test.com",
HTTPAdapter: &mockHTTPAdapter{},
StorageAdapter: &mockStorageAdapter{},
MaxRetries: -1,
})
if err == nil {
t.Fatal("expected error for negative MaxRetries")
}
})
t.Run("should return error for negative MaxBufferSize", func(t *testing.T) {
_, err := NewClient(ClientConfig{
APIKey: "test-key",
Endpoint: "http://test.com",
HTTPAdapter: &mockHTTPAdapter{},
StorageAdapter: &mockStorageAdapter{},
MaxBufferSize: -1,
})
if err == nil {
t.Fatal("expected error for negative MaxBufferSize")
}
})
t.Run("should return error when MaxBufferSize < MaxBatchSize", func(t *testing.T) {
_, err := NewClient(ClientConfig{
APIKey: "test-key",
Endpoint: "http://test.com",
HTTPAdapter: &mockHTTPAdapter{},
StorageAdapter: &mockStorageAdapter{},
MaxBatchSize: 100,
MaxBufferSize: 50,
})
if err == nil {
t.Fatal("expected error when MaxBufferSize < MaxBatchSize")
}
})
t.Run("should accept custom API key header", func(t *testing.T) {
customHeader := "Authorization"
client, err := NewClient(ClientConfig{
APIKey: "test-key",
Endpoint: "http://test.com",
APIKeyHeader: &customHeader,
HTTPAdapter: &mockHTTPAdapter{},
StorageAdapter: &mockStorageAdapter{},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if client == nil {
t.Fatal("expected client to be created")
}
})
}
func TestClient_TrackAutoInit(t *testing.T) {
t.Run("should auto-init when Track is called", func(t *testing.T) {
client := createTestClient()
defer client.Dispose()
err := client.Track("test_event", nil, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
t.Run("should allow tracking after explicit Init", func(t *testing.T) {
client := createTestClient()
client.Init()
defer client.Dispose()
err := client.Track("test_event", nil, nil)
if err != nil {
t.Fatalf("unexpected error after init: %v", err)
}
})
}
func TestClient_DisposedBehavior(t *testing.T) {
t.Run("should silently drop events after dispose", func(t *testing.T) {
client := createTestClient()
client.Init()
client.Dispose()
// Track after dispose should return nil (silently dropped)
err := client.Track("test_event", nil, nil)
if err != nil {
t.Fatalf("expected nil error after dispose, got: %v", err)
}
})
t.Run("should re-enable after explicit Init", func(t *testing.T) {
client := createTestClient()
client.Init()
client.Dispose()
// Re-init should work
client.Init()
defer client.Dispose()
err := client.Track("test_event", nil, nil)
if err != nil {
t.Fatalf("unexpected error after re-init: %v", err)
}
})
t.Run("should work before initialization", func(t *testing.T) {
client := createTestClient()
// Should not panic when called before init
client.Dispose()
})
t.Run("should work multiple times", func(t *testing.T) {
client := createTestClient()
client.Init()
client.Dispose()
client.Dispose()
})
t.Run("dispose should clear metadata", func(t *testing.T) {
client := createTestClient()
client.Init()
client.SetMetadata("key", "value")
client.Dispose()
metadata := client.GetMetadata()
if len(metadata) != 0 {
t.Fatal("expected metadata to be cleared after dispose")
}
})
}
func TestClient_TrackValidation(t *testing.T) {
t.Run("should reject empty event name", func(t *testing.T) {
client := createTestClient()
err := client.Track("", nil, nil)
if err == nil {
t.Fatal("expected error for empty event name")
}
if err.Error() != "event name cannot be empty" {
t.Fatalf("unexpected error message: %v", err)
}
})
}
func TestClient_MetadataManagement(t *testing.T) {
client := createTestClient()
t.Run("should set and get metadata", func(t *testing.T) {
client.SetMetadata("userId", "123")
client.SetMetadata("sessionId", "abc")
metadata := client.GetMetadata()
if metadata["userId"] != "123" {
t.Fatal("expected userId to be 123")
}
if metadata["sessionId"] != "abc" {
t.Fatal("expected sessionId to be abc")
}
})
t.Run("should return empty map when no metadata is set", func(t *testing.T) {
newClient := createTestClient()
metadata := newClient.GetMetadata()
if len(metadata) != 0 {
t.Fatal("expected empty metadata when none is set")
}
})
}
func TestClient_FlushEdgeCases(t *testing.T) {
t.Run("should work with empty queue", func(t *testing.T) {
client := createTestClient()
client.Init()
defer client.Dispose()
client.Flush()
})
t.Run("should work before initialization", func(t *testing.T) {
client := createTestClient()
client.Flush()
})
}
func TestClient_GetSessionId(t *testing.T) {
client := createTestClient()
sessionID := client.GetSessionId()
if sessionID != nil {
t.Fatalf("expected nil session ID for server environment, got %v", *sessionID)
}
}
func TestClient_Track(t *testing.T) {
client := createTestClient()
client.Init()
defer client.Dispose()
client.SetMetadata("userId", "123")
client.Track("page_view", map[string]any{"page": "/home"}, nil)
time.Sleep(100 * time.Millisecond)
if client.dispatcher.queue.Len() == 0 {
t.Fatal("expected event to be tracked")
}
}
func TestClient_TrackWithMetadata(t *testing.T) {
client := createTestClient()
client.Init()
defer client.Dispose()
metadata := map[string]any{"schemaVersion": "1.0.0"}
client.Track("user_signup", map[string]any{"email": "test@example.com"}, metadata)
time.Sleep(100 * time.Millisecond)
if client.dispatcher.queue.Len() == 0 {
t.Fatal("expected event with metadata to be tracked")
}
}
func TestClient_Flush(t *testing.T) {
mockHTTP := &mockHTTPAdapter{}
client, _ := NewClient(ClientConfig{
APIKey: "test-key",
Endpoint: "http://test.com",
HTTPAdapter: mockHTTP,
StorageAdapter: &mockStorageAdapter{},
})
client.Init()
defer client.Dispose()
client.Track("test_event", nil, nil)
client.Flush()
if mockHTTP.getCalls() != 1 {
t.Fatalf("expected 1 HTTP call, got %d", mockHTTP.getCalls())
}
}
func TestClient_DefaultConfig(t *testing.T) {
client := createTestClient()
if client.config.FlushInterval != 5*time.Second {
t.Fatal("expected default flush interval of 5s")
}
if client.config.MaxBatchSize != 10 {
t.Fatal("expected default max batch size of 10")
}
if client.config.MaxRetries != 3 {
t.Fatal("expected default max retries of 3")
}
}
func TestClient_InitEdgeCases(t *testing.T) {
t.Run("should handle init when already initialized", func(t *testing.T) {
client := createTestClient()
client.Init()
defer client.Dispose()
client.Init()
})
t.Run("should handle concurrent init calls safely", func(t *testing.T) {
client := createTestClient()
defer client.Dispose()
done := make(chan struct{})
for i := 0; i < 10; i++ {
go func() {
client.Init()
done <- struct{}{}
}()
}
for i := 0; i < 10; i++ {
<-done
}
// Verify initialization by tracking an event (uses public API only)
err := client.Track("test_event", nil, nil)
if err != nil {
t.Fatalf("expected initialized client to track events, got error: %v", err)
}
})
t.Run("should use provided LoggerAdapter", func(t *testing.T) {
config := createTestConfig()
customLogger := adapters.NewNoOpLoggerAdapter()
config.LoggerAdapter = customLogger
client, err := NewClient(config)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if client.loggerAdapter != customLogger {
t.Fatal("expected custom logger to be used")
}
})
}
func TestClient_SharedMetadataMerging(t *testing.T) {
client := createTestClient()
client.Init()
defer client.Dispose()
client.SetMetadata("userId", "123")
client.SetMetadata("appVersion", "1.0.0")
client.Track("test_event", map[string]any{"action": "click"}, map[string]any{"schemaVersion": "2.0.0"})
time.Sleep(50 * time.Millisecond)
if client.dispatcher.queue.Len() > 0 {
event, ok := client.dispatcher.queue.Dequeue()
if !ok {
t.Error("failed to dequeue event")
return
}
if event.Metadata["userId"] != "123" {
t.Errorf("expected userId to be 123, got %v", event.Metadata["userId"])
}
if event.Metadata["appVersion"] != "1.0.0" {
t.Errorf("expected appVersion to be 1.0.0, got %v", event.Metadata["appVersion"])
}
if event.Metadata["schemaVersion"] != "2.0.0" {
t.Errorf("expected schemaVersion to be 2.0.0, got %v", event.Metadata["schemaVersion"])
}
} else {
t.Error("expected event to be in queue")
}
}
func TestClient_SharedMetadataOverride(t *testing.T) {
client := createTestClient()
client.Init()
defer client.Dispose()
client.SetMetadata("environment", "test")
client.SetMetadata("version", "1.0.0")
client.Track("test_event", map[string]any{"action": "click"}, map[string]any{"version": "2.0.0", "source": "button"})
time.Sleep(50 * time.Millisecond)
if client.dispatcher.queue.Len() > 0 {
event, ok := client.dispatcher.queue.Dequeue()
if !ok {
t.Error("failed to dequeue event")
return
}
if event.Metadata["environment"] != "test" {
t.Errorf("expected environment to be test, got %v", event.Metadata["environment"])
}
if event.Metadata["version"] != "2.0.0" {
t.Errorf("expected version to be 2.0.0 (overridden), got %v", event.Metadata["version"])
}
if event.Metadata["source"] != "button" {
t.Errorf("expected source to be button, got %v", event.Metadata["source"])
}
} else {
t.Error("expected event to be in queue")
}
}
func TestClient_TrackWithOnlySharedMetadata(t *testing.T) {
client := createTestClient()
client.Init()
defer client.Dispose()
client.SetMetadata("userId", "123")
client.Track("test_event", nil, nil)
time.Sleep(50 * time.Millisecond)
if client.dispatcher.queue.Len() > 0 {
event, ok := client.dispatcher.queue.Dequeue()
if !ok {
t.Error("failed to dequeue event")
return
}
if event.Metadata["userId"] != "123" {
t.Errorf("expected userId to be 123, got %v", event.Metadata["userId"])
}
if len(event.Metadata) != 1 {
t.Errorf("expected 1 metadata field, got %d", len(event.Metadata))
}
} else {
t.Error("expected event to be in queue")
}
}
func TestClient_TrackWithNoMetadata(t *testing.T) {
client := createTestClient()
client.Init()
defer client.Dispose()
client.Track("test_event", nil, nil)
time.Sleep(50 * time.Millisecond)
if client.dispatcher.queue.Len() > 0 {
event, ok := client.dispatcher.queue.Dequeue()
if !ok {
t.Error("failed to dequeue event")
return
}
if len(event.Metadata) != 0 {
t.Errorf("expected metadata to be empty, got %v", event.Metadata)
}
} else {
t.Error("expected event to be in queue")
}
}
func TestClient_MetadataManager_IsEmpty(t *testing.T) {
client := createTestClient()
if !client.metadataManager.IsEmpty() {
t.Fatal("expected metadata manager to be empty")
}
client.SetMetadata("key", "value")
if client.metadataManager.IsEmpty() {
t.Fatal("expected metadata manager to not be empty")
}
}
func TestClient_MetadataManager_Clear(t *testing.T) {
client := createTestClient()
client.SetMetadata("key1", "value1")
client.SetMetadata("key2", "value2")
client.metadataManager.Clear()
if !client.metadataManager.IsEmpty() {
t.Fatal("expected metadata manager to be empty after clear")
}
}
func TestClient_StorageAdapterFailures(t *testing.T) {
storageAdapter := &mockStorageAdapter{err: errors.New("storage error")}
client, err := NewClient(ClientConfig{
APIKey: "test-key",
Endpoint: "https://api.example.com",
FlushInterval: 100 * time.Millisecond,
MaxBatchSize: 10,
MaxRetries: 3,
MaxBufferSize: 100,
HTTPAdapter: &mockHTTPAdapter{},
StorageAdapter: storageAdapter,
})
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
// Init should succeed even with storage error (restore logs, doesn't fail)
client.Init()
// Track should work even if storage fails
storageAdapter.err = errors.New("save error")
if err := client.Track("test_event", nil, nil); err != nil {
t.Errorf("Track should not fail even if storage fails: %v", err)
}
client.Dispose()
}
func TestClient_Close(t *testing.T) {
client, _ := NewClient(ClientConfig{
APIKey: "test-key",
Endpoint: "http://localhost:8080",
HTTPAdapter: &mockHTTPAdapter{},
StorageAdapter: &mockStorageAdapter{},
})
client.Init()
client.Close()
if !client.disposed {
t.Error("Close should dispose the client")
}
}