-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_interaction_test.go
More file actions
338 lines (293 loc) · 9.47 KB
/
Copy pathcommand_interaction_test.go
File metadata and controls
338 lines (293 loc) · 9.47 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
package chat_test
import (
"context"
"errors"
"net/http"
"sync"
"testing"
"time"
"github.com/coder/chat"
)
func newCommandRuntime(t *testing.T, state chat.State, adapter chat.Adapter) *chat.Chat {
t.Helper()
bot, err := chat.New(context.Background(),
chat.WithState(state),
chat.WithAdapter(adapter),
chat.WithRuntimeOptions(chat.RuntimeOptions{
DedupeTTL: time.Hour,
ThreadLockTTL: time.Hour,
Concurrency: chat.ConcurrencyDrop,
}),
)
if err != nil {
t.Fatalf("new runtime: %v", err)
}
return bot
}
func commandEvent(id string, threadID chat.ThreadID) chat.Event {
return chat.Event{
ID: id,
Adapter: "fake",
Tenant: "tenant",
ThreadID: threadID,
Command: &chat.Command{
Name: "/deploy",
Text: "staging now",
Args: []string{"staging", "now"},
Actor: chat.Actor{Adapter: "fake", Tenant: "tenant", ID: "user-1", BotKind: chat.BotHuman},
},
}
}
func interactionEvent(id string, threadID chat.ThreadID) chat.Event {
return chat.Event{
ID: id,
Adapter: "fake",
Tenant: "tenant",
ThreadID: threadID,
Interaction: &chat.Interaction{
Kind: chat.InteractionBlockAction,
ActionID: "approve",
Actor: chat.Actor{Adapter: "fake", Tenant: "tenant", ID: "user-1", BotKind: chat.BotHuman},
},
}
}
func TestCommandRoutesToOnCommandNeverMessageHooks(t *testing.T) {
t.Parallel()
bot := newCommandRuntime(t, newFakeState(), newFakeAdapter("fake"))
var commands int
bot.OnCommand(func(ctx context.Context, ev *chat.CommandEvent) error {
commands++
if ev.Command.Name != "/deploy" {
t.Fatalf("command name = %q", ev.Command.Name)
}
if ev.Event.ID != "cmd-1" {
t.Fatalf("event id = %q", ev.Event.ID)
}
if ev.Thread.ID() != "fake:v1:thread-1" {
t.Fatalf("thread id = %q", ev.Thread.ID())
}
if len(ev.Command.Args) != 2 {
t.Fatalf("args = %#v", ev.Command.Args)
}
return nil
})
bot.OnNewMention(func(context.Context, *chat.MessageEvent) error {
t.Fatal("command must not route to OnNewMention")
return nil
})
bot.OnSubscribedMessage(func(context.Context, *chat.MessageEvent) error {
t.Fatal("command must not route to OnSubscribedMessage")
return nil
})
bot.OnInteraction(func(context.Context, *chat.InteractionEvent) error {
t.Fatal("command must not route to OnInteraction")
return nil
})
if status := postEvent(t, bot, "fake", commandEvent("cmd-1", "fake:v1:thread-1")); status != http.StatusOK {
t.Fatalf("status = %d", status)
}
if commands != 1 {
t.Fatalf("commands = %d", commands)
}
}
func TestCommandInSubscribedThreadStillRoutesToOnCommand(t *testing.T) {
t.Parallel()
state := newFakeState()
bot := newCommandRuntime(t, state, newFakeAdapter("fake"))
if err := state.SubscribeThread(context.Background(), "fake:v1:thread-1"); err != nil {
t.Fatalf("subscribe: %v", err)
}
var commands int
bot.OnCommand(func(context.Context, *chat.CommandEvent) error {
commands++
return nil
})
bot.OnSubscribedMessage(func(context.Context, *chat.MessageEvent) error {
t.Fatal("command in subscribed thread must still route to OnCommand")
return nil
})
if status := postEvent(t, bot, "fake", commandEvent("cmd-sub", "fake:v1:thread-1")); status != http.StatusOK {
t.Fatalf("status = %d", status)
}
if commands != 1 {
t.Fatalf("commands = %d", commands)
}
}
func TestInteractionRoutesToOnInteraction(t *testing.T) {
t.Parallel()
bot := newCommandRuntime(t, newFakeState(), newFakeAdapter("fake"))
var interactions int
bot.OnInteraction(func(ctx context.Context, ev *chat.InteractionEvent) error {
interactions++
if ev.Interaction.ActionID != "approve" {
t.Fatalf("action id = %q", ev.Interaction.ActionID)
}
if ev.Interaction.Kind != chat.InteractionBlockAction {
t.Fatalf("kind = %v", ev.Interaction.Kind)
}
if ev.Thread.ID() != "fake:v1:thread-1" {
t.Fatalf("thread id = %q", ev.Thread.ID())
}
return nil
})
bot.OnNewMention(func(context.Context, *chat.MessageEvent) error {
t.Fatal("interaction must not route to a message hook")
return nil
})
if status := postEvent(t, bot, "fake", interactionEvent("int-1", "fake:v1:thread-1")); status != http.StatusOK {
t.Fatalf("status = %d", status)
}
if interactions != 1 {
t.Fatalf("interactions = %d", interactions)
}
}
func TestUnsetCommandAndInteractionHandlersAreNoOpAcks(t *testing.T) {
t.Parallel()
bot := newCommandRuntime(t, newFakeState(), newFakeAdapter("fake"))
if status := postEvent(t, bot, "fake", commandEvent("cmd-noop", "fake:v1:thread-1")); status != http.StatusOK {
t.Fatalf("command no-op status = %d", status)
}
if status := postEvent(t, bot, "fake", interactionEvent("int-noop", "fake:v1:thread-2")); status != http.StatusOK {
t.Fatalf("interaction no-op status = %d", status)
}
}
func TestEventWithNoPayloadStaysIgnored(t *testing.T) {
t.Parallel()
bot := newCommandRuntime(t, newFakeState(), newFakeAdapter("fake"))
bot.OnCommand(func(context.Context, *chat.CommandEvent) error {
t.Fatal("payload-less event must not route to OnCommand")
return nil
})
bot.OnInteraction(func(context.Context, *chat.InteractionEvent) error {
t.Fatal("payload-less event must not route to OnInteraction")
return nil
})
ev := chat.Event{ID: "empty", Adapter: "fake", Tenant: "tenant", ThreadID: "fake:v1:thread-1"}
if status := postEvent(t, bot, "fake", ev); status != http.StatusOK {
t.Fatalf("status = %d", status)
}
}
func TestCommandHandlerAtomicReplaceRaceSafe(t *testing.T) {
t.Parallel()
bot := newCommandRuntime(t, newFakeState(), newFakeAdapter("fake"))
var mu sync.Mutex
var routed []string
bot.OnCommand(func(ctx context.Context, ev *chat.CommandEvent) error {
mu.Lock()
routed = append(routed, "first:"+ev.Event.ID)
mu.Unlock()
return nil
})
bot.OnCommand(func(ctx context.Context, ev *chat.CommandEvent) error {
mu.Lock()
routed = append(routed, "replacement:"+ev.Event.ID)
mu.Unlock()
return nil
})
if status := postEvent(t, bot, "fake", commandEvent("cmd-replace", "fake:v1:thread-1")); status != http.StatusOK {
t.Fatalf("status = %d", status)
}
mu.Lock()
defer mu.Unlock()
if len(routed) != 1 || routed[0] != "replacement:cmd-replace" {
t.Fatalf("routed = %#v, want only the replacement", routed)
}
}
func TestCommandAckedOnHandlerErrorAndDedupedAndSelfFiltered(t *testing.T) {
t.Parallel()
state := newFakeState()
adapter := newFakeAdapter("fake")
bot := newCommandRuntime(t, state, adapter)
var calls int
bot.OnCommand(func(context.Context, *chat.CommandEvent) error {
calls++
return errors.New("command handler error")
})
event := commandEvent("cmd-err", "fake:v1:thread-1")
if status := postEvent(t, bot, "fake", event); status != http.StatusOK {
t.Fatalf("handler-error status = %d", status)
}
// Duplicate by Event Identity: handler runs once.
if status := postEvent(t, bot, "fake", event); status != http.StatusOK {
t.Fatalf("duplicate status = %d", status)
}
// Self command (bot-issued) is filtered: no handler call, still acked.
self := commandEvent("cmd-self", "fake:v1:thread-self")
self.Command.Actor = adapter.BotActor()
if status := postEvent(t, bot, "fake", self); status != http.StatusOK {
t.Fatalf("self command status = %d", status)
}
if calls != 1 {
t.Fatalf("calls = %d, want 1", calls)
}
}
func TestInteractionSelfFilteredAndLockConflictDropped(t *testing.T) {
t.Parallel()
state := newFakeState()
adapter := newFakeAdapter("fake")
bot := newCommandRuntime(t, state, adapter)
var calls int
bot.OnInteraction(func(context.Context, *chat.InteractionEvent) error {
calls++
return nil
})
// Self interaction filtered.
self := interactionEvent("int-self", "fake:v1:thread-self")
self.Interaction.Actor = adapter.BotActor()
if status := postEvent(t, bot, "fake", self); status != http.StatusOK {
t.Fatalf("self interaction status = %d", status)
}
// Lock conflict: hold the lock, then post; acknowledge-and-drop.
lease, acquired, err := state.AcquireLock(context.Background(), "fake:v1:thread-conflict", time.Hour)
if err != nil || !acquired {
t.Fatalf("acquire conflict lock acquired=%v err=%v", acquired, err)
}
if status := postEvent(t, bot, "fake", interactionEvent("int-conflict", "fake:v1:thread-conflict")); status != http.StatusOK {
t.Fatalf("conflict status = %d", status)
}
if _, err := state.ReleaseLock(context.Background(), lease); err != nil {
t.Fatalf("release: %v", err)
}
if calls != 0 {
t.Fatalf("calls = %d, want 0", calls)
}
}
func TestCommandRoutesThroughDeferredDetachedTail(t *testing.T) {
t.Parallel()
state := newFakeState()
adapter := newFakeAdapter("fake")
bot := newDeferredRuntime(t, state, adapter)
handlerStarted := make(chan struct{})
releaseHandler := make(chan struct{})
handlerDone := make(chan struct{})
bot.OnCommand(func(ctx context.Context, ev *chat.CommandEvent) error {
close(handlerStarted)
<-releaseHandler
// State mutation under the detached work context must still succeed.
if err := ev.Thread.Subscribe(ctx); err != nil {
return err
}
close(handlerDone)
return nil
})
if status := postEvent(t, bot, "fake", commandEvent("cmd-deferred", "fake:v1:thread-1")); status != http.StatusOK {
t.Fatalf("status = %d", status)
}
select {
case <-handlerStarted:
case <-time.After(time.Second):
t.Fatal("deferred command handler did not start")
}
// Ack returned before the handler completed (ack-then-work).
select {
case <-handlerDone:
t.Fatal("handler completed before ack returned")
default:
}
close(releaseHandler)
select {
case <-handlerDone:
case <-time.After(time.Second):
t.Fatal("deferred command handler did not finish")
}
}