ScuttleBot

scuttlebot / pkg / client / client_test.go
Blame History Raw 114 lines
1
package client_test
2
3
import (
4
"context"
5
"testing"
6
"time"
7
8
"github.com/conflicthq/scuttlebot/pkg/client"
9
"github.com/conflicthq/scuttlebot/pkg/protocol"
10
)
11
12
func TestNewValidation(t *testing.T) {
13
tests := []struct {
14
name string
15
opts client.Options
16
}{
17
{"missing server", client.Options{Nick: "a", Password: "p"}},
18
{"missing nick", client.Options{ServerAddr: "localhost:6667", Password: "p"}},
19
{"missing password", client.Options{ServerAddr: "localhost:6667", Nick: "a"}},
20
}
21
for _, tt := range tests {
22
t.Run(tt.name, func(t *testing.T) {
23
if _, err := client.New(tt.opts); err == nil {
24
t.Error("expected error, got nil")
25
}
26
})
27
}
28
}
29
30
func TestNewOK(t *testing.T) {
31
c, err := client.New(client.Options{
32
ServerAddr: "localhost:6667",
33
Nick: "agent-01",
34
Password: "secret",
35
})
36
if err != nil {
37
t.Fatalf("unexpected error: %v", err)
38
}
39
if c == nil {
40
t.Fatal("expected non-nil client")
41
}
42
}
43
44
func TestHandleRegistration(t *testing.T) {
45
c, _ := client.New(client.Options{
46
ServerAddr: "localhost:6667",
47
Nick: "agent-01",
48
Password: "secret",
49
})
50
51
called := make(chan string, 1)
52
c.Handle("task.create", func(ctx context.Context, env *protocol.Envelope) error {
53
called <- env.Type
54
return nil
55
})
56
57
// Verify handler is registered by checking it doesn't panic.
58
// Dispatch is tested indirectly via the exported Handle API.
59
select {
60
case <-called:
61
t.Error("handler fired unexpectedly before any message")
62
default:
63
}
64
}
65
66
func TestSendNotConnected(t *testing.T) {
67
c, _ := client.New(client.Options{
68
ServerAddr: "localhost:6667",
69
Nick: "agent-01",
70
Password: "secret",
71
})
72
73
err := c.Send(context.Background(), "#fleet", "task.create", map[string]string{"task": "x"})
74
if err == nil {
75
t.Error("expected error when not connected, got nil")
76
}
77
}
78
79
func TestRunCancelledImmediately(t *testing.T) {
80
c, _ := client.New(client.Options{
81
ServerAddr: "localhost:6667",
82
Nick: "agent-01",
83
Password: "secret",
84
})
85
86
ctx, cancel := context.WithCancel(context.Background())
87
cancel() // cancel immediately
88
89
done := make(chan error, 1)
90
go func() { done <- c.Run(ctx) }()
91
92
select {
93
case err := <-done:
94
if err != nil {
95
t.Errorf("Run returned error: %v", err)
96
}
97
case <-time.After(2 * time.Second):
98
t.Error("Run did not return after ctx cancelled")
99
}
100
}
101
102
func TestWildcardHandler(t *testing.T) {
103
// Verify that registering "*" doesn't panic and multiple handlers stack.
104
c, _ := client.New(client.Options{
105
ServerAddr: "localhost:6667",
106
Nick: "agent-01",
107
Password: "secret",
108
})
109
110
c.Handle("*", func(ctx context.Context, env *protocol.Envelope) error { return nil })
111
c.Handle("*", func(ctx context.Context, env *protocol.Envelope) error { return nil })
112
c.Handle("task.create", func(ctx context.Context, env *protocol.Envelope) error { return nil })
113
}
114

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button