ScuttleBot

scuttlebot / internal / api / chat_test.go
Blame History Raw 107 lines
1
package api
2
3
import (
4
"bytes"
5
"context"
6
"encoding/json"
7
"io"
8
"log/slog"
9
"net/http"
10
"net/http/httptest"
11
"testing"
12
13
"github.com/conflicthq/scuttlebot/internal/auth"
14
"github.com/conflicthq/scuttlebot/internal/bots/bridge"
15
"github.com/conflicthq/scuttlebot/internal/registry"
16
)
17
18
type stubChatBridge struct {
19
touched []struct {
20
channel string
21
nick string
22
}
23
}
24
25
func (b *stubChatBridge) Channels() []string { return nil }
26
func (b *stubChatBridge) JoinChannel(string) {}
27
func (b *stubChatBridge) LeaveChannel(string) {}
28
func (b *stubChatBridge) Messages(string) []bridge.Message { return nil }
29
func (b *stubChatBridge) Subscribe(string) (<-chan bridge.Message, func()) {
30
return make(chan bridge.Message), func() {}
31
}
32
func (b *stubChatBridge) Send(context.Context, string, string, string) error { return nil }
33
func (b *stubChatBridge) SendWithMeta(_ context.Context, _, _, _ string, _ *bridge.Meta) error {
34
return nil
35
}
36
func (b *stubChatBridge) Stats() bridge.Stats { return bridge.Stats{} }
37
func (b *stubChatBridge) Users(string) []string { return nil }
38
func (b *stubChatBridge) UsersWithModes(string) []bridge.UserInfo { return nil }
39
func (b *stubChatBridge) ChannelModes(string) string { return "" }
40
func (b *stubChatBridge) RefreshNames(string) {}
41
func (b *stubChatBridge) TouchUser(channel, nick string) {
42
b.touched = append(b.touched, struct{ channel, nick string }{channel: channel, nick: nick})
43
}
44
45
func TestHandleChannelPresence(t *testing.T) {
46
t.Helper()
47
48
bridgeStub := &stubChatBridge{}
49
reg := registry.New(nil, []byte("test-signing-key"))
50
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
51
srv := httptest.NewServer(New(reg, auth.TestStore("token"), bridgeStub, nil, nil, nil, nil, nil, "", logger).Handler())
52
defer srv.Close()
53
54
body, _ := json.Marshal(map[string]string{"nick": "codex-test"})
55
req, err := http.NewRequest(http.MethodPost, srv.URL+"/v1/channels/general/presence", bytes.NewReader(body))
56
if err != nil {
57
t.Fatal(err)
58
}
59
req.Header.Set("Authorization", "Bearer token")
60
req.Header.Set("Content-Type", "application/json")
61
62
resp, err := http.DefaultClient.Do(req)
63
if err != nil {
64
t.Fatal(err)
65
}
66
defer resp.Body.Close()
67
if resp.StatusCode != http.StatusNoContent {
68
t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusNoContent)
69
}
70
if len(bridgeStub.touched) != 1 {
71
t.Fatalf("TouchUser calls = %d, want 1", len(bridgeStub.touched))
72
}
73
if bridgeStub.touched[0].channel != "#general" || bridgeStub.touched[0].nick != "codex-test" {
74
t.Fatalf("TouchUser args = %#v", bridgeStub.touched[0])
75
}
76
}
77
78
func TestHandleChannelPresenceRequiresNick(t *testing.T) {
79
t.Helper()
80
81
bridgeStub := &stubChatBridge{}
82
reg := registry.New(nil, []byte("test-signing-key"))
83
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
84
srv := httptest.NewServer(New(reg, auth.TestStore("token"), bridgeStub, nil, nil, nil, nil, nil, "", logger).Handler())
85
defer srv.Close()
86
87
body, _ := json.Marshal(map[string]string{})
88
req, err := http.NewRequest(http.MethodPost, srv.URL+"/v1/channels/general/presence", bytes.NewReader(body))
89
if err != nil {
90
t.Fatal(err)
91
}
92
req.Header.Set("Authorization", "Bearer token")
93
req.Header.Set("Content-Type", "application/json")
94
95
resp, err := http.DefaultClient.Do(req)
96
if err != nil {
97
t.Fatal(err)
98
}
99
defer resp.Body.Close()
100
if resp.StatusCode != http.StatusBadRequest {
101
t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusBadRequest)
102
}
103
if len(bridgeStub.touched) != 0 {
104
t.Fatalf("TouchUser calls = %d, want 0", len(bridgeStub.touched))
105
}
106
}
107

Keyboard Shortcuts

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