|
1
|
package warden_test |
|
2
|
|
|
3
|
import ( |
|
4
|
"testing" |
|
5
|
"time" |
|
6
|
|
|
7
|
"github.com/conflicthq/scuttlebot/internal/bots/warden" |
|
8
|
) |
|
9
|
|
|
10
|
func newBot() *warden.Bot { |
|
11
|
return warden.New("localhost:6667", "pass", nil, |
|
12
|
map[string]warden.ChannelConfig{ |
|
13
|
"#fleet": {MessagesPerSecond: 5, Burst: 10, CoolDown: 60 * time.Second}, |
|
14
|
}, |
|
15
|
warden.ChannelConfig{MessagesPerSecond: 2, Burst: 5}, |
|
16
|
nil, |
|
17
|
) |
|
18
|
} |
|
19
|
|
|
20
|
func TestBotName(t *testing.T) { |
|
21
|
b := newBot() |
|
22
|
if b.Name() != "warden" { |
|
23
|
t.Errorf("Name(): got %q", b.Name()) |
|
24
|
} |
|
25
|
} |
|
26
|
|
|
27
|
func TestBotNew(t *testing.T) { |
|
28
|
b := newBot() |
|
29
|
if b == nil { |
|
30
|
t.Fatal("expected non-nil bot") |
|
31
|
} |
|
32
|
} |
|
33
|
|
|
34
|
func TestChannelConfigDefaults(t *testing.T) { |
|
35
|
// Zero-value config should get sane defaults applied. |
|
36
|
b := warden.New("localhost:6667", "pass", nil, |
|
37
|
nil, |
|
38
|
warden.ChannelConfig{}, // zero — should default |
|
39
|
nil, |
|
40
|
) |
|
41
|
if b == nil { |
|
42
|
t.Fatal("expected non-nil bot") |
|
43
|
} |
|
44
|
} |
|
45
|
|
|
46
|
func TestRateLimiterTokenBucket(t *testing.T) { |
|
47
|
// We test the rate limiter logic indirectly through the ChannelConfig. |
|
48
|
// The key invariant: high burst allows initial traffic, sustained rate is enforced. |
|
49
|
// Full rate-limit enforcement requires IRC wiring; this tests construction. |
|
50
|
cfg := warden.ChannelConfig{ |
|
51
|
MessagesPerSecond: 10, |
|
52
|
Burst: 20, |
|
53
|
CoolDown: 30 * time.Second, |
|
54
|
} |
|
55
|
b := warden.New("localhost:6667", "pass", nil, |
|
56
|
map[string]warden.ChannelConfig{"#fleet": cfg}, |
|
57
|
warden.ChannelConfig{}, |
|
58
|
nil, |
|
59
|
) |
|
60
|
if b == nil { |
|
61
|
t.Fatal("expected non-nil bot") |
|
62
|
} |
|
63
|
} |
|
64
|
|
|
65
|
func TestActionConstants(t *testing.T) { |
|
66
|
// Ensure action constants are stable. |
|
67
|
actions := map[warden.Action]string{ |
|
68
|
warden.ActionWarn: "warn", |
|
69
|
warden.ActionMute: "mute", |
|
70
|
warden.ActionKick: "kick", |
|
71
|
} |
|
72
|
for action, want := range actions { |
|
73
|
if string(action) != want { |
|
74
|
t.Errorf("Action constant: got %q, want %q", action, want) |
|
75
|
} |
|
76
|
} |
|
77
|
} |
|
78
|
|