Skip to content

Commit 989c43f

Browse files
committed
Make JSONRPC easier in conformance tests
1 parent 26f9668 commit 989c43f

File tree

1 file changed

+23
-35
lines changed

1 file changed

+23
-35
lines changed

Diff for: conformance/conformance_test.go

+23-35
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,19 @@ func TestCapabilities(t *testing.T) {
130130
anthropicServer := start(t, anthropic)
131131
githubServer := start(t, github)
132132

133-
req := newInitializeRequest(
134-
initializeRequestParams{
133+
req := initializeRequest{
134+
JSONRPC: "2.0",
135+
ID: 1,
136+
Method: "initialize",
137+
Params: initializeParams{
135138
ProtocolVersion: "2025-03-26",
136139
Capabilities: clientCapabilities{},
137140
ClientInfo: clientInfo{
138141
Name: "ConformanceTest",
139142
Version: "0.0.1",
140143
},
141144
},
142-
)
145+
}
143146

144147
require.NoError(t, anthropicServer.send(req))
145148

@@ -274,48 +277,39 @@ func (s server) receive(res response) error {
274277
return res.unmarshal(line)
275278
}
276279

280+
type request interface {
281+
marshal() ([]byte, error)
282+
}
283+
284+
type response interface {
285+
unmarshal([]byte) error
286+
}
287+
277288
type jsonRPRCRequest[params any] struct {
278289
JSONRPC string `json:"jsonrpc"`
279290
ID int `json:"id"`
280291
Method string `json:"method"`
281292
Params params `json:"params"`
282293
}
283294

295+
func (r jsonRPRCRequest[any]) marshal() ([]byte, error) {
296+
return json.Marshal(r)
297+
}
298+
284299
type jsonRPRCResponse[result any] struct {
285300
JSONRPC string `json:"jsonrpc"`
286301
ID int `json:"id"`
287302
Method string `json:"method"`
288303
Result result `json:"result"`
289304
}
290305

291-
type request interface {
292-
marshal() ([]byte, error)
293-
}
294-
295-
type response interface {
296-
unmarshal([]byte) error
297-
}
298-
299-
func newInitializeRequest(params initializeRequestParams) initializeRequest {
300-
return initializeRequest{
301-
jsonRPRCRequest: jsonRPRCRequest[initializeRequestParams]{
302-
JSONRPC: "2.0",
303-
ID: 1,
304-
Method: "initialize",
305-
Params: params,
306-
},
307-
}
308-
}
309-
310-
type initializeRequest struct {
311-
jsonRPRCRequest[initializeRequestParams]
306+
func (r *jsonRPRCResponse[any]) unmarshal(b []byte) error {
307+
return json.Unmarshal(b, r)
312308
}
313309

314-
func (r initializeRequest) marshal() ([]byte, error) {
315-
return json.Marshal(r)
316-
}
310+
type initializeRequest = jsonRPRCRequest[initializeParams]
317311

318-
type initializeRequestParams struct {
312+
type initializeParams struct {
319313
ProtocolVersion string `json:"protocolVersion"`
320314
Capabilities clientCapabilities `json:"capabilities"`
321315
ClientInfo clientInfo `json:"clientInfo"`
@@ -328,13 +322,7 @@ type clientInfo struct {
328322
Version string `json:"version"`
329323
}
330324

331-
type initializeResponse struct {
332-
jsonRPRCResponse[initializeResult]
333-
}
334-
335-
func (r *initializeResponse) unmarshal(b []byte) error {
336-
return json.Unmarshal(b, r)
337-
}
325+
type initializeResponse = jsonRPRCResponse[initializeResult]
338326

339327
type initializeResult struct {
340328
ProtocolVersion string `json:"protocolVersion"`

0 commit comments

Comments
 (0)