-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnector_builder_test.go
More file actions
272 lines (251 loc) · 8.59 KB
/
connector_builder_test.go
File metadata and controls
272 lines (251 loc) · 8.59 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
package agentremote
import (
"context"
"errors"
"sync"
"testing"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix/bridgev2/networkid"
"maunium.net/go/mautrix/event"
)
func TestConnectorBaseHookOrder(t *testing.T) {
var order []string
wantBridge := &bridgev2.Bridge{}
conn := NewConnector(ConnectorSpec{
Init: func(got *bridgev2.Bridge) {
if got != wantBridge {
t.Fatalf("expected init hook bridge %p, got %p", wantBridge, got)
}
order = append(order, "init")
},
Start: func(_ context.Context, got *bridgev2.Bridge) error {
if got != wantBridge {
t.Fatalf("expected start hook bridge %p, got %p", wantBridge, got)
}
order = append(order, "start")
return nil
},
Stop: func(_ context.Context, got *bridgev2.Bridge) {
if got != wantBridge {
t.Fatalf("expected stop hook bridge %p, got %p", wantBridge, got)
}
order = append(order, "stop")
},
})
conn.Init(wantBridge)
if err := conn.Start(context.Background()); err != nil {
t.Fatalf("start returned error: %v", err)
}
conn.Stop(context.Background())
want := []string{"init", "start", "stop"}
for i, step := range want {
if len(order) <= i || order[i] != step {
t.Fatalf("expected order %v, got %v", want, order)
}
}
}
func TestConnectorBaseLoginFlowsAndCreation(t *testing.T) {
expected := &fakeLoginProcess{}
conn := NewConnector(ConnectorSpec{
LoginFlows: func() []bridgev2.LoginFlow {
return []bridgev2.LoginFlow{{ID: "flow"}}
},
CreateLogin: func(context.Context, *bridgev2.User, string) (bridgev2.LoginProcess, error) {
return expected, nil
},
})
flows := conn.GetLoginFlows()
if len(flows) != 1 || flows[0].ID != "flow" {
t.Fatalf("unexpected login flows: %#v", flows)
}
got, err := conn.CreateLogin(context.Background(), &bridgev2.User{}, "flow")
if err != nil {
t.Fatalf("create login returned error: %v", err)
}
if got != expected {
t.Fatalf("expected %T, got %T", expected, got)
}
}
func TestTypedClientLoaderReusesAndRebuilds(t *testing.T) {
var mu sync.Mutex
clients := map[networkid.UserLoginID]bridgev2.NetworkAPI{}
created := 0
reused := 0
loader := TypedClientLoader(TypedClientLoaderSpec[*fakeClient]{
Accept: func(*bridgev2.UserLogin) (bool, string) { return true, "" },
LoadUserLoginConfig: LoadUserLoginConfig[*fakeClient]{
Mu: &mu,
Clients: clients,
BridgeName: "fake",
Update: func(c *fakeClient, _ *bridgev2.UserLogin) {
reused++
},
Create: func(*bridgev2.UserLogin) (*fakeClient, error) {
created++
return &fakeClient{}, nil
},
},
})
login := &bridgev2.UserLogin{UserLogin: &database.UserLogin{ID: "same"}}
if err := loader(context.Background(), login); err != nil {
t.Fatalf("first load returned error: %v", err)
}
if err := loader(context.Background(), login); err != nil {
t.Fatalf("second load returned error: %v", err)
}
if created != 1 {
t.Fatalf("expected 1 create, got %d", created)
}
if reused == 0 {
t.Fatalf("expected reuse callback to run")
}
clients[login.ID] = &fakeOtherClient{}
if err := loader(context.Background(), login); err != nil {
t.Fatalf("rebuild load returned error: %v", err)
}
if created != 2 {
t.Fatalf("expected rebuild to create second client, got %d creates", created)
}
}
func TestTypedClientLoaderAssignsBrokenLoginOnRejectedLogin(t *testing.T) {
loader := TypedClientLoader(TypedClientLoaderSpec[*fakeClient]{
Accept: func(*bridgev2.UserLogin) (bool, string) {
return false, "nope"
},
LoadUserLoginConfig: LoadUserLoginConfig[*fakeClient]{},
})
login := &bridgev2.UserLogin{UserLogin: &database.UserLogin{ID: "broken"}}
if err := loader(context.Background(), login); err != nil {
t.Fatalf("loader returned error: %v", err)
}
if _, ok := login.Client.(*BrokenLoginClient); !ok {
t.Fatalf("expected broken login client, got %T", login.Client)
}
}
func TestTypedClientLoaderUsesClientMapReferenceWhenInitialCacheIsNil(t *testing.T) {
var mu sync.Mutex
var clients map[networkid.UserLoginID]bridgev2.NetworkAPI
EnsureClientMap(&mu, &clients)
loader := TypedClientLoader(TypedClientLoaderSpec[*fakeClient]{
Accept: func(*bridgev2.UserLogin) (bool, string) { return true, "" },
LoadUserLoginConfig: LoadUserLoginConfig[*fakeClient]{
Mu: &mu,
ClientsRef: &clients,
BridgeName: "fake",
Create: func(*bridgev2.UserLogin) (*fakeClient, error) {
return &fakeClient{}, nil
},
},
})
login := &bridgev2.UserLogin{UserLogin: &database.UserLogin{ID: "login-ref"}}
if err := loader(context.Background(), login); err != nil {
t.Fatalf("loader returned error: %v", err)
}
if clients[login.ID] == nil {
t.Fatalf("expected client to be cached through ClientsRef")
}
}
func TestConnectorStopCanDisconnectCachedClients(t *testing.T) {
var mu sync.Mutex
clients := map[networkid.UserLoginID]bridgev2.NetworkAPI{
"a": &fakeClient{},
"b": &fakeClient{},
}
conn := NewConnector(ConnectorSpec{
Stop: func(context.Context, *bridgev2.Bridge) {
StopClients(&mu, &clients)
},
})
conn.Stop(context.Background())
for id, client := range clients {
fc := client.(*fakeClient)
if !fc.disconnected {
t.Fatalf("expected client %s to disconnect", id)
}
}
}
func TestConnectorBaseDefaultsBridgeInfoAndCapabilities(t *testing.T) {
conn := NewConnector(ConnectorSpec{ProtocolID: "ai-test"})
caps := conn.GetCapabilities()
if caps == nil || !caps.DisappearingMessages {
t.Fatalf("expected default capabilities, got %#v", caps)
}
infoVer, capVer := conn.GetBridgeInfoVersion()
wantInfo, wantCap := DefaultBridgeInfoVersion()
if infoVer != wantInfo || capVer != wantCap {
t.Fatalf("expected versions %d/%d, got %d/%d", wantInfo, wantCap, infoVer, capVer)
}
portal := &bridgev2.Portal{Portal: &database.Portal{RoomType: database.RoomTypeDM}}
content := &event.BridgeEventContent{}
conn.FillPortalBridgeInfo(portal, content)
if content.Protocol.ID != "ai-test" {
t.Fatalf("expected protocol id ai-test, got %q", content.Protocol.ID)
}
if content.BeeperRoomTypeV2 != "dm" {
t.Fatalf("expected dm bridge room type, got %q", content.BeeperRoomTypeV2)
}
}
type fakeClient struct {
disconnected bool
}
func (c *fakeClient) Connect(context.Context) {}
func (c *fakeClient) Disconnect() { c.disconnected = true }
func (c *fakeClient) IsLoggedIn() bool { return true }
func (c *fakeClient) LogoutRemote(context.Context) {}
func (c *fakeClient) IsThisUser(context.Context, networkid.UserID) bool { return false }
func (c *fakeClient) GetChatInfo(context.Context, *bridgev2.Portal) (*bridgev2.ChatInfo, error) {
return nil, nil
}
func (c *fakeClient) GetUserInfo(context.Context, *bridgev2.Ghost) (*bridgev2.UserInfo, error) {
return nil, nil
}
func (c *fakeClient) GetCapabilities(context.Context, *bridgev2.Portal) *event.RoomFeatures {
return &event.RoomFeatures{}
}
func (c *fakeClient) HandleMatrixMessage(context.Context, *bridgev2.MatrixMessage) (*bridgev2.MatrixMessageResponse, error) {
return nil, nil
}
type fakeOtherClient struct{ fakeClient }
type fakeLoginProcess struct{}
func (*fakeLoginProcess) Start(context.Context) (*bridgev2.LoginStep, error) { return nil, nil }
func (*fakeLoginProcess) Cancel() {}
var _ bridgev2.NetworkAPI = (*fakeClient)(nil)
func TestTypedClientLoaderPropagatesCreateErrorViaBrokenLogin(t *testing.T) {
loader := TypedClientLoader(TypedClientLoaderSpec[*fakeClient]{
Accept: func(*bridgev2.UserLogin) (bool, string) { return true, "" },
LoadUserLoginConfig: LoadUserLoginConfig[*fakeClient]{
BridgeName: "fake",
Create: func(*bridgev2.UserLogin) (*fakeClient, error) {
return nil, errors.New("boom")
},
},
})
login := &bridgev2.UserLogin{UserLogin: &database.UserLogin{ID: "broken-create"}}
if err := loader(context.Background(), login); err != nil {
t.Fatalf("loader returned error: %v", err)
}
if _, ok := login.Client.(*BrokenLoginClient); !ok {
t.Fatalf("expected broken login after create failure, got %T", login.Client)
}
}
func TestClientBaseBackgroundContextFallsBackToBackground(t *testing.T) {
var base ClientBase
var nilCtx context.Context
got := base.BackgroundContext(nilCtx)
if got == nil {
t.Fatal("expected non-nil context")
}
}
func TestClientBaseTracksLogin(t *testing.T) {
var base ClientBase
login := &bridgev2.UserLogin{UserLogin: &database.UserLogin{ID: "user"}}
base.SetUserLogin(login)
if base.GetUserLogin() != login {
t.Fatalf("expected stored login to match input")
}
}
var (
_ bridgev2.LoginProcess = (*fakeLoginProcess)(nil)
_ bridgev2.NetworkAPI = (*fakeOtherClient)(nil)
)