@@ -91,35 +91,10 @@ func newPipedServer(t *testing.T) (rawClient net.Conn, clientEnc *capnp.Encoder,
9191 }
9292}
9393
94- // sendConnect sends a Connect frame via enc.
95- func sendConnect (t * testing.T , enc * capnp.Encoder , toolName , version , taskID string ) error {
94+ // handshake sends a Connect frame via enc using Client.handshake .
95+ func handshake (t * testing.T , enc * capnp.Encoder , rawClient net. Conn , toolName , version , taskID string ) error {
9696 t .Helper ()
97- m , seg , err := capnp .NewMessage (capnp .SingleSegment (nil ))
98- if err != nil {
99- return fmt .Errorf ("send connect new message: %w" , err )
100- }
101- root , err := NewRootMessage (seg )
102- if err != nil {
103- return fmt .Errorf ("send connect new root: %w" , err )
104- }
105- c , err := root .NewConnect ()
106- if err != nil {
107- return fmt .Errorf ("send connect new connect: %w" , err )
108- }
109- if err := c .SetToolName (toolName ); err != nil {
110- return fmt .Errorf ("send connect set tool name: %w" , err )
111- }
112- if err := c .SetVersion (version ); err != nil {
113- return fmt .Errorf ("send connect set version: %w" , err )
114- }
115- if err := c .SetTaskID (taskID ); err != nil {
116- return fmt .Errorf ("send connect set task id: %w" , err )
117- }
118- c .SetProtoVersion (1 )
119- if err := enc .Encode (m ); err != nil {
120- return fmt .Errorf ("send connect encode: %w" , err )
121- }
122- return nil
97+ return (& Client {encoder : enc , conn : rawClient }).handshake (toolName , version , taskID )
12398}
12499
125100// sendChunk sends a Chunk frame via enc.
@@ -159,8 +134,7 @@ func TestNormalPath(t *testing.T) {
159134 rawClient , clientEnc , rec , wait := newPipedServer (t )
160135
161136 go func () {
162- defer rawClient .Close ()
163- if err := sendConnect (t , clientEnc , "dropwatch" , "1.0" , "" ); err != nil {
137+ if err := handshake (t , clientEnc , rawClient , "dropwatch" , "1.0" , "" ); err != nil {
164138 t .Logf ("send connect: %v" , err )
165139 return
166140 }
@@ -202,8 +176,7 @@ func TestErrorEnd(t *testing.T) {
202176 rawClient , clientEnc , rec , wait := newPipedServer (t )
203177
204178 go func () {
205- defer rawClient .Close ()
206- if err := sendConnect (t , clientEnc , "tool" , "v" , "" ); err != nil {
179+ if err := handshake (t , clientEnc , rawClient , "tool" , "v" , "" ); err != nil {
207180 t .Logf ("send connect: %v" , err )
208181 return
209182 }
@@ -229,8 +202,7 @@ func TestDataAndEndCombined(t *testing.T) {
229202 rawClient , clientEnc , rec , wait := newPipedServer (t )
230203
231204 go func () {
232- defer rawClient .Close ()
233- if err := sendConnect (t , clientEnc , "tool" , "v" , "" ); err != nil {
205+ if err := handshake (t , clientEnc , rawClient , "tool" , "v" , "" ); err != nil {
234206 t .Logf ("send connect: %v" , err )
235207 return
236208 }
@@ -257,7 +229,7 @@ func TestUnexpectedClose(t *testing.T) {
257229
258230 go func () {
259231 defer rawClient .Close ()
260- if err := sendConnect (t , clientEnc , "tool" , "v" , "" ); err != nil {
232+ if err := handshake (t , clientEnc , rawClient , "tool" , "v" , "" ); err != nil {
261233 t .Logf ("send connect: %v" , err )
262234 return
263235 }
@@ -294,7 +266,7 @@ func TestEmptyToolName(t *testing.T) {
294266 rawClient , clientEnc , rec , wait := newPipedServer (t )
295267
296268 go func () {
297- if err := sendConnect (t , clientEnc , "" , "v" , "" ); err != nil {
269+ if err := handshake (t , clientEnc , rawClient , "" , "v" , "" ); err != nil {
298270 t .Logf ("send connect: %v" , err )
299271 return
300272 }
@@ -313,9 +285,9 @@ func TestEmptyToolName(t *testing.T) {
313285// omits the client-side close so that "accept-then-loop" behavior would hang
314286// here until wait() trips its 2s timeout.
315287func TestEmptyToolNameClosesEarly (t * testing.T ) {
316- _ , clientEnc , rec , wait := newPipedServer (t )
288+ rawClient , clientEnc , rec , wait := newPipedServer (t )
317289
318- if err := sendConnect (t , clientEnc , "" , "v" , "" ); err != nil {
290+ if err := handshake (t , clientEnc , rawClient , "" , "v" , "" ); err != nil {
319291 t .Logf ("send connect: %v" , err )
320292 return
321293 }
@@ -333,7 +305,7 @@ func TestDecodeFailureAfterConnect(t *testing.T) {
333305 rawClient , clientEnc , rec , wait := newPipedServer (t )
334306
335307 go func () {
336- if err := sendConnect (t , clientEnc , "tool" , "v" , "" ); err != nil {
308+ if err := handshake (t , clientEnc , rawClient , "tool" , "v" , "" ); err != nil {
337309 t .Logf ("send connect: %v" , err )
338310 return
339311 }
0 commit comments