@@ -297,23 +297,50 @@ func TestRpcSessionStateE2E(t *testing.T) {
297297 forkedSession .Disconnect ()
298298 })
299299
300- t .Run ("should report error when forking session without persisted events" , func (t * testing.T ) {
300+ t .Run ("should handle forking session without persisted events" , func (t * testing.T ) {
301301 session , err := client .CreateSession (t .Context (), & copilot.SessionConfig {
302302 OnPermissionRequest : copilot .PermissionHandler .ApproveAll ,
303303 })
304304 if err != nil {
305305 t .Fatalf ("Failed to create session: %v" , err )
306306 }
307+ defer session .Disconnect ()
307308
308- _ , err = client .RPC .Sessions .Fork (t .Context (), & rpc.SessionsForkRequest {SessionID : session .SessionID })
309- if err == nil {
310- t .Fatal ("Expected fork on empty session to fail" )
309+ fork , err := client .RPC .Sessions .Fork (t .Context (), & rpc.SessionsForkRequest {SessionID : session .SessionID })
310+ if err != nil {
311+ errText := strings .ToLower (err .Error ())
312+ if ! strings .Contains (errText , "not found or has no persisted events" ) {
313+ t .Errorf ("Expected error mentioning 'not found or has no persisted events', got %v" , err )
314+ }
315+ if strings .Contains (errText , "unhandled method sessions.fork" ) {
316+ t .Errorf ("sessions.fork should be implemented; error suggests it isn't: %v" , err )
317+ }
318+ return
311319 }
312- if ! strings . Contains ( strings . ToLower ( err . Error ()), "not found or has no persisted events" ) {
313- t .Errorf ("Expected error mentioning 'not found or has no persisted events', got %v" , err )
320+ if fork == nil {
321+ t .Fatal ("Expected non-nil fork result" )
314322 }
315- if strings .Contains (strings .ToLower (err .Error ()), "unhandled method sessions.fork" ) {
316- t .Errorf ("sessions.fork should be implemented; error suggests it isn't: %v" , err )
323+ if strings .TrimSpace (fork .SessionID ) == "" {
324+ t .Fatal ("Expected non-empty fork session id" )
325+ }
326+ if fork .SessionID == session .SessionID {
327+ t .Errorf ("Expected fork session id to differ from source %q" , session .SessionID )
328+ }
329+
330+ forkedSession , err := client .ResumeSession (t .Context (), fork .SessionID , & copilot.ResumeSessionConfig {
331+ OnPermissionRequest : copilot .PermissionHandler .ApproveAll ,
332+ })
333+ if err != nil {
334+ t .Fatalf ("Failed to resume forked session: %v" , err )
335+ }
336+ defer forkedSession .Disconnect ()
337+
338+ forkedMessages , err := forkedSession .GetMessages (t .Context ())
339+ if err != nil {
340+ t .Fatalf ("Failed to read forked messages: %v" , err )
341+ }
342+ if forkedConversation := conversationMessages (forkedMessages ); len (forkedConversation ) != 0 {
343+ t .Errorf ("Expected empty forked conversation, got %v" , forkedConversation )
317344 }
318345 })
319346
0 commit comments