-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_test.go
More file actions
1207 lines (1038 loc) · 29.4 KB
/
config_test.go
File metadata and controls
1207 lines (1038 loc) · 29.4 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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package config
import (
"context"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/google/go-github/v50/github"
)
// Test pure functions that don't require external dependencies.
func TestMatchesRepo(t *testing.T) {
m := &Manager{}
tests := []struct {
name string
pattern string
repo string
expected bool
}{
{
name: "wildcard matches everything",
pattern: "*",
repo: "any-repo",
expected: true,
},
{
name: "exact match",
pattern: "goose",
repo: "goose",
expected: true,
},
{
name: "no match",
pattern: "goose",
repo: "slacker",
expected: false,
},
{
name: "case sensitive - no match",
pattern: "Goose",
repo: "goose",
expected: false,
},
{
name: "empty pattern does not match",
pattern: "",
repo: "repo",
expected: false,
},
{
name: "empty repo does not match non-empty pattern",
pattern: "repo",
repo: "",
expected: false,
},
{
name: "both empty is exact match",
pattern: "",
repo: "",
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := m.matchesRepo(tt.pattern, tt.repo)
if result != tt.expected {
t.Errorf("matchesRepo(%q, %q) = %v, want %v",
tt.pattern, tt.repo, result, tt.expected)
}
})
}
}
func TestAutoDiscoverChannels(t *testing.T) {
m := &Manager{}
tests := []struct {
name string
org string
repo string
expected []string
}{
{
name: "simple repo name",
org: "codeGROOVE-dev",
repo: "goose",
expected: []string{"goose"},
},
{
name: "repo with dashes",
org: "codeGROOVE-dev",
repo: "my-service",
expected: []string{"my-service"},
},
{
name: "uppercase repo becomes lowercase",
org: "codeGROOVE-dev",
repo: "MyRepo",
expected: []string{"myrepo"},
},
{
name: "mixed case repo",
org: "codeGROOVE-dev",
repo: "CodeGROOVE",
expected: []string{"codegroove"},
},
{
name: "repo with underscores",
org: "codeGROOVE-dev",
repo: "my_repo",
expected: []string{"my_repo"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := m.autoDiscoverChannels(tt.org, tt.repo)
if len(result) != len(tt.expected) {
t.Fatalf("autoDiscoverChannels() returned %d channels, want %d",
len(result), len(tt.expected))
}
for i := range result {
if result[i] != tt.expected[i] {
t.Errorf("autoDiscoverChannels()[%d] = %q, want %q",
i, result[i], tt.expected[i])
}
}
})
}
}
func TestCreateDefaultConfig(t *testing.T) {
cfg := createDefaultConfig()
// Verify default values
if cfg.Global.ReminderDMDelay != defaultReminderDMDelayMinutes {
t.Errorf("expected default ReminderDMDelay %d, got %d",
defaultReminderDMDelayMinutes, cfg.Global.ReminderDMDelay)
}
if !cfg.Global.DailyReminders {
t.Error("expected DailyReminders to be enabled by default")
}
if cfg.Channels == nil {
t.Error("expected Channels map to be initialized")
}
if cfg.Global.TeamID != "" {
t.Errorf("expected empty TeamID, got %q", cfg.Global.TeamID)
}
if cfg.Global.EmailDomain != "" {
t.Errorf("expected empty EmailDomain, got %q", cfg.Global.EmailDomain)
}
}
func TestConfigCache_GetSet(t *testing.T) {
cache := &configCache{
entries: make(map[string]configCacheEntry),
ttl: 5 * time.Minute,
}
// Test cache miss
cfg, found := cache.get("test-org")
if found {
t.Error("expected cache miss for unknown org")
}
if cfg != nil {
t.Error("expected nil config on cache miss")
}
// Set config
testConfig := &RepoConfig{
Global: struct {
TeamID string `yaml:"team_id"`
EmailDomain string `yaml:"email_domain"`
ReminderDMDelay int `yaml:"reminder_dm_delay"`
DailyReminders bool `yaml:"daily_reminders"`
}{
TeamID: "T123",
EmailDomain: "example.com",
},
}
cache.set("test-org", testConfig)
// Test cache hit
cfg, found = cache.get("test-org")
if !found {
t.Error("expected cache hit after setting config")
}
if cfg == nil {
t.Fatal("expected non-nil config on cache hit")
}
if cfg.Global.TeamID != "T123" {
t.Errorf("expected TeamID T123, got %q", cfg.Global.TeamID)
}
}
func TestConfigCache_Expiration(t *testing.T) {
cache := &configCache{
entries: make(map[string]configCacheEntry),
ttl: 50 * time.Millisecond, // Very short TTL for testing
}
testConfig := createDefaultConfig()
cache.set("test-org", testConfig)
// Immediate read should hit cache
_, found := cache.get("test-org")
if !found {
t.Error("expected cache hit immediately after set")
}
// Wait for expiration
time.Sleep(60 * time.Millisecond)
// Should now be expired
_, found = cache.get("test-org")
if found {
t.Error("expected cache miss after TTL expiration")
}
}
func TestConfigCache_Invalidate(t *testing.T) {
cache := &configCache{
entries: make(map[string]configCacheEntry),
ttl: 5 * time.Minute,
}
// Set multiple configs
cache.set("org1", createDefaultConfig())
cache.set("org2", createDefaultConfig())
// Verify both are cached
_, found1 := cache.get("org1")
_, found2 := cache.get("org2")
if !found1 || !found2 {
t.Fatal("expected both configs to be cached")
}
// Invalidate org1
cache.invalidate("org1")
// org1 should be gone, org2 should remain
_, found1 = cache.get("org1")
_, found2 = cache.get("org2")
if found1 {
t.Error("expected org1 to be invalidated")
}
if !found2 {
t.Error("expected org2 to remain in cache")
}
}
func TestConfigCache_InvalidateAll(t *testing.T) {
cache := &configCache{
entries: make(map[string]configCacheEntry),
ttl: 5 * time.Minute,
}
// Set multiple configs
cache.set("org1", createDefaultConfig())
cache.set("org2", createDefaultConfig())
cache.set("org3", createDefaultConfig())
// Verify all are cached
_, found1 := cache.get("org1")
_, found2 := cache.get("org2")
_, found3 := cache.get("org3")
if !found1 || !found2 || !found3 {
t.Fatal("expected all configs to be cached")
}
// Invalidate all
cache.invalidateAll()
// All should be gone
_, found1 = cache.get("org1")
_, found2 = cache.get("org2")
_, found3 = cache.get("org3")
if found1 || found2 || found3 {
t.Error("expected all configs to be invalidated")
}
// Cache should still be functional
cache.set("org4", createDefaultConfig())
_, found4 := cache.get("org4")
if !found4 {
t.Error("expected cache to work after invalidateAll")
}
}
func TestConfigCache_Stats(t *testing.T) {
cache := &configCache{
entries: make(map[string]configCacheEntry),
ttl: 5 * time.Minute,
}
// Initial stats should be zero
hits, misses := cache.stats()
if hits != 0 || misses != 0 {
t.Errorf("expected zero stats initially, got hits=%d misses=%d", hits, misses)
}
// Cache miss
_, _ = cache.get("org1")
hits, misses = cache.stats()
if hits != 0 || misses != 1 {
t.Errorf("expected 1 miss, got hits=%d misses=%d", hits, misses)
}
// Set and cache hit
cache.set("org1", createDefaultConfig())
_, _ = cache.get("org1")
hits, misses = cache.stats()
if hits != 1 || misses != 1 {
t.Errorf("expected 1 hit and 1 miss, got hits=%d misses=%d", hits, misses)
}
// Multiple hits
_, _ = cache.get("org1")
_, _ = cache.get("org1")
hits, misses = cache.stats()
if hits != 3 || misses != 1 {
t.Errorf("expected 3 hits and 1 miss, got hits=%d misses=%d", hits, misses)
}
// Multiple misses
_, _ = cache.get("org2")
_, _ = cache.get("org3")
hits, misses = cache.stats()
if hits != 3 || misses != 3 {
t.Errorf("expected 3 hits and 3 misses, got hits=%d misses=%d", hits, misses)
}
}
func TestManager_SettersAndGetters(t *testing.T) {
m := New()
// Test SetWorkspaceName
m.SetWorkspaceName("test-workspace")
if m.workspaceName != "test-workspace" {
t.Errorf("expected workspace name 'test-workspace', got %q", m.workspaceName)
}
// Test Domain with no config
domain := m.Domain("unknown-org")
if domain != "" {
t.Errorf("expected empty domain for unknown org, got %q", domain)
}
// Test DailyRemindersEnabled with no config (should default to true)
enabled := m.DailyRemindersEnabled("unknown-org")
if !enabled {
t.Error("expected daily reminders enabled by default")
}
// Test ReminderDMDelay with no config (should return default)
delay := m.ReminderDMDelay("unknown-org", "general")
if delay != defaultReminderDMDelayMinutes {
t.Errorf("expected default delay %d, got %d", defaultReminderDMDelayMinutes, delay)
}
// Test IsChannelMuted with no config
muted := m.IsChannelMuted("unknown-org", "general")
if muted {
t.Error("expected channel not muted when no config exists")
}
}
func TestManager_ConfigWithManualSetup(t *testing.T) {
m := New()
// Manually set a config without loading from GitHub
testConfig := &RepoConfig{
Channels: map[string]struct {
ReminderDMDelay *int `yaml:"reminder_dm_delay"`
Repos []string `yaml:"repos"`
Mute bool `yaml:"mute"`
}{
"dev": {
Repos: []string{"goose", "slacker"},
Mute: false,
},
"muted-channel": {
Repos: []string{"test"},
Mute: true,
},
},
Global: struct {
TeamID string `yaml:"team_id"`
EmailDomain string `yaml:"email_domain"`
ReminderDMDelay int `yaml:"reminder_dm_delay"`
DailyReminders bool `yaml:"daily_reminders"`
}{
TeamID: "T123456",
EmailDomain: "example.com",
ReminderDMDelay: 30,
DailyReminders: false,
},
}
m.mu.Lock()
m.configs["test-org"] = testConfig
m.mu.Unlock()
// Test Domain
domain := m.Domain("test-org")
if domain != "example.com" {
t.Errorf("expected domain 'example.com', got %q", domain)
}
// Test DailyRemindersEnabled
enabled := m.DailyRemindersEnabled("test-org")
if enabled {
t.Error("expected daily reminders disabled")
}
// Test ReminderDMDelay with global setting
delay := m.ReminderDMDelay("test-org", "unknown-channel")
if delay != 30 {
t.Errorf("expected delay 30, got %d", delay)
}
// Test IsChannelMuted
muted := m.IsChannelMuted("test-org", "muted-channel")
if !muted {
t.Error("expected muted-channel to be muted")
}
notMuted := m.IsChannelMuted("test-org", "dev")
if notMuted {
t.Error("expected dev channel not to be muted")
}
// Test WorkspaceName
workspaceName := m.WorkspaceName("test-org")
if workspaceName != "T123456" {
t.Errorf("expected workspace name 'T123456', got %q", workspaceName)
}
}
func TestManager_ReminderDMDelayWithChannelOverride(t *testing.T) {
m := New()
// Create config with channel-specific override
channelOverride := 15
testConfig := &RepoConfig{
Channels: map[string]struct {
ReminderDMDelay *int `yaml:"reminder_dm_delay"`
Repos []string `yaml:"repos"`
Mute bool `yaml:"mute"`
}{
"urgent": {
ReminderDMDelay: &channelOverride,
Repos: []string{"critical-service"},
},
},
Global: struct {
TeamID string `yaml:"team_id"`
EmailDomain string `yaml:"email_domain"`
ReminderDMDelay int `yaml:"reminder_dm_delay"`
DailyReminders bool `yaml:"daily_reminders"`
}{
ReminderDMDelay: 60, // Global default
},
}
m.mu.Lock()
m.configs["test-org"] = testConfig
m.mu.Unlock()
// Test channel-specific override
delay := m.ReminderDMDelay("test-org", "urgent")
if delay != 15 {
t.Errorf("expected channel override delay 15, got %d", delay)
}
// Test fallback to global setting
delay = m.ReminderDMDelay("test-org", "other-channel")
if delay != 60 {
t.Errorf("expected global delay 60, got %d", delay)
}
}
func TestManager_ChannelsForRepoWithWildcard(t *testing.T) {
m := New()
testConfig := &RepoConfig{
Channels: map[string]struct {
ReminderDMDelay *int `yaml:"reminder_dm_delay"`
Repos []string `yaml:"repos"`
Mute bool `yaml:"mute"`
}{
"all-repos": {
Repos: []string{"*"}, // Wildcard matches everything
},
"specific": {
Repos: []string{"goose"},
},
},
}
m.mu.Lock()
m.configs["test-org"] = testConfig
m.mu.Unlock()
// Test wildcard match
channels := m.ChannelsForRepo("test-org", "any-repo")
if len(channels) < 1 {
t.Fatal("expected at least 1 channel for wildcard match")
}
// Should include the wildcard channel
foundWildcard := false
for _, ch := range channels {
if ch == "all-repos" {
foundWildcard = true
break
}
}
if !foundWildcard {
t.Error("expected wildcard channel 'all-repos' to match")
}
// Test specific repo with multiple matches
channels = m.ChannelsForRepo("test-org", "goose")
if len(channels) < 2 {
t.Fatalf("expected at least 2 channels (wildcard + specific), got %d", len(channels))
}
foundSpecific := false
foundWildcard = false
for _, ch := range channels {
if ch == "specific" {
foundSpecific = true
}
if ch == "all-repos" {
foundWildcard = true
}
}
if !foundSpecific || !foundWildcard {
t.Errorf("expected both 'specific' and 'all-repos' channels, got %v", channels)
}
}
func TestManager_ChannelsForRepoWithMuting(t *testing.T) {
m := New()
testConfig := &RepoConfig{
Channels: map[string]struct {
ReminderDMDelay *int `yaml:"reminder_dm_delay"`
Repos []string `yaml:"repos"`
Mute bool `yaml:"mute"`
}{
"active": {
Repos: []string{"goose"},
Mute: false,
},
"muted": {
Repos: []string{"goose"},
Mute: true,
},
"goose": { // Auto-discovered channel can be muted
Mute: true,
},
},
}
m.mu.Lock()
m.configs["test-org"] = testConfig
m.mu.Unlock()
channels := m.ChannelsForRepo("test-org", "goose")
// Should only include active channel, not muted ones
if len(channels) != 1 {
t.Fatalf("expected 1 channel (muted channels should be excluded), got %d: %v", len(channels), channels)
}
if channels[0] != "active" {
t.Errorf("expected 'active' channel, got %q", channels[0])
}
}
func TestManager_CacheStats(t *testing.T) {
m := New()
// Initial stats should be zero
hits, misses := m.CacheStats()
if hits != 0 || misses != 0 {
t.Errorf("expected zero cache stats initially, got hits=%d misses=%d", hits, misses)
}
// Trigger cache operations by setting and getting
m.cache.set("test-org", createDefaultConfig())
_, _ = m.cache.get("test-org")
_, _ = m.cache.get("unknown-org")
hits, misses = m.CacheStats()
if hits != 1 {
t.Errorf("expected 1 cache hit, got %d", hits)
}
if misses != 1 {
t.Errorf("expected 1 cache miss, got %d", misses)
}
}
func TestManager_InvalidateConfig(t *testing.T) {
m := New()
// Set a config in cache
m.cache.set("test-org", createDefaultConfig())
// Verify it's cached
_, found := m.cache.get("test-org")
if !found {
t.Fatal("expected config to be cached")
}
// Invalidate
m.InvalidateConfig("test-org")
// Should be removed from cache
_, found = m.cache.get("test-org")
if found {
t.Error("expected config to be invalidated")
}
}
func TestManager_InvalidateAllConfigs(t *testing.T) {
m := New()
// Set multiple configs in cache
m.cache.set("org1", createDefaultConfig())
m.cache.set("org2", createDefaultConfig())
// Invalidate all
m.InvalidateAllConfigs()
// All should be removed
_, found1 := m.cache.get("org1")
_, found2 := m.cache.get("org2")
if found1 || found2 {
t.Error("expected all configs to be invalidated")
}
}
func TestManager_SetGitHubClientAndConfig(t *testing.T) {
m := New()
// Test Config with no config loaded
cfg, exists := m.Config("test-org")
if exists {
t.Error("expected config not to exist for unknown org")
}
if cfg != nil {
t.Error("expected nil config for unknown org")
}
// Manually set a config
testConfig := createDefaultConfig()
testConfig.Global.TeamID = "T123"
m.mu.Lock()
m.configs["test-org"] = testConfig
m.mu.Unlock()
// Test Config returns the config
cfg, exists = m.Config("test-org")
if !exists {
t.Fatal("expected config to exist after setting")
}
if cfg == nil {
t.Fatal("expected non-nil config")
}
if cfg.Global.TeamID != "T123" {
t.Errorf("expected TeamID T123, got %q", cfg.Global.TeamID)
}
// Test SetGitHubClient (coverage only - behavior tested in LoadConfig tests)
mockClient := &github.Client{}
m.SetGitHubClient("test-org", mockClient)
// Verify client was set
m.mu.RLock()
client := m.clients["test-org"]
m.mu.RUnlock()
if client != mockClient {
t.Error("expected SetGitHubClient to store the client")
}
}
func TestManager_WorkspaceNameEdgeCases(t *testing.T) {
m := New()
// Test WorkspaceName with config that has empty TeamID
emptyConfig := createDefaultConfig()
emptyConfig.Global.TeamID = ""
m.mu.Lock()
m.configs["test-org"] = emptyConfig
m.mu.Unlock()
workspaceName := m.WorkspaceName("test-org")
if workspaceName != "" {
t.Errorf("expected empty workspace name, got %q", workspaceName)
}
}
func TestManager_IsChannelMutedCaseInsensitive(t *testing.T) {
m := New()
testConfig := &RepoConfig{
Channels: map[string]struct {
ReminderDMDelay *int `yaml:"reminder_dm_delay"`
Repos []string `yaml:"repos"`
Mute bool `yaml:"mute"`
}{
"TestChannel": { // Mixed case in config
Mute: true,
},
},
}
m.mu.Lock()
m.configs["test-org"] = testConfig
m.mu.Unlock()
// Exact match (case-sensitive lookup)
muted := m.IsChannelMuted("test-org", "TestChannel")
if !muted {
t.Error("expected TestChannel to be muted")
}
// Different case - won't match (IsChannelMuted is case-sensitive)
notMuted := m.IsChannelMuted("test-org", "testchannel")
if notMuted {
t.Error("expected case-sensitive lookup to not match")
}
}
func TestManager_ReminderDMDelayZeroGlobal(t *testing.T) {
m := New()
testConfig := &RepoConfig{
Channels: map[string]struct {
ReminderDMDelay *int `yaml:"reminder_dm_delay"`
Repos []string `yaml:"repos"`
Mute bool `yaml:"mute"`
}{},
Global: struct {
TeamID string `yaml:"team_id"`
EmailDomain string `yaml:"email_domain"`
ReminderDMDelay int `yaml:"reminder_dm_delay"`
DailyReminders bool `yaml:"daily_reminders"`
}{
ReminderDMDelay: 0, // Explicitly disabled
},
}
m.mu.Lock()
m.configs["test-org"] = testConfig
m.mu.Unlock()
// Should fall back to default when global is 0
delay := m.ReminderDMDelay("test-org", "any-channel")
if delay != defaultReminderDMDelayMinutes {
t.Errorf("expected default delay %d when global is 0, got %d", defaultReminderDMDelayMinutes, delay)
}
}
func TestManager_ReminderDMDelayChannelZero(t *testing.T) {
m := New()
zeroDelay := 0
testConfig := &RepoConfig{
Channels: map[string]struct {
ReminderDMDelay *int `yaml:"reminder_dm_delay"`
Repos []string `yaml:"repos"`
Mute bool `yaml:"mute"`
}{
"urgent": {
ReminderDMDelay: &zeroDelay, // Explicitly disabled for this channel
},
},
Global: struct {
TeamID string `yaml:"team_id"`
EmailDomain string `yaml:"email_domain"`
ReminderDMDelay int `yaml:"reminder_dm_delay"`
DailyReminders bool `yaml:"daily_reminders"`
}{
ReminderDMDelay: 60,
},
}
m.mu.Lock()
m.configs["test-org"] = testConfig
m.mu.Unlock()
// Channel-specific 0 should be returned (not default)
delay := m.ReminderDMDelay("test-org", "urgent")
if delay != 0 {
t.Errorf("expected channel delay 0 (disabled), got %d", delay)
}
}
func TestManager_ChannelsForRepoNoConfig(t *testing.T) {
m := New()
// No config loaded - should auto-discover
channels := m.ChannelsForRepo("test-org", "goose")
if len(channels) != 1 {
t.Fatalf("expected 1 auto-discovered channel, got %d", len(channels))
}
if channels[0] != "goose" {
t.Errorf("expected auto-discovered channel 'goose', got %q", channels[0])
}
}
func TestManager_LoadConfigNoClient(t *testing.T) {
m := New()
ctx := context.Background()
// LoadConfig should fail if no GitHub client is set
err := m.LoadConfig(ctx, "test-org")
if err == nil {
t.Error("expected error when GitHub client not set")
}
if err != nil && !contains(err.Error(), "github client not initialized") {
t.Errorf("expected 'github client not initialized' error, got %v", err)
}
}
func TestManager_LoadConfigFromCache(t *testing.T) {
m := New()
ctx := context.Background()
// Pre-populate cache
cachedConfig := createDefaultConfig()
cachedConfig.Global.TeamID = "T999"
m.cache.set("test-org", cachedConfig)
// LoadConfig should use cached config (no GitHub client needed)
err := m.LoadConfig(ctx, "test-org")
if err != nil {
t.Fatalf("unexpected error loading from cache: %v", err)
}
// Verify config was loaded from cache
cfg, exists := m.Config("test-org")
if !exists {
t.Fatal("expected config to exist")
}
if cfg.Global.TeamID != "T999" {
t.Errorf("expected cached TeamID T999, got %q", cfg.Global.TeamID)
}
// Verify cache stats show a hit
hits, _ := m.CacheStats()
if hits < 1 {
t.Error("expected at least 1 cache hit")
}
}
func TestManager_ReloadConfig(t *testing.T) {
m := New()
ctx := context.Background()
// Pre-populate cache
oldConfig := createDefaultConfig()
oldConfig.Global.TeamID = "T111"
m.cache.set("test-org", oldConfig)
// Verify config is in cache
_, found := m.cache.get("test-org")
if !found {
t.Fatal("expected config to be in cache")
}
// ReloadConfig should invalidate cache and call LoadConfig
// Since we don't have a GitHub client, this will fail
err := m.ReloadConfig(ctx, "test-org")
if err == nil {
t.Error("expected error when reloading without GitHub client")
}
// Verify cache was invalidated (will be a cache miss now)
cfg, found := m.cache.get("test-org")
if found && cfg != nil && cfg.Global.TeamID == "T111" {
t.Error("expected cache to be invalidated by ReloadConfig")
}
}
// Helper function to check if a string contains a substring.
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(substr) == 0 ||
(len(s) > 0 && len(substr) > 0 && indexOf(s, substr) >= 0))
}
func indexOf(s, substr string) int {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return i
}
}
return -1
}
// createTestGitHubClient creates a GitHub client with a mock HTTP server.
func createTestGitHubClient(handler http.HandlerFunc) (*github.Client, *httptest.Server) {
server := httptest.NewServer(handler)
client := github.NewClient(nil)
client.BaseURL = must(client.BaseURL.Parse(server.URL + "/"))
return client, server
}
func must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
func TestManager_LoadConfigValidYAML(t *testing.T) {
validYAML := `
global:
team_id: T123456
email_domain: example.com
reminder_dm_delay: 30
daily_reminders: true
channels:
dev:
repos:
- goose
- slacker
all:
repos:
- "*"
`
handler := func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/repos/test-org/.codeGROOVE/contents/slack.yaml" {
content := base64.StdEncoding.EncodeToString([]byte(validYAML))
encoding := "base64"
response := github.RepositoryContent{
Type: github.String("file"),
Content: &content,
Encoding: &encoding,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
return
}
http.NotFound(w, r)
}
client, server := createTestGitHubClient(handler)
defer server.Close()
m := New()
m.SetGitHubClient("test-org", client)
ctx := context.Background()
err := m.LoadConfig(ctx, "test-org")
if err != nil {
t.Fatalf("unexpected error loading valid config: %v", err)
}
// Verify config was loaded
cfg, exists := m.Config("test-org")
if !exists {
t.Fatal("expected config to exist after loading")
}
if cfg.Global.TeamID != "T123456" {
t.Errorf("expected TeamID T123456, got %q", cfg.Global.TeamID)
}
if cfg.Global.EmailDomain != "example.com" {
t.Errorf("expected email domain example.com, got %q", cfg.Global.EmailDomain)
}
if cfg.Global.ReminderDMDelay != 30 {
t.Errorf("expected reminder delay 30, got %d", cfg.Global.ReminderDMDelay)
}
if len(cfg.Channels) != 2 {
t.Errorf("expected 2 channels, got %d", len(cfg.Channels))
}
}