-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathpermission_context_test.go
More file actions
260 lines (231 loc) · 8.62 KB
/
Copy pathpermission_context_test.go
File metadata and controls
260 lines (231 loc) · 8.62 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
package copilot
import (
"encoding/json"
"fmt"
"io"
"testing"
"time"
"github.com/github/copilot-sdk/go/internal/jsonrpc2"
"github.com/github/copilot-sdk/go/rpc"
)
// runPermissionExchange drives executePermissionAndRespond with the supplied
// handler and captures the raw JSON-RPC request frame the SDK emits (if any).
// The second return value reports whether a request was sent at all, so tests
// can assert that no-result decisions suppress the response entirely.
func runPermissionExchange(t *testing.T, handler PermissionHandlerFunc) (frame []byte, sent bool) {
t.Helper()
stdinR, stdinW := io.Pipe()
stdoutR, stdoutW := io.Pipe()
t.Cleanup(func() {
stdinR.Close()
stdinW.Close()
stdoutR.Close()
stdoutW.Close()
})
client := jsonrpc2.NewClient(stdinW, stdoutR)
client.Start()
t.Cleanup(client.Stop)
session := &Session{
SessionID: "session-1",
client: client,
RPC: rpc.NewSessionRPC(client, "session-1"),
}
frameCh := make(chan []byte, 1)
go func() {
captured, err := readTestJSONRPCFrame(stdinR)
if err != nil {
return
}
var request struct {
ID json.RawMessage `json:"id"`
}
_ = json.Unmarshal(captured, &request)
// Publish the captured frame before unblocking the RPC round trip so a
// sent response is always observable before executePermissionAndRespond
// returns.
frameCh <- captured
response := map[string]any{
"jsonrpc": "2.0",
"id": json.RawMessage(request.ID),
"result": map[string]any{"applied": true},
}
data, _ := json.Marshal(response)
_, _ = fmt.Fprintf(stdoutW, "Content-Length: %d\r\n\r\n%s", len(data), data)
}()
done := make(chan struct{})
go func() {
session.executePermissionAndRespond("permission-1", nil, handler)
close(done)
}()
select {
case captured := <-frameCh:
return captured, true
case <-done:
select {
case captured := <-frameCh:
return captured, true
default:
return nil, false
}
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for permission response")
return nil, false
}
}
// paramsOf extracts the top-level params object from a JSON-RPC request frame.
func paramsOf(t *testing.T, frame []byte) map[string]json.RawMessage {
t.Helper()
var request struct {
Method string `json:"method"`
Params map[string]json.RawMessage `json:"params"`
}
if err := json.Unmarshal(frame, &request); err != nil {
t.Fatalf("failed to unmarshal request frame: %v", err)
}
if request.Method != "session.permissions.handlePendingPermissionRequest" {
t.Fatalf("unexpected method %q", request.Method)
}
return request.Params
}
func sampleDecisionContext() *rpc.PermissionDecisionContext {
return &rpc.PermissionDecisionContext{
Outcome: PermissionDecisionOutcomeAutoApproved,
Source: PermissionDecisionSourceHostPolicy,
Surface: PermissionDecisionSurfaceSDK,
}
}
func TestPermissionDecisionContextForwardedAsSiblingOfResult(t *testing.T) {
frame, sent := runPermissionExchange(t, func(PermissionRequest, PermissionInvocation) (rpc.PermissionDecision, error) {
return NewAttributedPermissionResult(&rpc.PermissionDecisionApproveOnce{}, sampleDecisionContext()), nil
})
if !sent {
t.Fatal("expected a permission response to be sent")
}
params := paramsOf(t, frame)
// decisionContext must be a top-level sibling of result.
rawContext, ok := params["decisionContext"]
if !ok {
t.Fatal("expected decisionContext to be present as a top-level sibling of result")
}
var context rpc.PermissionDecisionContext
if err := json.Unmarshal(rawContext, &context); err != nil {
t.Fatalf("failed to unmarshal decisionContext: %v", err)
}
if context.Outcome != PermissionDecisionOutcomeAutoApproved ||
context.Source != PermissionDecisionSourceHostPolicy ||
context.Surface != PermissionDecisionSurfaceSDK {
t.Fatalf("unexpected decisionContext contents: %#v", context)
}
// result must exist and must NOT contain a nested decisionContext.
rawResult, ok := params["result"]
if !ok {
t.Fatal("expected result to be present")
}
var result map[string]json.RawMessage
if err := json.Unmarshal(rawResult, &result); err != nil {
t.Fatalf("failed to unmarshal result: %v", err)
}
if _, nested := result["decisionContext"]; nested {
t.Fatal("decisionContext must not be nested inside result")
}
}
func TestPermissionDecisionContextOmittedWithoutAttribution(t *testing.T) {
frame, sent := runPermissionExchange(t, func(PermissionRequest, PermissionInvocation) (rpc.PermissionDecision, error) {
return &rpc.PermissionDecisionApproveOnce{}, nil
})
if !sent {
t.Fatal("expected a permission response to be sent")
}
params := paramsOf(t, frame)
if _, ok := params["decisionContext"]; ok {
t.Fatal("expected decisionContext to be absent when no context is supplied")
}
if _, ok := params["result"]; !ok {
t.Fatal("expected result to be present")
}
}
func TestAttributedResultReplacesRatherThanNests(t *testing.T) {
first := &rpc.PermissionDecisionContext{
Outcome: PermissionDecisionOutcomePromptedUser,
Source: PermissionDecisionSourceHumanResponse,
Surface: PermissionDecisionSurfaceTui,
}
second := sampleDecisionContext()
wrapped := NewAttributedPermissionResult(NewAttributedPermissionResult(&rpc.PermissionDecisionApproveOnce{}, first), second)
if wrapped.DecisionContext != second {
t.Fatalf("expected the second context to replace the first, got %#v", wrapped.DecisionContext)
}
// The underlying decision must be the plain approve-once, not another wrapper.
if _, ok := wrapped.PermissionDecision.(*rpc.PermissionDecisionApproveOnce); !ok {
t.Fatalf("expected unwrapped decision to be *rpc.PermissionDecisionApproveOnce, got %T", wrapped.PermissionDecision)
}
frame, sent := runPermissionExchange(t, func(PermissionRequest, PermissionInvocation) (rpc.PermissionDecision, error) {
return wrapped, nil
})
if !sent {
t.Fatal("expected a permission response to be sent")
}
params := paramsOf(t, frame)
rawContext, ok := params["decisionContext"]
if !ok {
t.Fatal("expected decisionContext to be present")
}
var context rpc.PermissionDecisionContext
if err := json.Unmarshal(rawContext, &context); err != nil {
t.Fatalf("failed to unmarshal decisionContext: %v", err)
}
if context.Surface != PermissionDecisionSurfaceSDK {
t.Fatalf("expected replaced surface %q, got %q", PermissionDecisionSurfaceSDK, context.Surface)
}
}
func TestAttributedNoResultStillSuppressesResponse(t *testing.T) {
frame, sent := runPermissionExchange(t, func(PermissionRequest, PermissionInvocation) (rpc.PermissionDecision, error) {
return NewAttributedPermissionResult(&rpc.PermissionDecisionNoResult{}, sampleDecisionContext()), nil
})
if sent {
t.Fatalf("expected no response to be sent for an attributed no-result decision, got frame: %s", frame)
}
}
// A handler may dereference the wrapper and return it by value. The embedded
// interface promotes its methods to the value type, so the value form also
// satisfies rpc.PermissionDecision and must be unwrapped identically to the
// pointer form -- otherwise the wrapper itself is sent as result and the
// context is silently dropped.
func TestValueFormAttributedResultIsUnwrapped(t *testing.T) {
frame, sent := runPermissionExchange(t, func(PermissionRequest, PermissionInvocation) (rpc.PermissionDecision, error) {
return *NewAttributedPermissionResult(&rpc.PermissionDecisionApproveOnce{}, sampleDecisionContext()), nil
})
if !sent {
t.Fatal("expected a permission response to be sent")
}
params := paramsOf(t, frame)
if _, ok := params["decisionContext"]; !ok {
t.Fatal("expected decisionContext to be forwarded for a value-form attributed result")
}
var result map[string]json.RawMessage
if err := json.Unmarshal(params["result"], &result); err != nil {
t.Fatalf("failed to unmarshal result: %v", err)
}
if _, nested := result["decisionContext"]; nested {
t.Fatal("decisionContext must not be nested inside result")
}
if _, leaked := result["PermissionDecision"]; leaked {
t.Fatal("the wrapper leaked into result instead of being unwrapped")
}
}
func TestAttributedResultReplacesContextOnValueForm(t *testing.T) {
first := sampleDecisionContext()
second := &rpc.PermissionDecisionContext{
Outcome: PermissionDecisionOutcomePromptedUser,
Source: PermissionDecisionSourceHumanResponse,
Surface: PermissionDecisionSurfaceTui,
}
valueForm := *NewAttributedPermissionResult(&rpc.PermissionDecisionApproveOnce{}, first)
replaced := NewAttributedPermissionResult(valueForm, second)
if replaced.DecisionContext != second {
t.Fatal("expected the second context to replace the first")
}
if _, nested := replaced.PermissionDecision.(AttributedPermissionResult); nested {
t.Fatal("value-form attribution must be replaced, not nested")
}
}