Repository navigation
Expand file tree
/
Copy pathrpc_queue_e2e_test.go
More file actions
204 lines (181 loc) · 6.54 KB
/
Copy pathrpc_queue_e2e_test.go
File metadata and controls
204 lines (181 loc) · 6.54 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
package e2e
import (
"strings"
"testing"
"time"
copilot "github.com/github/copilot-sdk/go"
"github.com/github/copilot-sdk/go/internal/e2e/testharness"
"github.com/github/copilot-sdk/go/rpc"
)
// Mirrors dotnet/test/E2E/RpcQueueE2ETests.cs (snapshot category "rpc_queue").
func TestRPCQueueE2E(t *testing.T) {
ctx := testharness.NewTestContext(t)
client := ctx.NewClient()
t.Cleanup(func() { client.ForceStop() })
t.Run("fresh queue is empty and empty mutations are noops", func(t *testing.T) {
session := createQueueSession(t, client)
defer session.Disconnect()
assertQueueEmpty(t, session)
remove, err := session.RPC.Queue.RemoveMostRecent(t.Context())
if err != nil {
t.Fatalf("Queue.RemoveMostRecent failed: %v", err)
}
if remove.Removed {
t.Fatal("Expected RemoveMostRecent Removed=false on empty queue")
}
assertQueueEmpty(t, session)
if _, err := session.RPC.Queue.Clear(t.Context()); err != nil {
t.Fatalf("Queue.Clear failed: %v", err)
}
assertQueueEmpty(t, session)
})
t.Run("pending items reports queued command and remove and clear update queue", func(t *testing.T) {
session := createQueueSession(t, client)
defer session.Disconnect()
interest, err := session.RPC.EventLog.RegisterInterest(t.Context(), &rpc.RegisterEventInterestParams{EventType: string(copilot.SessionEventTypeCommandQueued)})
if err != nil {
t.Fatalf("EventLog.RegisterInterest failed: %v", err)
}
defer func() {
_, _ = session.RPC.EventLog.ReleaseInterest(t.Context(), &rpc.ReleaseEventInterestParams{Handle: interest.Handle})
_, _ = session.RPC.Queue.Clear(t.Context())
}()
firstCommand := "/sdk-queue-first-" + randomHex(t)
secondCommand := "/sdk-queue-second-" + randomHex(t)
thirdCommand := "/sdk-queue-third-" + randomHex(t)
firstQueued := make(chan *copilot.CommandQueuedData, 1)
unsubscribe := session.On(func(event copilot.SessionEvent) {
data, ok := event.Data.(*copilot.CommandQueuedData)
if ok && data.Command == firstCommand {
select {
case firstQueued <- data:
default:
}
}
})
defer unsubscribe()
first, err := session.RPC.Commands.Enqueue(t.Context(), &rpc.EnqueueCommandParams{Command: firstCommand})
if err != nil {
t.Fatalf("Commands.Enqueue(first) failed: %v", err)
}
if !first.Queued {
t.Fatal("Expected first command to be queued")
}
var firstEvent *copilot.CommandQueuedData
select {
case firstEvent = <-firstQueued:
case <-time.After(30 * time.Second):
t.Fatalf("Timed out waiting for first command.queued event")
}
second, err := session.RPC.Commands.Enqueue(t.Context(), &rpc.EnqueueCommandParams{Command: secondCommand})
if err != nil {
t.Fatalf("Commands.Enqueue(second) failed: %v", err)
}
if !second.Queued {
t.Fatal("Expected second command to be queued")
}
waitForCommandInPendingItems(t, session, secondCommand)
remove, err := session.RPC.Queue.RemoveMostRecent(t.Context())
if err != nil {
t.Fatalf("Queue.RemoveMostRecent failed: %v", err)
}
if !remove.Removed {
t.Fatal("Expected RemoveMostRecent to remove second queued command")
}
waitForCommandNotInPendingItems(t, session, secondCommand)
third, err := session.RPC.Commands.Enqueue(t.Context(), &rpc.EnqueueCommandParams{Command: thirdCommand})
if err != nil {
t.Fatalf("Commands.Enqueue(third) failed: %v", err)
}
if !third.Queued {
t.Fatal("Expected third command to be queued")
}
waitForCommandInPendingItems(t, session, thirdCommand)
if _, err := session.RPC.Queue.Clear(t.Context()); err != nil {
t.Fatalf("Queue.Clear failed: %v", err)
}
waitForCommandNotInPendingItems(t, session, thirdCommand)
stop := true
completed, err := session.RPC.Commands.RespondToQueuedCommand(t.Context(), &rpc.CommandsRespondToQueuedCommandRequest{
RequestID: firstEvent.RequestID,
Result: rpc.QueuedCommandHandled{StopProcessingQueue: &stop},
})
if err != nil {
t.Fatalf("Commands.RespondToQueuedCommand failed: %v", err)
}
if !completed.Success {
t.Fatal("Expected response to first queued command to succeed")
}
waitForQueueEmpty(t, session)
})
}
func createQueueSession(t *testing.T, client *copilot.Client) *copilot.Session {
t.Helper()
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
})
if err != nil {
t.Fatalf("CreateSession failed: %v", err)
}
return session
}
func assertQueueEmpty(t *testing.T, session *copilot.Session) {
t.Helper()
pending, err := session.RPC.Queue.PendingItems(t.Context())
if err != nil {
t.Fatalf("Queue.PendingItems failed: %v", err)
}
if len(pending.Items) != 0 || len(pending.SteeringMessages) != 0 {
t.Fatalf("Expected empty queue, got %+v", pending)
}
}
func waitForCommandInPendingItems(t *testing.T, session *copilot.Session, command string) {
t.Helper()
var matched *rpc.QueuePendingItems
waitForRPCCondition(t, 30*time.Second, "queued command "+command+" to appear", func() (bool, error) {
pending, err := session.RPC.Queue.PendingItems(t.Context())
if err != nil {
return false, err
}
for i := range pending.Items {
if isPendingCommand(pending.Items[i], command) {
matched = &pending.Items[i]
return true, nil
}
}
return false, nil
})
if matched.Kind != rpc.QueuePendingItemsKindCommand {
t.Fatalf("Expected command pending item, got %+v", matched)
}
if !strings.Contains(matched.DisplayText, strings.TrimPrefix(command, "/")) && matched.DisplayText != command {
t.Fatalf("Expected pending item display text to include %q, got %q", command, matched.DisplayText)
}
}
func waitForCommandNotInPendingItems(t *testing.T, session *copilot.Session, command string) {
t.Helper()
waitForRPCCondition(t, 30*time.Second, "queued command "+command+" to leave queue", func() (bool, error) {
pending, err := session.RPC.Queue.PendingItems(t.Context())
if err != nil {
return false, err
}
for _, item := range pending.Items {
if isPendingCommand(item, command) {
return false, nil
}
}
return true, nil
})
}
func waitForQueueEmpty(t *testing.T, session *copilot.Session) {
t.Helper()
waitForRPCCondition(t, 30*time.Second, "queue to empty", func() (bool, error) {
pending, err := session.RPC.Queue.PendingItems(t.Context())
return err == nil && len(pending.Items) == 0 && len(pending.SteeringMessages) == 0, err
})
assertQueueEmpty(t, session)
}
func isPendingCommand(item rpc.QueuePendingItems, command string) bool {
return item.Kind == rpc.QueuePendingItemsKindCommand &&
(item.DisplayText == command || strings.Contains(item.DisplayText, strings.TrimPrefix(command, "/")))
}