-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathmessage_source_test.go
More file actions
296 lines (281 loc) · 8.64 KB
/
Copy pathmessage_source_test.go
File metadata and controls
296 lines (281 loc) · 8.64 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
package copilot
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"reflect"
"strings"
"testing"
"time"
"github.com/github/copilot-sdk/go/internal/jsonrpc2"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
)
func messageSourceTestCases() []struct {
name string
value MessageSource
wire string
} {
return []struct {
name string
value MessageSource
wire string
}{
{name: "omitted"},
{name: "user", value: MessageSourceUser, wire: "user"},
{name: "system", value: MessageSourceSystem, wire: "system"},
{name: "agent", value: MessageSourceAgent("reviewer"), wire: "agent-reviewer"},
{name: "empty agent id", value: MessageSourceAgent(""), wire: "agent-"},
{name: "opaque agent id", value: MessageSourceAgent(" Agent/É "), wire: "agent- Agent/É "},
{name: "prefixed agent id", value: MessageSourceAgent("agent-reviewer"), wire: "agent-agent-reviewer"},
}
}
func TestSession_SendMessageSource(t *testing.T) {
previousPropagator := otel.GetTextMapPropagator()
otel.SetTextMapPropagator(propagation.TraceContext{})
defer otel.SetTextMapPropagator(previousPropagator)
const traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
const tracestate = "vendor=value"
for _, source := range messageSourceTestCases() {
t.Run(source.name, func(t *testing.T) {
for _, mode := range []string{"", "enqueue", "immediate"} {
name := mode
if name == "" {
name = "defaults"
}
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second)
defer cancel()
options := MessageOptions{Prompt: "hello", Source: source.value}
want := map[string]any{"sessionId": "session-1", "prompt": "hello"}
if source.wire != "" {
want["source"] = source.wire
}
if mode != "" {
options.Mode = mode
options.AgentMode = AgentModePlan
options.DisplayPrompt = "display text"
options.Attachments = []Attachment{
&AttachmentFile{Path: "/workspace/main.go", DisplayName: "main.go"},
}
options.RequestHeaders = map[string]string{"X-Test": "value"}
ctx = contextWithTraceParent(ctx, traceparent, tracestate)
want["mode"] = mode
want["agentMode"] = "plan"
want["displayPrompt"] = "display text"
want["attachments"] = []any{
map[string]any{"type": "file", "path": "/workspace/main.go", "displayName": "main.go"},
}
want["requestHeaders"] = map[string]any{"X-Test": "value"}
want["traceparent"] = traceparent
want["tracestate"] = tracestate
}
params := captureMessageSourceRequest(t, nil, nil, func(session *Session) {
messageID, err := session.Send(ctx, options)
if err != nil {
t.Fatalf("Send failed: %v", err)
}
if messageID != "message-1" {
t.Fatalf("expected message-1, got %q", messageID)
}
})
if !reflect.DeepEqual(params, want) {
t.Fatalf("unexpected session.send params:\ngot %#v\nwant %#v", params, want)
}
})
}
})
}
}
func TestSession_SendAndWaitMessageSource(t *testing.T) {
for _, source := range messageSourceTestCases() {
for _, mode := range []string{"", "enqueue", "immediate"} {
for _, tc := range []struct {
name string
events []SessionEvent
rpcError *jsonrpc2.Error
wantContent string
wantError string
}{
{
name: "idle without assistant",
events: []SessionEvent{{Data: &SessionIdleData{}}},
},
{
name: "assistant then idle",
events: []SessionEvent{
{Data: &AssistantMessageData{MessageID: "assistant-1", Content: "done"}},
{Data: &SessionIdleData{}},
},
wantContent: "done",
},
{
name: "session error",
events: []SessionEvent{{Data: &SessionErrorData{Message: "model failed"}}},
wantError: "session error: model failed",
},
{
name: "RPC error",
rpcError: &jsonrpc2.Error{Code: -32602, Message: "invalid prompt"},
wantError: "invalid prompt",
},
} {
t.Run(source.name+"/"+mode+"/"+tc.name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), 2*time.Second)
defer cancel()
params := captureMessageSourceRequest(t, tc.rpcError, tc.events, func(session *Session) {
result, err := session.SendAndWait(ctx, MessageOptions{
Prompt: "background update",
Source: source.value,
Mode: mode,
AgentMode: AgentModePlan,
DisplayPrompt: "Background update",
Attachments: []Attachment{
&AttachmentFile{Path: "/workspace/main.go", DisplayName: "main.go"},
},
RequestHeaders: map[string]string{"X-Test": "value"},
})
if tc.wantError != "" {
if err == nil || !strings.Contains(err.Error(), tc.wantError) {
t.Fatalf("expected error containing %q, got %v", tc.wantError, err)
}
if tc.rpcError != nil {
var rpcError *jsonrpc2.Error
if !errors.As(err, &rpcError) || rpcError.Code != tc.rpcError.Code {
t.Fatalf("expected wrapped RPC error, got %v", err)
}
}
} else if err != nil {
t.Fatalf("SendAndWait failed: %v", err)
}
if tc.wantContent == "" {
if result != nil {
t.Fatalf("expected no assistant message, got %#v", result)
}
} else {
if result == nil {
t.Fatal("expected an assistant message")
}
message, ok := result.Data.(*AssistantMessageData)
if !ok || message.Content != tc.wantContent {
t.Fatalf("unexpected assistant message: %#v", result.Data)
}
}
})
want := map[string]any{
"sessionId": "session-1",
"prompt": "background update",
"agentMode": "plan",
"displayPrompt": "Background update",
"attachments": []any{
map[string]any{"type": "file", "path": "/workspace/main.go", "displayName": "main.go"},
},
"requestHeaders": map[string]any{"X-Test": "value"},
}
if source.wire != "" {
want["source"] = source.wire
}
if mode != "" {
want["mode"] = mode
}
if !reflect.DeepEqual(params, want) {
t.Fatalf("unexpected session.send params:\ngot %#v\nwant %#v", params, want)
}
})
}
}
}
}
func captureMessageSourceRequest(t *testing.T, rpcError *jsonrpc2.Error, events []SessionEvent, invoke func(*Session)) map[string]any {
return captureSessionSendRequest(t, rpcError, events, false, invoke)
}
func captureSessionSendRequest(t *testing.T, rpcError *jsonrpc2.Error, events []SessionEvent, beforeResponse bool, invoke func(*Session)) map[string]any {
t.Helper()
stdinR, stdinW := io.Pipe()
stdoutR, stdoutW := io.Pipe()
defer stdinR.Close()
defer stdinW.Close()
defer stdoutR.Close()
defer stdoutW.Close()
client := jsonrpc2.NewClient(stdinW, stdoutR)
client.Start()
defer client.Stop()
session := newSession("session-1", client, "", false)
defer session.stopEventProcessing()
paramsCh := make(chan map[string]any, 1)
errCh := make(chan error, 1)
go func() {
frame, err := readTestJSONRPCFrame(stdinR)
if err != nil {
errCh <- err
return
}
var request struct {
ID json.RawMessage `json:"id"`
Method string `json:"method"`
Params map[string]any `json:"params"`
}
if err := json.Unmarshal(frame, &request); err != nil {
errCh <- err
return
}
if request.Method != "session.send" {
errCh <- fmt.Errorf("expected session.send, got %s", request.Method)
return
}
response := map[string]any{"jsonrpc": "2.0", "id": request.ID}
if rpcError != nil {
response["error"] = rpcError
} else {
response["result"] = map[string]any{"messageId": "message-1"}
}
data, err := json.Marshal(response)
if err != nil {
errCh <- err
return
}
if beforeResponse && len(events) > 0 {
processed := make(chan struct{})
count := 0
unsubscribe := session.On(func(SessionEvent) {
count++
if count == len(events) {
close(processed)
}
})
for _, event := range events {
session.dispatchEvent(event)
}
select {
case <-processed:
case <-time.After(time.Second):
errCh <- fmt.Errorf("pre-admission events were not dispatched")
unsubscribe()
return
}
unsubscribe()
}
if _, err := fmt.Fprintf(stdoutW, "Content-Length: %d\r\n\r\n%s", len(data), data); err != nil {
errCh <- err
return
}
if !beforeResponse {
for _, event := range events {
session.dispatchEvent(event)
}
}
paramsCh <- request.Params
}()
invoke(session)
select {
case params := <-paramsCh:
return params
case err := <-errCh:
t.Fatal(err)
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for session.send request")
}
return nil
}